pos-be/src/modules/configuration/couch/domain/managers/data-scheduling.handler.ts

65 lines
2.8 KiB
TypeScript

import { EventsHandler, IEventHandler } from '@nestjs/cqrs';
import { CouchService } from '../../data/services/couch.service';
import { DataSchedulingDeletedEvent } from 'src/modules/configuration/data-scheduling/domain/entities/event/data-scheduling-deleted.event';
import { DataSchedulingChangeStatusEvent } from 'src/modules/configuration/data-scheduling/domain/entities/event/data-scheduling-change-status.event';
import { DataSchedulingUpdatedEvent } from 'src/modules/configuration/data-scheduling/domain/entities/event/data-scheduling-updated.event';
import { DataSchedulingCreatedEvent } from 'src/modules/configuration/data-scheduling/domain/entities/event/data-scheduling-created.event';
import { InjectRepository } from '@nestjs/typeorm';
import { DataSchedulingDefaultModel } from 'src/modules/configuration/data-scheduling/data/models/data-scheduling-default.model';
import { DataSchedulingModel } from 'src/modules/configuration/data-scheduling/data/models/data-scheduling.model';
import { Repository } from 'typeorm';
import * as momentTz from 'moment-timezone';
import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
import { DataSchedulingEntity } from 'src/modules/configuration/data-scheduling/domain/entities/data-scheduling.entity';
import { decryptionTotal } from 'src/modules/configuration/data-scheduling/infrastructure/helpers';
@EventsHandler(
DataSchedulingCreatedEvent,
DataSchedulingUpdatedEvent,
DataSchedulingChangeStatusEvent,
DataSchedulingDeletedEvent,
)
export class DataSchedulingUpdatedHandler implements IEventHandler {
constructor(
private couchService: CouchService,
@InjectRepository(DataSchedulingDefaultModel)
private repository: Repository<DataSchedulingDefaultModel>,
@InjectRepository(DataSchedulingModel)
private repoSchedule: Repository<DataSchedulingModel>,
) {}
async handle() {
const activeData = await this.getActiveData();
console.log(
activeData,
'handle when data scheduling status change and data updated',
);
}
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.repository
.createQueryBuilder(TABLE_NAME.DATA_SCHEDULING_DEFAULT)
.getOne();
return { value: defaultData?.default_value };
}
return { value: decryptionTotal(findData.indexing_key as string), date };
}
}