Skip to content

Commit

Permalink
feat: add search poi endpoint with latitude/longitude
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementNumericite committed Jul 1, 2021
1 parent 911a7aa commit a919b45
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
8 changes: 8 additions & 0 deletions back-strapi/api/commande/config/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
"policies": []
}
},
{
"method": "GET",
"path": "/mondial-relay/:latitude/:longitude",
"handler": "commande.searchPOI",
"config": {
"policies": []
}
},
{
"method": "PUT",
"path": "/commandes/:id",
Expand Down
51 changes: 50 additions & 1 deletion back-strapi/api/commande/controllers/commande.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,42 @@ const http = require('http');
const fs = require('fs');
const soap = require('soap');
const md5 = require('md5');
const mondialRelayUrl = 'http://api.mondialrelay.com/Web_Services.asmx?WSDL';

/**
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-controllers)
* to customize this controller
*/

const buildSearchPoiParams = (latitude, longitude) => {
let searchPoiParams = {
Enseigne: strapi.config.get('server.mondialRelay.id'),
Pays: 'FR',
Ville: '',
CP: '',
Latitude: latitude.substr(0, 10),
Longitude: longitude.substr(0, 9),
Taille: '',
Poids: '',
Action: '',
DelaiEnvoi: '0',
RayonRecherche: '40',
};

let securityKey = '';
for (const prop in searchPoiParams) {
securityKey += searchPoiParams[prop];
}

securityKey += strapi.config.get('server.mondialRelay.secret');

const hash = md5(securityKey);

searchPoiParams.Security = hash.toUpperCase();

return searchPoiParams;
}

const buildMrParams = (order) => {
const orderMrParams = {
mode: '24R',
Expand Down Expand Up @@ -108,7 +138,6 @@ module.exports = {
let mondial_relay_pdf_url;
if (tmp_order.delivery === 'pickup') {
tmp_order.name = tmp_order.poi_name
const mondialRelayUrl = 'http://api.mondialrelay.com/Web_Services.asmx?WSDL';
const soapClient = await soap.createClientAsync(mondialRelayUrl);
const mrParams = buildMrParams(tmp_order)

Expand Down Expand Up @@ -176,5 +205,25 @@ module.exports = {
}

return entity;
},
async searchPOI(ctx) {
const { latitude, longitude } = ctx.params

const searchPoiParams = buildSearchPoiParams(latitude, longitude)

const soapClient = await soap.createClientAsync(mondialRelayUrl);

const response = await soapClient.WSI3_PointRelais_RechercheAsync(searchPoiParams);
const response_item = response[0]

if (response_item && response_item.WSI3_PointRelais_RechercheResult) {
const mrResults = response_item.WSI3_PointRelais_RechercheResult;

if (mrResults.STAT == 0) {
return _.get(mrResults, 'PointsRelais.PointRelais_Details', [])
}
}

return ctx.badRequest(null, 'Error while trying to fetch mondial relais API');
}
};

0 comments on commit a919b45

Please sign in to comment.