Add function for fixing and improving the API responses

This commit is contained in:
Vencislav Atanasov 2024-09-22 03:57:55 +03:00
parent 4774dcda11
commit 2e83ef89b4
1 changed files with 12 additions and 0 deletions

View File

@ -41,3 +41,15 @@ export function calculateProgress(...elements) {
isNotStarted: completeCount === 0, isNotStarted: completeCount === 0,
}; };
} }
export const addIdAndRelations = (items, relations = []) =>
Object.fromEntries(Object.entries(items).map(([id, item]) =>
([id, {
id,
...item,
...Object.fromEntries(relations.map(([field, collection, idField]) => ([
field,
Array.isArray(item[idField]) ? item[idField].map(id => collection[id]) : collection[item[idField]],
]))),
}])
));