Skip to content

Commit

Permalink
Fix duplicated entries on lists
Browse files Browse the repository at this point in the history
List with no entries are returned as empty lists
  • Loading branch information
manelcecs committed Nov 21, 2023
1 parent 2763234 commit 94e31b5
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 52 deletions.
5 changes: 4 additions & 1 deletion src/domain-services/categories/category-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions src/domain-services/flows/flow-search-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) ?? [];
Expand Down Expand Up @@ -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(),
};
Expand Down
30 changes: 15 additions & 15 deletions src/domain-services/flows/graphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 11 additions & 5 deletions src/domain-services/location/location-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class LocationService {
async getLocationsForFlows(
locationsFO: Array<InstanceDataOfModel<Database['flowObject']>>,
models: Database
): Promise<Map<number, Set<BaseLocation>>> {
): Promise<Map<number, BaseLocation[]>> {
const locationObjectsIDs: LocationId[] = locationsFO.map((locFO) =>
createBrandedValue(locFO.objectID)
);
Expand All @@ -47,20 +47,26 @@ export class LocationService {
},
});

const locationsMap = new Map<number, Set<BaseLocation>>();
const locationsMap = new Map<number, BaseLocation[]>();

for (const locFO of locationsFO) {
const flowId = locFO.flowID;
if (!locationsMap.has(flowId)) {
locationsMap.set(flowId, new Set<BaseLocation>());
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;
}
Expand Down
18 changes: 11 additions & 7 deletions src/domain-services/organizations/organization-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 21 additions & 12 deletions src/domain-services/plans/plan-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/domain-services/report-details/report-detail-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/domain-services/usage-years/usage-year-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 94e31b5

Please sign in to comment.