88
99import { Injectable } from '@nestjs/common' ;
1010import { InjectModel } from '@nestjs/mongoose' ;
11- import {
12- Document ,
13- Model ,
14- Query ,
15- Types ,
16- UpdateQuery ,
17- UpdateWithAggregationPipeline ,
18- } from 'mongoose' ;
11+ import { Model , Types } from 'mongoose' ;
1912
20- import { BaseRepository , DeleteResult } from '@/utils/generics/base-repository' ;
21- import { TFilterQuery } from '@/utils/types/filter.types' ;
13+ import { BaseRepository } from '@/utils/generics/base-repository' ;
14+ import {
15+ Args ,
16+ postDelete ,
17+ preCreate ,
18+ preDelete ,
19+ preUpdate ,
20+ preUpdateMany ,
21+ } from '@/utils/types/lifecycle-hook-manager.types' ;
2222
2323import { BlockCreateDto , BlockDto , BlockUpdateDto } from '../dto/block.dto' ;
2424import {
@@ -63,10 +63,8 @@ export class BlockRepository extends BaseRepository<
6363 *
6464 * @param doc - The document that is being created.
6565 */
66- async preCreate (
67- _doc : Document < unknown , object , Block > & Block & { _id : Types . ObjectId } ,
68- ) : Promise < void > {
69- if ( _doc ) this . checkDeprecatedAttachmentUrl ( _doc ) ;
66+ async preCreate ( ...[ doc ] : Args < preCreate < Block > > ) : Promise < void > {
67+ if ( doc ) this . checkDeprecatedAttachmentUrl ( doc ) ;
7068 }
7169
7270 /**
@@ -77,19 +75,9 @@ export class BlockRepository extends BaseRepository<
7775 * @param updates - The update data.
7876 */
7977 async preUpdate (
80- _query : Query <
81- Document < Block , any , any > ,
82- Document < Block , any , any > ,
83- unknown ,
84- Block ,
85- 'findOneAndUpdate'
86- > ,
87- criteria : TFilterQuery < Block > ,
88- updates :
89- | UpdateWithAggregationPipeline
90- | UpdateQuery < Document < Block , any , any > > ,
78+ ...[ , criteria , updates ] : Args < preUpdate < Block > >
9179 ) : Promise < void > {
92- const update : BlockUpdateDto = updates ?. [ ' $set' ] ;
80+ const update = '$set' in updates ? updates . $set : { } ;
9381
9482 if ( update ?. category ) {
9583 const movedBlock = await this . findOne ( criteria ) ;
@@ -109,7 +97,10 @@ export class BlockRepository extends BaseRepository<
10997 { $set : { attachedBlock : null } } ,
11098 ) ;
11199 }
112- this . checkDeprecatedAttachmentUrl ( update ) ;
100+
101+ if ( update ) {
102+ this . checkDeprecatedAttachmentUrl ( update ) ;
103+ }
113104 }
114105
115106 /**
@@ -120,18 +111,10 @@ export class BlockRepository extends BaseRepository<
120111 * @param updates - The update data.
121112 */
122113 async preUpdateMany (
123- _query : Query <
124- Document < Block , any , any > ,
125- Document < Block , any , any > ,
126- unknown ,
127- Block ,
128- 'updateMany' ,
129- Record < string , never >
130- > ,
131- criteria : TFilterQuery < Block > ,
132- updates : UpdateQuery < Document < Block , any , any > > ,
114+ ...[ , criteria , updates ] : Args < preUpdateMany < Block > >
133115 ) : Promise < void > {
134- const categoryId : string = updates . $set . category ;
116+ const categoryId = '$set' in updates && updates . $set ?. category ;
117+
135118 if ( categoryId ) {
136119 const movedBlocks = await this . find ( criteria ) ;
137120
@@ -226,16 +209,7 @@ export class BlockRepository extends BaseRepository<
226209 * @param query - The delete query.
227210 * @param result - The result of the delete operation.
228211 */
229- async postDelete (
230- _query : Query <
231- DeleteResult ,
232- Document < Block , any , any > ,
233- unknown ,
234- Block ,
235- 'deleteOne' | 'deleteMany'
236- > ,
237- result : DeleteResult ,
238- ) {
212+ async postDelete ( ...[ , result ] : Args < postDelete < Block > > ) {
239213 if ( result . deletedCount > 0 ) {
240214 }
241215 }
@@ -247,16 +221,7 @@ export class BlockRepository extends BaseRepository<
247221 * @param query - The delete query.
248222 * @param criteria - The filter criteria for finding blocks to delete.
249223 */
250- async preDelete (
251- _query : Query <
252- DeleteResult ,
253- Document < Block , any , any > ,
254- unknown ,
255- Block ,
256- 'deleteOne' | 'deleteMany'
257- > ,
258- criteria : TFilterQuery < Block > ,
259- ) {
224+ async preDelete ( ...[ , criteria ] : Args < preDelete < Block > > ) {
260225 const docsToDelete = await this . model . find ( criteria ) ;
261226 const idsToDelete = docsToDelete . map ( ( { id } ) => id ) ;
262227 if ( idsToDelete . length > 0 ) {
0 commit comments