From 9baf9999eca0834b689c7782457ba277527db3d9 Mon Sep 17 00:00:00 2001 From: R-Sourabh Date: Wed, 16 Oct 2024 14:28:13 +0530 Subject: [PATCH 1/3] Improved: Added logic to fetch the facilities for the linked facility group if it exceeds the view size (#322) --- src/services/FacilityService.ts | 90 ++++++++++++++++----------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/src/services/FacilityService.ts b/src/services/FacilityService.ts index 6fc03b63..f77085d7 100644 --- a/src/services/FacilityService.ts +++ b/src/services/FacilityService.ts @@ -554,53 +554,53 @@ const fetchArchivedFacilities = async (): Promise => { const fetchFacilityCountByGroup = async (facilityGroupIds: any): Promise => { if (!facilityGroupIds.length) return [] - const requests = [] - - const facilityGroupIdList = facilityGroupIds - while (facilityGroupIdList.length) { - const batch = facilityGroupIdList.splice(0, 10) - const params = { - inputFields: { - facilityGroupId: batch, - facilityGroupId_op: "in" - }, - viewSize: 250, // maximum view size - entityName: 'FacilityGroupAndMember', - noConditionFind: "Y", - filterByDate: 'Y', - fieldList: ['facilityGroupId', 'facilityId'] - } - requests.push(params) - } - - const facilityCountResponse = await Promise.allSettled(requests.map((params) => api({ - url: 'performFind', - method: 'POST', - data: params - }))) - - const hasFailedResponse = facilityCountResponse.some((response: any) => hasError(response.value) && !response?.data?.count) - if (hasFailedResponse) { - logger.error('Failed to fetch facility count for some groups') + let allResponseData = [] as any + let viewIndex = 0; + + const params = { + inputFields: { + facilityGroupId: facilityGroupIds, // Send all facilityGroupIds at once + facilityGroupId_op: "in" + }, + viewSize: 250, // maximum view size + viewIndex: viewIndex, + entityName: 'FacilityGroupAndMember', + noConditionFind: "Y", + filterByDate: 'Y', + fieldList: ['facilityGroupId', 'facilityId'] } - - // taking out the response from Promise.allSettled's 'value' field first - const allResponseData = facilityCountResponse.map((response: any) => response.value) - .reduce((responseData: any, response: any) => { - if (!hasError(response)) { - responseData.push(...response.data.docs) + + try { + let responseData; + do { + params.viewIndex = viewIndex; + const facilityCountResponse = await api({ + url: 'performFind', + method: 'POST', + data: params + }) + + if (hasError(facilityCountResponse) && !facilityCountResponse?.data?.count) { + logger.error('Failed to fetch facility count for some groups') } - return responseData - }, []) - - return allResponseData.reduce((facilityCountByGroup: any, responseData: any) => { - if (facilityCountByGroup[responseData.facilityGroupId]) { - facilityCountByGroup[responseData.facilityGroupId] += 1 - } else { - facilityCountByGroup[responseData.facilityGroupId] = 1 - } - return facilityCountByGroup - }, {}) + + responseData = facilityCountResponse?.data?.docs + allResponseData = allResponseData.concat(responseData) + viewIndex++ + } while (responseData.length >= 250) + + return allResponseData.reduce((facilityCountByGroup: any, responseData: any) => { + if (facilityCountByGroup[responseData.facilityGroupId]) { + facilityCountByGroup[responseData.facilityGroupId] += 1 + } else { + facilityCountByGroup[responseData.facilityGroupId] = 1 + } + return facilityCountByGroup + }, {}) + } catch (error) { + logger.error(error) + return {} + } } const fetchProductStoreCountByGroup = async (facilityGroupIds: Array): Promise => { From 54cff5d4ce6e4981792ca49fdd16a7caa2b16b6e Mon Sep 17 00:00:00 2001 From: R-Sourabh Date: Wed, 16 Oct 2024 17:41:43 +0530 Subject: [PATCH 2/3] Improved: the logic of fetching facilityCountByGroup service(#324) --- src/services/FacilityService.ts | 62 ++++++++++++++++----------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/src/services/FacilityService.ts b/src/services/FacilityService.ts index f77085d7..d3df4743 100644 --- a/src/services/FacilityService.ts +++ b/src/services/FacilityService.ts @@ -554,46 +554,44 @@ const fetchArchivedFacilities = async (): Promise => { const fetchFacilityCountByGroup = async (facilityGroupIds: any): Promise => { if (!facilityGroupIds.length) return [] - let allResponseData = [] as any + let facilityMemberResponses = [] as any; let viewIndex = 0; - - const params = { - inputFields: { - facilityGroupId: facilityGroupIds, // Send all facilityGroupIds at once - facilityGroupId_op: "in" - }, - viewSize: 250, // maximum view size - viewIndex: viewIndex, - entityName: 'FacilityGroupAndMember', - noConditionFind: "Y", - filterByDate: 'Y', - fieldList: ['facilityGroupId', 'facilityId'] - } - + let resp = {} as any; + try { - let responseData; do { - params.viewIndex = viewIndex; - const facilityCountResponse = await api({ + const params = { + inputFields: { + facilityGroupId: facilityGroupIds, // Send all facilityGroupIds at once + facilityGroupId_op: "in" + }, + viewSize: 250, // maximum view size + viewIndex, + entityName: 'FacilityGroupAndMember', + noConditionFind: "Y", + filterByDate: 'Y', + fieldList: ['facilityGroupId', 'facilityId'] + }; + + resp = await api({ url: 'performFind', method: 'POST', data: params - }) - - if (hasError(facilityCountResponse) && !facilityCountResponse?.data?.count) { - logger.error('Failed to fetch facility count for some groups') + }); + + if (!hasError(resp) && resp.data.count) { + facilityMemberResponses = [...facilityMemberResponses, ...resp.data.docs]; + viewIndex++; + } else { + throw resp.data; } - - responseData = facilityCountResponse?.data?.docs - allResponseData = allResponseData.concat(responseData) - viewIndex++ - } while (responseData.length >= 250) - - return allResponseData.reduce((facilityCountByGroup: any, responseData: any) => { - if (facilityCountByGroup[responseData.facilityGroupId]) { - facilityCountByGroup[responseData.facilityGroupId] += 1 + } while (resp.data.docs.length >= 250); + + return facilityMemberResponses.reduce((facilityCountByGroup: any, facilityData: any) => { + if (facilityCountByGroup[facilityData.facilityGroupId]) { + facilityCountByGroup[facilityData.facilityGroupId] += 1; } else { - facilityCountByGroup[responseData.facilityGroupId] = 1 + facilityCountByGroup[facilityData.facilityGroupId] = 1 } return facilityCountByGroup }, {}) From 2ec523bc0cc9b28dd696fa43983a89efd1cd63c6 Mon Sep 17 00:00:00 2001 From: R-Sourabh Date: Wed, 16 Oct 2024 18:09:53 +0530 Subject: [PATCH 3/3] Improved: removed a comment as we are sending the facilityGroup in batch(#322) --- src/services/FacilityService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/FacilityService.ts b/src/services/FacilityService.ts index d3df4743..dd8e7335 100644 --- a/src/services/FacilityService.ts +++ b/src/services/FacilityService.ts @@ -562,7 +562,7 @@ const fetchFacilityCountByGroup = async (facilityGroupIds: any): Promise => do { const params = { inputFields: { - facilityGroupId: facilityGroupIds, // Send all facilityGroupIds at once + facilityGroupId: facilityGroupIds, facilityGroupId_op: "in" }, viewSize: 250, // maximum view size