2018-10-01 06:17:36 +00:00
|
|
|
|
2018-12-21 19:22:13 +00:00
|
|
|
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
2018-10-01 06:17:36 +00:00
|
|
|
|
2018-07-03 13:00:33 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('listSaleTracking', {
|
|
|
|
description: 'Returns all ticket sale trackings',
|
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'filter',
|
|
|
|
type: 'Object',
|
2018-10-30 12:58:02 +00:00
|
|
|
description: 'Filter defining where and paginated data'
|
2018-07-03 13:00:33 +00:00
|
|
|
}],
|
|
|
|
returns: {
|
2018-10-30 12:58:02 +00:00
|
|
|
type: ['Object'],
|
2018-07-03 13:00:33 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/listSaleTracking`,
|
|
|
|
verb: 'get'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.listSaleTracking = async filter => {
|
2018-10-01 06:17:36 +00:00
|
|
|
let stmt = new ParameterizedSQL(`
|
|
|
|
SELECT
|
|
|
|
st.id,
|
|
|
|
s.ticketFk,
|
|
|
|
s.quantity,
|
|
|
|
s.concept,
|
|
|
|
s.itemFk,
|
|
|
|
st.originalQuantity,
|
|
|
|
st.created,
|
|
|
|
st.workerFk,
|
2019-01-31 13:14:39 +00:00
|
|
|
u.nickname userNickname,
|
2019-02-15 11:39:21 +00:00
|
|
|
u.id userId,
|
2018-10-01 06:17:36 +00:00
|
|
|
ste.name AS state
|
|
|
|
FROM saleTracking st
|
|
|
|
JOIN sale s ON s.id = st.saleFk
|
|
|
|
JOIN worker w ON w.id = st.workerFk
|
2019-01-31 13:14:39 +00:00
|
|
|
JOIN account.user u ON u.id = w.userFk
|
2018-10-01 06:17:36 +00:00
|
|
|
JOIN state ste ON ste.id = st.stateFk`);
|
2018-07-03 13:00:33 +00:00
|
|
|
|
2018-10-30 12:58:02 +00:00
|
|
|
stmt.merge(Self.makeSuffix(filter));
|
2018-07-03 13:00:33 +00:00
|
|
|
|
2018-10-01 06:17:36 +00:00
|
|
|
let trackings = await Self.rawStmt(stmt);
|
2018-07-03 13:00:33 +00:00
|
|
|
|
|
|
|
let salesFilter = {
|
|
|
|
include: [
|
|
|
|
{
|
2018-11-06 09:49:38 +00:00
|
|
|
relation: 'item'
|
2018-07-03 13:00:33 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
where: {ticketFk: filter.where.ticketFk}
|
|
|
|
};
|
|
|
|
|
|
|
|
let sales = await Self.app.models.Sale.find(salesFilter);
|
|
|
|
|
|
|
|
trackings.forEach(tracking => {
|
|
|
|
sales.forEach(sale => {
|
|
|
|
if (tracking.itemFk == sale.itemFk)
|
|
|
|
tracking.item = sale.item();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return trackings;
|
|
|
|
};
|
|
|
|
};
|