From 6da2118ab5b1fc777a70a9e24df72f45875b39d7 Mon Sep 17 00:00:00 2001 From: Firman Ramdhani <33869609+firmanramdhani@users.noreply.github.com> Date: Tue, 10 Jun 2025 16:38:06 +0700 Subject: [PATCH] feat: sync time group to couch --- src/modules/configuration/couch/constants.ts | 1 + .../domain/managers/time-group.handle.ts | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/modules/configuration/couch/domain/managers/time-group.handle.ts diff --git a/src/modules/configuration/couch/constants.ts b/src/modules/configuration/couch/constants.ts index 8171752..fa8be79 100644 --- a/src/modules/configuration/couch/constants.ts +++ b/src/modules/configuration/couch/constants.ts @@ -3,4 +3,5 @@ export const DatabaseListen = [ 'vip_code', 'pos_activity', 'pos_cash_activity', + 'time_groups', ]; diff --git a/src/modules/configuration/couch/domain/managers/time-group.handle.ts b/src/modules/configuration/couch/domain/managers/time-group.handle.ts new file mode 100644 index 0000000..b93fe04 --- /dev/null +++ b/src/modules/configuration/couch/domain/managers/time-group.handle.ts @@ -0,0 +1,65 @@ +import { EventsHandler, IEventHandler } from '@nestjs/cqrs'; +import { CouchService } from '../../data/services/couch.service'; +import { STATUS } from 'src/core/strings/constants/base.constants'; +import { TimeGroupDeletedEvent } from 'src/modules/item-related/time-group/domain/entities/event/time-group-deleted.event'; +import { TimeGroupChangeStatusEvent } from 'src/modules/item-related/time-group/domain/entities/event/time-group-change-status.event'; +import { TimeGroupUpdatedEvent } from 'src/modules/item-related/time-group/domain/entities/event/time-group-updated.event'; + +@EventsHandler(TimeGroupDeletedEvent) +export class TimeGroupDeletedHandler + implements IEventHandler +{ + constructor(private couchService: CouchService) {} + + async handle(event: TimeGroupDeletedEvent) { + const data = await this.couchService.deleteDoc( + { + _id: event.data.id, + ...event.data.data, + }, + 'time_groups', + ); + } +} + +@EventsHandler(TimeGroupChangeStatusEvent, TimeGroupUpdatedEvent) +export class TimeGroupUpdatedHandler + implements IEventHandler +{ + constructor(private couchService: CouchService) {} + + async handle(event: TimeGroupChangeStatusEvent) { + const dataOld = event.data.old; + const data = event.data.data; + + // change status to active + if (dataOld?.status != data.status && data.status == STATUS.ACTIVE) { + await this.couchService.createDoc( + { + _id: data.id, + ...data, + }, + 'time_groups', + ); + } else if (dataOld?.status != data.status) { + await this.couchService.deleteDoc( + { + _id: data.id, + ...data, + }, + 'time_groups', + ); + } + + // update + else { + await this.couchService.updateDoc( + { + _id: data.id, + ...data, + }, + 'time_groups', + ); + } + } +}