From cfe8a5638b883a5a44e14e6bf9110f9f6c7ea033 Mon Sep 17 00:00:00 2001 From: moksha-hub Date: Thu, 19 Dec 2024 14:06:42 +0530 Subject: [PATCH] Update utils.js --- src/api/helpers/utils.js | 155 ++++++++++++++++----------------------- 1 file changed, 65 insertions(+), 90 deletions(-) diff --git a/src/api/helpers/utils.js b/src/api/helpers/utils.js index 9fbca6b97..b681fac29 100644 --- a/src/api/helpers/utils.js +++ b/src/api/helpers/utils.js @@ -23,109 +23,84 @@ export const aliasesRelations = ['aliasSet.aliases.language']; export const identifiersRelations = ['identifierSet.identifiers.type']; export const relationshipsRelations = ['relationshipSet.relationships.type']; -/** - * allowOnlyGetMethod is a function to allow the API to send a response only for GET requests. - * - * @param {object} req - Object containing information about the HTTP request. - * @param {object} res - Object to send back the desired HTTP response. - * @param {function} next - Callback function to proceed to the next middleware. - * @returns {object} - Returns to the endpoint if the request type is GET; otherwise, responds with an error and status code 405. - */ export function allowOnlyGetMethod(req, res, next) { - if (req.method === 'GET') { - return next(); - } - return res.set('Allow', 'GET') - .status(405) - .send({message: `${req.method} method for the "${req.path}" route is not supported. Only GET method is allowed.`}); + if (req.method === 'GET') { + return next(); + } + return res.set('Allow', 'GET') + .status(405) + .send({message: `${req.method} method for the "${req.path}" route is not supported. Only GET method is allowed`}); } -/** - * Fetch and filter browsed relationships based on the provided entity type and filtering methods. - * - * @param {object} orm - ORM instance for database operations. - * @param {object} locals - Local variables containing the entity and relationships. - * @param {string} browsedEntityType - The type of entity being browsed. - * @param {function} getEntityInfoMethod - Method to extract information from the entity. - * @param {Array} fetchRelated - Relationships to fetch for the entity. - * @param {function} filterRelationshipMethod - Method to filter relationships based on custom criteria. - * @returns {Array} - Returns an array of filtered relationships. - */ export async function getBrowsedRelationships( - orm, - locals, - browsedEntityType, - getEntityInfoMethod, - fetchRelated, - filterRelationshipMethod + orm, + locals, + browsedEntityType, + getEntityInfoMethod, + fetchRelated, + filterRelationshipMethod ) { - const {entity, relationships} = locals; + const {entity, relationships} = locals; + + if (!relationships?.length) { + return []; + } - if (!relationships.length) { - return []; - } + try { + const processedRelationships = []; - try { - const fetchedRelationships = await Promise.all( - relationships.map(async (relationship) => { - let relEntity; + for (const relationship of relationships) { + let relEntity; - if ( - entity.bbid === relationship.sourceBbid && - relationship.target.type.toLowerCase() === browsedEntityType.toLowerCase() - ) { - relEntity = relationship.target; - } else if ( - relationship.source.type.toLowerCase() === browsedEntityType.toLowerCase() - ) { - relEntity = relationship.source; - } + if (entity.bbid === relationship.sourceBbid && + relationship.target.type.toLowerCase() === browsedEntityType.toLowerCase()) { + relEntity = relationship.target; + } + else if (relationship.source.type.toLowerCase() === browsedEntityType.toLowerCase()) { + relEntity = relationship.source; + } - if (relEntity) { - try { - const loadedRelEntity = await loadEntity(orm, relEntity, fetchRelated); - const formattedRelEntity = getEntityInfoMethod(loadedRelEntity); + if (!relEntity) { + continue; + } - if (!filterRelationshipMethod(formattedRelEntity)) { - return null; - } + try { + const loadedRelEntity = await loadEntity(orm, relEntity, fetchRelated); + const formattedRelEntity = getEntityInfoMethod(loadedRelEntity); - return { - entity: formattedRelEntity, - relationships: [ - { - relationshipType: _get(relationship, 'type.label', null), - relationshipTypeID: _get(relationship, 'type.id', null), - }, - ], - }; - } catch (err) { - console.error('Error loading related entity:', err); - return null; - } - } - return null; - }) - ); + if (!filterRelationshipMethod(formattedRelEntity)) { + continue; + } - // Remove falsy values (nulls returned above) - const filteredRelationships = fetchedRelationships.filter(Boolean); + processedRelationships.push({ + entity: formattedRelEntity, + relationships: [{ + relationshipType: _get(relationship, 'type.label', null), + relationshipTypeID: _get(relationship, 'type.id', null) + }] + }); + } + catch (err) { + console.error('Error processing relationship entity:', err); + continue; + } + } - return filteredRelationships.reduce((accumulator, relationship) => { - const entityAlreadyExists = accumulator.find( - (rel) => rel.entity.bbid === relationship.entity.bbid - ); - if (entityAlreadyExists) { - entityAlreadyExists.relationships.push(...relationship.relationships); - } else { - accumulator.push(relationship); - } - return accumulator; - }, []); - } catch (error) { - console.error('Error fetching browsed relationships:', error); - throw new Error('Failed to fetch browsed relationships.'); - } + return processedRelationships.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 (err) { + console.error('Error processing relationships:', err); + throw new Error('Failed to fetch browsed relationships'); + } }