|
1 | 1 | import { Injectable } from '@nestjs/common'; |
2 | 2 | import { InjectModel } from '@nestjs/mongoose'; |
3 | 3 | import { Agents } from '~/core/agents/_schemas/agents.schema'; |
4 | | -import { Model } from 'mongoose'; |
| 4 | +import { Document, Model, ModifyResult, Query, QueryOptions, SaveOptions, Types, UpdateQuery } from 'mongoose'; |
5 | 5 | import { AbstractServiceSchema } from '~/_common/abstracts/abstract.service.schema'; |
| 6 | +import { AgentsCreateDto } from './_dto/agents.dto'; |
| 7 | +import { hash } from 'argon2'; |
6 | 8 |
|
7 | 9 | @Injectable() |
8 | 10 | export class AgentsService extends AbstractServiceSchema { |
9 | 11 | constructor(@InjectModel(Agents.name) protected _model: Model<Agents>) { |
10 | 12 | super(); |
11 | 13 | } |
| 14 | + |
| 15 | + public async create<T extends Agents | Document>( |
| 16 | + data?: AgentsCreateDto, |
| 17 | + options?: SaveOptions, |
| 18 | + ): Promise<Document<T, any, T>> { |
| 19 | + data.password = await hash(data.password); |
| 20 | + return await super.create(data, options); |
| 21 | + } |
| 22 | + |
| 23 | + public async update<T extends Agents | Document>( |
| 24 | + _id: Types.ObjectId | any, |
| 25 | + update: UpdateQuery<T> & any, |
| 26 | + options?: QueryOptions<T>, |
| 27 | + ): Promise<ModifyResult<Query<T, T, any, T>>> { |
| 28 | + if (update.password) { |
| 29 | + update.password = await hash(update.password); |
| 30 | + } |
| 31 | + if (update.$set?.password) { |
| 32 | + update.$set.password = await hash(update.$set.password); |
| 33 | + } |
| 34 | + return await super.update( |
| 35 | + _id, |
| 36 | + { |
| 37 | + ...update, |
| 38 | + $set: { |
| 39 | + ...(update?.$set || {}), |
| 40 | + }, |
| 41 | + }, |
| 42 | + options, |
| 43 | + ); |
| 44 | + } |
12 | 45 | } |
0 commit comments