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 cfe8a56 commit ae5bcd4
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions src/api/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import {get as _get} from 'lodash';
import {loadEntity} from './middleware';


export const aliasesRelations = ['aliasSet.aliases.language'];
export const identifiersRelations = ['identifierSet.identifiers.type'];
export const relationshipsRelations = ['relationshipSet.relationships.type'];
Expand Down Expand Up @@ -47,10 +48,8 @@ export async function getBrowsedRelationships(
}

try {
const processedRelationships = [];

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

if (entity.bbid === relationship.sourceBbid &&
relationship.target.type.toLowerCase() === browsedEntityType.toLowerCase()) {
Expand All @@ -61,32 +60,29 @@ export async function getBrowsedRelationships(
}

if (!relEntity) {
continue;
return null;
}

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

if (!filterRelationshipMethod(formattedRelEntity)) {
continue;
}

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;
if (!filterRelationshipMethod(formattedRelEntity)) {
return null;
}
}

return processedRelationships.reduce((accumulator, relationship) => {
return {
entity: formattedRelEntity,
relationships: [{
relationshipType: _get(relationship, 'type.label', null),
relationshipTypeID: _get(relationship, 'type.id', null)
}]
};
});

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);
Expand All @@ -97,10 +93,9 @@ export async function getBrowsedRelationships(
return accumulator;
}, []);
}
catch (err) {
console.error('Error processing relationships:', err);
catch (error) {
console.error('Error processing relationships:', error);

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L97

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


0 comments on commit ae5bcd4

Please sign in to comment.