2024-08-29 12:48:21 +00:00
|
|
|
const axios = require('axios');
|
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('sendPois', {
|
|
|
|
description: 'Sends a set of pois',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'tickets',
|
|
|
|
type: ['number'],
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/sendPois`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Self.sendPois = async tickets => {
|
|
|
|
const config = await Self.app.models.QuadmindsApiConfig.findOne();
|
|
|
|
if (!config) throw new UserError('Config params not set');
|
|
|
|
|
|
|
|
let pois = await Self.rawSql(`
|
|
|
|
WITH deliveryNotes AS (
|
|
|
|
SELECT t.id, t.routeFk, tn.description
|
|
|
|
FROM ticket t
|
|
|
|
JOIN ticketObservation tn ON tn.ticketFk = t.id
|
|
|
|
JOIN observationType ot ON ot.id = tn.observationTypeFk
|
|
|
|
WHERE ot.code = 'delivery'
|
|
|
|
)
|
|
|
|
SELECT a.id code,
|
|
|
|
c.socialName name,
|
2024-08-30 10:30:40 +00:00
|
|
|
IF(ABS(a.latitude - ROUND(a.latitude)) < 0.000001, NULL, a.latitude) latitude,
|
|
|
|
IF(ABS(a.longitude - ROUND(a.longitude)) < 0.000001, NULL, a.longitude) longitude,
|
|
|
|
a.street,
|
|
|
|
a.city locality,
|
|
|
|
p.name state,
|
|
|
|
co.name country,
|
2024-08-29 12:48:21 +00:00
|
|
|
CONCAT_WS(', ', IFNULL(a.street, ''), IFNULL(a.city, ''), IFNULL(p.name, '')) longAddress,
|
|
|
|
CONCAT(IFNULL(a.mobile, c.mobile)) phoneNumber,
|
|
|
|
dn.description poiDeliveryComments,
|
|
|
|
c.email email
|
|
|
|
FROM ticket t
|
|
|
|
JOIN address a ON a.id = t.addressFk
|
|
|
|
JOIN province p ON p.id = a.provinceFk
|
|
|
|
JOIN country co ON co.id = p.countryFk
|
|
|
|
JOIN client c ON c.id = t.clientFk
|
|
|
|
LEFT JOIN deliveryNotes dn ON dn.id = t.id
|
|
|
|
WHERE t.id IN (?)
|
|
|
|
GROUP BY t.id
|
|
|
|
`, [tickets]);
|
|
|
|
|
|
|
|
// Transformo code en string ya que lo obtenermos como integer
|
|
|
|
pois = pois.map(poi => {
|
|
|
|
return {
|
|
|
|
...poi,
|
|
|
|
code: poi.code.toString(),
|
2024-08-30 10:30:40 +00:00
|
|
|
latitude: poi.latitude || undefined,
|
|
|
|
longitude: poi.longitude || undefined,
|
|
|
|
address: {
|
|
|
|
street: poi.street || undefined,
|
|
|
|
locality: poi.locality || undefined,
|
|
|
|
state: poi.state || undefined,
|
|
|
|
country: poi.country || undefined
|
|
|
|
},
|
2024-08-29 12:48:21 +00:00
|
|
|
poiDeliveryComments: poi.poiDeliveryComments || undefined,
|
|
|
|
phoneNumber: poi.phoneNumber || undefined,
|
|
|
|
email: poi.email || undefined
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
await axios.post(`${config.url}pois`, pois, {
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Saas-Apikey': config.key
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|