/* * @author gs * @date 2020/10/15 11:34 * @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 { Kit } from '../model/kit'; import { KitType } from './type/kit.type'; @Injectable() export class KitService extends BaseService { constructor( @InjectModel(MongoDBCollection.Kit) protected readonly model: Model ) { super(model); } async getKitMap(): Promise<{ [key: string]: KitType }> { const kitList = await this.model.find(); return kitList.reduce( (previousValue: { [key: string]: KitType }, currentValue: Kit) => ({ ...previousValue, [currentValue._id.toString()]: { id: currentValue._id.toString(), name: currentValue.name, updatedAt: currentValue.updatedAt, createdAt: currentValue.createdAt, isEnabled: currentValue.isEnabled, description: currentValue.description || { zhCN: '', en: '' }, authorId: currentValue.authorId || '', type: currentValue.type, cover: currentValue.cover || '', thumb: currentValue.thumb || '', banner: currentValue.banner || '', purchaseCount: currentValue.purchaseCount, price: currentValue.price, discount: currentValue.discount, memberDiscount: currentValue.memberDiscount } }), {} ); } }