Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to rerun bank transaction sync #1488

Merged
merged 2 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ Watch releases of this repository to be notified about future updates:
## Contributors ✨

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->

[![All Contributors](https://img.shields.io/badge/all_contributors-69-orange.svg?style=flat-square)](#contributors-)

<!-- ALL-CONTRIBUTORS-BADGE:END -->

Please check [contributors guide](https://github.com/podkrepi-bg/frontend/blob/master/CONTRIBUTING.md) for:
Expand Down
6 changes: 4 additions & 2 deletions public/locales/bg/bank-transactions.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
"to": "До",
"apply-ref-heading": "Приложи код на кампания",
"apply-ref": "Приложи кода",
"edit": "Редактирай"
"edit": "Редактирай",
"start-sync": "Стартирай синхронизация"
},
"matched-ref": "Разпознат код",
"payment-ref": "Код на кампанията"
"payment-ref": "Код на кампанията",
"rerun-dates": "Повтори банково синхронизиране"
}
2 changes: 1 addition & 1 deletion public/locales/en/expenses.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"bank": "Bank",
"advertising": "Advertising",
"other": "Other"
},
},
"alerts": {
"new-row": {
"error": "Error occurred trying to create an expense.",
Expand Down
24 changes: 23 additions & 1 deletion src/components/admin/bank-transactions/grid/GridAppbar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useTranslation } from 'next-i18next'
import { Box, TextField, Toolbar, Tooltip, Typography } from '@mui/material'
import { GetApp as DownloadFileIcon } from '@mui/icons-material'
import { GetApp as DownloadFileIcon, RotateLeftOutlined } from '@mui/icons-material'

import { useMutation } from '@tanstack/react-query'
import { useExportToExcel } from 'service/bankTransaction'
Expand All @@ -9,6 +9,7 @@ import { downloadFile } from '../../../../common/util/downloadFile'
import { useMemo, useState } from 'react'
import { useStores } from 'common/hooks/useStores'
import { debounce } from 'lodash'
import RerunTransactionSyncModal from './RerunTransactionsSyncDialog'

const addIconStyles = {
background: '#4ac3ff',
Expand All @@ -21,6 +22,7 @@ const addIconStyles = {
export default function GridAppbar() {
const { bankTransactionsStore } = useStores()
const { t } = useTranslation()

const exportToExcel = useMutation({
mutationFn: useExportToExcel(),
onError: () => AlertStore.show(t('common:alerts.error'), 'error'),
Expand All @@ -29,6 +31,7 @@ export default function GridAppbar() {
AlertStore.show(t('common:alerts.success'), 'success')
},
})

const [searchValue, setSearchValue] = useState('')

const debounceSearch = useMemo(
Expand All @@ -50,6 +53,15 @@ export default function GridAppbar() {
debounceSearch(searchText)
}

const [isRerunSyncOpen, setIsRerunSyncOpen] = useState(false)
const handleRerunSyncOpen = () => {
setIsRerunSyncOpen(true)
}
const handleRerunSyncClose = () => {
setIsRerunSyncOpen(false)
//TODO: refetch() after sync
}

return (
<Toolbar
sx={{
Expand Down Expand Up @@ -81,6 +93,16 @@ export default function GridAppbar() {
/>
</Tooltip>
</Box>
<Box sx={{ display: 'flex', alignItems: 'center' }}>
<Tooltip title={t('bank-transactions:rerun-dates') || ''}>
<RotateLeftOutlined
sx={addIconStyles}
fontSize="large"
onClick={() => handleRerunSyncOpen()}
/>
</Tooltip>
<RerunTransactionSyncModal isOpen={isRerunSyncOpen} handleClose={handleRerunSyncClose} />
</Box>
</Box>
</Toolbar>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { Dialog, Grid, DialogContent, DialogTitle } from '@mui/material'
import GenericForm from 'components/common/form/GenericForm'
import SubmitButton from 'components/common/form/SubmitButton'
import { useState } from 'react'
import { useTranslation } from 'next-i18next'
import * as yup from 'yup'
import CloseModalButton from 'components/common/CloseModalButton'
import { RerunTransactionsDatesInput } from 'gql/bank-transactions'
import { useRerunBankImportForDate } from 'service/bankTransaction'
import FormDatePicker from 'components/common/form/FormDatePicker'
import { useMutation } from '@tanstack/react-query'
import { AlertStore } from 'stores/AlertStore'

const validationSchema: yup.SchemaOf<RerunTransactionsDatesInput> = yup.object().defined().shape({
startDate: yup.date().required(),
endDate: yup.date().required(),
})

export default function RerunTransactionSyncModal({
isOpen,
handleClose,
}: {
isOpen: boolean
handleClose: () => void
}) {
const { t } = useTranslation()
const [loading, setLoading] = useState(false)

const rerunSyncMutation = useMutation({
mutationFn: useRerunBankImportForDate(),
onError: () => AlertStore.show(t('common:alerts.error'), 'error'),
onSuccess: () => {
AlertStore.show(t('common:alerts.success'), 'success')
handleClose()
},
})

async function onSubmit(values: RerunTransactionsDatesInput) {
setLoading(true)
try {
await rerunSyncMutation.mutateAsync(values)
} finally {
setLoading(false)
}
}

return (
<Dialog
open={isOpen}
onClose={handleClose}
sx={{ scroll: 'none' }}
fullWidth={true}
maxWidth={'sm'}>
<DialogContent
style={{
overflow: 'hidden',
padding: '4rem',
paddingTop: '1rem',
width: '100%',
}}>
<Grid style={{ display: 'flex', justifyContent: 'end', marginRight: '-4rem' }}>
<CloseModalButton href={''} onClose={handleClose} />
</Grid>
<DialogTitle style={{ display: 'flex', justifyContent: 'center', width: '100%' }}>
{t('bank-transactions:rerun-dates')}
</DialogTitle>
<Grid container direction="column" component="section">
<GenericForm
onSubmit={onSubmit}
initialValues={{ startDate: new Date(), endDate: new Date() }}
validationSchema={validationSchema}>
<Grid container direction="row" spacing={3}>
<Grid item xs={6} sx={{ marginBottom: '1rem' }}>
<FormDatePicker label={t('bank-transactions:cta.from')} name="startDate" />
</Grid>
<Grid item xs={6} sx={{ marginBottom: '1rem' }}>
<FormDatePicker label={t('bank-transactions:cta.to')} name="endDate" />
</Grid>
<Grid item xs={12}>
<SubmitButton
fullWidth
label="bank-transactions:cta.start-sync"
loading={loading}
/>
</Grid>
</Grid>
</GenericForm>
</Grid>
</DialogContent>
</Dialog>
)
}
5 changes: 5 additions & 0 deletions src/gql/bank-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ export type BankTransactionEditRefResponse = {
paymentRef: string
status: string
}

export type RerunTransactionsDatesInput = {
startDate: Date | string
endDate: Date | string
}
1 change: 1 addition & 0 deletions src/service/apiEndpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export const endpoints = {
exportToExcel: <Endpoint>{ url: '/bank-transaction/export-excel', method: 'GET' },
editPaymentRef: (id: string) =>
<Endpoint>{ url: `/bank-transaction/${id}/edit-ref`, method: 'PUT' },
rerunDates: <Endpoint>{ url: '/bank-transaction/rerun-dates', method: 'POST' },
},
documents: {
documentsList: <Endpoint>{ url: '/document', method: 'GET' },
Expand Down
18 changes: 17 additions & 1 deletion src/service/bankTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { AxiosResponse } from 'axios'
import { BankTransactionEditRefInput, BankTransactionEditRefResponse } from 'gql/bank-transactions'
import {
BankTransactionEditRefInput,
BankTransactionEditRefResponse,
BankTransactionsHistoryResponse,
RerunTransactionsDatesInput,
} from 'gql/bank-transactions'
import { useSession } from 'next-auth/react'
import { apiClient } from './apiClient'
import { endpoints } from './apiEndpoints'
Expand All @@ -24,3 +29,14 @@ export function useEditTransactionPaymentRef(slug: string) {
>(endpoints.bankTransactions.editPaymentRef(slug).url, data, authConfig(session?.accessToken))
}
}

export const useRerunBankImportForDate = () => {
const { data: session } = useSession()
return async (transactionDates: RerunTransactionsDatesInput) => {
return await apiClient.post<string, AxiosResponse<BankTransactionsHistoryResponse>>(
endpoints.bankTransactions.rerunDates.url,
transactionDates,
authConfig(session?.accessToken),
)
}
}