/* * @author gs * @date 2020/10/15 14:07 * @modified-user songxiwen * @modified-date 2020/10/22 13:54 * @modified-description 增加map方法 */ import { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { BaseService } from '../../../base/base.service'; import { MongoDBCollection } from '../../../constant'; import { KitCategoryType } from './type/kit.type'; import { KitCategory } from '../model/kit.category'; @Injectable() export class KitCategoryService extends BaseService { constructor( @InjectModel(MongoDBCollection.KitCategory) protected readonly model: Model ) { super(model); } async getKitCategoryMap(): Promise<{ [key: string]: KitCategoryType }> { const kitCategoryList = await this.model.find(); return kitCategoryList.reduce( ( previousValue: { [key: string]: KitCategoryType }, currentValue: KitCategory ) => ({ ...previousValue, [currentValue._id.toString()]: { id: currentValue._id.toString(), name: currentValue.name, updatedAt: currentValue.updatedAt, createdAt: currentValue.createdAt, isEnabled: currentValue.isEnabled } }), {} ); } }