120 lines
4.0 KiB
TypeScript
120 lines
4.0 KiB
TypeScript
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,
|
|
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';
|
|
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 {
|
|
@Inject()
|
|
protected userProvider: UserProvider;
|
|
|
|
constructor(
|
|
private eventBus: EventBus,
|
|
|
|
@InjectRepository(DataSchedulingDefaultModel)
|
|
private repository: Repository<DataSchedulingDefaultModel>,
|
|
|
|
@InjectRepository(DataSchedulingLogModel)
|
|
private repositoryLog: Repository<DataSchedulingLogModel>,
|
|
) {}
|
|
|
|
private getUser(): UsersSession {
|
|
try {
|
|
return this.userProvider?.user ?? BLANK_USER;
|
|
} catch (error) {
|
|
return BLANK_USER;
|
|
}
|
|
}
|
|
|
|
get tableName(): string {
|
|
return TABLE_NAME.DATA_SCHEDULING_DEFAULT;
|
|
}
|
|
|
|
private queryBuilder(): SelectQueryBuilder<DataSchedulingDefaultModel> {
|
|
return this.repository.createQueryBuilder(this.tableName);
|
|
}
|
|
|
|
async update(
|
|
body: EditDataSchedulingDefaultDto,
|
|
): Promise<DataSchedulingDefaultEntity> {
|
|
if (body.default_value > 100) {
|
|
throw new Error('Value tidak boleh lebih dari 100.');
|
|
}
|
|
|
|
const userData = this.getUser();
|
|
const dateNow = new Date().getTime();
|
|
const existData = await this.queryBuilder().getOne();
|
|
|
|
const payload: DataSchedulingDefaultEntity = {
|
|
id: existData?.id,
|
|
default_value: body.default_value,
|
|
creator_id: userData.id as any,
|
|
creator_name: userData.name,
|
|
editor_id: userData.id as any,
|
|
editor_name: userData.name,
|
|
created_at: dateNow,
|
|
updated_at: dateNow,
|
|
};
|
|
|
|
const saveData = await this.repository.save(payload);
|
|
|
|
if (existData?.default_value !== saveData?.default_value) {
|
|
const description = existData?.id
|
|
? `<div><b>${saveData.editor_name}</b> changed the <b><i>Default Percentage</i></b> setting from <b><i>${existData.default_value}%</i></b> to <b><i>${saveData.default_value}%</i></b>.</div>`
|
|
: `<div><b>${saveData.creator_name}</b> created the <b><i>Default Percentage</i></b> setting with a value of <b><i>${saveData.default_value}%</i></b>.</div>`;
|
|
|
|
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;
|
|
}
|
|
|
|
async getData() {
|
|
return this.queryBuilder().getOne();
|
|
}
|
|
|
|
async publishEventUpdates() {
|
|
await this.eventBus.publish(
|
|
new DataSchedulingChangeStatusEvent({ data: null } as any),
|
|
);
|
|
}
|
|
|
|
async setupActiveScheduling() {
|
|
await this.publishEventUpdates();
|
|
return { message: 'Success setup transaction schedule.' };
|
|
}
|
|
}
|