brf/server/routes/api/entries.ts

31 lines
871 B
TypeScript

import _ from 'lodash'
import { Type, type Static, type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox'
import knex from '../../lib/knex.ts'
const entryRoutes: FastifyPluginCallbackTypebox = (fastify, _, done) => {
fastify.route({
url: '/',
method: 'GET',
schema: {
querystring: Type.Object({
journal: Type.String(),
year: Type.Number(),
}),
},
async handler(req) {
const financialYearId = (await knex('financialYear').first('id').where('year', req.query.year))?.id
const journalId = (await knex('journal').first('id').where('identifier', req.query.journal))?.id
if (!financialYearId || !journalId) {
return null
}
return knex('entry').select('*').orderBy('entryDate').where({ financialYearId, journalId })
},
})
done()
}
export default entryRoutes