@icodebible/utils
Version:
Collection of essential utilities that help manipulation and transformation of various js object.
258 lines (252 loc) • 3.94 kB
JavaScript
/**
*
*/
const _ = require('lodash');
/**
*
*/
const { isArray, isObject } = require('../../helpers');
const { SplitWordByCamelCase } = require('../split-by-camelcase');
/**
*
*/
exports.UIDToIDTransformation = async (
repositoryInstance,
payload,
entityMapper
) => {
/**
*
*/
let mPayload = {};
const idendityChecker = ['id', 'uid'];
/**
*
*/
if (payload) {
/**
*
*/
for (const key of await _.keys(payload)) {
/**
*
*/
if (payload[key] && isArray(payload[key])) {
/**
*
*/
const arr = await payload[key];
/**
*
*/
mPayload = {
...mPayload,
[]: await getTransformedObjectRelation(
repositoryInstance,
arr,
entityMapper,
key
),
};
} else if (payload[key] && isObject(payload[key])) {
/**
*
*/
const obj = await payload[key];
/**
*
*/
mPayload = {
...mPayload,
[]: await getTransformedObjectRelation(
repositoryInstance,
obj,
entityMapper,
key
),
};
} else if (
_.endsWith(key, 'id') &&
!_.includes(idendityChecker, _.toLower(_.trim(key)))
) {
/**
*
*/
const entityReferenceIdProp = await key;
/**
*
*/
const replacable = _.replace(
_.clone(entityReferenceIdProp),
'id',
'Id'
);
/**
*
*/
const arrWord = SplitWordByCamelCase(replacable);
/**
*
*/
if (_.isEqual(_.last(arrWord), 'Id')) {
/**
*
*/
const queryKey = _.toLower(_.head(arrWord));
/**
*
*/
const idWrapper = await UIDToIDResolver(
payload[key],
repositoryInstance,
entityMapper[queryKey]
);
// console.log('ID WRAPPER::: ' + JSON.stringify(idWrapper));
/**
*
*/
mPayload = _.merge(mPayload, {
[]: idWrapper.hasOwnProperty('id')
? idWrapper.id
: '',
});
if (
payload.hasOwnProperty(queryKey) &&
idWrapper.hasOwnProperty('id')
) {
mPayload = _.merge(mPayload, {
[]: _.merge(
payload[queryKey],
{
id: idWrapper.id,
}
),
});
}
}
} else {
/**
*
*/
mPayload = {
...mPayload,
[]: payload[key],
};
}
}
/**
*
*/
return mPayload;
} else {
/**
* Return message no payload passed to be sanitized
*/
return 'No payload supplied';
}
};
/**
*
* @param payload
* @param key
*/
getTransformedObjectRelation = async (
repositoryInstance,
payload,
entityMapper,
key
) => {
/**
*
*/
if (isArray(payload)) {
/**
*
*/
return await Promise.all(
_.map(payload, async (obj) => {
/**
*
*/
if (
(await _.has(obj, 'id')) &&
(await _.isNumber(obj.id))
) {
/**
*
*/
return await {
...obj,
};
} else {
/**
*
*/
return _.merge(
obj,
await UIDToIDResolver(
obj.uid,
repositoryInstance,
entityMapper[key]
)
);
}
})
);
} else if (isObject(payload)) {
/**
*
*/
return {
...payload,
...(await UIDToIDResolver(
payload.uid,
repositoryInstance,
entityMapper[key]
)),
};
}
};
/**
*
*/
UIDToIDResolver = async (uid, repositoryInstance, tablename) => {
/**
*
*/
const query = `SELECT * FROM public.${tablename} WHERE uid = '${uid}' LIMIT 1`;
/**
*
*/
const queryResults = await repositoryInstance.query(query);
/**
*
*/
return queryResults.length >= 1
? await getEntityRelationIdProps(queryResults)
: {};
};
/**
*
*/
getEntityRelationIdProps = (queryResults) => {
/**
*
*/
return _.map(queryResults, (result) => {
/**
*
*/
if (result) {
/**
*
*/
return _.pickBy(result, (value, key) => {
/**
*
*/
return key === 'id';
});
}
})[0];
};