Skip to content

Commit

Permalink
update user.lastAccessed time field
Browse files Browse the repository at this point in the history
  • Loading branch information
IGSON2 committed May 5, 2024
1 parent b45cf43 commit 4732c98
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 42 deletions.
2 changes: 1 addition & 1 deletion backend/api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions backend/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@ 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
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,
`last_accessed_at` timestamp NOT NULL DEFAULT (now()),
`password_changed_at` timestamp NOT NULL DEFAULT (now()),
`address_changed_at` timestamp
);
Expand Down
5 changes: 3 additions & 2 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: 2 additions & 1 deletion backend/db/sqlc/querier.go

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

33 changes: 18 additions & 15 deletions backend/db/sqlc/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bitmoi/backend/contract"
"context"
"database/sql"
"errors"
"fmt"
"math/big"
"time"
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions backend/db/sqlc/users.sql.go

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

0 comments on commit 4732c98

Please sign in to comment.