fix: fix break down bundling item rates
continuous-integration/drone/tag Build is passing Details

fix/bug-firman^2 20.1.45-alpha.1
shancheas 2024-08-20 17:16:14 +07:00
parent 8abdbb7b55
commit 7c7b121b49
5 changed files with 76 additions and 6 deletions

View File

@ -4,14 +4,64 @@ import { ItemRateEntity } from '../../domain/entities/item-rate.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { ItemRateModel } from '../models/item-rate.model';
import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants';
import { Repository } from 'typeorm';
import { In, Repository } from 'typeorm';
import { ItemModel } from 'src/modules/item-related/item/data/models/item.model';
@Injectable()
export class ItemRateDataService extends BaseDataService<ItemRateEntity> {
constructor(
@InjectRepository(ItemRateModel, CONNECTION_NAME.DEFAULT)
private repo: Repository<ItemRateModel>,
@InjectRepository(ItemModel, CONNECTION_NAME.DEFAULT)
private itemRepo: Repository<ItemModel>,
) {
super(repo);
}
async itemsRateTotal(season_id: string, items: string[]): Promise<number> {
const rates = await this.repo.find({
where: {
season_period_id: season_id,
item_id: In(items),
},
});
return rates.reduce((total, current) => total + Number(current.price), 0);
}
async updateRatesBySeasonItem(
season_id: string,
item_id: string,
price: number,
): Promise<void> {
this.repo.update(
{
season_period_id: season_id,
item_id,
},
{ price },
);
}
async findBundlingByItem(item_id: string): Promise<ItemModel[]> {
const bundlings = await this.itemRepo.find({
where: {
breakdown_bundling: true,
bundling_items: {
id: item_id,
},
},
});
return Promise.all(
await bundlings.map(async (bundling) => {
return await this.itemRepo.findOne({
relations: ['bundling_items'],
where: {
id: bundling.id,
},
});
}),
);
}
}

View File

@ -5,7 +5,6 @@ import { ItemRateEntity } from '../entities/item-rate.entity';
import { DeleteItemRateManager } from './managers/delete-item-rate.manager';
import { UpdateItemRateManager } from './managers/update-item-rate.manager';
import { BaseDataOrchestrator } from 'src/core/modules/domain/usecase/orchestrators/base-data.orchestrator';
import { STATUS } from 'src/core/strings/constants/base.constants';
import { BatchResult } from 'src/core/response/domain/ok-response.interface';
import { BatchDeleteItemRateManager } from './managers/batch-delete-item-rate.manager';
import { TABLE_NAME } from 'src/core/strings/constants/table.constants';

View File

@ -20,6 +20,7 @@ export class UpdateItemRateManager extends BaseUpdateManager<ItemRateEntity> {
}
async afterProcess(): Promise<void> {
this.updateBundlingBreakdown(this.dataId);
return;
}
@ -42,4 +43,25 @@ export class UpdateItemRateManager extends BaseUpdateManager<ItemRateEntity> {
},
];
}
async updateBundlingBreakdown(id: string) {
const { item_id, season_period_id } =
await this.dataService.getOneByOptions({
where: { id },
});
const bundlings = await this.dataService.findBundlingByItem(item_id);
for (const bundling of bundlings) {
const ids = bundling.bundling_items.map((item) => item.id);
const totalRates = await this.dataService.itemsRateTotal(
season_period_id,
ids,
);
this.dataService.updateRatesBySeasonItem(
season_period_id,
bundling.id,
totalRates,
);
}
}
}

View File

@ -1,11 +1,9 @@
import { Body, Controller, Delete, Param, Post, Put } from '@nestjs/common';
import { Body, Controller, Get, Param, Put } from '@nestjs/common';
import { ItemRateDataOrchestrator } from '../domain/usecases/item-rate-data.orchestrator';
import { ItemRateDto } from './dto/item-rate.dto';
import { MODULE_NAME } from 'src/core/strings/constants/module.constants';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { ItemRateEntity } from '../domain/entities/item-rate.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';
@ApiTags(`${MODULE_NAME.ITEM_RATE.split('-').join(' ')} - data`)

View File

@ -19,13 +19,14 @@ import { ItemRateModel } from './data/models/item-rate.model';
import { SeasonPeriodHolidayHandler } from './domain/usecases/handlers/item-created.handler';
import { SeasonPeriodDataService } from 'src/modules/season-related/season-period/data/services/season-period-data.service';
import { SeasonPeriodModel } from 'src/modules/season-related/season-period/data/models/season-period.model';
import { ItemModel } from '../item/data/models/item.model';
@Global()
@Module({
imports: [
ConfigModule.forRoot(),
TypeOrmModule.forFeature(
[ItemRateModel, SeasonPeriodModel],
[ItemRateModel, SeasonPeriodModel, ItemModel],
CONNECTION_NAME.DEFAULT,
),
CqrsModule,