Skip to content

Commit

Permalink
Fetch reportDetails categories
Browse files Browse the repository at this point in the history
  • Loading branch information
manelcecs committed Jan 25, 2024
1 parent 61fc155 commit 315124a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
84 changes: 83 additions & 1 deletion src/domain-services/categories/category-service.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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<InstanceDataOfModel<Database['categoryRef']>> =
await models.categoryRef.find({
where: {
[Cond.OR]: listOfCategoryRefORs as Array<
Condition<InstanceDataOfModel<Database['categoryRef']>>
>,
},
});

const mapOfCategoriesAndReportDetails = new Map<number, ReportDetail[]>();

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<InstanceDataOfModel<Database['category']>> =
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;
}
}
6 changes: 3 additions & 3 deletions src/domain-services/flows/flow-search-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 315124a

Please sign in to comment.