From 4732c982bf1a850251f93867e00eee4ecdcb833f Mon Sep 17 00:00:00 2001 From: Igson <91243660+IGSON2@users.noreply.github.com> Date: Sun, 5 May 2024 15:42:55 +0900 Subject: [PATCH] update user.lastAccessed time field --- backend/api/admin.go | 2 +- backend/api/user.go | 4 +-- backend/db/migrate/000002_add_user.up.sql | 2 +- backend/db/mock/store.go | 5 ++-- backend/db/sqlc/admin.sql.go | 28 +++++++++---------- backend/db/sqlc/models.go | 2 +- backend/db/sqlc/querier.go | 3 ++- backend/db/sqlc/transactions.go | 33 ++++++++++++----------- backend/db/sqlc/users.sql.go | 9 ++++--- 9 files changed, 46 insertions(+), 42 deletions(-) diff --git a/backend/api/admin.go b/backend/api/admin.go index 777605c..79d377e 100644 --- a/backend/api/admin.go +++ b/backend/api/admin.go @@ -47,7 +47,7 @@ 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.Time.Format("06.01.02 15:04:05"), + LastAccess: user.LastAccessedAt.Format("06.01.02 15:04:05"), }) } return c.Status(fiber.StatusOK).JSON(response) diff --git a/backend/api/user.go b/backend/api/user.go index b1fc606..744c280 100644 --- a/backend/api/user.go +++ b/backend/api/user.go @@ -40,6 +40,7 @@ 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 @@ -47,9 +48,6 @@ func convertUserResponse(user db.User) UserResponse { if user.MetamaskAddress.Valid { uR.MetamaskAddress = user.MetamaskAddress.String } - if user.LastAccessedAt.Valid { - uR.LastAccessedAt = user.LastAccessedAt.Time - } return uR } diff --git a/backend/db/migrate/000002_add_user.up.sql b/backend/db/migrate/000002_add_user.up.sql index e68bcbf..71fffe1 100644 --- a/backend/db/migrate/000002_add_user.up.sql +++ b/backend/db/migrate/000002_add_user.up.sql @@ -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, + `last_accessed_at` timestamp NOT NULL DEFAULT (now()), `password_changed_at` timestamp NOT NULL DEFAULT (now()), `address_changed_at` timestamp ); diff --git a/backend/db/mock/store.go b/backend/db/mock/store.go index ff6bd30..13b8a98 100644 --- a/backend/db/mock/store.go +++ b/backend/db/mock/store.go @@ -9,6 +9,7 @@ import ( context "context" sql "database/sql" reflect "reflect" + time "time" gomock "github.com/golang/mock/gomock" ) @@ -1266,10 +1267,10 @@ func (mr *MockStoreMockRecorder) GetUserCompScoreSummary(arg0, arg1 interface{}) } // GetUserLastAccessedAt mocks base method. -func (m *MockStore) GetUserLastAccessedAt(arg0 context.Context, arg1 string) (sql.NullTime, error) { +func (m *MockStore) GetUserLastAccessedAt(arg0 context.Context, arg1 string) (time.Time, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetUserLastAccessedAt", arg0, arg1) - ret0, _ := ret[0].(sql.NullTime) + ret0, _ := ret[0].(time.Time) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/backend/db/sqlc/admin.sql.go b/backend/db/sqlc/admin.sql.go index e8d5146..b2aeeed 100644 --- a/backend/db/sqlc/admin.sql.go +++ b/backend/db/sqlc/admin.sql.go @@ -162,20 +162,20 @@ type GetAdminUsersParams struct { } type GetAdminUsersRow struct { - ID int64 `json:"id"` - UserID string `json:"user_id"` - Nickname string `json:"nickname"` - PracBalance float64 `json:"prac_balance"` - WmoiBalance float64 `json:"wmoi_balance"` - RecommenderCode string `json:"recommender_code"` - CreatedAt time.Time `json:"created_at"` - LastAccessedAt sql.NullTime `json:"last_accessed_at"` - Attendance int64 `json:"attendance"` - Referral int64 `json:"referral"` - PracWin int64 `json:"prac_win"` - PracLose int64 `json:"prac_lose"` - CompWin int64 `json:"comp_win"` - CompLose int64 `json:"comp_lose"` + ID int64 `json:"id"` + UserID string `json:"user_id"` + Nickname string `json:"nickname"` + PracBalance float64 `json:"prac_balance"` + WmoiBalance float64 `json:"wmoi_balance"` + RecommenderCode string `json:"recommender_code"` + CreatedAt time.Time `json:"created_at"` + LastAccessedAt time.Time `json:"last_accessed_at"` + Attendance int64 `json:"attendance"` + Referral int64 `json:"referral"` + PracWin int64 `json:"prac_win"` + PracLose int64 `json:"prac_lose"` + CompWin int64 `json:"comp_win"` + CompLose int64 `json:"comp_lose"` } func (q *Queries) GetAdminUsers(ctx context.Context, arg GetAdminUsersParams) ([]GetAdminUsersRow, error) { diff --git a/backend/db/sqlc/models.go b/backend/db/sqlc/models.go index 69af897..c66e5b0 100644 --- a/backend/db/sqlc/models.go +++ b/backend/db/sqlc/models.go @@ -172,7 +172,7 @@ type User struct { WmoiBalance float64 `json:"wmoi_balance"` RecommenderCode string `json:"recommender_code"` CreatedAt time.Time `json:"created_at"` - LastAccessedAt sql.NullTime `json:"last_accessed_at"` + LastAccessedAt time.Time `json:"last_accessed_at"` PasswordChangedAt time.Time `json:"password_changed_at"` AddressChangedAt sql.NullTime `json:"address_changed_at"` } diff --git a/backend/db/sqlc/querier.go b/backend/db/sqlc/querier.go index 86117ff..74c9ad2 100644 --- a/backend/db/sqlc/querier.go +++ b/backend/db/sqlc/querier.go @@ -7,6 +7,7 @@ package db import ( "context" "database/sql" + "time" ) type Querier interface { @@ -90,7 +91,7 @@ type Querier interface { GetUserByNickName(ctx context.Context, nickname string) (User, error) GetUserByRecommenderCode(ctx context.Context, recommenderCode string) (User, error) GetUserCompScoreSummary(ctx context.Context, nickname string) (GetUserCompScoreSummaryRow, error) - GetUserLastAccessedAt(ctx context.Context, userID string) (sql.NullTime, error) + GetUserLastAccessedAt(ctx context.Context, userID string) (time.Time, error) GetUserPracBalance(ctx context.Context, userID string) (float64, error) GetUserPracRankByPNL(ctx context.Context, arg GetUserPracRankByPNLParams) ([]GetUserPracRankByPNLRow, error) GetUserPracRankByROE(ctx context.Context, arg GetUserPracRankByROEParams) ([]GetUserPracRankByROERow, error) diff --git a/backend/db/sqlc/transactions.go b/backend/db/sqlc/transactions.go index 4df4b47..205c4ef 100644 --- a/backend/db/sqlc/transactions.go +++ b/backend/db/sqlc/transactions.go @@ -4,7 +4,6 @@ import ( "bitmoi/backend/contract" "context" "database/sql" - "errors" "fmt" "math/big" "time" @@ -159,32 +158,36 @@ type CheckAttendTxParams struct { func (store *SqlStore) CheckAttendTx(ctx context.Context, arg CheckAttendTxParams) (float64, error) { + var lastAccessed time.Time err := store.execTx(ctx, func(q *Queries) error { user, err := q.GetUser(ctx, arg.UserID) if user.UserID == "" || err != nil { return fmt.Errorf("failed to attendence due to cannot find user. err: %w", err) } - if !user.LastAccessedAt.Valid || user.LastAccessedAt.Time.Before(arg.TodayMidnight) { - _, err = q.UpdateUserLastAccessedAt(ctx, UpdateUserLastAccessedAtParams{ - LastAccessedAt: sql.NullTime{Time: time.Now(), Valid: true}, - UserID: arg.UserID, - }) - if err != nil { - return fmt.Errorf("failed to attendence due to cannot update last accessed at. err: %w", err) - } + lastAccessed = user.LastAccessedAt - err = store.AppendPracBalanceTx(ctx, arg.AppendPracBalanceTxParams) + _, err = q.UpdateUserLastAccessedAt(ctx, UpdateUserLastAccessedAtParams{ + LastAccessedAt: time.Now(), + UserID: arg.UserID, + }) - if err != nil { - return fmt.Errorf("failed to attendence due to cannot update prac balance. err: %w", err) - } - return nil + if err != nil { + return fmt.Errorf("failed to attendence due to cannot update last accessed at. err: %w", err) } - return errors.New("attendance update time has not yet arrived") + return nil }) + if err != nil { + return arg.Amount, err + } + + if lastAccessed.Before(arg.TodayMidnight) { + err = store.AppendPracBalanceTx(ctx, arg.AppendPracBalanceTxParams) + } + return arg.Amount, err + } type SettleImdScoreTxParams struct { diff --git a/backend/db/sqlc/users.sql.go b/backend/db/sqlc/users.sql.go index b4cb169..ed2145a 100644 --- a/backend/db/sqlc/users.sql.go +++ b/backend/db/sqlc/users.sql.go @@ -8,6 +8,7 @@ package db import ( "context" "database/sql" + "time" ) const appendUserCompBalance = `-- name: AppendUserCompBalance :execresult @@ -296,9 +297,9 @@ SELECT last_accessed_at FROM users WHERE user_id = ? ` -func (q *Queries) GetUserLastAccessedAt(ctx context.Context, userID string) (sql.NullTime, error) { +func (q *Queries) GetUserLastAccessedAt(ctx context.Context, userID string) (time.Time, error) { row := q.db.QueryRowContext(ctx, getUserLastAccessedAt, userID) - var last_accessed_at sql.NullTime + var last_accessed_at time.Time err := row.Scan(&last_accessed_at) return last_accessed_at, err } @@ -370,8 +371,8 @@ WHERE user_id = ? ` type UpdateUserLastAccessedAtParams struct { - LastAccessedAt sql.NullTime `json:"last_accessed_at"` - UserID string `json:"user_id"` + LastAccessedAt time.Time `json:"last_accessed_at"` + UserID string `json:"user_id"` } func (q *Queries) UpdateUserLastAccessedAt(ctx context.Context, arg UpdateUserLastAccessedAtParams) (sql.Result, error) {