feat: implementation save log when update default percentage
parent
7dadd3f8f1
commit
8527b8ee1d
|
@ -18,40 +18,43 @@ export class DataSchedulingChangeStatusHandler
|
||||||
constructor(private service: DataSchedulingLogDataService) {}
|
constructor(private service: DataSchedulingLogDataService) {}
|
||||||
|
|
||||||
async handle(event: DataSchedulingChangeStatusEvent) {
|
async handle(event: DataSchedulingChangeStatusEvent) {
|
||||||
const oldData = event?.data?.old;
|
// Prevent execution if the event data is null, which can happen if triggered from the default percentage update service.
|
||||||
const newData = event?.data?.data;
|
if (event.data?.data) {
|
||||||
|
const oldData = event?.data?.old;
|
||||||
|
const newData = event?.data?.data;
|
||||||
|
|
||||||
const oldStatus = capitalizeEachWord(oldData?.status);
|
const oldStatus = capitalizeEachWord(oldData?.status);
|
||||||
const newStatus = capitalizeEachWord(newData.status);
|
const newStatus = capitalizeEachWord(newData?.status);
|
||||||
|
|
||||||
const scheduleName = newData?.name || 'an item';
|
const scheduleName = newData?.name || 'an item';
|
||||||
const editorName = newData.editor_name || 'System';
|
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 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 = {
|
const payload: DataSchedulingLogEntity = {
|
||||||
type: SCHEDULING_LOG_TYPE_ENUM.DATA_SCHEDULING,
|
type: SCHEDULING_LOG_TYPE_ENUM.DATA_SCHEDULING,
|
||||||
action: SCHEDULING_LOG_ACTION_ENUM.CHANGE_STATUS,
|
action: SCHEDULING_LOG_ACTION_ENUM.CHANGE_STATUS,
|
||||||
log_created_at: new Date().getTime(),
|
log_created_at: new Date().getTime(),
|
||||||
|
|
||||||
data_id: newData?.id,
|
data_id: newData?.id,
|
||||||
name: newData?.name,
|
name: newData?.name,
|
||||||
indexing_key: newData?.indexing_key,
|
indexing_key: newData?.indexing_key,
|
||||||
schedule_date_from: newData?.schedule_date_from,
|
schedule_date_from: newData?.schedule_date_from,
|
||||||
schedule_date_to: newData?.schedule_date_to,
|
schedule_date_to: newData?.schedule_date_to,
|
||||||
|
|
||||||
status: newData?.status,
|
status: newData?.status,
|
||||||
creator_id: newData?.creator_id,
|
creator_id: newData?.creator_id,
|
||||||
creator_name: newData?.creator_name,
|
creator_name: newData?.creator_name,
|
||||||
editor_id: newData?.editor_id,
|
editor_id: newData?.editor_id,
|
||||||
editor_name: newData?.editor_name,
|
editor_name: newData?.editor_name,
|
||||||
created_at: newData?.created_at,
|
created_at: newData?.created_at,
|
||||||
updated_at: newData?.updated_at,
|
updated_at: newData?.updated_at,
|
||||||
description: description,
|
description: description,
|
||||||
};
|
};
|
||||||
|
|
||||||
await this.service.create(payload as any);
|
await this.service.create(payload as any);
|
||||||
this.logger.verbose(
|
this.logger.verbose(
|
||||||
`[SCHEDULING LOG] Change status data for ID: ${payload.data_id}`,
|
`[SCHEDULING LOG] Change status data for ID: ${payload.data_id}`,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,12 @@ import { Inject, Injectable } from '@nestjs/common';
|
||||||
import { UserProvider, UsersSession } from 'src/core/sessions';
|
import { UserProvider, UsersSession } from 'src/core/sessions';
|
||||||
import { BLANK_USER } from 'src/core/strings/constants/base.constants';
|
import { BLANK_USER } from 'src/core/strings/constants/base.constants';
|
||||||
import { EditDataSchedulingDefaultDto } from '../../../infrastructure/dto/data-scheduling.dto';
|
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 { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { DataSchedulingDefaultModel } from '../../../data/models/data-scheduling-default.model';
|
import { DataSchedulingDefaultModel } from '../../../data/models/data-scheduling-default.model';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
|
@ -10,6 +15,7 @@ import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
|
||||||
import { SelectQueryBuilder } from 'typeorm';
|
import { SelectQueryBuilder } from 'typeorm';
|
||||||
import { EventBus } from '@nestjs/cqrs';
|
import { EventBus } from '@nestjs/cqrs';
|
||||||
import { DataSchedulingChangeStatusEvent } from '../../entities/event/data-scheduling-change-status.event';
|
import { DataSchedulingChangeStatusEvent } from '../../entities/event/data-scheduling-change-status.event';
|
||||||
|
import { DataSchedulingLogModel } from '../../../data/models/data-scheduling-log.model';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DataSchedulingManager {
|
export class DataSchedulingManager {
|
||||||
|
@ -21,6 +27,9 @@ export class DataSchedulingManager {
|
||||||
|
|
||||||
@InjectRepository(DataSchedulingDefaultModel)
|
@InjectRepository(DataSchedulingDefaultModel)
|
||||||
private repository: Repository<DataSchedulingDefaultModel>,
|
private repository: Repository<DataSchedulingDefaultModel>,
|
||||||
|
|
||||||
|
@InjectRepository(DataSchedulingLogModel)
|
||||||
|
private repositoryLog: Repository<DataSchedulingLogModel>,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
private getUser(): UsersSession {
|
private getUser(): UsersSession {
|
||||||
|
@ -60,7 +69,35 @@ export class DataSchedulingManager {
|
||||||
created_at: dateNow,
|
created_at: dateNow,
|
||||||
updated_at: dateNow,
|
updated_at: dateNow,
|
||||||
};
|
};
|
||||||
|
|
||||||
const saveData = await this.repository.save(payload);
|
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();
|
await this.publishEventUpdates();
|
||||||
return saveData;
|
return saveData;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue