diff --git a/src/controller/keywordCategory.controller.ts b/src/controller/keywordCategory.controller.ts index 7ac75ca..7465b41 100644 --- a/src/controller/keywordCategory.controller.ts +++ b/src/controller/keywordCategory.controller.ts @@ -6,7 +6,6 @@ import { HttpCode } from '../errors/HttpCode'; import * as KeywordCategoryService from '../service/keywordCategory.service'; import { logger } from '../util/logger'; import { IKeywordCategoryUpdatePayload } from '../model/keywordCategory'; -import mongoose from 'mongoose'; import { CustomRequest } from '../middleware/requestedInfo'; const createKeywordCategory = async (req: Request, res: Response, next: NextFunction) => { diff --git a/src/middleware/requestedInfo.ts b/src/middleware/requestedInfo.ts index 0f551d7..3b52efd 100644 --- a/src/middleware/requestedInfo.ts +++ b/src/middleware/requestedInfo.ts @@ -11,13 +11,13 @@ import { getKeywordCategory } from '../service/keywordCategory.service'; import { IKeyword } from 'src/model/keyword'; import { getUser } from '../service/user.service'; import { IUserDocument } from '../model/user'; -import { IKeywordCategory } from '../model/keywordCategory'; +import { IKeywordCategoryDocument } from '../model/keywordCategory'; export interface CustomRequest extends Request { requestedMeme?: IMemeDocument; requestedUser?: IUserDocument; requestedKeyword?: IKeyword; - requestedKeywordCategory?: IKeywordCategory; + requestedKeywordCategory?: IKeywordCategoryDocument; } export const getRequestedMemeInfo = async ( @@ -119,7 +119,7 @@ export const getRequestedKeywordCategoryInfo = async ( if (_.isNull(categoryName)) { return next(new CustomError(`'categoryId' should be provided`, HttpCode.BAD_REQUEST)); } - const category = await getKeywordCategory(categoryName); + const category: IKeywordCategoryDocument = await getKeywordCategory(categoryName); if (_.isNull(category)) { return next( new CustomError(`KeywordCategory(${categoryName}) does not exist`, HttpCode.NOT_FOUND), diff --git a/src/model/keywordCategory.ts b/src/model/keywordCategory.ts index 896588e..fdef705 100644 --- a/src/model/keywordCategory.ts +++ b/src/model/keywordCategory.ts @@ -1,4 +1,4 @@ -import mongoose, { Schema } from 'mongoose'; +import mongoose, { Schema, Types } from 'mongoose'; export interface IKeywordCategoryCreatePayload { name: string; isRecommend: boolean; @@ -10,9 +10,16 @@ export interface IKeywordCategoryUpdatePayload { } export interface IKeywordCategory { - _id: string; name: string; isRecommend: boolean; +} + +export interface IKeywordCategoryDocument extends Document { + _id: Types.ObjectId; + name: string; + isRecommend: boolean; + createdAt: Date; + updatedAt: Date; isDeleted: boolean; } @@ -29,7 +36,7 @@ const KeywordCategorySchema: Schema = new Schema( }, ); -export const KeywordCategoryModel = mongoose.model( +export const KeywordCategoryModel = mongoose.model( 'KeywordCategory', KeywordCategorySchema, ); diff --git a/src/service/keywordCategory.service.ts b/src/service/keywordCategory.service.ts index 9c64b90..3fa1516 100644 --- a/src/service/keywordCategory.service.ts +++ b/src/service/keywordCategory.service.ts @@ -1,7 +1,7 @@ import { KeywordCategoryModel, IKeywordCategoryCreatePayload, - IKeywordCategory, + IKeywordCategoryDocument, } from '../model/keywordCategory'; import CustomError from '../errors/CustomError'; import { HttpCode } from '../errors/HttpCode'; @@ -9,14 +9,16 @@ import { logger } from '../util/logger'; async function createKeywordCategory( info: IKeywordCategoryCreatePayload, -): Promise { +): Promise { try { - const newCategory = new KeywordCategoryModel({ + const newCategory = await KeywordCategoryModel.create({ ...info, }); await newCategory.save(); - logger.info(`Created new keyword category: ${JSON.stringify(newCategory)}`); - return newCategory.toObject(); + const newCategoryObj = newCategory.toObject(); + + logger.info(`Created new keyword category: ${JSON.stringify(newCategoryObj)}`); + return newCategoryObj; } catch (err) { logger.error(`Failed to create category ${info.name}: ${err.message}`); throw new CustomError('Failed to create category', HttpCode.INTERNAL_SERVER_ERROR); @@ -25,19 +27,20 @@ async function createKeywordCategory( async function updateKeywordCategory( categoryName: string, - updateInfos: Partial, -): Promise { + updateInfo: any, +): Promise { const updatedCategory = await KeywordCategoryModel.findOneAndUpdate( - { name: categoryName }, - updateInfos, - { - new: true, - }, - ).lean(); + { name: categoryName, isDeleted: false }, + { $set: updateInfo }, + { new: true }, + ); + if (!updatedCategory) { throw new CustomError(`Category with ID ${updatedCategory} not found`, HttpCode.NOT_FOUND); } - return updatedCategory; + logger.info(`Update keyword category - category(${categoryName})`); + + return updatedCategory.toObject(); } async function deleteKeywordCategory(categoryName: string): Promise { @@ -50,13 +53,13 @@ async function deleteKeywordCategory(categoryName: string): Promise { return true; } -async function getKeywordCategory(categoryName: string): Promise { +async function getKeywordCategory(categoryName: string): Promise { try { const keywordCategory = await KeywordCategoryModel.findOne({ name: categoryName, isDeleted: false, - }).lean(); - return keywordCategory; + }); + return keywordCategory.toObject(); } catch (err) { logger.info(`Failed to get a KeywordCategory Info By id (${categoryName})`); }