Skip to content

Commit

Permalink
[BUG] prioritize grant role on expert page #641 (#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
UcDust authored Nov 8, 2024
1 parent 8cd4d52 commit 4a36c29
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions services/base-service/spa/client/public/lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,41 @@ class Utils {

/**
* @method getGrantRole
* @description given a GrantType vivo role @type, returns the role to display in AE. defaults to Researcher if no match
* @description given a relationship with a GrantType vivo role @type, returns the role to display in AE and the relationship ID (if exists).
* defaults to Researcher with null relationship ID if no match
*
* @param {String | Array} type
* @param {Object | Array} roles relationship object with @type or array of @type
*
* @return {String} readable roles
* @return {Object} readable roles and relationship id (if exists)
*/
getGrantRole(roles) {
let readableRole = 'Researcher';
let relationshipId = null;

if( !Array.isArray(roles) ) roles = [roles];

if( roles.includes('PrincipalInvestigatorRole') ) readableRole = 'Principal Investigator';
else if( roles.includes('CoPrincipalInvestigatorRole') ) readableRole = 'Co-Principal Investigator';
else if( roles.includes('LeaderRole') ) readableRole = 'Leader';
const normalizeType = (type) => Array.isArray(type) ? type : [type];

try {
let piRole = roles.find(r => normalizeType(r['@type']).includes('PrincipalInvestigatorRole'));
let leaderRole = roles.find(r => normalizeType(r['@type']).includes('LeaderRole'));
let copiRole = roles.find(r => normalizeType(r['@type']).includes('CoPrincipalInvestigatorRole'));

if( piRole ) {
readableRole = 'Principal Investigator';
relationshipId = piRole['@id'];
} else if( leaderRole ) {
readableRole = 'Leader';
relationshipId = leaderRole['@id'];
} else if( copiRole ) {
readableRole = 'Co-Principal Investigator';
relationshipId = copiRole['@id'];
}
} catch(e) {
console.error('Error parsing grant roles', roles);
}

return readableRole;
return { relationshipId, role: readableRole };
}

/**
Expand Down Expand Up @@ -120,7 +140,7 @@ class Utils {
let relatedBy = g.relatedBy || [];
if( !Array.isArray(relatedBy) ) relatedBy = [relatedBy];

let expertsRelationship = {};
let expertsRelationships = [];
let otherRelationships = [];

relatedBy.forEach(r => {
Expand All @@ -130,27 +150,27 @@ class Utils {

relates.forEach(relate => {
if( typeof relate === 'string' && relate.trim().toLowerCase() === expertId.trim().toLowerCase() ) {
expertsRelationship = r;
expertsRelationships.push(r);
isExpert = true;
} else if( relate['@id'] && relate['@id'].includes(expertId) ) {
expertsRelationship = r;
expertsRelationships.push(r);
isExpert = true;
}
});
if( !isExpert ) otherRelationships.push(r);
});

if( filterHidden && !expertsRelationship['is-visible'] ) {
if( filterHidden && !expertsRelationships.some(r => r['is-visible']) ) {
console.warn('Invalid grant is-visible, should be true', g);
return;
}

g.isVisible = expertsRelationship['is-visible'];
g.relationshipId = expertsRelationship['@id'];
g.isVisible = expertsRelationships.some(r => r['is-visible']);
// g.relationshipId = expertsRelationship['@id'];

// determine pi/copi in otherRelationships
let contributors = otherRelationships.map(r => {
let contributorRole = this.getGrantRole(r['@type'] || '');
let { role: contributorRole } = this.getGrantRole(r);
if( contributorRole !== 'Co-Principal Investigator' ) return;

let contributorName = r.relates.filter(relate => relate.name)[0]?.name || '';
Expand All @@ -165,7 +185,7 @@ class Utils {
g.contributors = contributors.filter(c => c); // remove undefined

// determine role/type using expertsRelationship
g.role = this.getGrantRole(expertsRelationship['@type'] || '');
({ role: g.role, relationshipId: g.relationshipId } = this.getGrantRole(expertsRelationships));

// determine type(s) from all types excluding 'Grant', and split everything after 'Grant_' by uppercase letters with space
// should just be one type, but just in case
Expand Down

0 comments on commit 4a36c29

Please sign in to comment.