fix: fix get recent documents

This commit is contained in:
fantasticit 2022-05-25 00:49:11 +08:00
parent 429f9bce11
commit 0f3e1fc2dc
4 changed files with 34 additions and 39 deletions

View File

@ -30,7 +30,7 @@ export const useRecentDocuments = () => {
const { data, error, isLoading, refetch } = useQuery( const { data, error, isLoading, refetch } = useQuery(
DocumentApiDefinition.recent.client(), DocumentApiDefinition.recent.client(),
getRecentVisitedDocuments, getRecentVisitedDocuments,
{ staleTime: 0 } { staleTime: 0, refetchOnMount: true }
); );
return { data, error, loading: isLoading, refresh: refetch }; return { data, error, loading: isLoading, refresh: refetch };
}; };

View File

@ -16,7 +16,7 @@ class MyApp extends App<{ isMobile: boolean }> {
queryClient: new QueryClient({ queryClient: new QueryClient({
defaultOptions: { defaultOptions: {
queries: { queries: {
refetchOnMount: true, refetchOnMount: false,
refetchOnWindowFocus: true, refetchOnWindowFocus: true,
retry: false, retry: false,
staleTime: 30000, staleTime: 30000,

View File

@ -9,3 +9,20 @@ export const dateFormat = (date = null, format = 'yyyy-MM-dd HH:mm:ss') => {
return dateFormatFn(t, format); 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)
);
};

View File

@ -1,4 +1,5 @@
import { ViewEntity } from '@entities/view.entity'; import { ViewEntity } from '@entities/view.entity';
import { convertDateToMysqlTimestamp } from '@helpers/date.helper';
import { ONE_DAY } from '@helpers/log.helper'; import { ONE_DAY } from '@helpers/log.helper';
import { parseUserAgent } from '@helpers/ua.helper'; import { parseUserAgent } from '@helpers/ua.helper';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
@ -68,43 +69,20 @@ export class ViewService {
}> }>
> { > {
const now = Date.now(); 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 const ret = await this.viewRepo.query(
.where('view.userId=:userId', { userId }) `SELECT documentId, ANY_VALUE(created_at) as visitedAt
.andWhere('view.createdAt BETWEEN :start AND :end', { FROM view
start: new Date(now - 3 * ONE_DAY), WHERE view.userId = '${userId}'
end: new Date(now), AND (view.created_at BETWEEN '${from}' AND '${end}')
}) GROUP BY documentId
.orderBy('view.createdAt', 'DESC'); LIMIT ${count}
`
const ret = await queryBuilder.getMany(); );
ret.sort((a, b) => -new Date(a.visitedAt).getTime() + new Date(b.visitedAt).getTime());
// const map = {}; return ret;
// 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;
} }
} }