feat: implementation save log when update default percentage

pull/175/head
Firman Ramdhani 2025-07-10 19:52:41 +07:00
parent 7dadd3f8f1
commit 8527b8ee1d
2 changed files with 70 additions and 30 deletions

View File

@ -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 = `<p><b>${editorName}</b> changed the status of <b>${scheduleName}</b> from <b><i>${oldStatus}</i></b> to <b><i>${newStatus}</i></b>.</p>`;
const scheduleName = newData?.name || 'an item';
const editorName = newData.editor_name || 'System';
const description = `<p><b>${editorName}</b> changed the status of <b>${scheduleName}</b> from <b><i>${oldStatus}</i></b> to <b><i>${newStatus}</i></b>.</p>`;
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}`,
);
}
}
}

View File

@ -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<DataSchedulingDefaultModel>,
@InjectRepository(DataSchedulingLogModel)
private repositoryLog: Repository<DataSchedulingLogModel>,
) {}
private getUser(): UsersSession {
@ -60,7 +69,35 @@ export class DataSchedulingManager {
created_at: dateNow,
updated_at: dateNow,
};
const saveData = await this.repository.save(payload);
if (existData?.default_value !== saveData?.default_value) {
const description = existData?.id
? `<p><b>${saveData.editor_name}</b> mengubah pengaturan <b><i>Default Percentage</i></b> dari <b><i>${existData.default_value}%</i></b> menjadi <b><i>${saveData.default_value}%</i></b>.</p>`
: `<p><b>${saveData.creator_name}</b> membuat pengaturan <b><i>Default Percentage</i></b> dengan value <b><i>${saveData.default_value}%</i></b>.</p>`;
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;
}