2024-08-23 11:12:11 +00:00
|
|
|
const axios = require('axios');
|
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2024-08-29 12:48:21 +00:00
|
|
|
const moment = require('moment');
|
2024-08-23 11:12:11 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('sendOrders', {
|
2024-08-29 12:48:21 +00:00
|
|
|
description: 'Sends a set of orders',
|
2024-08-23 11:12:11 +00:00
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
2024-08-29 12:48:21 +00:00
|
|
|
arg: 'tickets',
|
2024-08-23 11:12:11 +00:00
|
|
|
type: ['number'],
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/sendOrders`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
2024-08-29 12:48:21 +00:00
|
|
|
Self.sendOrders = async tickets => {
|
2024-08-23 11:12:11 +00:00
|
|
|
const config = await Self.app.models.QuadmindsApiConfig.findOne();
|
|
|
|
if (!config) throw new UserError('Config params not set');
|
|
|
|
|
2024-09-02 09:48:32 +00:00
|
|
|
if (tickets.length > config.maxObjects)
|
|
|
|
throw new UserError(`Quadminds does not support more than ${config.maxObjects} tickets`);
|
2024-08-29 12:48:21 +00:00
|
|
|
|
2024-09-02 09:48:32 +00:00
|
|
|
let poisData = [];
|
|
|
|
let isOk;
|
|
|
|
for (let offset = 0; !isOk; offset = offset + config.limit) {
|
|
|
|
const pois = await axios.get(`${config.url}pois/search?limit=${config.limit}&offset=${offset}`, {
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'X-Saas-Apikey': config.key
|
|
|
|
}
|
|
|
|
});
|
|
|
|
pois.data.data.length ? poisData.push(...pois.data.data) : isOk = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const poiMap = new Map(poisData.map(poi => [poi.code, poi._id]));
|
2024-08-29 12:48:21 +00:00
|
|
|
|
|
|
|
let orders = await Self.rawSql(`
|
|
|
|
SELECT a.id poiCode,
|
|
|
|
t.id code,
|
|
|
|
t.shipped date,
|
|
|
|
'PEDIDO' operation,
|
|
|
|
t.totalWithVat totalAmount,
|
2024-09-10 05:43:11 +00:00
|
|
|
t.totalWithoutVat totalAmountWithoutTaxes,
|
|
|
|
SUM(sv.volume) volume
|
2024-08-29 11:21:59 +00:00
|
|
|
FROM ticket t
|
2024-08-23 11:12:11 +00:00
|
|
|
JOIN address a ON a.id = t.addressFk
|
2024-09-10 05:43:11 +00:00
|
|
|
JOIN saleVolume sv ON sv.ticketFk = t.id
|
2024-08-23 11:12:11 +00:00
|
|
|
WHERE t.id IN (?)
|
|
|
|
GROUP BY t.id
|
2024-08-29 12:48:21 +00:00
|
|
|
`, [tickets]);
|
2024-08-23 11:12:11 +00:00
|
|
|
|
|
|
|
// Transformo code en string ya que lo obtenermos como integer
|
2024-08-29 12:48:21 +00:00
|
|
|
orders = orders.map(order => {
|
2024-08-23 11:12:11 +00:00
|
|
|
return {
|
2024-08-29 12:48:21 +00:00
|
|
|
...order,
|
|
|
|
poiId: poiMap.get(order.poiCode.toString()) || undefined,
|
|
|
|
code: order.code.toString(),
|
|
|
|
date: moment(order.date).format('YYYY-MM-DD'),
|
|
|
|
totalAmount: order.totalAmount || undefined,
|
2024-09-10 05:43:11 +00:00
|
|
|
totalAmountWithoutTaxes: order.totalAmountWithoutTaxes || undefined,
|
|
|
|
timeWindow: [{
|
2024-09-18 05:14:52 +00:00
|
|
|
from: config.orderTimeFrom,
|
|
|
|
to: config.orderTimeTo
|
2024-09-10 05:43:11 +00:00
|
|
|
}],
|
|
|
|
orderMeasures: [{
|
|
|
|
constraintId: 3, // Volumen
|
|
|
|
value: order.volume
|
|
|
|
}]
|
2024-08-23 11:12:11 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2024-08-29 12:48:21 +00:00
|
|
|
await axios.post(`${config.url}orders`, orders, {
|
2024-08-23 11:12:11 +00:00
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Saas-Apikey': config.key
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|