try { let addLocationInput = { ...locationInput, distance: { text: "", value: 0, }, duration: { text: "", value: 0, }, durationInTraffic: { text: "", value: 0, }, createdBy: new ObjectId(adminId), updatedBy: new ObjectId(adminId), }; // const createdLocation: any = await createLocation(addLocationInput); let createdLocation = true; if (createdLocation) { try { let locationId = locationsDataListArr.find((locationEle: any) => locationEle.type === body.type && locationEle.status !== "archived"); const updateLocationInput = { status: "archived", updatedBy: new ObjectId(adminId) }; // const updatedLocation = await findAndUpdateLocation({ _id: orderId }, updateLocationInput, { new: true }); const updatedLocation = true; if (updatedLocation) { const updatedLocationsDataListArr: any = await findLocations({ bookingOrder: orderDataObj._id, status: { $ne: "archived" } }, 0, 0, [], { position: 1, }); if (updatedLocationsDataListArr.length > 0) { let locationUpdated = 0; let distance: number = 0; let duration: number = 0; let durationInTraffic: number = 0; for (let locationIndex = 0; locationIndex < updatedLocationsDataListArr.length; locationIndex++) { const locationDataObj = updatedLocationsDataListArr[locationIndex]; if (locationDataObj.position == 1) continue; const locationId = locationDataObj._id; const origin = { lat: updatedLocationsDataListArr[locationIndex - 1].latitude, lng: updatedLocationsDataListArr[locationIndex - 1].longitude, }; const destination = { lat: locationDataObj.latitude, lng: locationDataObj.longitude }; const request = { origins: [origin], destinations: [destination], travelMode: "DRIVING", unitSystem: "METRIC", departure_time: moment().add("1", "minute").toDate(), }; // console.log("request", request); const response = await client.distancematrix({ params: { ...request, key: APIKey, }, /* validateStatus: function (status) { return status < 500; // Resolve only if the status code is less than 500 }, */ }); /* .then(async (r) => { for (let index = 0; index < r.data.rows.length; index++) { const element = r.data.rows[index].elements[0]; try { let updateLocation = { distance: element.distance, duration: element.duration, durationInTraffic: element.duration_in_traffic, }; /* const updatedLocation = await findAndUpdateLocation({ _id: locationId }, updateLocation, { new: true, }); * / locationUpdated++; console.log("updateLocation", updateLocation); distance += element.distance.value; duration += element.duration.value; durationInTraffic += element.duration_in_traffic.value; logger.info(`updateLocationsMatrix: Location matrix updated using the API, ORDER: ${orderId}, LOCATION: ${locationId}`); } catch (error: any) { logger.error( error, `updateLocationsMatrix: ERROR in Location matrix update issue using the API, ORDER: ${orderId}, LOCATION: ${locationId}` ); } } }) .catch((e) => { if (e.response && e.response.data && e.response.data.error_message) { // console.log("e.response.data.error_message", e.response.data.error_message); logger.error( `updateLocationsMatrix: e.response.data.error_message, ORDER: ${orderId}, Message: ${e.response.data.error_message}` ); let responseBody = { status: false, message: e?.response?.data?.error_message ? e.response.data.error_message : e.message, data: {}, errors: omit(e.response, ["request"]), }; return common.response(res, responseBody, StatusCodes.INTERNAL_SERVER_ERROR); } }) */ if (response.status == 200) { logger.info(omit(response, ["config.httpsAgent", "request"]), "CashFreeAuthorizeResponseData"); } else { logger.error(response.data, "CashFreeAuthorizeResponseDataError"); throw response.data; } if (locationIndex + 1 == updatedLocationsDataListArr.length && updatedLocationsDataListArr.length == locationUpdated + 1) { try { let updateOrder = { locationsMatrixUpdated: true, distance: Number((distance / 1000).toFixed(2)), duration: duration, durationInTraffic: durationInTraffic, }; console.log("updateOrder", updateOrder); /* const updatedOrder = await findAndUpdateOrder({ _id: orderId }, updateOrder, { new: true, }); */ logger.info(`updateLocationsMatrix: Order matrix updated using the API, ORDER: ${orderId}`); } catch (error: any) { logger.error(error, `updateLocationsMatrix: ERROR in Order martix update issue using the API, ORDER: ${orderId}`); } } } } } } catch (error: any) { logger.error(error); let responseBody = { status: false, message: error.message, data: {}, errors: error.errors, }; return common.response(res, responseBody, StatusCodes.INTERNAL_SERVER_ERROR); } } } catch (error: any) { logger.error(error); let responseBody = { status: false, message: error.message, data: {}, errors: error.errors, }; return common.response(res, responseBody, StatusCodes.INTERNAL_SERVER_ERROR); }