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, @InjectRepository(DataSchedulingLogModel) private repositoryLog: Repository, ) {} 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 { return this.repository.createQueryBuilder(this.tableName); } async update( body: EditDataSchedulingDefaultDto, ): Promise { 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 ? `
${saveData.editor_name} changed the Default Percentage setting from ${existData.default_value}% to ${saveData.default_value}%.
` : `
${saveData.creator_name} created the Default Percentage setting with a value of ${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; } 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.' }; } }