diff --git a/packages/client/src/data/document.tsx b/packages/client/src/data/document.tsx index 4999301b..e9aad94c 100644 --- a/packages/client/src/data/document.tsx +++ b/packages/client/src/data/document.tsx @@ -30,7 +30,7 @@ export const useRecentDocuments = () => { const { data, error, isLoading, refetch } = useQuery( DocumentApiDefinition.recent.client(), getRecentVisitedDocuments, - { staleTime: 0 } + { staleTime: 0, refetchOnMount: true } ); return { data, error, loading: isLoading, refresh: refetch }; }; diff --git a/packages/client/src/pages/_app.tsx b/packages/client/src/pages/_app.tsx index 799d7216..257a8189 100644 --- a/packages/client/src/pages/_app.tsx +++ b/packages/client/src/pages/_app.tsx @@ -16,7 +16,7 @@ class MyApp extends App<{ isMobile: boolean }> { queryClient: new QueryClient({ defaultOptions: { queries: { - refetchOnMount: true, + refetchOnMount: false, refetchOnWindowFocus: true, retry: false, staleTime: 30000, diff --git a/packages/server/src/helpers/date.helper.ts b/packages/server/src/helpers/date.helper.ts index e95825a9..060444d1 100644 --- a/packages/server/src/helpers/date.helper.ts +++ b/packages/server/src/helpers/date.helper.ts @@ -9,3 +9,20 @@ export const dateFormat = (date = null, format = 'yyyy-MM-dd HH:mm:ss') => { return dateFormatFn(t, format); }; + +export const convertDateToMysqlTimestamp = (time) => { + const date = new Date(time); + return ( + date.getUTCFullYear() + + '-' + + ('00' + (date.getUTCMonth() + 1)).slice(-2) + + '-' + + ('00' + date.getUTCDate()).slice(-2) + + ' ' + + ('00' + date.getUTCHours()).slice(-2) + + ':' + + ('00' + date.getUTCMinutes()).slice(-2) + + ':' + + ('00' + date.getUTCSeconds()).slice(-2) + ); +}; diff --git a/packages/server/src/services/view.service.ts b/packages/server/src/services/view.service.ts index 427c40d7..efafc635 100644 --- a/packages/server/src/services/view.service.ts +++ b/packages/server/src/services/view.service.ts @@ -1,4 +1,5 @@ import { ViewEntity } from '@entities/view.entity'; +import { convertDateToMysqlTimestamp } from '@helpers/date.helper'; import { ONE_DAY } from '@helpers/log.helper'; import { parseUserAgent } from '@helpers/ua.helper'; import { Injectable } from '@nestjs/common'; @@ -68,43 +69,20 @@ export class ViewService { }> > { const now = Date.now(); - const queryBuilder = this.viewRepo.createQueryBuilder('view'); + const from = convertDateToMysqlTimestamp(now - 3 * ONE_DAY); + const end = convertDateToMysqlTimestamp(now); + const count = 20; - queryBuilder - .where('view.userId=:userId', { userId }) - .andWhere('view.createdAt BETWEEN :start AND :end', { - start: new Date(now - 3 * ONE_DAY), - end: new Date(now), - }) - .orderBy('view.createdAt', 'DESC'); - - const ret = await queryBuilder.getMany(); - - // const map = {}; - - // ret.forEach((item) => { - // const key = item.documentId; - // if (!map[key]) { - // map[key] = item; - // } - // const mapItem = map[key]; - // const isGreaterThan = new Date(mapItem.createdAt).valueOf() < new Date(item.createdAt).valueOf(); - // if (isGreaterThan) { - // map[key] = item; - // } - // }); - - const res = ret.slice(0, 20).map((item) => { - return { - documentId: item.documentId, - visitedAt: item.createdAt, - }; - }); - - // res.sort((a, b) => { - // return -new Date(a.visitedAt).valueOf() + new Date(b.visitedAt).valueOf(); - // }); - - return res; + const ret = await this.viewRepo.query( + `SELECT documentId, ANY_VALUE(created_at) as visitedAt + FROM view + WHERE view.userId = '${userId}' + AND (view.created_at BETWEEN '${from}' AND '${end}') + GROUP BY documentId + LIMIT ${count} + ` + ); + ret.sort((a, b) => -new Date(a.visitedAt).getTime() + new Date(b.visitedAt).getTime()); + return ret; } }