Skip to content

Commit

Permalink
Update utils.js
Browse files Browse the repository at this point in the history
  • Loading branch information
moksha-hub authored Dec 19, 2024
1 parent 1eb2012 commit cfe8a56
Showing 1 changed file with 65 additions and 90 deletions.
155 changes: 65 additions & 90 deletions src/api/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} fetchRelated - Relationships to fetch for the entity.
* @param {function} filterRelationshipMethod - Method to filter relationships based on custom criteria.
* @returns {Array<object>} - 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;

Check failure on line 64 in src/api/helpers/utils.js

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L64

Unexpected use of continue statement (no-continue)
}

if (!filterRelationshipMethod(formattedRelEntity)) {
return null;
}
try {
const loadedRelEntity = await loadEntity(orm, relEntity, fetchRelated);

Check failure on line 68 in src/api/helpers/utils.js

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L68

Unexpected `await` inside a loop (no-await-in-loop)
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;

Check failure on line 72 in src/api/helpers/utils.js

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L72

Unexpected use of continue statement (no-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);

Check failure on line 84 in src/api/helpers/utils.js

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L84

Unexpected console statement (no-console)
continue;

Check failure on line 85 in src/api/helpers/utils.js

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L85

Unexpected use of continue statement (no-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);

Check failure on line 101 in src/api/helpers/utils.js

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L101

Unexpected console statement (no-console)
throw new Error('Failed to fetch browsed relationships');
}
}


0 comments on commit cfe8a56

Please sign in to comment.