import _ from 'lodash' import * as z from 'zod' import type { FastifyPluginCallbackZod } from 'fastify-type-provider-zod' import knex from '../../lib/knex.ts' const objectRoutes: FastifyPluginCallbackZod = (fastify, _, done) => { fastify.route({ url: '/', method: 'GET', handler() { return knex('object AS o') .select('o.id', 'o.number', 'o.name', 'd.number AS dimensionNumber', 'd.name AS dimensionName') .innerJoin('dimension AS d', function () { this.on('o.dimensionId', '=', 'd.id') }) }, }) fastify.route({ url: '/:id', method: 'GET', schema: { params: z.object({ id: z.number(), }), }, async handler(req) { return knex('transaction AS t') .select('t.entryId', 'e.transactionDate', 't.accountNumber', 't.amount') .innerJoin('transactions_to_objects AS to', function () { this.on('t.id', 'to.transactionId') }) .innerJoin('entry AS e', function () { this.on('e.id', '=', 't.entryId') }) .where('to.objectId', req.params.id) }, }) done() } export default objectRoutes