Skip to content

Commit

Permalink
Merge pull request #229 from pepfar-datim/2406-change-api-calls
Browse files Browse the repository at this point in the history
2406 change api calls
  • Loading branch information
maggiedemeke authored Jan 31, 2023
2 parents 8a94e0b + d65d3e1 commit 6f856b9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "datim-approvals",
"version": "2.2.0",
"version": "2.2.1",
"private": true,
"dependencies": {
"@dhis2/app-runtime": "^3.2.7",
Expand Down
1 change: 0 additions & 1 deletion src/modules/list/components/list.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export default class List extends React.Component<{
if (!f.ou || !f.period || !f.workflow) return;
this.setState({mechanisms: null, loading: {mechanisms: true}});
fetchMechanisms(this.state.filters).then(mechanisms=>{
console.log(mechanisms)
this.setState({mechanisms, loading:{mechanisms: false}});
}).catch((e)=>{
console.error(e);
Expand Down
106 changes: 66 additions & 40 deletions src/modules/list/services/mechanisms.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,23 @@ const agencyGroupSet = 'bw8KHXzxd9i';
const partnerGroupSet = 'BOyWrF33hiR';

function generateMechanismsUrl(filters){
return `/dataApprovals/categoryOptionCombos?wf=${filters.workflow}&pe=${filters.period}&ou=${filters.ou}`;
return `/dataApprovals/categoryOptionCombos?wf=${filters.workflow}&pe=${filters.period}`;
}

function getMechanismInfoUrl(ids){
function getMechanismInfoUrl(filters, isSuperUser){

let filter;
if (ids.length < 500) filter = `filter=categoryOptionCombos.id:in:[${ids.join(',')}]`;
else filter = 'filter=categories.id:eq:SH885jaRe0o';
let fields = 'fields=id,name,organisationUnits[id,name],categoryOptionGroups[id,name,groupSets[id]],categoryOptionCombos[id,name]';
if(filters.ou === 'ybg3MO3hcf4') return `/categoryOptions.json?paging=false&${fields}`;
else
{

if(isSuperUser){
filter = `filter=id:in:[xEzelmtHWPn,OM58NubPbx1,mXjFJEexCHJ,t6dWOH7W5Ml]&filter=organisationUnits.id:eq:${filters.ou}&rootJunction=OR`;
}
else
filter = `filter=organisationUnits.id:eq:${filters.ou}`;
}
return `/categoryOptions.json?paging=false&${filter}&${fields}`;
}

Expand Down Expand Up @@ -61,42 +70,59 @@ function filterSystemMechs(isSuperUser:boolean){
}
}


function filterOUbased(mechi, mech, orgUnit, isSuperUser){
let exists = false
if (isSuperUser && orgUnit !== 'ybg3MO3hcf4'){
if (mechi.organisationUnits.length !== 0){
if( mechi.categoryOptionCombos[0].id === mech.id && mechi.organisationUnits[0].id === mech.ou){
exists = true
}
}
else {
if (mechi.categoryOptionCombos[0].id === mech.id){
if(orgUnit === mech.ou ){
exists = true
}
}
}
}
else {
if(mechi.categoryOptionCombos[0].id===mech.id) exists = true
}
return exists
}

export async function fetchMechanisms(filters:SearchFilters):Promise<SearchMechanism[]>{
let isSuperUser:boolean = await checkSuperUser();
return getData(generateMechanismsUrl(filters)).then(mechResp=>{
// console.log(mechResp, mechResp)
if (mechResp.httpStatusCode===409) return [];
let mechanismIds = mechResp.map(m=>m.id);
return getData(getMechanismInfoUrl(mechanismIds)).then(categoryOptionsResp=>{
let mechanisms:MechanismModel[] = mechResp.map(mech=>{
let mechInfo = categoryOptionsResp.categoryOptions.filter(i=>i.categoryOptionCombos[0].id===mech.id)[0];
if (categoryOptionsResp.categoryOptions.filter(i=>i.id===mech.id).length>1) console.log(`Two info records per mechanism ${mech.id} ${mechInfo.name}`);
let localOU = getOu(mech, mechInfo, isSuperUser);
let status = getStatus(getWorkflowTypeById(filters.workflow), mech.level.level, mech.accepted);
return {
info: {
name: mechInfo.name,
ou: localOU.name||'N/A',
partner: getInfoByGroupSet(mechInfo, partnerGroupSet),
agency: getInfoByGroupSet(mechInfo, agencyGroupSet),
},
state: {
status: status,
actions: getPermittedActions(mech.permissions, status),
view: mech.permissions.mayReadData
},
meta: {
cocId: mech.id,
ou: mech.ou,
coId: mechInfo.id
}
};
}).filter(mech=>mech).filter(filterSystemMechs(isSuperUser))
return tranformMechanisms(mechanisms);
}).catch(e=>{
console.error(e);
return [];
})
}).catch(e=>{return []});

let getMechData = await getData(generateMechanismsUrl(filters))
let getMechinfoData = await getData(getMechanismInfoUrl(filters,isSuperUser))
if (getMechData.httpStatusCode===409) return [];
let mechanisms:MechanismModel[] = await getMechData.map(mech=>{
let mechInfo = getMechinfoData.categoryOptions.filter(i=>filterOUbased(i,mech,filters.ou,isSuperUser))[0]
if (getMechinfoData.categoryOptions.filter(i=>i.id===mech.id).length>1) console.log(`Two info records per mechanism ${mech.id} ${mechInfo.name}`);
if (mechInfo){
let localOU = getOu(mech, mechInfo, isSuperUser);
let status = getStatus(getWorkflowTypeById(filters.workflow), mech.level.level, mech.accepted);
return {
info: {
name: mechInfo.name,
ou: localOU.name||'N/A',
partner: getInfoByGroupSet(mechInfo, partnerGroupSet),
agency: getInfoByGroupSet(mechInfo, agencyGroupSet),
},
state: {
status: status,
actions: getPermittedActions(mech.permissions, status),
view: mech.permissions.mayReadData
},
meta: {
cocId: mech.id,
ou: mech.ou,
coId: mechInfo.id
}
};
}
}).filter(mech=>mech).filter(filterSystemMechs(isSuperUser))
return tranformMechanisms(mechanisms);
}

0 comments on commit 6f856b9

Please sign in to comment.