62 lines
1.6 KiB
TypeScript
62 lines
1.6 KiB
TypeScript
import _ from 'lodash'
|
|
import { Type, type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox'
|
|
import knex from '../../lib/knex.ts'
|
|
import StatusError from '../../lib/status_error.ts'
|
|
|
|
const transactionRoutes: FastifyPluginCallbackTypebox = (fastify, _, done) => {
|
|
fastify.route({
|
|
url: '/',
|
|
method: 'GET',
|
|
schema: {
|
|
querystring: Type.Object({
|
|
year: Type.Optional(Type.Number()),
|
|
accountNumber: Type.Optional(Type.Number()),
|
|
}),
|
|
},
|
|
async handler(req) {
|
|
const query: { financialYearId?: number; accountNumber?: number } = {}
|
|
|
|
if (req.query.year) {
|
|
const year = await knex('financialYear').first('*').where('year', req.query.year)
|
|
|
|
if (!year) throw new StatusError(404, `Year ${req.query.year} not found.`)
|
|
query.financialYearId = year.id
|
|
}
|
|
|
|
if (req.query.accountNumber) {
|
|
query.accountNumber = req.query.accountNumber
|
|
}
|
|
|
|
return knex('transaction AS t')
|
|
.select(
|
|
't.accountNumber',
|
|
'e.transactionDate',
|
|
't.entryId',
|
|
't.amount',
|
|
't.description',
|
|
't.invoiceId',
|
|
'e.description AS entryDescription',
|
|
)
|
|
.innerJoin('entry AS e', 't.entry_id', 'e.id')
|
|
.where(query)
|
|
},
|
|
})
|
|
|
|
fastify.route({
|
|
url: '/:id',
|
|
method: 'GET',
|
|
schema: {
|
|
params: Type.Object({
|
|
id: Type.Number(),
|
|
}),
|
|
},
|
|
async handler(req) {
|
|
return knex('transaction').first('*').where('id', req.params.id)
|
|
},
|
|
})
|
|
|
|
done()
|
|
}
|
|
|
|
export default transactionRoutes
|