diff --git a/src/core/modules/domain/usecase/managers/base-custom.manager.ts b/src/core/modules/domain/usecase/managers/base-custom.manager.ts index 2fd87d7..58215f2 100644 --- a/src/core/modules/domain/usecase/managers/base-custom.manager.ts +++ b/src/core/modules/domain/usecase/managers/base-custom.manager.ts @@ -5,7 +5,7 @@ export abstract class BaseCustomManager extends BaseManager { protected result: any; abstract get entityTarget(): any; - setData(entity: Entity): void { + setData(entity: any): void { this.data = entity; } diff --git a/src/modules/item-related/item/domain/usecases/item-data.orchestrator.ts b/src/modules/item-related/item/domain/usecases/item-data.orchestrator.ts index 69a2463..0c9296e 100644 --- a/src/modules/item-related/item/domain/usecases/item-data.orchestrator.ts +++ b/src/modules/item-related/item/domain/usecases/item-data.orchestrator.ts @@ -15,6 +15,8 @@ import { BatchInactiveItemManager } from './managers/batch-inactive-item.manager import { BatchActiveItemManager } from './managers/batch-active-item.manager'; import { BatchDeleteItemManager } from './managers/batch-delete-item.manager'; import { TABLE_NAME } from 'src/core/strings/constants/table.constants'; +import { UpdateItemRatePriceManager } from './managers/update-item-rate-price.manager'; +import { ItemRateReadService } from 'src/modules/item-related/item-rate/data/services/item-rate-read.service'; @Injectable() export class ItemDataOrchestrator extends BaseDataTransactionOrchestrator { @@ -28,8 +30,10 @@ export class ItemDataOrchestrator extends BaseDataTransactionOrchestrator { + this.updatePriceManager.setData(data); + this.updatePriceManager.setService(this.serviceRateData, TABLE_NAME.ITEM); + await this.updatePriceManager.execute(); + return this.updatePriceManager.getResult(); + } + async delete(dataId, tenantId?: string): Promise { this.deleteManager.setData(dataId); this.deleteManager.setService(this.serviceData, TABLE_NAME.ITEM); diff --git a/src/modules/item-related/item/domain/usecases/managers/update-item-rate-price.manager.ts b/src/modules/item-related/item/domain/usecases/managers/update-item-rate-price.manager.ts new file mode 100644 index 0000000..e11f875 --- /dev/null +++ b/src/modules/item-related/item/domain/usecases/managers/update-item-rate-price.manager.ts @@ -0,0 +1,72 @@ +import { Injectable } from '@nestjs/common'; +import { BaseCustomManager } from 'src/core/modules/domain/usecase/managers/base-custom.manager'; +import { ItemEntity } from '../../entities/item.entity'; +import { EventTopics } from 'src/core/strings/constants/interface.constants'; +import { ItemModel } from '../../../data/models/item.model'; +import { In, LessThanOrEqual, MoreThanOrEqual } from 'typeorm'; + +@Injectable() +export class UpdateItemRatePriceManager extends BaseCustomManager { + protected rates = []; + get entityTarget(): any { + return ItemModel; + } + + get eventTopics(): EventTopics[] { + return []; + } + + async validateProcess(): Promise { + return; + } + + async beforeProcess(): Promise { + let query; + const item_ids = this.data.items.map((item) => { + return item.item.id; + }); + + if (this.data.season_period_id) { + query = { + item_id: In(item_ids), + season_period: { + id: this.data.season_period_id, + }, + }; + } else { + query = { + item_id: In(item_ids), + season_period: { + start_date: MoreThanOrEqual(this.data.booking_date), + end_date: LessThanOrEqual(this.data.booking_date), + }, + }; + } + + this.rates = await this.dataService.getManyByOptions({ + where: query, + }); + return; + } + + async process(): Promise { + this.data.items.map((item) => { + const current_price = this.rates.find( + (rate) => rate.item_id == item.item.id, + ); + + Object.assign(item, { + total_price: current_price?.price ?? item.item.base_price, + }); + }); + return; + } + + async afterProcess(): Promise { + return; + } + + async getResult() { + return this.data.items; + } +} diff --git a/src/modules/item-related/item/infrastructure/dto/update-item-price.dto.ts b/src/modules/item-related/item/infrastructure/dto/update-item-price.dto.ts new file mode 100644 index 0000000..b8fa0f5 --- /dev/null +++ b/src/modules/item-related/item/infrastructure/dto/update-item-price.dto.ts @@ -0,0 +1,43 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class UpdateItemPriceDto { + @ApiProperty({ + type: [Object], + required: true, + example: [ + { + item: { + id: 'bee5c493-fb35-4ceb-b7a1-7bc3edb3c63b', + name: 'TEnant 2 wahana air', + item_type: 'wahana', + base_price: '100000', + hpp: '0', + tenant: { + id: 'e19a4637-d4db-48cc-89ce-501913d07cdd', + name: 'e19a4637-d4db-48cc-89ce-501913d07cdd', + share_margin: null, + }, + item_category: { + id: '88633772-ec34-4645-bc04-6cfdce6af0cf', + name: 'Wahana Air', + }, + }, + qty: 1, + total_price: '100000', + }, + ], + }) + items: Object[]; + + @ApiProperty({ + type: String, + example: 'uuid', + }) + season_period_id: string; + + @ApiProperty({ + type: Date, + example: '2024-08-17', + }) + booking_date: Date; +} diff --git a/src/modules/item-related/item/infrastructure/item-data.controller.ts b/src/modules/item-related/item/infrastructure/item-data.controller.ts index 44a855d..f416665 100644 --- a/src/modules/item-related/item/infrastructure/item-data.controller.ts +++ b/src/modules/item-related/item/infrastructure/item-data.controller.ts @@ -15,6 +15,7 @@ import { ItemEntity } from '../domain/entities/item.entity'; import { BatchResult } from 'src/core/response/domain/ok-response.interface'; import { BatchIdsDto } from 'src/core/modules/infrastructure/dto/base-batch.dto'; import { Public } from 'src/core/guards'; +import { UpdateItemPriceDto } from './dto/update-item-price.dto'; @ApiTags(`${MODULE_NAME.ITEM.split('-').join(' ')} - data`) @Controller(`v1/${MODULE_NAME.ITEM}`) @@ -28,6 +29,11 @@ export class ItemDataController { return await this.orchestrator.create(data); } + @Post('update-price') + async updatePrice(@Body() body: UpdateItemPriceDto): Promise { + return await this.orchestrator.updatePrice(body); + } + @Put('/batch-delete') async batchDeleted(@Body() body: BatchIdsDto): Promise { return await this.orchestrator.batchDelete(body.ids); diff --git a/src/modules/item-related/item/item.module.ts b/src/modules/item-related/item/item.module.ts index 456f7a2..bb35be2 100644 --- a/src/modules/item-related/item/item.module.ts +++ b/src/modules/item-related/item/item.module.ts @@ -25,6 +25,7 @@ import { ItemModel } from './data/models/item.model'; import { ItemRateModel } from '../item-rate/data/models/item-rate.model'; import { ItemRateReadService } from '../item-rate/data/services/item-rate-read.service'; import { IndexItemRatesManager } from './domain/usecases/managers/index-item-rates.manager'; +import { UpdateItemRatePriceManager } from './domain/usecases/managers/update-item-rate-price.manager'; @Global() @Module({ @@ -51,6 +52,7 @@ import { IndexItemRatesManager } from './domain/usecases/managers/index-item-rat BatchActiveItemManager, BatchConfirmItemManager, BatchInactiveItemManager, + UpdateItemRatePriceManager, ItemDataService, ItemReadService,