Skip to content

Commit 4e02fd4

Browse files
pokerspokers
and
pokers
authored
[BMOL-24] Refactorying, timezone utility (#11)
* BMOL-24 Refactorying * BMOL-24 Refactorying and use Timezone. --------- Co-authored-by: pokers <[email protected]>
1 parent d7222d0 commit 4e02fd4

8 files changed

+55
-80
lines changed

deploy/dbMigrations/20230311173534_add_mybook_table.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11

2+
/*
3+
test data
4+
insert into tbl_mybook values('38d44dba-910a-49cb-88b0-440353164777', '31a1c45d-4026-4533-a63d-b44d58408556', 'testbook1', 'testboo1 contents', '["author1", "author2"]', '["trans1", "trans2"]', 'publisher1', 'thumbnail url', '00000000', 0, 512, '["image1", "image2"]', false, false, '{}', now(), now(), null);
5+
insert into tbl_mybook values('4b4acc85-c0de-41ed-8077-fa9d8a1807e9', '31a1c45d-4026-4533-a63d-b44d58408556', 'testbook2', 'testboo2 contents', '["author2-1", "author2-2"]', '["trans2-1", "trans2-2"]', 'publisher2', 'thumbnail url 2', '11111111', 0, 600, '["image2-1", "image2-2"]', false, false, '{}', now(), now(), null);
6+
insert into tbl_mybook values('84e65492-0bc3-4507-9f14-36edf07ba9a0', '31a1c45d-4026-4533-a63d-b44d58408556', 'testbook2', 'testboo2 contents', '["author3-1", "author3-2"]', '["trans3-1", "trans3-2"]', 'publisher3', 'thumbnail url 3', '22222222', 0, 812, '["image3-1", "image3-2"]', false, false, '{}', now(), now(), null);
7+
*/
28
exports.up = (knex) => {
39
return knex.schema.dropTableIfExists('tbl_mybook').createTable('tbl_mybook', (table)=>{
410
table.uuid('id').notNullable().primary().index('tbl_mybook_id_index')

deploy/dbMigrations/20230311175534_add_myhistory_table.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
1+
/**
2+
* test data
3+
insert into tbl_myhistory values('75d332e1-3de0-44ee-95df-265f0cacdbf0', '31a1c45d-4026-4533-a63d-b44d58408556', '38d44dba-910a-49cb-88b0-440353164777', 360, '2023-03-05 18:34:33', '2023-03-05 18:34:33', null);
4+
insert into tbl_myhistory values('5be35a18-fd1a-4136-9837-50ac6243a88e', '31a1c45d-4026-4533-a63d-b44d58408556', '38d44dba-910a-49cb-88b0-440353164777', 237, '2023-03-06 21:30:00', '2023-03-06 21:30:00', null);
5+
insert into tbl_myhistory values('94b71146-ec35-4f00-ab5a-20551a17017f', '31a1c45d-4026-4533-a63d-b44d58408556', '38d44dba-910a-49cb-88b0-440353164777', 245, '2023-03-07 03:30:00', '2023-03-07 03:30:00', null);
6+
insert into tbl_myhistory values('27245397-22ea-4af5-b5c2-8f1dcc4a2947', '31a1c45d-4026-4533-a63d-b44d58408556', '38d44dba-910a-49cb-88b0-440353164777', 3678, '2023-03-12 01:30:00', '2023-03-12 01:30:00', null);
7+
insert into tbl_myhistory values('c7d2e046-df3b-4cb4-b1c4-e9c1fdf2eb36', '31a1c45d-4026-4533-a63d-b44d58408556', '38d44dba-910a-49cb-88b0-440353164777', 341, '2023-03-17 13:30:00', '2023-03-17 13:30:00', null);
8+
insert into tbl_myhistory values('7fb01edb-e29e-48bc-a5cf-70d977ddd5de', '31a1c45d-4026-4533-a63d-b44d58408556', '38d44dba-910a-49cb-88b0-440353164777', 741, '2023-03-17 17:30:00', '2023-03-17 17:30:00', null);
9+
insert into tbl_myhistory values('8d3b0fd6-afe9-4c6a-b6f6-50a23fbc71e4', '31a1c45d-4026-4533-a63d-b44d58408556', '38d44dba-910a-49cb-88b0-440353164777', 861, '2023-03-17 22:30:00', '2023-03-17 22:30:00', null);
10+
insert into tbl_myhistory values('cfb5a016-8d5f-410c-9497-e7be1a0a19db', '31a1c45d-4026-4533-a63d-b44d58408556', '38d44dba-910a-49cb-88b0-440353164777', 341, '2023-03-19 10:30:00', '2023-03-19 10:30:00', null);
11+
*/
212
exports.up = (knex) => {
313
return knex.schema.dropTableIfExists('tbl_myhistory').createTable('tbl_myhistory', (table)=>{
414
table.uuid('id').notNullable().primary().index('tbl_myhistory_id_index')

src/app.js

+6-47
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,19 @@
11
const koa = require('koa')
22
const koaBody = require('koa-body')
3-
const uuid = require('uuid')
43

54
const { config } = require('../deploy/config/config')
65
const { initRouter } = require('./routes')
7-
const { log, requestLogger } = require('./utils/logging')
6+
const { log } = require('./utils/logging')
7+
8+
const { corsMiddleware, loggingMiddleware } = require('./middleware')
89

910
const app = new koa()
1011
app.use(koaBody.koaBody())
1112

12-
// Set CORS
13-
app.use(async (ctx, next)=>{
14-
if(ctx.method === 'OPTIONS'){
15-
if (!ctx.get('Access-Control-Requrest-Method')){
16-
await next()
17-
}
18-
ctx.set('Access-Control-Allow-Origin', '*')
19-
ctx.set('Access-Control-Allow-Credentials', 'true')
20-
ctx.set('Access-Control-Allow-Methods', 'GET')
21-
ctx.set('Access-Control-Allow-Headers', '*')
22-
ctx.status = 204
23-
}else{
24-
ctx.set('Access-Control-Allow-Origin', '*')
25-
ctx.set('Access-Control-Allow-Credentials', 'true')
26-
ctx.set('Vary', 'Origin')
27-
await next();
28-
}
29-
})
30-
31-
// error logging
32-
app.use(async (ctx, next)=>{
33-
ctx.state.requestId = uuid.v4()
34-
try{
35-
await next()
36-
}catch(err){
37-
const errorObject = {
38-
...err,
39-
message: err.message,
40-
trace: err.stack,
41-
}
42-
43-
ctx.status = err.status || err.statusCode || 500
44-
ctx.body = {
45-
error: errorObject,
46-
meta: {
47-
requestId: ctx.state.requestId,
48-
now: +new Date(),
49-
},
50-
}
5113

52-
requestLogger(ctx, {
53-
message: err.message,
54-
trace: err.stack,
55-
})
56-
}
57-
})
14+
// set middleware
15+
app.use(corsMiddleware)
16+
app.use(loggingMiddleware)
5817

5918
const router = initRouter()
6019
app.use(router.routes())

src/controllers/accountController.js

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const joinMe = async (ctx)=>{
2929
const {
3030
query,
3131
headers,
32-
request: { body }
3332
} = ctx
3433
const inst = new AccountService()
3534
inst.addNewAccount()

src/dao/bookTimerDao.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,30 @@ const { MyBookNotFound, InternalServerError } = require('../services/errorServic
66
class BookTimerDao {
77
constructor(){
88
this._daoName = 'BookTimerDao'
9+
this._repo = {
10+
bookHistory: new BookHistoryRepository(),
11+
account: new AccountRepository(),
12+
}
913
}
1014

1115
get daoName(){
1216
return this._daoName
1317
}
1418

1519
async getBookTimerInfoByBookId(bookId){
16-
const bookHistoryRepo = new BookHistoryRepository()
17-
const accountRepo = new AccountRepository()
1820
const bookTimerDecorator = new BookTimerDecorator()
1921

20-
const accountInfo = await accountRepo.getAccountByBookId(bookId)
21-
const bookHistoryInfo = await bookHistoryRepo.getBookHistoryListByBookId(bookId)
22+
const [accountInfo, bookHistoryInfo] = await Promise.all([
23+
this._repo.account.getAccountByBookId(bookId),
24+
this._repo.bookHistory.getBookHistoryListByBookId(bookId)
25+
])
2226

2327
// deleted book check
2428
if (!accountInfo || !bookHistoryInfo){
2529
throw new MyBookNotFound(bookId)
2630
}
2731

28-
const bookTimerInfo = await bookTimerDecorator.decorateBookTimer(accountInfo, bookHistoryInfo)
32+
const bookTimerInfo = bookTimerDecorator.decorateBookTimer(bookId, {accountInfo, bookHistoryInfo})
2933

3034
const result = {
3135
data : bookTimerInfo

src/dao/bookTimerDecorator.js

+20-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
const { getTimezoneDate, isSameDay, getYMDHMS } = require('../utils/timezoneDate')
22

33
class BookTimerDecorator {
44
constructor(){
@@ -9,35 +9,30 @@ class BookTimerDecorator {
99
return this._serviceName
1010
}
1111

12-
async decorateBookTimer(userInfo, historyInfo){
13-
14-
let history = new Array()
15-
let daily = 0
16-
17-
// today date, format: yyyy-mm-dd
18-
const date = new Date()
19-
const today = date.getFullYear() + '-' + ('0'+(date.getMonth() + 1)).slice(-2) + '-' + date.getDate()
12+
decorateBookTimer(bookId, {accountInfo, bookHistoryInfo}){
2013

21-
// history array
22-
for (const h of historyInfo){
23-
24-
// if created_at == today, add daily time
25-
if (h.created_at.toISOString().split('T')[0] == today ){
26-
daily += h.reading_time
27-
}
14+
let dailyTime = 0
15+
// 오늘 날짜 계산 yyyy-mm-dd format
16+
const now = getTimezoneDate()
17+
const history = bookHistoryInfo.map(item=>{
18+
const itemDate = getTimezoneDate(item.updated_at)
2819

29-
history.push({"id": h.id,
30-
"date": h.created_at.toISOString().split('T')[0],
31-
"time": h.reading_time})
32-
33-
}
20+
if(isSameDay(now, itemDate)){
21+
dailyTime += item.reading_time
22+
}
23+
return {
24+
id: item.id,
25+
date: getYMDHMS(itemDate),
26+
time: item.reading_time
27+
}
28+
})
3429

3530
const data = {
36-
"user_id": userInfo.user_id,
37-
"target_time": userInfo.target_read_time,
38-
"daily": daily,
31+
"user_id": accountInfo.user_id,
32+
"target_time": accountInfo.target_read_time,
33+
"daily": dailyTime, // 오늘 모든 책을 읽은 전체 시간
3934
"book":{
40-
"book_id": historyInfo[0].mybook_id,
35+
"book_id": bookId,
4136
"history": history
4237
}
4338
}

src/dao/repositories/bookHistoryRepository.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class BookHistoryRepository {
1717
.where('tb.id', bookId)
1818
.whereNull('tb.deleted_at')
1919
.whereNull('th.deleted_at')
20-
.orderBy('th.created_at', 'desc')
20+
.orderBy('th.updated_at', 'desc')
2121
.limit(100)
2222

2323
return await query

src/services/accountService.js

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class AccountService {
3434
throw new InvalidOauthType(oauthType)
3535
}
3636

37+
38+
3739
const accountDao = new AccountDao()
3840
const accountInfo = await accountDao.getAccountInfoByOauthId(oauthId, oauthType)
3941
return accountInfo

0 commit comments

Comments
 (0)