Skip to content

Commit

Permalink
Merge pull request #1 from moksha-hub/moksha-hub-async/await-patch1
Browse files Browse the repository at this point in the history
Update utils.js with async/await syntax
  • Loading branch information
moksha-hub authored Dec 18, 2024
2 parents eeb62bf + 5a73196 commit 32fa6a1
Showing 1 changed file with 82 additions and 58 deletions.
140 changes: 82 additions & 58 deletions src/api/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,83 +16,107 @@
* 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';

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L19

There should be no space after '{' (object-curly-spacing)

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L19

There should be no space before '}' (object-curly-spacing)
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)

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L20

There should be no space after '{' (object-curly-spacing)

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L20

There should be no space before '}' (object-curly-spacing)

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
* 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)
* allowOnlyGetMethod(req, res, next)
*/

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') {

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)
return next();

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)
}

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 1 tab but found 4 spaces (indent)
return res.set('Allow', 'GET')

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L41

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

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L42

Expected indentation of 2 tabs but found 8 spaces (indent)
.send({

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L43

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

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L44

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

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L44

Unexpected trailing comma (comma-dangle)
});

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L45

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

export async function getBrowsedRelationships(
orm,

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L49

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

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L50

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

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L51

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

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L52

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

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L53

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

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L54

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

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L56

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

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L56

There should be no space after '{' (object-curly-spacing)

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L56

There should be no space before '}' (object-curly-spacing)

if (!relationships.length) {

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L58

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

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 2 tabs but found 8 spaces (indent)
}

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)

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

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)
const relationshipsResults = [];

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L63

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

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

Trailing spaces not allowed (no-trailing-spaces)
for (const relationship of relationships) {

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)
let relEntity;

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 3 tabs but found 12 spaces (indent)

if (!relationships.length > 0) {
return [];
}
const relationshipsPromises = relationships
.map(async relationship => {
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 (

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 3 tabs but found 12 spaces (indent)
entity.bbid === relationship.sourceBbid &&

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 4 tabs but found 16 spaces (indent)
relationship.target.type.toLowerCase() === browsedEntityType.toLowerCase()
) {

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 3 tabs but found 12 spaces (indent)
relEntity = relationship.target;

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

Expected indentation of 4 tabs but found 16 spaces (indent)
} else 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 3 tabs but found 12 spaces (indent)

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

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

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 4 tabs but found 16 spaces (indent)
) {

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

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L75

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

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)
}

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 3 tabs but found 12 spaces (indent)

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)
}]
};
}
return null;
});
const fetchedRelationshipsPromises = await Promise.all(relationshipsPromises);
// Remove falsy values (nulls returned above)
const filteredRelationships = fetchedRelationshipsPromises.filter(Boolean);
if (relEntity) {

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 3 tabs but found 12 spaces (indent)
try {

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

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 81 in src/api/helpers/utils.js

View workflow job for this annotation

GitHub Actions / ESLint

src/api/helpers/utils.js#L81

Unexpected `await` inside a loop (no-await-in-loop)
const formattedRelEntity = getEntityInfoMethod(loadedRelEntity);

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 5 tabs but found 20 spaces (indent)

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;
}, []);
if (!filterRelationshipMethod(formattedRelEntity)) {
continue;
}

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
);
}
}
}

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;
}, []);
} catch (err) {
console.error(`Error processing relationships: ${err.message}`, err);
throw new Error('Failed to fetch browsed relationships.');
}
}

0 comments on commit 32fa6a1

Please sign in to comment.