From d6d8280a3635204a584099220960a7b1e9075d1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Mon, 12 Apr 2021 19:28:57 +0100 Subject: [PATCH] feat: allow for cashbox batch editing --- lib/cashbox-batches.js | 7 +- .../graphql/resolvers/cashbox.resolver.js | 3 + lib/new-admin/graphql/types/cashbox.type.js | 4 + .../src/pages/Maintenance/CashCassettes.js | 12 +- .../src/pages/Maintenance/CashboxHistory.js | 111 ++++++++++++++++-- 5 files changed, 119 insertions(+), 18 deletions(-) diff --git a/lib/cashbox-batches.js b/lib/cashbox-batches.js index e9693d97..134cec42 100644 --- a/lib/cashbox-batches.js +++ b/lib/cashbox-batches.js @@ -30,9 +30,14 @@ function getBatches () { }), res)) } +function editBatchById (id, performedBy) { + const sql = `UPDATE cashbox_batches SET performed_by=$1 WHERE id=$2` + return db.none(sql, [performedBy, id]) +} + function getBillsByBatchId (id) { const sql = `SELECT * FROM bills WHERE cashbox_batch_id=$1` return db.any(sql, [id]) } -module.exports = { createCashboxBatch, getBatches, getBillsByBatchId } +module.exports = { createCashboxBatch, getBatches, getBillsByBatchId, editBatchById } diff --git a/lib/new-admin/graphql/resolvers/cashbox.resolver.js b/lib/new-admin/graphql/resolvers/cashbox.resolver.js index 55584817..f44a1cbf 100644 --- a/lib/new-admin/graphql/resolvers/cashbox.resolver.js +++ b/lib/new-admin/graphql/resolvers/cashbox.resolver.js @@ -3,6 +3,9 @@ const cashbox = require('../../../cashbox-batches') const resolvers = { Query: { cashboxBatches: () => cashbox.getBatches() + }, + Mutation: { + editBatch: (...[, { id, performedBy }]) => cashbox.editBatchById(id, performedBy) } } diff --git a/lib/new-admin/graphql/types/cashbox.type.js b/lib/new-admin/graphql/types/cashbox.type.js index 82a66c30..defb8f1c 100644 --- a/lib/new-admin/graphql/types/cashbox.type.js +++ b/lib/new-admin/graphql/types/cashbox.type.js @@ -14,6 +14,10 @@ const typeDef = gql` type Query { cashboxBatches: [CashboxBatch] } + + type Mutation { + editBatch(id: ID, performedBy: String): CashboxBatch + } ` module.exports = typeDef diff --git a/new-lamassu-admin/src/pages/Maintenance/CashCassettes.js b/new-lamassu-admin/src/pages/Maintenance/CashCassettes.js index 7d71f352..4d02c273 100644 --- a/new-lamassu-admin/src/pages/Maintenance/CashCassettes.js +++ b/new-lamassu-admin/src/pages/Maintenance/CashCassettes.js @@ -12,8 +12,8 @@ import { NumberInput, CashCassetteInput } from 'src/components/inputs/formik' import TitleSection from 'src/components/layout/TitleSection' import { EmptyTable } from 'src/components/table' import { ReactComponent as EditIcon } from 'src/styling/icons/action/edit/enabled.svg' -import { ReactComponent as ReverseListingViewIcon } from 'src/styling/icons/circle buttons/listing-view/white.svg' -import { ReactComponent as ListingViewIcon } from 'src/styling/icons/circle buttons/listing-view/zodiac.svg' +import { ReactComponent as ReverseHistoryIcon } from 'src/styling/icons/circle buttons/history/white.svg' +import { ReactComponent as HistoryIcon } from 'src/styling/icons/circle buttons/history/zodiac.svg' import { fromNamespace } from 'src/utils/config' import styles from './CashCassettes.styles.js' @@ -208,8 +208,8 @@ const CashCassettes = () => { title="Cash Cassettes" button={{ text: 'Cashbox history', - icon: ListingViewIcon, - inverseIcon: ReverseListingViewIcon, + icon: HistoryIcon, + inverseIcon: ReverseHistoryIcon, toggle: setShowHistory }} iconClassName={classes.listViewButton} @@ -234,7 +234,9 @@ const CashCassettes = () => { )} )} - {showHistory && } + {showHistory && ( + + )} { +const CashboxHistory = ({ machines, currency }) => { const classes = useStyles() - const { data } = useQuery(GET_BATCHES) + const [editing, setEditing] = useState(false) + const [error, setError] = useState(false) + const [fields, setFields] = useState({}) + + const { data, loading } = useQuery(GET_BATCHES) + + const [editBatch] = useMutation(EDIT_BATCH, { + refetchQueries: () => ['cashboxBatches'] + }) const batches = R.path(['cashboxBatches'])(data) @@ -81,6 +110,24 @@ const CashboxHistory = ({ machines }) => { ) } + const save = row => { + schema + .isValid(fields) + .then(() => { + setError(false) + editBatch({ + variables: { id: row.id, performedBy: fields?.performedBy } + }) + }) + .catch(setError(true)) + return close() + } + + const close = () => { + setFields({}) + return setEditing(false) + } + const elements = [ { name: 'operation', @@ -96,7 +143,7 @@ const CashboxHistory = ({ machines }) => { { name: 'machine', header: 'Machine', - width: 190, + width: 200, textAlign: 'left', view: it => { return R.find(R.propEq('id', it.deviceId))(machines).name @@ -117,9 +164,13 @@ const CashboxHistory = ({ machines }) => { { name: 'total', header: 'Total', - width: 125, + width: 100, textAlign: 'right', - view: it => R.sum(R.map(b => R.prop('fiat', b), it.bills)) + view: it => ( + + {R.sum(R.map(b => R.prop('fiat', b), it.bills))} {currency} + + ) }, { name: 'date', @@ -138,22 +189,58 @@ const CashboxHistory = ({ machines }) => { { name: 'performedBy', header: 'Performed by', - width: 200, + width: 190, textAlign: 'left', - view: it => (R.isNil(it.performedBy) ? 'Unknown entity' : it.performedBy) + view: it => { + if (!editing) + return R.isNil(it.performedBy) ? 'Unknown entity' : it.performedBy + return ( + + setFields({ ...fields, performedBy: e.target.value }) + } + error={error} + width={190 * 0.85} + value={fields.performedBy ?? ''} + /> + ) + } }, { name: '', header: 'Edit', - width: 120, + width: 150, textAlign: 'right', - view: it => 'aaaaa' + view: it => { + if (!editing) + return ( + { + setFields({}) + setEditing(true) + }}> + + + ) + return ( +
+ save(it)}> + Save + + + Cancel + +
+ ) + } } ] return ( <> - + {!loading && ( + + )} ) }