43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import _ from 'lodash'
|
|
import * as z from 'zod'
|
|
import type { FastifyPluginCallbackZod } from 'fastify-type-provider-zod'
|
|
|
|
const objectRoutes: FastifyPluginCallbackZod = (fastify, _, done) => {
|
|
const { db } = fastify
|
|
|
|
fastify.route({
|
|
url: '/',
|
|
method: 'GET',
|
|
handler() {
|
|
return db
|
|
.selectFrom('object as o')
|
|
.innerJoin('dimension as d', 'd.id', 'o.dimensionId')
|
|
.select(['o.id', 'o.number', 'o.name', 'd.number as dimensionNumber', 'd.name as dimensionName'])
|
|
.execute()
|
|
},
|
|
})
|
|
|
|
fastify.route({
|
|
url: '/:id/transactions',
|
|
method: 'GET',
|
|
schema: {
|
|
params: z.object({
|
|
id: z.coerce.number(),
|
|
}),
|
|
},
|
|
async handler(request) {
|
|
return db
|
|
.selectFrom('transaction as t')
|
|
.innerJoin('transactionsToObjects as to', 't.id', 'to.transactionId')
|
|
.innerJoin('entry as e', 'e.id', 't.entryId')
|
|
.select(['t.entryId', 'e.transactionDate', 't.accountNumber', 't.amount'])
|
|
.where('to.objectId', '=', request.params.id)
|
|
.execute()
|
|
},
|
|
})
|
|
|
|
done()
|
|
}
|
|
|
|
export default objectRoutes
|