brf/server/routes/api/suppliers.ts
2025-12-09 12:00:39 +01:00

54 lines
1.2 KiB
TypeScript

import _ from 'lodash'
import { Type, 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() {
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)
},
})
fastify.route({
url: '/merge',
method: 'POST',
schema: {
body: Type.Object({
ids: Type.Array(Type.Number()),
}),
},
async handler(req) {
const suppliers = await knex('supplier').select('*').whereIn('id', req.body.ids)
const trx = await knex.transaction()
await trx('invoice').update('supplier_id', req.body.ids[0]).whereIn('supplierId', req.body.ids.slice(1))
await trx('supplier').delete().whereIn('id', req.body.ids.slice(1))
// 556744-4301
trx.commit()
return suppliers
},
})
done()
}
export default journalRoutes