server: fix view record time

This commit is contained in:
fantasticit 2022-05-25 10:00:59 +08:00
parent bdfdef3cd3
commit 5e6f283032
1 changed files with 17 additions and 11 deletions

View File

@ -19,16 +19,22 @@ export class ViewService {
* @returns * @returns
*/ */
async create({ userId = 'public', documentId, userAgent }) { async create({ userId = 'public', documentId, userAgent }) {
const data = { const old = await this.viewRepo.findOne({ documentId, userId });
let data;
if (old) {
data = await this.viewRepo.merge(old, {
updatedAt: convertDateToMysqlTimestamp(Date.now()),
});
} else {
data = await this.viewRepo.create({
userId, userId,
documentId, documentId,
originUserAgent: userAgent, originUserAgent: userAgent,
parsedUserAgent: parseUserAgent(userAgent).text, parsedUserAgent: parseUserAgent(userAgent).text,
}; });
}
const res = await this.viewRepo.create(data); const ret = await this.viewRepo.save(data);
const ret = await this.viewRepo.save(res);
return ret; return ret;
} }
@ -74,10 +80,10 @@ export class ViewService {
const count = 20; const count = 20;
const ret = await this.viewRepo.query( const ret = await this.viewRepo.query(
`SELECT documentId, ANY_VALUE(created_at) as visitedAt `SELECT documentId, ANY_VALUE(updated_at) as visitedAt
FROM view FROM view
WHERE view.userId = '${userId}' WHERE view.userId = '${userId}'
AND (view.created_at BETWEEN '${from}' AND '${end}') AND (view.updated_at BETWEEN '${from}' AND '${end}')
GROUP BY documentId GROUP BY documentId
LIMIT ${count} LIMIT ${count}
` `