const fetchGeneralAddressCode = async (organisationId, extendedModels, params, options = {}) => {
const { searchType, queryString } = params;
** searchType is hardcoded at frontend and sent in the body**
if (!searchType) {
throw helper.wrongInputError('Location Key should be present');
}
const metaData = await addressHierarchy.getAddressMetadata(extendedModels, organisationId);
if (!metaData) {
throw helper.wrongInputError('Metadata not found');
}
const hierarchy = metaData.hierarchy || [];
const nodeHierarchyMetaData = hierarchy.find(obj => helper.sanitizeStringCode(obj.location_key) === helper.sanitizeStringCode(searchType));
if (!nodeHierarchyMetaData) {
throw helper.wrongInputError('Invalid Location Key');
}
const parentMetaDataArr = [];
let searchTypeNodeFound = false;
if (Array.isArray(hierarchy) && hierarchy.length > 0) {
for (let i = 0; i < (hierarchy.length - 1); i++) {
if (searchTypeNodeFound || helper.sanitizeStringCode(hierarchy[i].location_key) === helper.sanitizeStringCode(searchType)) {
searchTypeNodeFound = true;
parentMetaDataArr.push(hierarchy[i + 1]);
}
}
}
const queryParameters = [];
let heirarchyId = nodeHierarchyMetaData.id;
let nodeCode = helper.sanitizeStringCode(queryString);
const toRet = {};
toRet.data = {};
queryParameters.push(organisationId);
queryParameters.push(heirarchyId);
queryParameters.push(nodeCode);
const selectPart = 'SELECT addressNode.id as node_id,addressNode.code as node_code,addressNode.name as node_name,addressNode.hierarchy_id as hierarchy_id';
const fromPart = ' FROM addressNode';
let extendedSelectPart = '';
let joinPart = '';
const wherePart = ' WHERE addressNode.organisation_id = $1 and addressNode.hierarchy_id = $2 and addressNode.code = $3';
if (parentMetaDataArr.length) {
extendedSelectPart = ',addressNode.parent_node_id as parent_node_id,parentAddressNode.code as parent_code,parentAddressNode.name as parent_name';
joinPart = ' JOIN addressNode AS parentAddressNode on addressNode.parent_node_id = parentAddressNode.id';
}
const queryToExecute = selectPart + extendedSelectPart + fromPart + joinPart + wherePart;
const result = await helper.executeQueryAsync(extendedModels.AddressNode, queryToExecute, queryParameters, options);
if (result[0] && result[0].node_name) {
toRet.data[searchType] = result[0].node_name;
} else {
throw helper.wrongInputError('Data not found for this location key');
}
let nextNodesResult = result;
for (let i = 0; i < parentMetaDataArr.length; i++) {
try {
heirarchyId = parentMetaDataArr[i].id;
nodeCode = nextNodesResult[0].parent_code;
const locationKey = parentMetaDataArr[i].location_key;
queryParameters[1] = heirarchyId;
queryParameters[2] = nodeCode;
if (i === (parentMetaDataArr.length - 1)) {
const lastNodeQueryResult = selectPart + fromPart + wherePart;
nextNodesResult = await helper.executeQueryAsync(extendedModels.AddressNode, lastNodeQueryResult, queryParameters, options);
} else {
nextNodesResult = await helper.executeQueryAsync(extendedModels.AddressNode, queryToExecute, queryParameters, options);
}
if (nextNodesResult[0] && nextNodesResult[0].node_name) {
toRet.data[locationKey] = nextNodesResult[0].node_name;
}
} catch (err) {
//do nothing
}
}
return toRet;
}; |