salix/modules/client/back/methods/client/lastActiveTickets.js

64 lines
2.0 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('lastActiveTickets', {
description: 'Returns the last three active tickets of a client',
accessType: 'READ',
accepts: [
{
arg: 'id',
type: 'number',
required: true,
description: 'Client id',
http: {source: 'path'}
},
{
arg: 'ticketId',
type: 'number',
required: true
}
],
returns: {
type: ['object'],
root: true
},
http: {
path: `/:id/lastActiveTickets`,
verb: 'GET'
}
});
Self.lastActiveTickets = async(id, ticketId, options) => {
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const date = Date.vnNew();
date.setHours(0, 0, 0, 0);
const ticket = await Self.app.models.Ticket.findById(ticketId, null, myOptions);
const query = `
SELECT
t.id,
t.shipped,
a.name AS agencyName,
w.name AS warehouseName,
ad.nickname AS nickname,
ad.city AS city,
ad.postalCode AS postalCode,
ad.street AS street,
pr.name AS name
FROM ticket t
JOIN vn.ticketState ts ON t.id = ts.ticketFk
JOIN vn.agencyMode a ON t.agencyModeFk = a.id
JOIN vn.warehouse w ON t.warehouseFk = w.id
JOIN vn.address ad ON t.addressFk = ad.id
JOIN vn.province pr ON ad.provinceFk = pr.id
WHERE t.shipped >= ? AND t.clientFk = ? AND ts.alertLevel = 0
AND t.id <> ? AND t.warehouseFk = ?
ORDER BY t.shipped
LIMIT 10`;
return Self.rawSql(query, [date, id, ticketId, ticket.warehouseFk], myOptions);
};
};