feat: implementation save log when update default percentage
parent
7dadd3f8f1
commit
8527b8ee1d
|
@ -18,11 +18,13 @@ export class DataSchedulingChangeStatusHandler
|
|||
constructor(private service: DataSchedulingLogDataService) {}
|
||||
|
||||
async handle(event: DataSchedulingChangeStatusEvent) {
|
||||
// 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 newStatus = capitalizeEachWord(newData?.status);
|
||||
|
||||
const scheduleName = newData?.name || 'an item';
|
||||
const editorName = newData.editor_name || 'System';
|
||||
|
@ -55,3 +57,4 @@ export class DataSchedulingChangeStatusHandler
|
|||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue