From d2600f5783431d8c9e40b69dc90482feeefc4639 Mon Sep 17 00:00:00 2001 From: manelcecs Date: Mon, 4 Dec 2023 13:29:19 +0100 Subject: [PATCH] Remove throws to avoid breaking the process Remove unneeded variables from 'searchFlowsBatches' --- .../categories/category-service.ts | 9 ++--- .../flows/flow-search-service.ts | 10 +---- src/domain-services/flows/graphql/resolver.ts | 1 - .../location/location-service.ts | 19 +++++---- .../organizations/organization-service.ts | 30 +++++++------- src/domain-services/plans/plan-service.ts | 40 ++++++++----------- .../report-details/report-detail-service.ts | 23 +++++------ .../usage-years/usage-year-service.ts | 21 +++++----- 8 files changed, 66 insertions(+), 87 deletions(-) diff --git a/src/domain-services/categories/category-service.ts b/src/domain-services/categories/category-service.ts index c64c54b4..c7fe5131 100644 --- a/src/domain-services/categories/category-service.ts +++ b/src/domain-services/categories/category-service.ts @@ -53,11 +53,10 @@ export class CategoryService { const category = categories.find((cat) => cat.id === catRef.categoryID); - if (!category) { - throw new Error(`Category with ID ${catRef.categoryID} does not exist`); - } - - if (!categoriesPerFlow.some((cat) => cat.id === category.id.valueOf())) { + if ( + category && + !categoriesPerFlow.some((cat) => cat.id === category.id.valueOf()) + ) { const mappedCategory = this.mapCategoryToFlowCategory(category, catRef); categoriesPerFlow.push(mappedCategory); } diff --git a/src/domain-services/flows/flow-search-service.ts b/src/domain-services/flows/flow-search-service.ts index 245f33dc..ca0ee5db 100644 --- a/src/domain-services/flows/flow-search-service.ts +++ b/src/domain-services/flows/flow-search-service.ts @@ -540,28 +540,22 @@ export class FlowSearchService { ): Promise { const flowSearchResponse = await this.search(models, args); - const batchesMissing = - Math.round(flowSearchResponse.total / args.limit) - 1; const flows: FlowPaged[] = flowSearchResponse.flows; let hasNextPage = flowSearchResponse.hasNextPage; - let batchCount = 1; let cursor = flowSearchResponse.endCursor; let nextArgs: SearchFlowsArgs = { ...args, afterCursor: cursor }; let nextFlowSearchResponse: FlowSearchResult; while (hasNextPage) { - batchCount++; - nextFlowSearchResponse = await this.search(models, nextArgs); flows.push(...nextFlowSearchResponse.flows); - hasNextPage = - nextFlowSearchResponse.hasNextPage && batchCount <= batchesMissing; - + hasNextPage = nextFlowSearchResponse.hasNextPage; cursor = nextFlowSearchResponse.endCursor; + // Update the cursor for the next iteration nextArgs = { ...args, afterCursor: cursor }; } diff --git a/src/domain-services/flows/graphql/resolver.ts b/src/domain-services/flows/graphql/resolver.ts index 00091d92..1e149518 100644 --- a/src/domain-services/flows/graphql/resolver.ts +++ b/src/domain-services/flows/graphql/resolver.ts @@ -41,7 +41,6 @@ export default class FlowResolver { ): Promise { // Set default batch size to 1000 args.limit = args.limit > 0 ? args.limit : 1000; - return await this.flowSearchService.searchBatches(context.models, args); } } diff --git a/src/domain-services/location/location-service.ts b/src/domain-services/location/location-service.ts index cb6311da..04274ee6 100644 --- a/src/domain-services/location/location-service.ts +++ b/src/domain-services/location/location-service.ts @@ -56,16 +56,15 @@ export class LocationService { } const location = locations.find((loc) => loc.id === locFO.objectID); - if (!location) { - throw new Error(`Location with ID ${locFO.objectID} does not exist`); - } - const locationsPerFlow = locationsMap.get(flowId)!; - if (!locationsPerFlow.some((loc) => loc.id === location.id)) { - const locationMapped = this.mapLocationsToFlowLocations( - location, - locFO - ); - locationsPerFlow.push(locationMapped); + if (location) { + 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 2a73dae0..8a788578 100644 --- a/src/domain-services/organizations/organization-service.ts +++ b/src/domain-services/organizations/organization-service.ts @@ -26,22 +26,20 @@ export class OrganizationService { (org) => org.id === orgFO.objectID ); - if (!organization) { - throw new Error( - `Organization with ID ${orgFO.objectID} does not exist` - ); - } - - 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); + if (organization) { + 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); + } } } diff --git a/src/domain-services/plans/plan-service.ts b/src/domain-services/plans/plan-service.ts index d7aaa617..3796566e 100644 --- a/src/domain-services/plans/plan-service.ts +++ b/src/domain-services/plans/plan-service.ts @@ -78,29 +78,23 @@ export class PlanService { (planFO) => planFO.objectID === plan.id ); - if (!planVersion.length) { - throw new Error(`Plan with ID ${plan.id} does not have a version`); - } - - 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, []); - } - - const plansPerFlow = plansMap.get(flowId)!; - - if (!plansPerFlow.some((plan) => plan.id === plan.id)) { - const planMapped = this.mapPlansToFlowPlans( - plan, - planVersion[0], - planFlowObject?.refDirection ?? null - ); - plansPerFlow.push(planMapped); + if (planVersion.length && planFlowObject) { + const flowId = planFlowObject && planFlowObject.flowID; + + if (!plansMap.has(flowId)) { + plansMap.set(flowId, []); + } + + const plansPerFlow = plansMap.get(flowId)!; + + 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 e702ddde..6de2df9d 100644 --- a/src/domain-services/report-details/report-detail-service.ts +++ b/src/domain-services/report-details/report-detail-service.ts @@ -31,19 +31,18 @@ export class ReportDetailService { (report) => report && flowId === report?.flowID ); - if (!reportDetail) { - throw new Error(`Report detail with flow ID ${flowId} does not exist`); - } + if (reportDetail) { + const reportDetailsPerFlow = reportDetailsMap.get(flowId)!; - const reportDetailsPerFlow = reportDetailsMap.get(flowId)!; - if ( - !reportDetailsPerFlow.some( - (report) => report.id === reportDetail.id.valueOf() - ) - ) { - const reportDetailMapped = - this.mapReportDetailsToFlowReportDetail(reportDetail); - reportDetailsPerFlow.push(reportDetailMapped); + if ( + !reportDetailsPerFlow.some( + (report) => report.id === reportDetail.id.valueOf() + ) + ) { + const reportDetailMapped = + this.mapReportDetailsToFlowReportDetail(reportDetail); + 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 d6463108..e903f4d2 100644 --- a/src/domain-services/usage-years/usage-year-service.ts +++ b/src/domain-services/usage-years/usage-year-service.ts @@ -30,18 +30,15 @@ export class UsageYearService { (uYear) => uYear.id === usageYearFO.objectID ); - if (!usageYear) { - throw new Error( - `Usage year with ID ${usageYearFO.objectID} does not exist` - ); - } - const usageYearsPerFlow = usageYearsMap.get(flowId)!; - if (!usageYearsPerFlow.some((uYear) => uYear.year === usageYear.year)) { - const usageYearMapped = this.mapUsageYearsToFlowUsageYears( - usageYear, - usageYearFO.refDirection - ); - usageYearsPerFlow.push(usageYearMapped); + if (usageYear) { + const usageYearsPerFlow = usageYearsMap.get(flowId)!; + if (!usageYearsPerFlow.some((uYear) => uYear.year === usageYear.year)) { + const usageYearMapped = this.mapUsageYearsToFlowUsageYears( + usageYear, + usageYearFO.refDirection + ); + usageYearsPerFlow.push(usageYearMapped); + } } }