From 0122d39573b514f6ca9b40e35ef7637a75212ae0 Mon Sep 17 00:00:00 2001 From: conganhhcmus Date: Sat, 30 Mar 2024 00:14:54 +0700 Subject: [PATCH] fix: update same time when view chapter --- src/models/viewer.ts | 3 +-- src/repositories/viewer.ts | 6 ++++-- src/services/chapters.ts | 2 ++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/models/viewer.ts b/src/models/viewer.ts index 151ad75..33e2b42 100644 --- a/src/models/viewer.ts +++ b/src/models/viewer.ts @@ -1,11 +1,10 @@ -import moment from 'moment'; import mongoose, { Types } from 'mongoose'; const ViewerSchema = new mongoose.Schema({ comicId: { type: Types.ObjectId, required: true, index: true }, chapterId: { type: Types.ObjectId, required: true, index: true }, userId: { type: Types.ObjectId, required: false, default: null, index: true }, - createTime: { type: Date, required: false, default: moment().utc().toDate(), index: true }, + createTime: { type: Date, required: true, index: true }, }); export default mongoose.model('Viewer', ViewerSchema); diff --git a/src/repositories/viewer.ts b/src/repositories/viewer.ts index 874929a..2203407 100644 --- a/src/repositories/viewer.ts +++ b/src/repositories/viewer.ts @@ -7,8 +7,10 @@ import { Types } from 'mongoose'; export const addViewer = (values: Record): Promise => new ViewerModel(values).save().then((viewer) => viewer.toObject()); -export const updateViewer = async (values: Record) => - await ViewerModel.updateOne({ ...values }, { $set: { ...values } }, { upsert: true }); +export const updateViewer = async (values: Record) => { + const { createTime, ...query } = values; + return await ViewerModel.updateOne({ ...query }, { $set: { ...values } }, { upsert: true }); +}; export const getViewerGroupByUserId = async (q: string, page: number, type: number): Promise => { const date = moment().utc().endOf('day').subtract(type, 'days').toDate(); diff --git a/src/services/chapters.ts b/src/services/chapters.ts index 479ea25..89c9d5c 100644 --- a/src/services/chapters.ts +++ b/src/services/chapters.ts @@ -3,6 +3,7 @@ import * as comicRepository from '@/repositories/comics'; import * as viewerRepository from '@/repositories/viewer'; import { Chapter, ChapterResponse } from '@/types/chapter'; import { Viewer } from '@/types/viewer'; +import moment from 'moment'; export const getAllChapterByComicId = async (page: number, q: string, comicId: string) => { const result = await chapterRepository.getAllChapterByComicId(page, q, comicId); @@ -26,6 +27,7 @@ export const getChapterById = async (id: string, userId: string, skipCount: bool userId: userId, chapterId: id, comicId: result.comicId, + createTime: moment().utc().toDate(), } as Viewer; if (userId) {