Skip to content

Commit 514c82c

Browse files
committed
Refactor event emitter event names in AbstractServiceSchema and add before state tracking for create, update, and delete operations
1 parent b295c78 commit 514c82c

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/_common/abstracts/abstract.service.schema.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export abstract class AbstractServiceSchema extends AbstractService implements S
221221
let created = document.save(options)
222222
if (this.eventEmitter) {
223223
const afterEvents = await this.eventEmitter?.emitAsync(
224-
[this.moduleName.toLowerCase(), this.serviceName.toLowerCase(), 'service', 'afterFindAndCount'].join(EventEmitterSeparator),
224+
[this.moduleName.toLowerCase(), this.serviceName.toLowerCase(), 'service', 'afterCreate'].join(EventEmitterSeparator),
225225
{ created },
226226
)
227227
for (const afterEvent of afterEvents) {
@@ -255,6 +255,7 @@ export abstract class AbstractServiceSchema extends AbstractService implements S
255255
if (beforeEvent?.options) options = { ...options, ...beforeEvent.options }
256256
}
257257
}
258+
const before = await this._model.findOne<Query<T | null, T, any, T>>({ _id }).exec();
258259
let updated = await this._model
259260
.findOneAndUpdate<Query<T | null, T, any, T>>(
260261
{ _id },
@@ -283,8 +284,8 @@ export abstract class AbstractServiceSchema extends AbstractService implements S
283284
}
284285
if (this.eventEmitter) {
285286
const afterEvents = await this.eventEmitter?.emitAsync(
286-
[this.moduleName.toLowerCase(), this.serviceName.toLowerCase(), 'service', 'afterFindAndCount'].join(EventEmitterSeparator),
287-
{ updated },
287+
[this.moduleName.toLowerCase(), this.serviceName.toLowerCase(), 'service', 'afterUpdate'].join(EventEmitterSeparator),
288+
{ before, updated },
288289
)
289290
for (const afterEvent of afterEvents) {
290291
if (afterEvent?.updated) updated = { ...updated, ...afterEvent.updated }
@@ -367,15 +368,16 @@ export abstract class AbstractServiceSchema extends AbstractService implements S
367368
if (beforeEvent?.options) options = { ...options, ...beforeEvent.options }
368369
}
369370
}
371+
const before = await this._model.findOne<Query<T | null, T, any, T>>({ _id }).exec();
370372
let deleted = await this._model.findByIdAndDelete<Query<T | null, T, any, T>>({ _id }, options).exec()
371373
if (!deleted) {
372374
this.logger.debug(['findById', JSON.stringify(Object.values(arguments))].join(' '))
373375
throw new NotFoundException()
374376
}
375377
if (this.eventEmitter) {
376378
const afterEvents = await this.eventEmitter?.emitAsync(
377-
[this.moduleName.toLowerCase(), this.serviceName.toLowerCase(), 'service', 'afterFindAndCount'].join(EventEmitterSeparator),
378-
{ deleted },
379+
[this.moduleName.toLowerCase(), this.serviceName.toLowerCase(), 'service', 'afterDelete'].join(EventEmitterSeparator),
380+
{ before, deleted },
379381
)
380382
for (const afterEvent of afterEvents) {
381383
if (afterEvent?.deleted) deleted = { ...deleted, ...afterEvent.deleted }

src/management/lifecycle/lifecycle.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { MongooseModule } from '@nestjs/mongoose';
33
import { Lifecycle, LifecycleSchema } from './_schemas/lifecycle.schema';
44
import { LifecycleController } from './lifecycle.controller';
55
import { LifecycleService } from './lifecycle.service';
6+
import { IdentitiesModule } from '../identities/identities.module';
67

78
@Module({
89
imports: [
@@ -12,6 +13,7 @@ import { LifecycleService } from './lifecycle.service';
1213
schema: LifecycleSchema,
1314
},
1415
]),
16+
IdentitiesModule,
1517
],
1618
providers: [LifecycleService],
1719
controllers: [LifecycleController],

0 commit comments

Comments
 (0)