Skip to content

Commit

Permalink
updateLastAccessedTime +9H
Browse files Browse the repository at this point in the history
  • Loading branch information
IGSON2 committed May 5, 2024
1 parent 53bb538 commit 6c91a8a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
5 changes: 1 addition & 4 deletions backend/api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"math"
"strings"
"time"

"github.com/gofiber/fiber/v2"
)
Expand Down Expand Up @@ -48,9 +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"),
// Mysql DB의 time_zone이 Asia/Seoul이여도, Default로 생성되는 값에만 적용되고 Update로 변환되는 argument에는 적용되지 않는다.
// LastAccess 필드는 Default로 생성되지 않고, update되기 때문에 client에게 제공할 때 별도의 time zone 변환이 필요하다.
LastAccess: user.LastAccessedAt.Time.In(time.Local).Format("06.01.02 15:04:05"),
LastAccess: user.LastAccessedAt.Time.Format("06.01.02 15:04:05"),
})
}
return c.Status(fiber.StatusOK).JSON(response)
Expand Down
3 changes: 2 additions & 1 deletion backend/db/sqlc/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ func (store *SqlStore) CheckAttendTx(ctx context.Context, arg CheckAttendTxParam
lastAccessed = user.LastAccessedAt.Time

_, err = q.UpdateUserLastAccessedAt(ctx, UpdateUserLastAccessedAtParams{
LastAccessedAt: sql.NullTime{Time: time.Now(), Valid: true},
// Mysql DB의 time_zone이 Asia/Seoul이여도, Default로 생성되는 값에만 적용되고 Update로 변환되는 argument에는 UTC가 적용된다.
LastAccessedAt: sql.NullTime{Time: time.Now().Add(9 * time.Hour), Valid: true},
UserID: arg.UserID,
})

Expand Down
18 changes: 8 additions & 10 deletions backend/db/sqlc/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,21 @@ func TestLastaccessedAt(t *testing.T) {
store := newTestStore(t)

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

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

_, err = store.UpdateUserLastAccessedAt(context.Background(), UpdateUserLastAccessedAtParams{
LastAccessedAt: sql.NullTime{Time: now.Add(9 * time.Hour), Valid: true},
_, err := store.UpdateUserLastAccessedAt(context.Background(), UpdateUserLastAccessedAtParams{
LastAccessedAt: sql.NullTime{Time: now, 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)
crZ, _ := userA.CreatedAt.Zone()
laZ, _ := userA.LastAccessedAt.Time.Zone()
nowZ, _ := now.Zone()
t.Logf("now: %v, created_at: %v, last_accessed_at: %v", nowZ, crZ, laZ)
t.Logf("now: %v, last_accessed_at: %v", now, userA.LastAccessedAt.Time)
}

0 comments on commit 6c91a8a

Please sign in to comment.