brf/server/routes/api/objects.ts

43 lines
1.1 KiB
TypeScript

import _ from 'lodash'
import { Type, type Static, type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox'
import knex from '../../lib/knex.ts'
const objectRoutes: FastifyPluginCallbackTypebox = (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: Type.Object({
id: Type.Number(),
}),
},
async handler(req) {
return knex('transaction AS t')
.select('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