Skip to content

Commit

Permalink
feat: unique duplicate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
web-mi committed Feb 28, 2024
1 parent 2e02954 commit 623432a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 45 deletions.
24 changes: 2 additions & 22 deletions src/api/apiAgencySelection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { endpoints } from '../resources/scripts/endpoints';
import { fetchData, FETCH_METHODS, FETCH_ERRORS } from './fetchData';
import { AgencyDataInterface } from '../globalState/interfaces';
import { apiGetConsultingType } from './apiGetConsultingType';
import { loadConsultingTypesForAgencies } from '../utils/loadConsultingTypesForAgencies';

export const apiAgencySelection = async (
{
Expand Down Expand Up @@ -52,26 +52,6 @@ export const apiAgencySelection = async (
return agencies;
}

// Get unique consultingTypes to prevent multiple requests to api
const uniqueConsultingTypeIds = [
...new Set(
agencies.map((a) => a?.consultingType).filter(Boolean)
)
];

return Promise.all(
uniqueConsultingTypeIds.map((consultingTypeId) =>
apiGetConsultingType({
consultingTypeId
})
)
).then((consultingTypes) =>
agencies.map((a) => ({
...a,
consultingTypeRel: consultingTypes.find(
(c) => c.id === a.consultingType
)
}))
);
return loadConsultingTypesForAgencies(agencies);
});
};
28 changes: 5 additions & 23 deletions src/api/apiGetConsultant.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { endpoints } from '../resources/scripts/endpoints';
import { fetchData, FETCH_METHODS, FETCH_ERRORS } from './fetchData';
import { ConsultantDataInterface } from '../globalState/interfaces';
import { apiGetConsultingType } from './apiGetConsultingType';
import { loadConsultingTypesForAgencies } from '../utils/loadConsultingTypesForAgencies';

export const apiGetConsultant = async (
consultantId: string,
Expand All @@ -14,32 +14,14 @@ export const apiGetConsultant = async (
method: FETCH_METHODS.GET,
skipAuth: true,
responseHandling: [FETCH_ERRORS.CATCH_ALL]
}).then((user: ConsultantDataInterface) => {
}).then(async (user: ConsultantDataInterface) => {
if (!fetchConsultingTypeDetails) {
return user;
}

// Get unique consultingTypes to prevent multiple requests to api
const uniqueConsultingTypeIds = [
...new Set(
user.agencies.map((a) => a?.consultingType).filter(Boolean)
)
];

return Promise.all(
uniqueConsultingTypeIds.map((consultingTypeId) =>
apiGetConsultingType({
consultingTypeId
})
)
).then((consultingTypes) => ({
return {
...user,
agencies: user.agencies.map((a) => ({
...a,
consultingTypeRel: consultingTypes.find(
(c) => c.id === a.consultingType
)
}))
}));
agencies: await loadConsultingTypesForAgencies(user.agencies)
};
});
};
26 changes: 26 additions & 0 deletions src/utils/loadConsultingTypesForAgencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { apiGetConsultingType } from '../api';
import { AgencyDataInterface } from '../globalState/interfaces';

export const loadConsultingTypesForAgencies = async (
agencies: AgencyDataInterface[]
): Promise<AgencyDataInterface[]> => {
// Get unique consultingTypes to prevent multiple requests to api
const uniqueConsultingTypeIds = [
...new Set(agencies.map((a) => a?.consultingType).filter(Boolean))
];

return Promise.all(
uniqueConsultingTypeIds.map((consultingTypeId) =>
apiGetConsultingType({
consultingTypeId
})
)
).then((consultingTypes) =>
agencies.map((a) => ({
...a,
consultingTypeRel: consultingTypes.find(
(c) => c.id === a.consultingType
)
}))
);
};

0 comments on commit 623432a

Please sign in to comment.