diff --git a/src/modules/comment/comment.controller.ts b/src/modules/comment/comment.controller.ts index 0bafea46..b40f877d 100644 --- a/src/modules/comment/comment.controller.ts +++ b/src/modules/comment/comment.controller.ts @@ -120,7 +120,7 @@ export class CommentController { @Query() query: CommentRefTypesDto, ) { if (!isMaster) { - await this.commentService.ValidAuthorName(body.author) + await this.commentService.validAuthorName(body.author) } const { ref } = query @@ -176,7 +176,7 @@ export class CommentController { @IpLocation() ipLocation: IpRecord, ) { if (!isMaster) { - await this.commentService.ValidAuthorName(author) + await this.commentService.validAuthorName(author) } const { id } = params diff --git a/src/modules/comment/comment.service.ts b/src/modules/comment/comment.service.ts index e9300f3f..9f71924a 100644 --- a/src/modules/comment/comment.service.ts +++ b/src/modules/comment/comment.service.ts @@ -148,7 +148,7 @@ export class CommentService { return comment } - async ValidAuthorName(author: string): Promise { + async validAuthorName(author: string): Promise { const isExist = await this.userService.model.findOne({ name: author, }) diff --git a/src/modules/recently/recently.model.ts b/src/modules/recently/recently.model.ts index 260daf59..1e50c7a6 100644 --- a/src/modules/recently/recently.model.ts +++ b/src/modules/recently/recently.model.ts @@ -1,9 +1,17 @@ -import { IsMongoId, IsOptional, IsString } from 'class-validator' +import { + IsEnum, + IsMongoId, + IsOptional, + IsString, + ValidateIf, +} from 'class-validator' import { modelOptions, prop } from '@typegoose/typegoose' import { BaseModel } from '~/shared/model/base.model' +import { CommentRefTypes } from '../comment/comment.model' + export type RefType = { title: string url: string @@ -18,12 +26,6 @@ export class RecentlyModel extends BaseModel { @prop({ required: true }) @IsString() content: string - @prop() - @IsOptional() - @IsMongoId() - refId?: string - - ref?: RefType /** * @deprecated @@ -39,4 +41,30 @@ export class RecentlyModel extends BaseModel { @IsString() @IsOptional() language?: string + + @prop({ refPath: 'refType' }) + @IsOptional() + @IsMongoId() + ref: RefType + + @prop({ enum: CommentRefTypes }) + @IsEnum(CommentRefTypes) + @ValidateIf((model) => model.ref) + refType: CommentRefTypes + + get refId() { + return (this.ref as any)?._id ?? this.ref + } + + set refId(id: string) { + return + // if (!id) { + // return + // } + // if (this.ref) { + // ;(this.ref as any)._id = id + // } else { + // this.ref = id as any + // } + } } diff --git a/src/modules/recently/recently.service.ts b/src/modules/recently/recently.service.ts index 0a161abc..1a3ece1a 100644 --- a/src/modules/recently/recently.service.ts +++ b/src/modules/recently/recently.service.ts @@ -21,7 +21,15 @@ export class RecentlyService { } async getAll() { - return this.model.find().sort({ created: -1 }).lean() + return this.model + .find() + .sort({ created: -1 }) + + .populate({ + path: 'ref', + select: '-text', + }) + .lean() } async getOffset({ @@ -49,10 +57,25 @@ export class RecentlyService { ) .limit(size) .sort({ _id: -1 }) + .populate([ + { + path: 'ref', + select: '-text', + }, + ]) .lean() } async getLatestOne() { - return await this.model.findOne().sort({ created: -1 }).lean() + return await this.model + .findOne() + .sort({ created: -1 }) + .populate([ + { + path: 'ref', + select: '-text', + }, + ]) + .lean() } async create(model: RecentlyModel) { @@ -67,14 +90,29 @@ export class RecentlyService { content: model.content, language: model.language, project: model.project, - refId: model.refId, + ref: model.refId, + refType: model.refType, }) + + const withRef = await this.model + .findById(res._id) + .populate([ + { + path: 'ref', + select: '-text', + }, + ]) + .lean() process.nextTick(async () => { - await this.eventManager.broadcast(BusinessEvents.RECENTLY_CREATE, res, { - scope: EventScope.TO_SYSTEM_VISITOR, - }) + await this.eventManager.broadcast( + BusinessEvents.RECENTLY_CREATE, + withRef, + { + scope: EventScope.TO_SYSTEM_VISITOR, + }, + ) }) - return res + return withRef } async delete(id: string) {