110 lines
3.5 KiB
TypeScript
110 lines
3.5 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,
|
|
DataSchedulingEntity,
|
|
} 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 { DataSchedulingModel } from '../../../data/models/data-scheduling.model';
|
|
import { decryptionTotal } from '../../../infrastructure/helpers';
|
|
import * as momentTz from 'moment-timezone';
|
|
import { EventBus } from '@nestjs/cqrs';
|
|
import { DataSchedulingChangeStatusEvent } from '../../entities/event/data-scheduling-change-status.event';
|
|
|
|
@Injectable()
|
|
export class DataSchedulingManager {
|
|
@Inject()
|
|
protected userProvider: UserProvider;
|
|
|
|
constructor(
|
|
private eventBus: EventBus,
|
|
|
|
@InjectRepository(DataSchedulingDefaultModel)
|
|
private repository: Repository<DataSchedulingDefaultModel>,
|
|
|
|
@InjectRepository(DataSchedulingModel)
|
|
private repoSchedule: Repository<DataSchedulingModel>,
|
|
) {}
|
|
|
|
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,
|
|
};
|
|
|
|
return this.repository.save(payload);
|
|
}
|
|
|
|
async getData() {
|
|
return this.queryBuilder().getOne();
|
|
}
|
|
|
|
async getActiveData() {
|
|
const timeZoneWIB = 'Asia/Jakarta';
|
|
const nowInWIB = momentTz().tz(timeZoneWIB).format('YYYY-MM-DD');
|
|
const date = nowInWIB;
|
|
|
|
const qb = this.repoSchedule.createQueryBuilder(TABLE_NAME.DATA_SCHEDULING);
|
|
|
|
const findData: DataSchedulingEntity = await qb
|
|
.where('status = :status', { status: 'active' })
|
|
.andWhere('schedule_date_from <= :date', { date: date })
|
|
.andWhere('schedule_date_to >= :date', { date: date })
|
|
.getOne();
|
|
|
|
if (!findData) {
|
|
const defaultData = await this.queryBuilder().getOne();
|
|
return { value: defaultData?.default_value };
|
|
}
|
|
|
|
return { value: decryptionTotal(findData.indexing_key as string), date };
|
|
}
|
|
|
|
async setupActiveScheduling() {
|
|
const activeSchedule = await this.getActiveData();
|
|
await this.eventBus.publish(
|
|
new DataSchedulingChangeStatusEvent({ data: activeSchedule } as any),
|
|
);
|
|
|
|
return { message: 'Success setup transaction schedule.' };
|
|
}
|
|
}
|