67 lines
1.6 KiB
TypeScript
67 lines
1.6 KiB
TypeScript
import _ from 'lodash'
|
|
import * as z from 'zod'
|
|
import type { FastifyPluginCallbackZod } from 'fastify-type-provider-zod'
|
|
import { paginate } from '../../lib/kysely_helpers.ts'
|
|
|
|
const transactionRoutes: FastifyPluginCallbackZod = (fastify, _, done) => {
|
|
const { db } = fastify
|
|
|
|
fastify.route({
|
|
url: '/',
|
|
method: 'GET',
|
|
schema: {
|
|
querystring: z.object({
|
|
year: z.optional(z.coerce.number()),
|
|
accountNumber: z.optional(z.coerce.number()),
|
|
limit: z.coerce.number().default(100),
|
|
offset: z.coerce.number().default(0),
|
|
sort: z.literal(['accountNumber', 'e.transactionDate', 't.id']).default('t.id'),
|
|
}),
|
|
},
|
|
async handler(request) {
|
|
const { offset, limit, sort, ...where } = request.query
|
|
|
|
const baseQuery = db
|
|
.selectFrom('transaction as t')
|
|
.innerJoin('entry as e', 't.entryId', 'e.id')
|
|
.innerJoin('financialYear as fy', 'e.financialYearId', 'fy.id')
|
|
|
|
return paginate(
|
|
baseQuery.select([
|
|
't.accountNumber',
|
|
'e.transactionDate',
|
|
't.entryId',
|
|
't.amount',
|
|
't.description',
|
|
't.invoiceId',
|
|
'e.description as entryDescription',
|
|
]),
|
|
baseQuery,
|
|
{
|
|
where,
|
|
limit,
|
|
offset,
|
|
sort,
|
|
},
|
|
)
|
|
},
|
|
})
|
|
|
|
fastify.route({
|
|
url: '/:id',
|
|
method: 'GET',
|
|
schema: {
|
|
params: z.object({
|
|
id: z.number(),
|
|
}),
|
|
},
|
|
handler(req) {
|
|
return db.selectFrom('transaction').selectAll().where('id', '=', req.params.id).execute()
|
|
},
|
|
})
|
|
|
|
done()
|
|
}
|
|
|
|
export default transactionRoutes
|