Skip to content

Commit

Permalink
fix: update same time when view chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
conganhhcmus committed Mar 29, 2024
1 parent 5e7c68e commit 0122d39
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/models/viewer.ts
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 4 additions & 2 deletions src/repositories/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { Types } from 'mongoose';

export const addViewer = (values: Record<string, any>): Promise<ViewerResponse> => new ViewerModel(values).save().then((viewer) => viewer.toObject());

export const updateViewer = async (values: Record<string, any>) =>
await ViewerModel.updateOne({ ...values }, { $set: { ...values } }, { upsert: true });
export const updateViewer = async (values: Record<string, any>) => {
const { createTime, ...query } = values;
return await ViewerModel.updateOne({ ...query }, { $set: { ...values } }, { upsert: true });
};

export const getViewerGroupByUserId = async (q: string, page: number, type: number): Promise<AnalyticsData> => {
const date = moment().utc().endOf('day').subtract(type, 'days').toDate();
Expand Down
2 changes: 2 additions & 0 deletions src/services/chapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit 0122d39

Please sign in to comment.