refactor: unify bill related queries
This commit is contained in:
parent
98a2797494
commit
e2087d0e4d
5 changed files with 40 additions and 44 deletions
|
|
@ -2,9 +2,7 @@ const bills = require('../../services/bills')
|
||||||
|
|
||||||
const resolvers = {
|
const resolvers = {
|
||||||
Query: {
|
Query: {
|
||||||
bills: () => bills.getBills(),
|
bills: (...[, { filters }]) => bills.getBills(filters)
|
||||||
looseBills: () => bills.getLooseBills(),
|
|
||||||
looseBillsByMachine: (...[, { deviceId }]) => bills.getLooseBillsByMachine(deviceId)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,7 @@ const typeDef = gql`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Query {
|
type Query {
|
||||||
bills: [Bill] @auth
|
bills(filters: JSONObject): [Bill] @auth
|
||||||
looseBills: [Bill] @auth
|
|
||||||
looseBillsByMachine(deviceId: ID): [Bill] @auth
|
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,37 +1,29 @@
|
||||||
const _ = require('lodash/fp')
|
const _ = require('lodash/fp')
|
||||||
|
const pgp = require('pg-promise')()
|
||||||
|
|
||||||
const db = require('../../db')
|
const db = require('../../db')
|
||||||
|
|
||||||
// Get all bills with device id
|
const getBills = filters => {
|
||||||
const getBills = () => {
|
const deviceStatement = !_.isNil(filters.deviceId) ? `WHERE device_id = ${pgp.as.text(filters.deviceId)}` : ``
|
||||||
|
const batchStatement = filter => {
|
||||||
|
switch (filter) {
|
||||||
|
case 'none':
|
||||||
|
return `WHERE b.cashbox_batch_id IS NULL`
|
||||||
|
case 'any':
|
||||||
|
return `WHERE b.cashbox_batch_id IS NOT NULL`
|
||||||
|
default:
|
||||||
|
return _.isNil(filter) ? `` : `WHERE b.cashbox_batch_id = ${pgp.as.text(filter)}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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 LEFT OUTER JOIN (
|
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 LEFT OUTER JOIN (
|
||||||
SELECT id, device_id FROM cash_in_txs
|
SELECT id, device_id FROM cash_in_txs ${deviceStatement}
|
||||||
) AS cit ON cit.id = b.cash_in_txs_id`
|
) AS cit ON cit.id = b.cash_in_txs_id ${batchStatement(filters.batch)}`
|
||||||
|
|
||||||
return db.any(sql)
|
return db.any(sql)
|
||||||
.then(res => _.map(_.mapKeys(_.camelCase), res))
|
.then(res => _.map(_.mapKeys(_.camelCase), res))
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLooseBills () {
|
|
||||||
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 LEFT OUTER JOIN (
|
|
||||||
SELECT id, device_id FROM cash_in_txs
|
|
||||||
) AS cit ON cit.id = b.cash_in_txs_id WHERE b.cashbox_batch_id IS NULL`
|
|
||||||
|
|
||||||
return db.any(sql)
|
|
||||||
.then(res => _.map(_.mapKeys(_.camelCase), res))
|
|
||||||
}
|
|
||||||
|
|
||||||
function getLooseBillsByMachine (machineId) {
|
|
||||||
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 LEFT OUTER JOIN (
|
|
||||||
SELECT id, device_id FROM cash_in_txs WHERE device_id = $1
|
|
||||||
) AS cit ON cit.id = b.cash_in_txs_id WHERE b.cashbox_batch_id IS NULL`
|
|
||||||
|
|
||||||
return db.any(sql, [machineId])
|
|
||||||
.then(res => _.map(_.mapKeys(_.camelCase), res))
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getBills,
|
getBills
|
||||||
getLooseBills,
|
|
||||||
getLooseBillsByMachine
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ import styles from './Machines.styles'
|
||||||
const useStyles = makeStyles(styles)
|
const useStyles = makeStyles(styles)
|
||||||
|
|
||||||
const GET_INFO = gql`
|
const GET_INFO = gql`
|
||||||
query getMachine($deviceId: ID!) {
|
query getMachine($deviceId: ID!, $billFilters: JSONObject) {
|
||||||
machine(deviceId: $deviceId) {
|
machine(deviceId: $deviceId) {
|
||||||
name
|
name
|
||||||
deviceId
|
deviceId
|
||||||
|
|
@ -46,7 +46,7 @@ const GET_INFO = gql`
|
||||||
note
|
note
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
looseBillsByMachine(deviceId: $deviceId) {
|
bills(filters: $billFilters) {
|
||||||
id
|
id
|
||||||
fiat
|
fiat
|
||||||
deviceId
|
deviceId
|
||||||
|
|
@ -62,7 +62,11 @@ const Machines = () => {
|
||||||
const location = useLocation()
|
const location = useLocation()
|
||||||
const { data, loading, refetch } = useQuery(GET_INFO, {
|
const { data, loading, refetch } = useQuery(GET_INFO, {
|
||||||
variables: {
|
variables: {
|
||||||
deviceId: getMachineID(location.pathname)
|
deviceId: getMachineID(location.pathname),
|
||||||
|
billFilters: {
|
||||||
|
deviceId: getMachineID(location.pathname),
|
||||||
|
batch: 'none'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -72,7 +76,7 @@ const Machines = () => {
|
||||||
|
|
||||||
const machine = R.path(['machine'])(data) ?? {}
|
const machine = R.path(['machine'])(data) ?? {}
|
||||||
const config = R.path(['config'])(data) ?? {}
|
const config = R.path(['config'])(data) ?? {}
|
||||||
const bills = R.path(['looseBillsByMachine'])(data) ?? []
|
const bills = R.path(['bills'])(data) ?? []
|
||||||
|
|
||||||
const machineName = R.path(['name'])(machine) ?? null
|
const machineName = R.path(['name'])(machine) ?? null
|
||||||
const machineID = R.path(['deviceId'])(machine) ?? null
|
const machineID = R.path(['deviceId'])(machine) ?? null
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ const ValidationSchema = Yup.object().shape({
|
||||||
})
|
})
|
||||||
|
|
||||||
const GET_MACHINES_AND_CONFIG = gql`
|
const GET_MACHINES_AND_CONFIG = gql`
|
||||||
query getData {
|
query getData($billFilters: JSONObject) {
|
||||||
machines {
|
machines {
|
||||||
name
|
name
|
||||||
id: deviceId
|
id: deviceId
|
||||||
|
|
@ -75,7 +75,7 @@ const GET_MACHINES_AND_CONFIG = gql`
|
||||||
numberOfCassettes
|
numberOfCassettes
|
||||||
}
|
}
|
||||||
config
|
config
|
||||||
looseBills {
|
bills(filters: $billFilters) {
|
||||||
id
|
id
|
||||||
fiat
|
fiat
|
||||||
created
|
created
|
||||||
|
|
@ -125,7 +125,13 @@ const CashCassettes = () => {
|
||||||
const [editingSchema, setEditingSchema] = useState(null)
|
const [editingSchema, setEditingSchema] = useState(null)
|
||||||
const [selectedRadio, setSelectedRadio] = useState(null)
|
const [selectedRadio, setSelectedRadio] = useState(null)
|
||||||
|
|
||||||
const { data, loading: dataLoading } = useQuery(GET_MACHINES_AND_CONFIG)
|
const { data, loading: dataLoading } = useQuery(GET_MACHINES_AND_CONFIG, {
|
||||||
|
variables: {
|
||||||
|
billFilters: {
|
||||||
|
batch: 'none'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
const [wizard, setWizard] = useState(false)
|
const [wizard, setWizard] = useState(false)
|
||||||
const [machineId, setMachineId] = useState('')
|
const [machineId, setMachineId] = useState('')
|
||||||
|
|
||||||
|
|
@ -140,11 +146,9 @@ const CashCassettes = () => {
|
||||||
refetchQueries: () => ['getData']
|
refetchQueries: () => ['getData']
|
||||||
})
|
})
|
||||||
|
|
||||||
const bills = R.groupBy(bill => bill.deviceId)(
|
const bills = R.groupBy(bill => bill.deviceId)(R.path(['bills'])(data) ?? [])
|
||||||
R.path(['looseBills'])(data) ?? []
|
|
||||||
)
|
|
||||||
const deviceIds = R.uniq(
|
const deviceIds = R.uniq(
|
||||||
R.map(R.prop('deviceId'))(R.path(['looseBills'])(data) ?? [])
|
R.map(R.prop('deviceId'))(R.path(['bills'])(data) ?? [])
|
||||||
)
|
)
|
||||||
const cashout = data?.config && fromNamespace('cashOut')(data.config)
|
const cashout = data?.config && fromNamespace('cashOut')(data.config)
|
||||||
const locale = data?.config && fromNamespace('locale')(data.config)
|
const locale = data?.config && fromNamespace('locale')(data.config)
|
||||||
|
|
@ -209,7 +213,7 @@ const CashCassettes = () => {
|
||||||
<CashIn
|
<CashIn
|
||||||
currency={{ code: fiatCurrency }}
|
currency={{ code: fiatCurrency }}
|
||||||
notes={value}
|
notes={value}
|
||||||
total={R.sum(R.map(it => it.fiat, bills[id]))}
|
total={R.sum(R.map(it => it.fiat, bills[id] ?? []))}
|
||||||
/>
|
/>
|
||||||
),
|
),
|
||||||
input: NumberInput,
|
input: NumberInput,
|
||||||
|
|
@ -331,7 +335,7 @@ const CashCassettes = () => {
|
||||||
currencyCode={fiatCurrency}
|
currencyCode={fiatCurrency}
|
||||||
machines={machines}
|
machines={machines}
|
||||||
config={config}
|
config={config}
|
||||||
bills={bills}
|
bills={R.path(['bills'])(data)}
|
||||||
deviceIds={deviceIds}
|
deviceIds={deviceIds}
|
||||||
/>
|
/>
|
||||||
{wizard && (
|
{wizard && (
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue