From f79db3343ddc798b7d52951708efc08225543cc0 Mon Sep 17 00:00:00 2001 From: Firman Ramdhani <33869609+firmanramdhani@users.noreply.github.com> Date: Tue, 8 Jul 2025 11:12:30 +0700 Subject: [PATCH] feat: integration save to couch --- src/modules/configuration/couch/constants.ts | 1 + .../managers/data-scheduling.handler.ts | 32 +++++++++++++++++-- .../data-scheduling-default.manager.ts | 8 ++--- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/modules/configuration/couch/constants.ts b/src/modules/configuration/couch/constants.ts index fa8be79..3b8c14a 100644 --- a/src/modules/configuration/couch/constants.ts +++ b/src/modules/configuration/couch/constants.ts @@ -4,4 +4,5 @@ export const DatabaseListen = [ 'pos_activity', 'pos_cash_activity', 'time_groups', + 'api_configuration', ]; diff --git a/src/modules/configuration/couch/domain/managers/data-scheduling.handler.ts b/src/modules/configuration/couch/domain/managers/data-scheduling.handler.ts index ae74eaa..c61e3b2 100644 --- a/src/modules/configuration/couch/domain/managers/data-scheduling.handler.ts +++ b/src/modules/configuration/couch/domain/managers/data-scheduling.handler.ts @@ -13,6 +13,7 @@ import * as momentTz from 'moment-timezone'; import { TABLE_NAME } from 'src/core/strings/constants/table.constants'; import { DataSchedulingEntity } from 'src/modules/configuration/data-scheduling/domain/entities/data-scheduling.entity'; import { decryptionTotal } from 'src/modules/configuration/data-scheduling/infrastructure/helpers'; +import { Logger } from '@nestjs/common'; @EventsHandler( DataSchedulingCreatedEvent, @@ -21,6 +22,10 @@ import { decryptionTotal } from 'src/modules/configuration/data-scheduling/infra DataSchedulingDeletedEvent, ) export class DataSchedulingUpdatedHandler implements IEventHandler { + private readonly logger = new Logger(DataSchedulingUpdatedHandler.name); + private readonly permanentID = 'e6166c86-d85d-43f8-86ad-c9e85a88f68f'; + private readonly couchTableName = 'api_configuration'; + constructor( private couchService: CouchService, @@ -33,10 +38,31 @@ export class DataSchedulingUpdatedHandler implements IEventHandler { async handle() { const activeData = await this.getActiveData(); - console.log( - activeData, - 'handle when data scheduling status change and data updated', + const existData = await this.couchService.getDoc( + this.permanentID, + this.couchTableName, ); + if (!existData) { + this.logger.verbose('CREATE SCHEDULING CONFIG'); + await this.couchService.createDoc( + { + _id: this.permanentID, + id: this.permanentID, + ...activeData, + }, + this.couchTableName, + ); + } else if (existData) { + this.logger.verbose('UPDATE SCHEDULING CONFIG'); + await this.couchService.updateDoc( + { + _id: this.permanentID, + id: this.permanentID, + ...activeData, + }, + this.couchTableName, + ); + } } async getActiveData() { diff --git a/src/modules/configuration/data-scheduling/domain/usecases/managers/data-scheduling-default.manager.ts b/src/modules/configuration/data-scheduling/domain/usecases/managers/data-scheduling-default.manager.ts index 2766147..5866538 100644 --- a/src/modules/configuration/data-scheduling/domain/usecases/managers/data-scheduling-default.manager.ts +++ b/src/modules/configuration/data-scheduling/domain/usecases/managers/data-scheduling-default.manager.ts @@ -8,7 +8,6 @@ import { DataSchedulingDefaultModel } from '../../../data/models/data-scheduling import { Repository } from 'typeorm'; import { TABLE_NAME } from 'src/core/strings/constants/table.constants'; import { SelectQueryBuilder } from 'typeorm'; -import { DataSchedulingModel } from '../../../data/models/data-scheduling.model'; import { EventBus } from '@nestjs/cqrs'; import { DataSchedulingChangeStatusEvent } from '../../entities/event/data-scheduling-change-status.event'; @@ -22,9 +21,6 @@ export class DataSchedulingManager { @InjectRepository(DataSchedulingDefaultModel) private repository: Repository, - - @InjectRepository(DataSchedulingModel) - private repoSchedule: Repository, ) {} private getUser(): UsersSession { @@ -64,9 +60,9 @@ export class DataSchedulingManager { created_at: dateNow, updated_at: dateNow, }; - + const saveData = await this.repository.save(payload); await this.publishEventUpdates(); - return this.repository.save(payload); + return saveData; } async getData() { -- 2.40.1