Skip to content

Commit

Permalink
Updated utils.js
Browse files Browse the repository at this point in the history
  • Loading branch information
moksha-hub authored Dec 18, 2024
1 parent 5d7ff03 commit 1eb2012
Showing 1 changed file with 74 additions and 73 deletions.
147 changes: 74 additions & 73 deletions src/api/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,40 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

import { get as _get } from 'lodash';
import { loadEntity } from './middleware';
import {get as _get} from 'lodash';
import {loadEntity} from './middleware';

Check warning on line 20 in src/api/helpers/utils.js

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L20

Expected 2 empty lines after import statement not followed by another import (import/newline-after-import)

export const aliasesRelations = ['aliasSet.aliases.language'];
export const identifiersRelations = ['identifierSet.identifiers.type'];
export const relationshipsRelations = ['relationshipSet.relationships.type'];

/**
* Allows only GET method for the API
* allowOnlyGetMethod is a function to allow the API to send a response only for GET requests.
*
* @param {object} req - Request object
* @param {object} res - Response object
* @param {function} next - Callback function
* @returns {object} Response object if method is not GET
* @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') {

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L35

Expected indentation of 1 tab but found 4 spaces (indent)
return next();

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L36

Expected indentation of 2 tabs but found 8 spaces (indent)
}

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L37

Expected indentation of 1 tab but found 4 spaces (indent)

return res
.set('Allow', 'GET')
return res.set('Allow', 'GET')

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L38

Expected indentation of 1 tab but found 4 spaces (indent)
.status(405)

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L39

Expected indentation of 2 tabs but found 8 spaces (indent)
.send({
message: `${req.method} method for the "${req.path}" route is not supported. Only GET method is allowed.`,
});
.send({message: `${req.method} method for the "${req.path}" route is not supported. Only GET method is allowed.`});

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L40

Expected indentation of 2 tabs but found 8 spaces (indent)
}

/**
* Fetch browsed relationships based on the entity type
* Fetch and filter browsed relationships based on the provided entity type and filtering methods.
*
* @param {object} orm - ORM instance
* @param {object} locals - Local variables
* @param {string} browsedEntityType - Entity type to browse
* @param {function} getEntityInfoMethod - Method to get entity info
* @param {boolean} fetchRelated - Flag to fetch related entities
* @param {function} filterRelationshipMethod - Method to filter relationships
* @returns {Array} Array of browsed relationships
* @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,

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L55

Expected indentation of 1 tab but found 4 spaces (indent)
Expand All @@ -63,68 +59,73 @@ export async function getBrowsedRelationships(
fetchRelated,

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L59

Expected indentation of 1 tab but found 4 spaces (indent)
filterRelationshipMethod

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L60

Expected indentation of 1 tab but found 4 spaces (indent)
) {
const { entity, relationships } = locals;
const {entity, relationships} = locals;

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L62

Expected indentation of 1 tab but found 4 spaces (indent)

if (!relationships.length) {

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

Expected indentation of 1 tab but found 4 spaces (indent)
return [];

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L65

Expected indentation of 2 tabs but found 8 spaces (indent)
}

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L66

Expected indentation of 1 tab but found 4 spaces (indent)

const relationshipsResults = [];

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 (relEntity) {
try {
const loadedRelEntity = await loadEntity(orm, relEntity, fetchRelated);
const formattedRelEntity = getEntityInfoMethod(loadedRelEntity);
try {

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

Expected indentation of 1 tab but found 4 spaces (indent)
const fetchedRelationships = await Promise.all(

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L69

Expected indentation of 2 tabs but found 8 spaces (indent)
relationships.map(async (relationship) => {

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L70

Expected indentation of 3 tabs but found 12 spaces (indent)
let relEntity;

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L71

Expected indentation of 4 tabs but found 16 spaces (indent)

if (

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L73

Expected indentation of 4 tabs but found 16 spaces (indent)
entity.bbid === relationship.sourceBbid &&

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L74

Expected indentation of 5 tabs but found 20 spaces (indent)
relationship.target.type.toLowerCase() === browsedEntityType.toLowerCase()
) {

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L76

Expected indentation of 4 tabs but found 16 spaces (indent)
relEntity = relationship.target;

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L77

Expected indentation of 5 tabs but found 20 spaces (indent)
} else if (

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L78

Expected indentation of 4 tabs but found 16 spaces (indent)

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L78

Closing curly brace appears on the same line as the subsequent block (brace-style)
relationship.source.type.toLowerCase() === browsedEntityType.toLowerCase()

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L79

Expected indentation of 5 tabs but found 20 spaces (indent)
) {

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L80

Expected indentation of 4 tabs but found 16 spaces (indent)
relEntity = relationship.source;

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L81

Expected indentation of 5 tabs but found 20 spaces (indent)
}

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L82

Expected indentation of 4 tabs but found 16 spaces (indent)

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

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

Expected indentation of 4 tabs but found 16 spaces (indent)
try {

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

Expected indentation of 5 tabs but found 20 spaces (indent)
const loadedRelEntity = await loadEntity(orm, relEntity, fetchRelated);

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L86

Expected indentation of 6 tabs but found 24 spaces (indent)
const formattedRelEntity = getEntityInfoMethod(loadedRelEntity);

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L87

Expected indentation of 6 tabs but found 24 spaces (indent)

if (!filterRelationshipMethod(formattedRelEntity)) {

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L89

Expected indentation of 6 tabs but found 24 spaces (indent)
return null;

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L90

Expected indentation of 7 tabs but found 28 spaces (indent)
}

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L91

Expected indentation of 6 tabs but found 24 spaces (indent)

return {

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L93

Expected indentation of 6 tabs but found 24 spaces (indent)
entity: formattedRelEntity,

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L94

Expected indentation of 7 tabs but found 28 spaces (indent)
relationships: [

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L95

Expected indentation of 7 tabs but found 28 spaces (indent)
{

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L96

Expected indentation of 8 tabs but found 32 spaces (indent)
relationshipType: _get(relationship, 'type.label', null),

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

Expected indentation of 9 tabs but found 36 spaces (indent)
relationshipTypeID: _get(relationship, 'type.id', null),

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L98

Expected indentation of 9 tabs but found 36 spaces (indent)

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L98

Unexpected trailing comma (comma-dangle)
},

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L99

Expected indentation of 8 tabs but found 32 spaces (indent)
],
};
} catch (err) {
console.error('Error loading related entity:', err);
return null;
}
}
return null;
})
);

relationshipsResults.push({
entity: formattedRelEntity,
relationships: [
{
relationshipType: _get(relationship, 'type.label', null),
relationshipTypeID: _get(relationship, 'type.id', null),
},
],
});
} catch (err) {
console.error(
`Error loading entity for relationship: ${err.message}`,
err
);
// Remove falsy values (nulls returned above)
const filteredRelationships = fetchedRelationships.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);
} else {
accumulator.push(relationship);
}
}
return accumulator;
}, []);
} catch (error) {
console.error('Error fetching browsed relationships:', error);
throw new Error('Failed to fetch browsed relationships.');
}

return relationshipsResults.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;
}, []);
}


0 comments on commit 1eb2012

Please sign in to comment.