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

Updated syntax in utils.js with async/await #1146

Open
wants to merge 8 commits into
base: master
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
84 changes: 43 additions & 41 deletions src/api/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@ export const aliasesRelations = ['aliasSet.aliases.language'];
export const identifiersRelations = ['identifierSet.identifiers.type'];
export const relationshipsRelations = ['relationshipSet.relationships.type'];

/**
* allowOnlyGetMethod is function to allow api to send response only for get requests
*
* @param {object} req - req is an object containing information about the HTTP request
* @param {object} res - res to send back the desired HTTP response
* @param {function} next - this is a callback
* @returns {object} - return to endpoint if request type is GET otherwise respond error with status code 405
* @example
*
* allowOnlyGetMethod(req, res, next)
*/

export function allowOnlyGetMethod(req, res, next) {
if (req.method === 'GET') {
return next();
Expand All @@ -45,17 +33,24 @@ export function allowOnlyGetMethod(req, res, next) {
.send({message: `${req.method} method for the "${req.path}" route is not supported. Only GET method is allowed`});
}


export async function getBrowsedRelationships(orm, locals, browsedEntityType,
getEntityInfoMethod, fetchRelated, filterRelationshipMethod) {
export async function getBrowsedRelationships(
orm,
locals,
browsedEntityType,
getEntityInfoMethod,
fetchRelated,
filterRelationshipMethod
) {
const {entity, relationships} = locals;

if (!relationships.length > 0) {
if (!relationships?.length) {
return [];
}
const relationshipsPromises = relationships
.map(async relationship => {
let relEntity;

try {
const relationshipPromises = relationships.map(async (relationship) => {
let relEntity = null;

if (entity.bbid === relationship.sourceBbid &&
relationship.target.type.toLowerCase() === browsedEntityType.toLowerCase()) {
relEntity = relationship.target;
Expand All @@ -64,35 +59,42 @@ export async function getBrowsedRelationships(orm, locals, browsedEntityType,
relEntity = relationship.source;
}

if (relEntity) {
const loadedRelEntity = await loadEntity(orm, relEntity, fetchRelated);
const formattedRelEntity = getEntityInfoMethod(loadedRelEntity);
if (!filterRelationshipMethod(formattedRelEntity)) {
return null;
}
return {
entity: formattedRelEntity,
relationship: [{
relationshipType: _get(relationship, 'type.label', null),
relationshipTypeID: _get(relationship, 'type.id', null)
}]
};
if (!relEntity) {
return null;
}
return null;

const loadedRelEntity = await loadEntity(orm, relEntity, fetchRelated);
const formattedRelEntity = getEntityInfoMethod(loadedRelEntity);

if (!filterRelationshipMethod(formattedRelEntity)) {
return null;
}

return {
entity: formattedRelEntity,
relationships: [{
relationshipType: _get(relationship, 'type.label', null),
relationshipTypeID: _get(relationship, 'type.id', null)
}]
};
});
const fetchedRelationshipsPromises = await Promise.all(relationshipsPromises);
// Remove falsy values (nulls returned above)
const filteredRelationships = fetchedRelationshipsPromises.filter(Boolean);

return filteredRelationships
.reduce((accumulator, relationship) => {
const entityAlreadyExists = accumulator.find(rel => rel.entity.bbid === relationship.entity.bbid);
if (entityAlreadyExists) {
entityAlreadyExists.relationships.push(...relationship.relationships);
const results = await Promise.all(relationshipPromises);
const filteredResults = results.filter(Boolean);

return filteredResults.reduce((accumulator, relationship) => {
const existingEntity = accumulator.find(rel => rel.entity.bbid === relationship.entity.bbid);
if (existingEntity) {
existingEntity.relationships.push(...relationship.relationships);
}
else {
accumulator.push(relationship);
}
return accumulator;
}, []);
}
catch (error) {
const errorMessage = 'Failed to fetch browsed relationships';
throw new Error(errorMessage);
}
}
Loading