From 1dae9ec356437b47ffea5b671e9bc5e237428eea Mon Sep 17 00:00:00 2001 From: Firman Ramdhani <33869609+firmanramdhani@users.noreply.github.com> Date: Thu, 8 Aug 2024 14:51:38 +0700 Subject: [PATCH] feat(SPG-780): sync data session type --- .../configuration/couch/couch.module.ts | 11 ++++ .../domain/managers/season-type.handler.ts | 52 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/modules/configuration/couch/domain/managers/season-type.handler.ts diff --git a/src/modules/configuration/couch/couch.module.ts b/src/modules/configuration/couch/couch.module.ts index 22ef573..8de39ac 100644 --- a/src/modules/configuration/couch/couch.module.ts +++ b/src/modules/configuration/couch/couch.module.ts @@ -44,6 +44,12 @@ import { TransactionTaxModel } from 'src/modules/transaction/transaction/data/mo import { TransactionItemModel } from 'src/modules/transaction/transaction/data/models/transaction-item.model'; import { VipCodeCreatedHandler } from './domain/managers/vip-code.handler'; import { ItemRateModel } from 'src/modules/item-related/item-rate/data/models/item-rate.model'; +import { + SeasonTypeDeletedHandler, + SeasonTypeUpdatedHandler, +} from './domain/managers/season-type.handler'; +import { SeasonPeriodDataService } from 'src/modules/season-related/season-period/data/services/season-period-data.service'; +import { SeasonPeriodModel } from 'src/modules/season-related/season-period/data/models/season-period.model'; @Module({ imports: [ @@ -52,6 +58,7 @@ import { ItemRateModel } from 'src/modules/item-related/item-rate/data/models/it [ ItemModel, ItemRateModel, + SeasonPeriodModel, UserModel, TransactionModel, TransactionTaxModel, @@ -82,6 +89,10 @@ import { ItemRateModel } from 'src/modules/item-related/item-rate/data/models/it UserUpdatedHandler, UserPrivilegeUpdateHandler, + SeasonTypeDeletedHandler, + SeasonTypeUpdatedHandler, + + SeasonPeriodDataService, TransactionDataService, UserDataService, ItemDataService, diff --git a/src/modules/configuration/couch/domain/managers/season-type.handler.ts b/src/modules/configuration/couch/domain/managers/season-type.handler.ts new file mode 100644 index 0000000..811f6c3 --- /dev/null +++ b/src/modules/configuration/couch/domain/managers/season-type.handler.ts @@ -0,0 +1,52 @@ +import { EventsHandler, IEventHandler } from '@nestjs/cqrs'; +import { CouchService } from '../../data/services/couch.service'; +import { SeasonTypeDeletedEvent } from 'src/modules/season-related/season-type/domain/entities/event/season-type-deleted.event'; +import { SeasonTypeChangeStatusEvent } from 'src/modules/season-related/season-type/domain/entities/event/season-type-change-status.event'; +import { SeasonTypeUpdatedEvent } from 'src/modules/season-related/season-type/domain/entities/event/season-type-updated.event'; +import { SeasonPeriodDataService } from 'src/modules/season-related/season-period/data/services/season-period-data.service'; + +@EventsHandler(SeasonTypeDeletedEvent) +export class SeasonTypeDeletedHandler + implements IEventHandler +{ + constructor(private couchService: CouchService) {} + + async handle(event: SeasonTypeDeletedEvent) { + console.log('deleted session type'); + } +} + +@EventsHandler(SeasonTypeChangeStatusEvent, SeasonTypeUpdatedEvent) +export class SeasonTypeUpdatedHandler + implements IEventHandler +{ + constructor( + private couchService: CouchService, + private seasonPeriodService: SeasonPeriodDataService, + ) {} + + async handle(event: SeasonTypeChangeStatusEvent) { + const data = event.data.data; + const typeID = data.id; + const periods = await this.seasonPeriodService.getManyByOptions({ + where: { + season_type_id: typeID, + }, + relations: ['season_type'], + }); + + for (const period of periods) { + const dataID = period.id; + const couchData = await this.couchService.getDoc(dataID, 'season_period'); + if (couchData) { + await this.couchService.updateDoc( + { + _id: dataID, + ...period, + }, + 'season_period', + ); + } + } + } +}