From 8527b8ee1d2f40ed9eb30312cb167f8c01b05ef8 Mon Sep 17 00:00:00 2001 From: Firman Ramdhani <33869609+firmanramdhani@users.noreply.github.com> Date: Thu, 10 Jul 2025 19:52:41 +0700 Subject: [PATCH] feat: implementation save log when update default percentage --- .../data-scheduling-change-status.handler.ts | 61 ++++++++++--------- .../data-scheduling-default.manager.ts | 39 +++++++++++- 2 files changed, 70 insertions(+), 30 deletions(-) diff --git a/src/modules/configuration/data-scheduling/domain/usecases/handlers/data-scheduling-change-status.handler.ts b/src/modules/configuration/data-scheduling/domain/usecases/handlers/data-scheduling-change-status.handler.ts index 80c6fe7..59cb7da 100644 --- a/src/modules/configuration/data-scheduling/domain/usecases/handlers/data-scheduling-change-status.handler.ts +++ b/src/modules/configuration/data-scheduling/domain/usecases/handlers/data-scheduling-change-status.handler.ts @@ -18,40 +18,43 @@ export class DataSchedulingChangeStatusHandler constructor(private service: DataSchedulingLogDataService) {} async handle(event: DataSchedulingChangeStatusEvent) { - const oldData = event?.data?.old; - const newData = event?.data?.data; + // Prevent execution if the event data is null, which can happen if triggered from the default percentage update service. + if (event.data?.data) { + const oldData = event?.data?.old; + const newData = event?.data?.data; - const oldStatus = capitalizeEachWord(oldData?.status); - const newStatus = capitalizeEachWord(newData.status); + const oldStatus = capitalizeEachWord(oldData?.status); + const newStatus = capitalizeEachWord(newData?.status); - const scheduleName = newData?.name || 'an item'; - const editorName = newData.editor_name || 'System'; - const description = `
${editorName} changed the status of ${scheduleName} from ${oldStatus} to ${newStatus}.
`; + const scheduleName = newData?.name || 'an item'; + const editorName = newData.editor_name || 'System'; + const description = `${editorName} changed the status of ${scheduleName} from ${oldStatus} to ${newStatus}.
`; - const payload: DataSchedulingLogEntity = { - type: SCHEDULING_LOG_TYPE_ENUM.DATA_SCHEDULING, - action: SCHEDULING_LOG_ACTION_ENUM.CHANGE_STATUS, - log_created_at: new Date().getTime(), + const payload: DataSchedulingLogEntity = { + type: SCHEDULING_LOG_TYPE_ENUM.DATA_SCHEDULING, + action: SCHEDULING_LOG_ACTION_ENUM.CHANGE_STATUS, + log_created_at: new Date().getTime(), - data_id: newData?.id, - name: newData?.name, - indexing_key: newData?.indexing_key, - schedule_date_from: newData?.schedule_date_from, - schedule_date_to: newData?.schedule_date_to, + data_id: newData?.id, + name: newData?.name, + indexing_key: newData?.indexing_key, + schedule_date_from: newData?.schedule_date_from, + schedule_date_to: newData?.schedule_date_to, - status: newData?.status, - creator_id: newData?.creator_id, - creator_name: newData?.creator_name, - editor_id: newData?.editor_id, - editor_name: newData?.editor_name, - created_at: newData?.created_at, - updated_at: newData?.updated_at, - description: description, - }; + status: newData?.status, + creator_id: newData?.creator_id, + creator_name: newData?.creator_name, + editor_id: newData?.editor_id, + editor_name: newData?.editor_name, + created_at: newData?.created_at, + updated_at: newData?.updated_at, + description: description, + }; - await this.service.create(payload as any); - this.logger.verbose( - `[SCHEDULING LOG] Change status data for ID: ${payload.data_id}`, - ); + await this.service.create(payload as any); + this.logger.verbose( + `[SCHEDULING LOG] Change status data for ID: ${payload.data_id}`, + ); + } } } 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 5866538..97d68ba 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 @@ -2,7 +2,12 @@ import { Inject, Injectable } from '@nestjs/common'; import { UserProvider, UsersSession } from 'src/core/sessions'; import { BLANK_USER } from 'src/core/strings/constants/base.constants'; import { EditDataSchedulingDefaultDto } from '../../../infrastructure/dto/data-scheduling.dto'; -import { DataSchedulingDefaultEntity } from '../../entities/data-scheduling.entity'; +import { + DataSchedulingDefaultEntity, + DataSchedulingLogEntity, + SCHEDULING_LOG_ACTION_ENUM, + SCHEDULING_LOG_TYPE_ENUM, +} from '../../entities/data-scheduling.entity'; import { InjectRepository } from '@nestjs/typeorm'; import { DataSchedulingDefaultModel } from '../../../data/models/data-scheduling-default.model'; import { Repository } from 'typeorm'; @@ -10,6 +15,7 @@ import { TABLE_NAME } from 'src/core/strings/constants/table.constants'; import { SelectQueryBuilder } from 'typeorm'; import { EventBus } from '@nestjs/cqrs'; import { DataSchedulingChangeStatusEvent } from '../../entities/event/data-scheduling-change-status.event'; +import { DataSchedulingLogModel } from '../../../data/models/data-scheduling-log.model'; @Injectable() export class DataSchedulingManager { @@ -21,6 +27,9 @@ export class DataSchedulingManager { @InjectRepository(DataSchedulingDefaultModel) private repository: Repository${saveData.editor_name} mengubah pengaturan Default Percentage dari ${existData.default_value}% menjadi ${saveData.default_value}%.
` + : `${saveData.creator_name} membuat pengaturan Default Percentage dengan value ${saveData.default_value}%.
`; + + const logPayload: DataSchedulingLogEntity = { + type: SCHEDULING_LOG_TYPE_ENUM.DEFAULT_PERCENTAGE, + action: existData?.id + ? SCHEDULING_LOG_ACTION_ENUM.UPDATE + : SCHEDULING_LOG_ACTION_ENUM.CREATE, + log_created_at: new Date().getTime(), + status: undefined, + + data_id: saveData?.id, + creator_id: saveData?.creator_id, + creator_name: saveData?.creator_name, + editor_id: saveData?.editor_id, + editor_name: saveData?.editor_name, + created_at: saveData?.created_at, + updated_at: saveData?.updated_at, + default_value: saveData?.default_value, + description: description, + }; + + await this.repositoryLog.save(logPayload as any); + } await this.publishEventUpdates(); return saveData; }