chore: server code formatting
This commit is contained in:
parent
aedabcbdee
commit
68517170e2
234 changed files with 9824 additions and 6195 deletions
|
|
@ -4,8 +4,9 @@ const _ = require('lodash/fp')
|
|||
const uuid = require('uuid')
|
||||
const camelize = require('./utils')
|
||||
|
||||
function createCashboxBatch (deviceId, cashboxCount) {
|
||||
if (_.isEqual(0, cashboxCount)) throw new Error('Cash box is empty. Cash box batch could not be created.')
|
||||
function createCashboxBatch(deviceId, cashboxCount) {
|
||||
if (_.isEqual(0, cashboxCount))
|
||||
throw new Error('Cash box is empty. Cash box batch could not be created.')
|
||||
const sql = `INSERT INTO cash_unit_operation (id, device_id, created, operation_type) VALUES ($1, $2, now(), 'cash-box-empty') RETURNING *`
|
||||
const sql2 = `
|
||||
UPDATE bills SET cashbox_batch_id=$1
|
||||
|
|
@ -24,55 +25,93 @@ function createCashboxBatch (deviceId, cashboxCount) {
|
|||
const q1 = t.one(sql, [batchId, deviceId])
|
||||
const q2 = t.none(sql2, [batchId, deviceId])
|
||||
const q3 = t.none(sql3, [batchId, deviceId])
|
||||
return t.batch([q1, q2, q3])
|
||||
.then(([it]) => it)
|
||||
return t.batch([q1, q2, q3]).then(([it]) => it)
|
||||
})
|
||||
}
|
||||
|
||||
function updateMachineWithBatch (machineContext, oldCashboxCount) {
|
||||
function updateMachineWithBatch(machineContext, oldCashboxCount) {
|
||||
const cashUnits = machineContext.cashUnits
|
||||
const cashUnitNames = ['cashbox', 'cassette1', 'cassette2', 'cassette3', 'cassette4', 'recycler1', 'recycler2', 'recycler3', 'recycler4', 'recycler5', 'recycler6']
|
||||
const isValidContext = _.has(['deviceId', 'cashUnits'], machineContext) && _.has(cashUnitNames, cashUnits)
|
||||
const cassettes = _.filter(it => !_.isNil(it))([cashUnits.cassette1, cashUnits.cassette2, cashUnits.cassette3, cashUnits.cassette4])
|
||||
const isCassetteAmountWithinRange = _.inRange(constants.CASH_OUT_MINIMUM_AMOUNT_OF_CASSETTES, constants.CASH_OUT_MAXIMUM_AMOUNT_OF_CASSETTES + 1, _.size(cassettes))
|
||||
const cashUnitNames = [
|
||||
'cashbox',
|
||||
'cassette1',
|
||||
'cassette2',
|
||||
'cassette3',
|
||||
'cassette4',
|
||||
'recycler1',
|
||||
'recycler2',
|
||||
'recycler3',
|
||||
'recycler4',
|
||||
'recycler5',
|
||||
'recycler6',
|
||||
]
|
||||
const isValidContext =
|
||||
_.has(['deviceId', 'cashUnits'], machineContext) &&
|
||||
_.has(cashUnitNames, cashUnits)
|
||||
const cassettes = _.filter(it => !_.isNil(it))([
|
||||
cashUnits.cassette1,
|
||||
cashUnits.cassette2,
|
||||
cashUnits.cassette3,
|
||||
cashUnits.cassette4,
|
||||
])
|
||||
const isCassetteAmountWithinRange = _.inRange(
|
||||
constants.CASH_OUT_MINIMUM_AMOUNT_OF_CASSETTES,
|
||||
constants.CASH_OUT_MAXIMUM_AMOUNT_OF_CASSETTES + 1,
|
||||
_.size(cassettes),
|
||||
)
|
||||
if (!isValidContext && !isCassetteAmountWithinRange)
|
||||
throw new Error('Insufficient info to create a new cashbox batch')
|
||||
if (_.isEqual(0, oldCashboxCount)) throw new Error('Cash box is empty. Cash box batch could not be created.')
|
||||
if (_.isEqual(0, oldCashboxCount))
|
||||
throw new Error('Cash box is empty. Cash box batch could not be created.')
|
||||
|
||||
return db.tx(t => {
|
||||
const deviceId = machineContext.deviceId
|
||||
const batchId = uuid.v4()
|
||||
const q1 = t.none(`INSERT INTO cash_unit_operation (id, device_id, created, operation_type) VALUES ($1, $2, now(), 'cash-box-empty')`, [batchId, deviceId])
|
||||
const q2 = t.none(`UPDATE bills SET cashbox_batch_id=$1 FROM cash_in_txs
|
||||
const q1 = t.none(
|
||||
`INSERT INTO cash_unit_operation (id, device_id, created, operation_type) VALUES ($1, $2, now(), 'cash-box-empty')`,
|
||||
[batchId, deviceId],
|
||||
)
|
||||
const q2 = t.none(
|
||||
`UPDATE bills SET cashbox_batch_id=$1 FROM cash_in_txs
|
||||
WHERE bills.cash_in_txs_id = cash_in_txs.id AND
|
||||
cash_in_txs.device_id = $2 AND
|
||||
bills.destination_unit = 'cashbox' AND
|
||||
bills.cashbox_batch_id IS NULL`, [batchId, deviceId])
|
||||
const q3 = t.none(`UPDATE empty_unit_bills SET cashbox_batch_id=$1
|
||||
WHERE empty_unit_bills.device_id = $2 AND empty_unit_bills.cashbox_batch_id IS NULL`, [batchId, deviceId])
|
||||
const q4 = t.none(`
|
||||
bills.cashbox_batch_id IS NULL`,
|
||||
[batchId, deviceId],
|
||||
)
|
||||
const q3 = t.none(
|
||||
`UPDATE empty_unit_bills SET cashbox_batch_id=$1
|
||||
WHERE empty_unit_bills.device_id = $2 AND empty_unit_bills.cashbox_batch_id IS NULL`,
|
||||
[batchId, deviceId],
|
||||
)
|
||||
const q4 = t.none(
|
||||
`
|
||||
UPDATE devices SET cassette1=$1, cassette2=$2, cassette3=$3, cassette4=$4,
|
||||
recycler1=coalesce($5, recycler1), recycler2=coalesce($6, recycler2), recycler3=coalesce($7, recycler3),
|
||||
recycler4=coalesce($8, recycler4), recycler5=coalesce($9, recycler5), recycler6=coalesce($10, recycler6) WHERE device_id=$11
|
||||
`, [
|
||||
cashUnits.cassette1,
|
||||
cashUnits.cassette2,
|
||||
cashUnits.cassette3,
|
||||
cashUnits.cassette4,
|
||||
cashUnits.recycler1,
|
||||
cashUnits.recycler2,
|
||||
cashUnits.recycler3,
|
||||
cashUnits.recycler4,
|
||||
cashUnits.recycler5,
|
||||
cashUnits.recycler6,
|
||||
machineContext.deviceId
|
||||
])
|
||||
`,
|
||||
[
|
||||
cashUnits.cassette1,
|
||||
cashUnits.cassette2,
|
||||
cashUnits.cassette3,
|
||||
cashUnits.cassette4,
|
||||
cashUnits.recycler1,
|
||||
cashUnits.recycler2,
|
||||
cashUnits.recycler3,
|
||||
cashUnits.recycler4,
|
||||
cashUnits.recycler5,
|
||||
cashUnits.recycler6,
|
||||
machineContext.deviceId,
|
||||
],
|
||||
)
|
||||
|
||||
return t.batch([q1, q2, q3, q4])
|
||||
})
|
||||
}
|
||||
|
||||
function getBatches (from = new Date(0).toISOString(), until = new Date().toISOString()) {
|
||||
function getBatches(
|
||||
from = new Date(0).toISOString(),
|
||||
until = new Date().toISOString(),
|
||||
) {
|
||||
const sql = `
|
||||
SELECT
|
||||
cuo.id,
|
||||
|
|
@ -96,28 +135,25 @@ function getBatches (from = new Date(0).toISOString(), until = new Date().toISOS
|
|||
return db.any(sql, [from, until]).then(camelize)
|
||||
}
|
||||
|
||||
function editBatchById (id, performedBy) {
|
||||
function editBatchById(id, performedBy) {
|
||||
const sql = `UPDATE cash_unit_operation SET performed_by=$1 WHERE id=$2 AND cuo.operation_type = 'cash-box-empty'`
|
||||
return db.none(sql, [performedBy, id])
|
||||
}
|
||||
|
||||
function logFormatter (data) {
|
||||
return _.map(
|
||||
it => {
|
||||
return {
|
||||
id: it.id,
|
||||
deviceId: it.deviceId,
|
||||
created: it.created,
|
||||
operationType: it.operationType,
|
||||
billCount: it.billCount,
|
||||
fiatTotal: it.fiatTotal
|
||||
}
|
||||
},
|
||||
data
|
||||
)
|
||||
function logFormatter(data) {
|
||||
return _.map(it => {
|
||||
return {
|
||||
id: it.id,
|
||||
deviceId: it.deviceId,
|
||||
created: it.created,
|
||||
operationType: it.operationType,
|
||||
billCount: it.billCount,
|
||||
fiatTotal: it.fiatTotal,
|
||||
}
|
||||
}, data)
|
||||
}
|
||||
|
||||
function getMachineUnbatchedBills (deviceId) {
|
||||
function getMachineUnbatchedBills(deviceId) {
|
||||
const sql = `
|
||||
SELECT now() AS created, cash_in_txs.device_id, json_agg(b.*) AS bills FROM bills b LEFT OUTER JOIN cash_in_txs
|
||||
ON b.cash_in_txs_id = cash_in_txs.id
|
||||
|
|
@ -125,12 +161,13 @@ function getMachineUnbatchedBills (deviceId) {
|
|||
GROUP BY cash_in_txs.device_id
|
||||
`
|
||||
|
||||
return db.oneOrNone(sql, [deviceId])
|
||||
return db
|
||||
.oneOrNone(sql, [deviceId])
|
||||
.then(res => _.mapKeys(it => _.camelCase(it), res))
|
||||
.then(logFormatterSingle)
|
||||
}
|
||||
|
||||
function getBatchById (id) {
|
||||
function getBatchById(id) {
|
||||
const sql = `
|
||||
SELECT cb.id, cb.device_id, cb.created, cb.operation_type, cb.bill_count_override, cb.performed_by, json_agg(b.*) AS bills
|
||||
FROM cash_unit_operation AS cb
|
||||
|
|
@ -139,14 +176,22 @@ function getBatchById (id) {
|
|||
GROUP BY cb.id
|
||||
`
|
||||
|
||||
return db.oneOrNone(sql, [id]).then(res => _.mapKeys(it => _.camelCase(it), res))
|
||||
return db
|
||||
.oneOrNone(sql, [id])
|
||||
.then(res => _.mapKeys(it => _.camelCase(it), res))
|
||||
.then(logFormatterSingle)
|
||||
}
|
||||
|
||||
function logFormatterSingle (data) {
|
||||
function logFormatterSingle(data) {
|
||||
const bills = _.filter(
|
||||
it => !(_.isNil(it) || _.isNil(it.fiat_code) || _.isNil(it.fiat) || _.isNaN(it.fiat)),
|
||||
data.bills
|
||||
it =>
|
||||
!(
|
||||
_.isNil(it) ||
|
||||
_.isNil(it.fiat_code) ||
|
||||
_.isNil(it.fiat) ||
|
||||
_.isNaN(it.fiat)
|
||||
),
|
||||
data.bills,
|
||||
)
|
||||
|
||||
return {
|
||||
|
|
@ -161,9 +206,9 @@ function logFormatterSingle (data) {
|
|||
return acc
|
||||
},
|
||||
{},
|
||||
bills
|
||||
bills,
|
||||
),
|
||||
billsByDenomination: _.countBy(it => `${it.fiat} ${it.fiat_code}`, bills)
|
||||
billsByDenomination: _.countBy(it => `${it.fiat} ${it.fiat_code}`, bills),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -174,5 +219,5 @@ module.exports = {
|
|||
editBatchById,
|
||||
getBatchById,
|
||||
getMachineUnbatchedBills,
|
||||
logFormatter
|
||||
logFormatter,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue