Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved: Added logic to fetch the facilities for the linked facility group if it exceeds the view size (#322) #324

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 40 additions & 42 deletions src/services/FacilityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,53 +554,51 @@ const fetchArchivedFacilities = async (): Promise<any> => {

const fetchFacilityCountByGroup = async (facilityGroupIds: any): Promise<any> => {
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)
}
let facilityMemberResponses = [] as any;
let viewIndex = 0;
let resp = {} as any;

const facilityCountResponse = await Promise.allSettled(requests.map((params) => api({
url: 'performFind',
method: 'POST',
data: params
})))
try {
do {
const params = {
inputFields: {
facilityGroupId: facilityGroupIds,
facilityGroupId_op: "in"
},
viewSize: 250, // maximum view size
viewIndex,
entityName: 'FacilityGroupAndMember',
noConditionFind: "Y",
filterByDate: 'Y',
fieldList: ['facilityGroupId', 'facilityId']
};

const hasFailedResponse = facilityCountResponse.some((response: any) => hasError(response.value) && !response?.data?.count)
if (hasFailedResponse) {
logger.error('Failed to fetch facility count for some groups')
}
resp = await api({
url: 'performFind',
method: 'POST',
data: params
});

// 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)
if (!hasError(resp) && resp.data.count) {
facilityMemberResponses = [...facilityMemberResponses, ...resp.data.docs];
viewIndex++;
} else {
throw resp.data;
}
return responseData
}, [])
} while (resp.data.docs.length >= 250);

return allResponseData.reduce((facilityCountByGroup: any, responseData: any) => {
if (facilityCountByGroup[responseData.facilityGroupId]) {
facilityCountByGroup[responseData.facilityGroupId] += 1
} else {
facilityCountByGroup[responseData.facilityGroupId] = 1
}
return facilityCountByGroup
}, {})
return facilityMemberResponses.reduce((facilityCountByGroup: any, facilityData: any) => {
if (facilityCountByGroup[facilityData.facilityGroupId]) {
facilityCountByGroup[facilityData.facilityGroupId] += 1;
} else {
facilityCountByGroup[facilityData.facilityGroupId] = 1
}
return facilityCountByGroup
}, {})
} catch (error) {
logger.error(error)
return {}
}
}

const fetchProductStoreCountByGroup = async (facilityGroupIds: Array<string>): Promise<any> => {
Expand Down
Loading