restructure api routes

This commit is contained in:
Linus Miller 2025-11-30 16:52:24 +01:00
parent ee25824424
commit 295e42d0d3
20 changed files with 278 additions and 171 deletions

View File

@ -1,7 +1,7 @@
meta {
name: /api/accounts
type: http
seq: 9
seq: 2
}
get {
@ -12,4 +12,5 @@ get {
settings {
encodeUrl: true
timeout: 0
}

View File

@ -0,0 +1,21 @@
meta {
name: /api/entries
type: http
seq: 3
}
get {
url: {{base_url}}/api/entries?year=2015&journal=A
body: none
auth: inherit
}
params:query {
year: 2015
journal: A
}
settings {
encodeUrl: true
timeout: 0
}

View File

@ -1,7 +1,7 @@
meta {
name: Financial Years
name: /api/financial-years
type: http
seq: 2
seq: 4
}
get {

View File

@ -1,20 +0,0 @@
meta {
name: /api/invoice/:id
type: http
seq: 7
}
get {
url: {{base_url}}/api/invoices/:id
body: none
auth: inherit
}
params:path {
id: 234
}
settings {
encodeUrl: true
timeout: 0
}

View File

@ -0,0 +1,16 @@
meta {
name: /api/invoices/:id
type: http
seq: 8
}
get {
url: {{base_url}}/api/invoices/2631
body: none
auth: inherit
}
settings {
encodeUrl: true
timeout: 0
}

View File

@ -1,7 +1,7 @@
meta {
name: Invoices
name: /api/invoices
type: http
seq: 6
seq: 7
}
get {
@ -12,4 +12,5 @@ get {
settings {
encodeUrl: true
timeout: 0
}

View File

@ -1,7 +1,7 @@
meta {
name: Object
name: /api/objects/:id
type: http
seq: 3
seq: 5
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: Objects
name: /api/objects
type: http
seq: 4
seq: 6
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: Result
name: /api/results/:year
type: http
seq: 5
seq: 11
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: Results
name: /api/results
type: http
seq: 6
seq: 10
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/suppliers
type: http
seq: 8
seq: 9
}
get {
@ -12,4 +12,5 @@ get {
settings {
encodeUrl: true
timeout: 0
}

View File

@ -1,10 +1,14 @@
import _ from 'lodash'
import { Type, type Static, type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox'
import knex from '../lib/knex.ts'
import StatusError from '../lib/status_error.ts'
import accounts from './api/accounts.ts'
import entries from './api/entries.ts'
import financialYears from './api/financial_years.ts'
import invoices from './api/invoices.ts'
import journals from './api/journals.ts'
import objects from './api/objects.ts'
import results from './api/results.ts'
export const FinancialYear = Type.Object({
year: Type.Number(),
@ -16,139 +20,12 @@ export type FinancialYearType = Static<typeof FinancialYear>
const apiRoutes: FastifyPluginCallbackTypebox = (fastify, _, done) => {
fastify.register(accounts, { prefix: '/accounts' })
fastify.register(entries, { prefix: '/entries' })
fastify.register(financialYears, { prefix: '/financial-years' })
fastify.register(invoices, { prefix: '/invoices' })
fastify.route({
url: '/financial-years',
method: 'GET',
handler() {
return knex('financialYear').select('*')
},
})
fastify.route({
url: '/objects',
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: '/objects/:id',
method: 'GET',
schema: {
params: Type.Object({
id: Type.Number(),
}),
},
async handler(req) {
// const object = knex('object').first('*').where('id', req.params.id)
// if (!object) throw new StatusError(404)
console.log(req.params.id)
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)
},
})
fastify.route({
url: '/results',
method: 'GET',
async handler() {
const years = await knex('financialYear').select('*')
const accounts = await knex('account').select('*')
return Promise.all(
years.map((year) =>
knex('account AS a')
.select('a.number', 'a.description')
.sum('t.amount as amount')
.innerJoin('transaction AS t', function () {
this.on('t.accountNumber', '=', 'a.number')
})
.innerJoin('entry AS e', function () {
this.on('t.entryId', '=', 'e.id')
})
.groupBy('a.number', 'a.description')
.where('a.number', '>=', 3000)
.where('e.financialYearId', year.id)
.orderBy('a.number')
.then((result) => ({
startDate: year.startDate,
endDate: year.endDate,
result,
})),
),
).then((years) => ({
accounts,
years,
}))
},
})
fastify.route({
url: '/results/:year',
method: 'GET',
schema: {
params: Type.Object({
year: Type.Number(),
}),
},
async handler(req) {
const year = await knex('financialYear').first('*').where('year', req.params.year)
if (!year) return null
return knex('transaction AS t')
.select('t.accountNumber', 'a.description')
.sum('t.amount as amount')
.innerJoin('account AS a', function () {
this.on('t.accountNumber', '=', 'a.number')
})
.innerJoin('entry AS e', function () {
this.on('t.entryId', '=', 'e.id')
})
.groupBy('t.accountNumber', 'a.description')
.where('t.accountNumber', '>=', 3000)
.where('e.financialYearId', year.id)
.orderBy('t.accountNumber')
},
})
fastify.route({
url: '/suppliers',
method: 'GET',
async handler(req) {
return knex('supplier').select('*').orderBy('name')
},
})
fastify.route({
url: '/suppliers/:id',
method: 'GET',
schema: {
params: Type.Object({
id: Type.Number(),
}),
},
async handler(req) {
return knex('supplier').first('*').where('id', req.params.id)
},
})
fastify.register(journals, { prefix: '/journals' })
fastify.register(objects, { prefix: '/objects' })
fastify.register(results, { prefix: '/results' })
done()
}

View File

@ -2,7 +2,7 @@ import _ from 'lodash'
import { Type, type Static, type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox'
import knex from '../../lib/knex.ts'
const apiRoutes: FastifyPluginCallbackTypebox = (fastify, _, done) => {
const accountRoutes: FastifyPluginCallbackTypebox = (fastify, _, done) => {
fastify.route({
url: '/',
method: 'GET',
@ -14,4 +14,4 @@ const apiRoutes: FastifyPluginCallbackTypebox = (fastify, _, done) => {
done()
}
export default apiRoutes
export default accountRoutes

View File

@ -0,0 +1,30 @@
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

View File

@ -0,0 +1,17 @@
import _ from 'lodash'
import { Type, type Static, type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox'
import knex from '../../lib/knex.ts'
const financialYearRoutes: FastifyPluginCallbackTypebox = (fastify, _, done) => {
fastify.route({
url: '/',
method: 'GET',
handler() {
return knex('financialYear').select('*')
},
})
done()
}
export default financialYearRoutes

View File

@ -3,7 +3,7 @@ import { Type, type FastifyPluginCallbackTypebox } from '@fastify/type-provider-
import knex from '../../lib/knex.ts'
import StatusError from '../../lib/status_error.ts'
const apiRoutes: FastifyPluginCallbackTypebox = (fastify, _, done) => {
const invoiceRoutes: FastifyPluginCallbackTypebox = (fastify, _, done) => {
fastify.route({
url: '/',
method: 'GET',
@ -124,4 +124,4 @@ const apiRoutes: FastifyPluginCallbackTypebox = (fastify, _, done) => {
done()
}
export default apiRoutes
export default invoiceRoutes

View File

@ -0,0 +1,17 @@
import _ from 'lodash'
import { Type, type Static, type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox'
import knex from '../../lib/knex.ts'
const journalRoutes: FastifyPluginCallbackTypebox = (fastify, _, done) => {
fastify.route({
url: '/',
method: 'GET',
handler() {
return knex('journal').select('*').orderBy('identifier')
},
})
done()
}
export default journalRoutes

View File

@ -0,0 +1,42 @@
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

View File

@ -0,0 +1,74 @@
import _ from 'lodash'
import { Type, type Static, type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox'
import knex from '../../lib/knex.ts'
const resultRoutes: FastifyPluginCallbackTypebox = (fastify, _, done) => {
fastify.route({
url: '/',
method: 'GET',
async handler() {
const years = await knex('financialYear').select('*')
const accounts = await knex('account').select('*')
return Promise.all(
years.map((year) =>
knex('account AS a')
.select('a.number', 'a.description')
.sum('t.amount as amount')
.innerJoin('transaction AS t', function () {
this.on('t.accountNumber', '=', 'a.number')
})
.innerJoin('entry AS e', function () {
this.on('t.entryId', '=', 'e.id')
})
.groupBy('a.number', 'a.description')
.where('a.number', '>=', 3000)
.where('e.financialYearId', year.id)
.orderBy('a.number')
.then((result) => ({
startDate: year.startDate,
endDate: year.endDate,
result,
})),
),
).then((years) => ({
accounts,
years,
}))
},
})
fastify.route({
url: '/:year',
method: 'GET',
schema: {
params: Type.Object({
year: Type.Number(),
}),
},
async handler(req) {
const year = await knex('financialYear').first('*').where('year', req.params.year)
if (!year) return null
return knex('transaction AS t')
.select('t.accountNumber', 'a.description')
.sum('t.amount as amount')
.innerJoin('account AS a', function () {
this.on('t.accountNumber', '=', 'a.number')
})
.innerJoin('entry AS e', function () {
this.on('t.entryId', '=', 'e.id')
})
.groupBy('t.accountNumber', 'a.description')
.where('t.accountNumber', '>=', 3000)
.where('e.financialYearId', year.id)
.orderBy('t.accountNumber')
},
})
done()
}
export default resultRoutes

View File

@ -0,0 +1,30 @@
import _ from 'lodash'
import { Type, type Static, type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox'
import knex from '../../lib/knex.ts'
const journalRoutes: FastifyPluginCallbackTypebox = (fastify, _, done) => {
fastify.route({
url: '/',
method: 'GET',
async handler(req) {
return knex('supplier').select('*').orderBy('name')
},
})
fastify.route({
url: '/:id',
method: 'GET',
schema: {
params: Type.Object({
id: Type.Number(),
}),
},
async handler(req) {
return knex('supplier').first('*').where('id', req.params.id)
},
})
done()
}
export default journalRoutes