/
Autofill address fields based on address hierarchy

Autofill address fields based on address hierarchy

New Backend API to fetch address hierarchy for a selected node :

Ā 

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; };
//success response { "data": { "pincode": "DAM", "city": "Z1", "state": "DAMMAM" } }
{ "searchType": "pincode", "queryString": "DAM" }

Changes in filter for address node search:

  1. Instead of showing only Name of options in dropdown it will now be shown as Name (Code).
    This will require another update in find query in common/models/customer-portal-parts/address-node-api-helper.js ā†’ getAddressNodeData

  2. Current filter is applicable only on code or name, changes to add both code and name search
    common/models/customer-portal-parts/address-node-api-helper.js ā†’ getAddressNodeData

Current filter logic:

Updated filter logic:

Frontend changes:

  1. Changes are to be done at 3 places

    1. Add Consignment modal (src/components/pages/details/AddDetails.tsx )

    2. Counter Booking ā†’ Add Address ( src/components/pages/OpsDashboard/Manage/counterBooking/AddAddress2.tsx )

    3. Customer-Portal ā†’ src/components/address/create-address.tsx

  2. List to be populated in dropdown will be fetched after entering atleast 3 characters in the input field for corresponding node( pincode, city, state, country).

  3. Debouncing will be implemented at all places wherever we are fetching list to be populated in the dropdown. (500ms)

  4. In case allowOverrideHierarchy is false , we will let user only enter values from dropdown only.

  5. In case allowOverrideHierarchy is true: user can either select from dropdown or enter other values too.

  6. Only those fields will be disabled for which data is fetched, only when allow override hierarchy is false

Customer Portal changes

  1. Changes in src/components/address/create-address.tsx

General Function to be called whenever any option from rendered dropdown is selected, it make call to api api/CustomerPortal/fetchGeneralAddressCode

  1. In case isAddressHierarchyPresent is false then we will render only Simple Input text field.

  2. If isAddressHierarchyPresent is true:

  • It fetches data from AddressNode table on basis of address hierarchy setup, and returns data for current and higher nodes to populate.

  • It also handle the logic to either disbale the higher level nodes based on whether their value is fetched and allow_override_hierarchy is false.

  • It also update (auto populate) the value of higher nodes.

  • Drop down only on those fields which are in hierarchy

Function to be called on typing text to be searched in address fields( pincode, city, state country)

Debouncing logic is also applied :

Following rendering logic is used for all nodes (pincode, state, city, country)

  • It renders only dropdown for case when allow_override_hierarchy is false (user can only select options from dropdown only).

  • When allow_override_hierarchy is true: user can type and search will be done in db, if data found user can select any option from dropdown and on basis of it higher level nodes is auto-populated, and can also enter anything out of option in dropdown for current node and higher nodes too.

  • On clearing a nodeā€™s value, all higher node's value will also be cleared.

Ā 

Additional Details:

Changes in src/network/pickup.api.ts

Changes in src/network/api.constants.ts

Ā 

CRMDashboard changes:

Similar logic as of Customer-Portal is used for rendering like on basis of isAddressHierarchyPresent and allowOverrideHierarchy.

Above logic is updated in

  1. src/components/pages/OpsDashboard/Manage/counterBooking/AddAddress2.tsx

  2. src/components/pages/details/AddDetails.tsx

Add label

Related content

Check digit logic in CRN for aramax
Check digit logic in CRN for aramax
Read with this
OTP Issue happening only FOFO stores
OTP Issue happening only FOFO stores
Read with this
File lists
Read with this