From 315124a33dfc5c546388fe158bd01b5d10dbeecc Mon Sep 17 00:00:00 2001 From: manelcecs Date: Thu, 25 Jan 2024 12:25:34 +0100 Subject: [PATCH] Fetch reportDetails categories --- .../categories/category-service.ts | 84 ++++++++++++++++++- .../flows/flow-search-service.ts | 6 +- 2 files changed, 86 insertions(+), 4 deletions(-) diff --git a/src/domain-services/categories/category-service.ts b/src/domain-services/categories/category-service.ts index 293116f1..a6f14bd9 100644 --- a/src/domain-services/categories/category-service.ts +++ b/src/domain-services/categories/category-service.ts @@ -1,8 +1,13 @@ import { type Database } from '@unocha/hpc-api-core/src/db'; import { type FlowId } from '@unocha/hpc-api-core/src/db/models/flow'; -import { Op } from '@unocha/hpc-api-core/src/db/util/conditions'; +import { + Cond, + Op, + type Condition, +} from '@unocha/hpc-api-core/src/db/util/conditions'; import { type InstanceDataOfModel } from '@unocha/hpc-api-core/src/db/util/raw-model'; import { Service } from 'typedi'; +import { type ReportDetail } from '../report-details/graphql/types'; import { type Category } from './graphql/types'; @Service() @@ -117,4 +122,81 @@ export class CategoryService { return categoryRef; } + + async addChannelToReportDetails( + models: Database, + reportDetails: ReportDetail[] + ) { + const listOfCategoryRefORs = []; + + for (const reportDetail of reportDetails) { + const orClause = { + objectID: reportDetail.id, + objectType: 'reportDetail', + }; + + listOfCategoryRefORs.push(orClause); + } + + const categoriesRef: Array> = + await models.categoryRef.find({ + where: { + [Cond.OR]: listOfCategoryRefORs as Array< + Condition> + >, + }, + }); + + const mapOfCategoriesAndReportDetails = new Map(); + + for (const categoryRef of categoriesRef) { + const reportDetail = reportDetails.find( + (reportDetail) => reportDetail.id === categoryRef.objectID.valueOf() + ); + + if (!reportDetail) { + continue; + } + + if ( + !mapOfCategoriesAndReportDetails.has(categoryRef.categoryID.valueOf()) + ) { + mapOfCategoriesAndReportDetails.set( + categoryRef.categoryID.valueOf(), + [] + ); + } + + const reportDetailsPerCategory = mapOfCategoriesAndReportDetails.get( + categoryRef.categoryID.valueOf() + )!; + reportDetailsPerCategory.push(reportDetail); + } + + const categories: Array> = + await models.category.find({ + where: { + id: { + [Op.IN]: categoriesRef.map((catRef) => catRef.categoryID), + }, + }, + }); + + for (const [ + category, + reportDetails, + ] of mapOfCategoriesAndReportDetails.entries()) { + const categoryObj = categories.find((cat) => cat.id === category); + + if (!categoryObj) { + continue; + } + + for (const reportDetail of reportDetails) { + reportDetail.channel = categoryObj.name; + } + } + + return reportDetails; + } } diff --git a/src/domain-services/flows/flow-search-service.ts b/src/domain-services/flows/flow-search-service.ts index 08efe751..4d3933d3 100644 --- a/src/domain-services/flows/flow-search-service.ts +++ b/src/domain-services/flows/flow-search-service.ts @@ -225,9 +225,9 @@ export class FlowSearchService { const reportDetails = reportDetailsMap.get(flow.id) ?? []; const reportDetailsWithChannel = - this.reportDetailService.addChannelToReportDetails( - reportDetails, - categories + await this.categoryService.addChannelToReportDetails( + models, + reportDetails ); let parkedParentSource: FlowParkedParentSource | null = null;