diff --git a/src/domain-services/categories/category-service.ts b/src/domain-services/categories/category-service.ts index 20560423..c64c54b4 100644 --- a/src/domain-services/categories/category-service.ts +++ b/src/domain-services/categories/category-service.ts @@ -57,7 +57,10 @@ export class CategoryService { throw new Error(`Category with ID ${catRef.categoryID} does not exist`); } - categoriesPerFlow.push(this.mapCategoryToFlowCategory(category, catRef)); + if (!categoriesPerFlow.some((cat) => cat.id === category.id.valueOf())) { + const mappedCategory = this.mapCategoryToFlowCategory(category, catRef); + categoriesPerFlow.push(mappedCategory); + } } return categoriesMap; diff --git a/src/domain-services/flows/flow-search-service.ts b/src/domain-services/flows/flow-search-service.ts index 71a60111..c6d4f58d 100644 --- a/src/domain-services/flows/flow-search-service.ts +++ b/src/domain-services/flows/flow-search-service.ts @@ -147,7 +147,7 @@ export class FlowSearchService { const flowLink = flowLinksMap.get(flow.id) ?? []; const categories = categoriesMap.get(flow.id) ?? []; const organizations = organizationsMap.get(flow.id) ?? []; - const locations = [...(locationsMap.get(flow.id) ?? [])]; + const locations = locationsMap.get(flow.id) ?? []; const plans = plansMap.get(flow.id) ?? []; const usageYears = usageYearsMap.get(flow.id) ?? []; const externalReferences = externalReferencesMap.get(flow.id) ?? []; @@ -431,12 +431,11 @@ export class FlowSearchService { usageYears, childIDs, parentIDs, - origAmount: flow.origAmount ? flow.origAmount.toString() : '', - origCurrency: flow.origCurrency ? flow.origCurrency.toString() : '', + origAmount: flow.origAmount ? flow.origAmount.toString() : null, + origCurrency: flow.origCurrency ? flow.origCurrency.toString() : null, externalReferences, reportDetails, - parkedParentSource: - parkedParentSource.length > 0 ? parkedParentSource : null, + parkedParentSource, // Paged item field cursor: flow.id.valueOf(), }; diff --git a/src/domain-services/flows/graphql/types.ts b/src/domain-services/flows/graphql/types.ts index 1307e47f..cfa313bb 100644 --- a/src/domain-services/flows/graphql/types.ts +++ b/src/domain-services/flows/graphql/types.ts @@ -61,41 +61,41 @@ export class BaseFlow extends BaseType { @ObjectType() export class Flow extends BaseFlow { - @Field(() => [Category], { nullable: true }) + @Field(() => [Category], { nullable: false }) categories: Category[]; - @Field(() => [Organization], { nullable: true }) + @Field(() => [Organization], { nullable: false }) organizations: Organization[]; - @Field(() => [BasePlan], { nullable: true }) + @Field(() => [BasePlan], { nullable: false }) plans: BasePlan[]; - @Field(() => [BaseLocation], { nullable: true }) + @Field(() => [BaseLocation], { nullable: false }) locations: BaseLocation[]; - @Field(() => [UsageYear], { nullable: true }) + @Field(() => [UsageYear], { nullable: false }) usageYears: UsageYear[]; - @Field(() => [Number], { nullable: true }) + @Field(() => [Number], { nullable: false }) childIDs: number[]; - @Field(() => [Number], { nullable: true }) + @Field(() => [Number], { nullable: false }) parentIDs: number[]; - @Field({ nullable: true }) - origAmount: string; + @Field(() => String, { nullable: true }) + origAmount: string | null; - @Field({ nullable: true }) - origCurrency: string; + @Field(() => String, { nullable: true }) + origCurrency: string | null; - @Field(() => [FlowExternalReference], { nullable: true }) + @Field(() => [FlowExternalReference], { nullable: false }) externalReferences: FlowExternalReference[]; - @Field(() => [ReportDetail], { nullable: true }) + @Field(() => [ReportDetail], { nullable: false }) reportDetails: ReportDetail[]; - @Field(() => [FlowParkedParentSource], { nullable: true }) - parkedParentSource: FlowParkedParentSource[] | null; + @Field(() => [FlowParkedParentSource], { nullable: false }) + parkedParentSource: FlowParkedParentSource[]; } @ObjectType() diff --git a/src/domain-services/location/location-service.ts b/src/domain-services/location/location-service.ts index eb83dbe0..cb6311da 100644 --- a/src/domain-services/location/location-service.ts +++ b/src/domain-services/location/location-service.ts @@ -33,7 +33,7 @@ export class LocationService { async getLocationsForFlows( locationsFO: Array>, models: Database - ): Promise>> { + ): Promise> { const locationObjectsIDs: LocationId[] = locationsFO.map((locFO) => createBrandedValue(locFO.objectID) ); @@ -47,20 +47,26 @@ export class LocationService { }, }); - const locationsMap = new Map>(); + const locationsMap = new Map(); for (const locFO of locationsFO) { const flowId = locFO.flowID; if (!locationsMap.has(flowId)) { - locationsMap.set(flowId, new Set()); + locationsMap.set(flowId, []); } const location = locations.find((loc) => loc.id === locFO.objectID); if (!location) { throw new Error(`Location with ID ${locFO.objectID} does not exist`); } - const locationMapped = this.mapLocationsToFlowLocations(location, locFO); - locationsMap.get(flowId)!.add(locationMapped); + const locationsPerFlow = locationsMap.get(flowId)!; + if (!locationsPerFlow.some((loc) => loc.id === location.id)) { + const locationMapped = this.mapLocationsToFlowLocations( + location, + locFO + ); + locationsPerFlow.push(locationMapped); + } } return locationsMap; } diff --git a/src/domain-services/organizations/organization-service.ts b/src/domain-services/organizations/organization-service.ts index 411531c4..2a73dae0 100644 --- a/src/domain-services/organizations/organization-service.ts +++ b/src/domain-services/organizations/organization-service.ts @@ -32,13 +32,17 @@ export class OrganizationService { ); } - const organizationMapped: Organization = - this.mapOrganizationsToOrganizationFlows( - organization, - orgFO.refDirection - ); - - organizationsMap.get(flowId)!.push(organizationMapped); + const organizationPerFlow = organizationsMap.get(flowId)!; + if ( + !organizationPerFlow.some((org) => org.id === organization.id.valueOf()) + ) { + const organizationMapped: Organization = + this.mapOrganizationsToOrganizationFlows( + organization, + orgFO.refDirection + ); + organizationsMap.get(flowId)!.push(organizationMapped); + } } return organizationsMap; diff --git a/src/domain-services/plans/plan-service.ts b/src/domain-services/plans/plan-service.ts index f4d98301..d7aaa617 100644 --- a/src/domain-services/plans/plan-service.ts +++ b/src/domain-services/plans/plan-service.ts @@ -74,24 +74,33 @@ export class PlanService { }, }); - const planFlowOobject = plansFO.find( + const planFlowObject = plansFO.find( (planFO) => planFO.objectID === plan.id ); - const flowId = planFlowOobject && planFlowOobject.flowID; + if (!planVersion.length) { + throw new Error(`Plan with ID ${plan.id} does not have a version`); + } - const planMapped = this.mapPlansToFlowPlans( - plan, - planVersion[0], - planFlowOobject?.refDirection ?? null - ); + if (!planFlowObject) { + throw new Error(`Plan with ID ${plan.id} does not have a flow object`); + } + + const flowId = planFlowObject && planFlowObject.flowID; + + if (!plansMap.has(flowId)) { + plansMap.set(flowId, []); + } - if (flowId) { - if (!plansMap.has(flowId)) { - plansMap.set(flowId, []); - } + const plansPerFlow = plansMap.get(flowId)!; - plansMap.get(flowId)!.push(planMapped); + if (!plansPerFlow.some((plan) => plan.id === plan.id)) { + const planMapped = this.mapPlansToFlowPlans( + plan, + planVersion[0], + planFlowObject?.refDirection ?? null + ); + plansPerFlow.push(planMapped); } } diff --git a/src/domain-services/report-details/report-detail-service.ts b/src/domain-services/report-details/report-detail-service.ts index 9d0f898a..b7ef5533 100644 --- a/src/domain-services/report-details/report-detail-service.ts +++ b/src/domain-services/report-details/report-detail-service.ts @@ -30,10 +30,19 @@ export class ReportDetailService { (report) => report && flowId === report?.flowID ); - if (reportDetail) { + if (!reportDetail) { + throw new Error(`Report detail with flow ID ${flowId} does not exist`); + } + + const reportDetailsPerFlow = reportDetailsMap.get(flowId)!; + if ( + !reportDetailsPerFlow.some( + (report) => report.id === reportDetail.id.valueOf() + ) + ) { const reportDetailMapped = this.mapReportDetailsToFlowReportDetail(reportDetail); - reportDetailsMap.get(flowId)?.push(reportDetailMapped); + reportDetailsPerFlow.push(reportDetailMapped); } } diff --git a/src/domain-services/usage-years/usage-year-service.ts b/src/domain-services/usage-years/usage-year-service.ts index dfb1e6f5..7a495460 100644 --- a/src/domain-services/usage-years/usage-year-service.ts +++ b/src/domain-services/usage-years/usage-year-service.ts @@ -35,11 +35,14 @@ export class UsageYearService { `Usage year with ID ${usageYearFO.objectID} does not exist` ); } - const usageYearMapped = this.mapUsageYearsToFlowUsageYears( - usageYear, - usageYearFO.refDirection - ); - usageYearsMap.get(flowId)!.push(usageYearMapped); + const usageYearsPerFlow = usageYearsMap.get(flowId)!; + if (!usageYearsPerFlow.some((uYear) => uYear.year === usageYear.year)) { + const usageYearMapped = this.mapUsageYearsToFlowUsageYears( + usageYear, + usageYearFO.refDirection + ); + usageYearsPerFlow.push(usageYearMapped); + } } return usageYearsMap;