refactor: simplify bills query building
This commit is contained in:
parent
e83e74f9d1
commit
1331aad0ac
1 changed files with 24 additions and 21 deletions
|
|
@ -3,39 +3,42 @@ const pgp = require('pg-promise')()
|
||||||
|
|
||||||
const db = require('../../db')
|
const db = require('../../db')
|
||||||
|
|
||||||
const getBills = filters => {
|
const WHERE = conds => (conds ? 'WHERE ' + conds : '')
|
||||||
const deviceStatement = !_.isNil(filters.deviceId)
|
|
||||||
? `WHERE device_id = ${pgp.as.text(filters.deviceId)}`
|
const AND = (...clauses) => clauses.filter(clause => !!clause).join(' AND ')
|
||||||
: ``
|
|
||||||
const batchStatement = filter => {
|
const getBatchIDCondition = filter => {
|
||||||
switch (filter) {
|
switch (filter) {
|
||||||
case 'none':
|
case 'none':
|
||||||
return `WHERE b.cashbox_batch_id IS NULL`
|
return 'b.cashbox_batch_id IS NULL'
|
||||||
case 'any':
|
case 'any':
|
||||||
return `WHERE b.cashbox_batch_id IS NOT NULL`
|
return 'b.cashbox_batch_id IS NOT NULL'
|
||||||
default:
|
default:
|
||||||
return _.isNil(filter)
|
return _.isNil(filter)
|
||||||
? ``
|
? ''
|
||||||
: `WHERE b.cashbox_batch_id = ${pgp.as.text(filter)}`
|
: `b.cashbox_batch_id = ${pgp.as.text(filter)}`
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const getBills = filters => {
|
||||||
|
const deviceIDCondition = !_.isNil(filters.deviceId)
|
||||||
|
? `device_id = ${pgp.as.text(filters.deviceId)}`
|
||||||
|
: ''
|
||||||
|
const batchIDCondition = getBatchIDCondition(filters.batch)
|
||||||
|
|
||||||
const sql = `SELECT b.id, b.fiat, b.fiat_code, b.created, b.cashbox_batch_id, cit.device_id AS device_id
|
const sql = `SELECT b.id, b.fiat, b.fiat_code, b.created, b.cashbox_batch_id, cit.device_id AS device_id
|
||||||
FROM bills b
|
FROM bills b
|
||||||
LEFT OUTER JOIN (
|
LEFT OUTER JOIN (
|
||||||
SELECT id, device_id
|
SELECT id, device_id
|
||||||
FROM cash_in_txs
|
FROM cash_in_txs
|
||||||
${deviceStatement}
|
WHERE ${AND(deviceIDCondition)}
|
||||||
) AS cit
|
) AS cit
|
||||||
ON cit.id = b.cash_in_txs_id
|
ON cit.id = b.cash_in_txs_id
|
||||||
${batchStatement(filters.batch)}
|
WHERE ${AND(batchIDCondition, "b.destination_unit = 'cashbox'")}`
|
||||||
${_.isNil(batchStatement(filters.batch)) ? `WHERE` : `AND`}
|
|
||||||
b.destination_unit = 'cashbox'`
|
|
||||||
|
|
||||||
const sql2 = `SELECT b.id, b.fiat, b.fiat_code, b.created, b.cashbox_batch_id, b.device_id
|
const sql2 = `SELECT b.id, b.fiat, b.fiat_code, b.created, b.cashbox_batch_id, b.device_id
|
||||||
FROM empty_unit_bills b
|
FROM empty_unit_bills b
|
||||||
${deviceStatement}
|
${WHERE(AND(deviceIDCondition, batchIDCondition))}`
|
||||||
${!_.isNil(filters.deviceId) && !_.isNil(filters.batch) ? `AND ${_.replace('WHERE', '', batchStatement(filters.batch))}` : `${batchStatement(filters.batch)}`}`
|
|
||||||
|
|
||||||
return Promise.all([db.any(sql), db.any(sql2)]).then(
|
return Promise.all([db.any(sql), db.any(sql2)]).then(
|
||||||
([bills, operationalBills]) =>
|
([bills, operationalBills]) =>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue