diff --git a/src/modules/configuration/couch/couch.module.ts b/src/modules/configuration/couch/couch.module.ts index 060254c..2698071 100644 --- a/src/modules/configuration/couch/couch.module.ts +++ b/src/modules/configuration/couch/couch.module.ts @@ -57,6 +57,10 @@ import { TimeGroupUpdatedHandler, } from './domain/managers/time-group.handle'; +import { DataSchedulingUpdatedHandler } from './domain/managers/data-scheduling.handler'; +import { DataSchedulingDefaultModel } from '../data-scheduling/data/models/data-scheduling-default.model'; +import { DataSchedulingModel } from '../data-scheduling/data/models/data-scheduling.model'; + @Module({ imports: [ ConfigModule.forRoot(), @@ -71,6 +75,9 @@ import { TransactionTaxModel, TransactionItemModel, TransactionDemographyModel, + + DataSchedulingDefaultModel, + DataSchedulingModel, ], CONNECTION_NAME.DEFAULT, ), @@ -104,6 +111,8 @@ import { SeasonTypeDeletedHandler, SeasonTypeUpdatedHandler, + DataSchedulingUpdatedHandler, + SeasonPeriodDataService, TransactionDataService, UserDataService, diff --git a/src/modules/configuration/couch/domain/managers/data-scheduling.handler.ts b/src/modules/configuration/couch/domain/managers/data-scheduling.handler.ts new file mode 100644 index 0000000..ae74eaa --- /dev/null +++ b/src/modules/configuration/couch/domain/managers/data-scheduling.handler.ts @@ -0,0 +1,64 @@ +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, + + @InjectRepository(DataSchedulingModel) + private repoSchedule: Repository, + ) {} + + 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 }; + } +} diff --git a/src/modules/configuration/data-scheduling/domain/usecases/managers/data-scheduling-default.manager.ts b/src/modules/configuration/data-scheduling/domain/usecases/managers/data-scheduling-default.manager.ts index 040bc97..2766147 100644 --- a/src/modules/configuration/data-scheduling/domain/usecases/managers/data-scheduling-default.manager.ts +++ b/src/modules/configuration/data-scheduling/domain/usecases/managers/data-scheduling-default.manager.ts @@ -2,18 +2,13 @@ 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 { DataSchedulingDefaultEntity } 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'; @@ -70,6 +65,7 @@ export class DataSchedulingManager { updated_at: dateNow, }; + await this.publishEventUpdates(); return this.repository.save(payload); } @@ -77,33 +73,14 @@ export class DataSchedulingManager { 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 publishEventUpdates() { + await this.eventBus.publish( + new DataSchedulingChangeStatusEvent({ data: null } as any), + ); } async setupActiveScheduling() { - const activeSchedule = await this.getActiveData(); - await this.eventBus.publish( - new DataSchedulingChangeStatusEvent({ data: activeSchedule } as any), - ); - + await this.publishEventUpdates(); return { message: 'Success setup transaction schedule.' }; } }