Skip to content

Commit

Permalink
update admin users api
Browse files Browse the repository at this point in the history
lastaccessed at 타임존 변경
  • Loading branch information
IGSON2 committed May 5, 2024
1 parent a342a60 commit 07cab2d
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 56 deletions.
7 changes: 6 additions & 1 deletion backend/api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math"
"strings"
"time"

"github.com/gofiber/fiber/v2"
)
Expand All @@ -24,6 +25,8 @@ type AdminUserResponse struct {
LastAccess string `json:"last_access"`
}

var asiaSeoul, _ = time.LoadLocation("Asia/Seoul")

func (s *Server) GetUsers(c *fiber.Ctx) error {
users, err := s.store.GetAdminUsers(c.Context(), db.GetAdminUsersParams{
Limit: 1000,
Expand All @@ -47,7 +50,9 @@ func (s *Server) GetUsers(c *fiber.Ctx) error {
Referral: user.Referral,
RecomCode: user.RecommenderCode,
SignUpDate: user.CreatedAt.Format("06.01.02 15:04:05"),
LastAccess: user.LastAccessedAt.Format("06.01.02 15:04:05"),
// Mysql DB의 time_zone이 Asia/Seoul이여도, Default로 생성되는 값에만 적용되고 Update로 변환되는 argument에는 적용되지 않는다.
// LastAccess 필드는 Default로 생성되지 않고, update되기 때문에 client에게 제공할 때 별도의 time zone 변환이 필요하다.
LastAccess: user.LastAccessedAt.Time.In(asiaSeoul).Format("06.01.02 15:04:05"),
})
}
return c.Status(fiber.StatusOK).JSON(response)
Expand Down
22 changes: 0 additions & 22 deletions backend/api/token.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package api

import (
db "bitmoi/backend/db/sqlc"
"bitmoi/backend/utilities"
"context"
"database/sql"
"fmt"
"time"
Expand Down Expand Up @@ -122,23 +120,3 @@ func (s *Server) verifyToken(c *fiber.Ctx) error {
userRes := convertUserResponse(user)
return c.Status(fiber.StatusOK).JSON(userRes)
}

const (
AttendanceReward = 1000
AttendanceTitle = "출석 체크 보상"
AttendanceGiver = "시스템"
AttendanceMethod = "자동"
)

func (s *Server) checkAttendance(ctx context.Context, userId string) (float64, error) {
return s.store.CheckAttendTx(ctx, db.CheckAttendTxParams{
AppendPracBalanceTxParams: db.AppendPracBalanceTxParams{
UserID: userId,
Amount: AttendanceReward,
Title: AttendanceTitle,
Giver: AttendanceGiver,
Method: AttendanceMethod,
},
TodayMidnight: time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local),
})
}
25 changes: 24 additions & 1 deletion backend/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bitmoi/backend/token"
"bitmoi/backend/utilities"
"bitmoi/backend/worker"
"context"
"database/sql"
"fmt"
"time"
Expand Down Expand Up @@ -40,14 +41,16 @@ func convertUserResponse(user db.User) UserResponse {
RecommenderCode: user.RecommenderCode,
PasswordChangedAt: user.PasswordChangedAt,
CreatedAt: user.CreatedAt,
LastAccessedAt: user.LastAccessedAt,
}
if user.PhotoUrl.Valid {
uR.PhotoURL = user.PhotoUrl.String
}
if user.MetamaskAddress.Valid {
uR.MetamaskAddress = user.MetamaskAddress.String
}
if user.LastAccessedAt.Valid {
uR.LastAccessedAt = user.LastAccessedAt.Time
}
return uR
}

Expand Down Expand Up @@ -425,3 +428,23 @@ func (s *Server) getAccumulationHist(c *fiber.Ctx) error {

return c.Status(fiber.StatusOK).JSON(histories)
}

const (
AttendanceReward = 1000
AttendanceTitle = "출석 체크 보상"
AttendanceGiver = "시스템"
AttendanceMethod = "자동"
)

func (s *Server) checkAttendance(ctx context.Context, userId string) (float64, error) {
return s.store.CheckAttendTx(ctx, db.CheckAttendTxParams{
AppendPracBalanceTxParams: db.AppendPracBalanceTxParams{
UserID: userId,
Amount: AttendanceReward,
Title: AttendanceTitle,
Giver: AttendanceGiver,
Method: AttendanceMethod,
},
TodayMidnight: time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local),
})
}
31 changes: 31 additions & 0 deletions backend/api/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
"io"
"net/http"
"reflect"
"strings"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -147,3 +149,32 @@ func TestGetLastUserID(t *testing.T) {

t.Log(id)
}

func TestCheckAttendacne(t *testing.T) {
if testing.Short() {
t.Skip()
}

s := newTestServer(t, newTestStore(t), nil)

userID := utilities.MakeRanString(10)

_, err := s.store.CreateUser(context.Background(), db.CreateUserParams{
UserID: userID,
Nickname: utilities.MakeRanString(10),
Email: utilities.MakeRanEmail(),
RecommenderCode: strings.ToUpper(utilities.MakeRanString(8)),
})
require.NoError(t, err)

user, err := s.store.GetUser(context.Background(), userID)
require.NoError(t, err)
require.False(t, user.LastAccessedAt.Valid)

_, err = s.checkAttendance(context.Background(), userID)
require.NoError(t, err)

user, err = s.store.GetUser(context.Background(), userID)
require.NoError(t, err)
require.WithinDuration(t, time.Now(), user.LastAccessedAt.Time, time.Second*5)
}
2 changes: 1 addition & 1 deletion backend/db/migrate/000002_add_user.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ CREATE TABLE `users` (
`wmoi_balance` double NOT NULL DEFAULT 0 CHECK (wmoi_balance >= 0),
`recommender_code` varchar(50) NOT NULL UNIQUE,
`created_at` timestamp NOT NULL DEFAULT (now()),
`last_accessed_at` timestamp NOT NULL DEFAULT (now()),
`last_accessed_at` timestamp,
`password_changed_at` timestamp NOT NULL DEFAULT (now()),
`address_changed_at` timestamp
);
Expand Down
5 changes: 2 additions & 3 deletions backend/db/mock/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions backend/db/sqlc/admin.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/db/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions backend/db/sqlc/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions backend/db/sqlc/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bitmoi/backend/contract"
"context"
"database/sql"
"errors"
"fmt"
"math/big"
"time"
Expand Down Expand Up @@ -165,10 +166,10 @@ func (store *SqlStore) CheckAttendTx(ctx context.Context, arg CheckAttendTxParam
return fmt.Errorf("failed to attendence due to cannot find user. err: %w", err)
}

lastAccessed = user.LastAccessedAt
lastAccessed = user.LastAccessedAt.Time

_, err = q.UpdateUserLastAccessedAt(ctx, UpdateUserLastAccessedAtParams{
LastAccessedAt: time.Now(),
LastAccessedAt: sql.NullTime{Time: time.Now(), Valid: true},
UserID: arg.UserID,
})

Expand All @@ -182,12 +183,11 @@ func (store *SqlStore) CheckAttendTx(ctx context.Context, arg CheckAttendTxParam
return arg.Amount, err
}

if lastAccessed.Before(arg.TodayMidnight) {
err = store.AppendPracBalanceTx(ctx, arg.AppendPracBalanceTxParams)
if lastAccessed.After(arg.TodayMidnight) {
return arg.Amount, errors.New("already checked attendance today")
}

return arg.Amount, err

return arg.Amount, store.AppendPracBalanceTx(ctx, arg.AppendPracBalanceTxParams)
}

type SettleImdScoreTxParams struct {
Expand Down
9 changes: 4 additions & 5 deletions backend/db/sqlc/users.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions backend/db/sqlc/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package db

import (
"context"
"database/sql"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand All @@ -21,3 +23,31 @@ func TestGetUsers(t *testing.T) {
require.NoError(t, err)
t.Logf("rows: %v", rows)
}

func TestLastaccessedAt(t *testing.T) {
if testing.Short() {
t.Skip()
}
store := newTestStore(t)

userID := "coactadmin"
zone, err := time.LoadLocation("Asia/Seoul")
require.NoError(t, err)

// zone = time.UTC
now := time.Now().In(zone)

_, err = store.UpdateUserLastAccessedAt(context.Background(), UpdateUserLastAccessedAtParams{
LastAccessedAt: sql.NullTime{Time: now.Add(9 * time.Hour), Valid: true},
UserID: userID,
})
require.NoError(t, err)

userA, err := store.GetUser(context.Background(), userID)
require.NoError(t, err)
require.WithinDuration(t, now, userA.LastAccessedAt.Time, time.Second*5)

compareTime := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), time.Now().Hour()-1, 0, 0, 0, time.Local)
require.True(t, compareTime.Before(userA.LastAccessedAt.Time))
t.Log(compareTime, userA.LastAccessedAt.Time)
}

0 comments on commit 07cab2d

Please sign in to comment.