Add lookup tables, payment methods, tax exemptions, and processor link APIs
Replace unit_status and item_condition pgEnums with company-scoped lookup tables that support custom values. Add account_payment_method table, tax_exemption table with approve/revoke workflow, and CRUD routes for processor links. Validate inventory unit status/condition against lookup tables at service layer.
This commit is contained in:
@@ -5,8 +5,20 @@ import {
|
||||
MemberCreateSchema,
|
||||
MemberUpdateSchema,
|
||||
PaginationSchema,
|
||||
ProcessorLinkCreateSchema,
|
||||
ProcessorLinkUpdateSchema,
|
||||
PaymentMethodCreateSchema,
|
||||
PaymentMethodUpdateSchema,
|
||||
TaxExemptionCreateSchema,
|
||||
TaxExemptionUpdateSchema,
|
||||
} from '@forte/shared/schemas'
|
||||
import { AccountService, MemberService } from '../../services/account.service.js'
|
||||
import {
|
||||
AccountService,
|
||||
MemberService,
|
||||
ProcessorLinkService,
|
||||
PaymentMethodService,
|
||||
TaxExemptionService,
|
||||
} from '../../services/account.service.js'
|
||||
|
||||
export const accountRoutes: FastifyPluginAsync = async (app) => {
|
||||
// --- Accounts ---
|
||||
@@ -94,4 +106,137 @@ export const accountRoutes: FastifyPluginAsync = async (app) => {
|
||||
if (!member) return reply.status(404).send({ error: { message: 'Member not found', statusCode: 404 } })
|
||||
return reply.send(member)
|
||||
})
|
||||
|
||||
// --- Processor Links ---
|
||||
|
||||
app.post('/accounts/:accountId/processor-links', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const { accountId } = request.params as { accountId: string }
|
||||
const parsed = ProcessorLinkCreateSchema.safeParse({ ...(request.body as object), accountId })
|
||||
if (!parsed.success) {
|
||||
return reply.status(400).send({ error: { message: 'Validation failed', details: parsed.error.flatten(), statusCode: 400 } })
|
||||
}
|
||||
const link = await ProcessorLinkService.create(app.db, request.companyId, parsed.data)
|
||||
return reply.status(201).send(link)
|
||||
})
|
||||
|
||||
app.get('/accounts/:accountId/processor-links', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const { accountId } = request.params as { accountId: string }
|
||||
const links = await ProcessorLinkService.listByAccount(app.db, request.companyId, accountId)
|
||||
return reply.send({ data: links })
|
||||
})
|
||||
|
||||
app.patch('/processor-links/:id', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const parsed = ProcessorLinkUpdateSchema.safeParse(request.body)
|
||||
if (!parsed.success) {
|
||||
return reply.status(400).send({ error: { message: 'Validation failed', details: parsed.error.flatten(), statusCode: 400 } })
|
||||
}
|
||||
const link = await ProcessorLinkService.update(app.db, request.companyId, id, parsed.data)
|
||||
if (!link) return reply.status(404).send({ error: { message: 'Processor link not found', statusCode: 404 } })
|
||||
return reply.send(link)
|
||||
})
|
||||
|
||||
app.delete('/processor-links/:id', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const link = await ProcessorLinkService.delete(app.db, request.companyId, id)
|
||||
if (!link) return reply.status(404).send({ error: { message: 'Processor link not found', statusCode: 404 } })
|
||||
return reply.send(link)
|
||||
})
|
||||
|
||||
// --- Payment Methods ---
|
||||
|
||||
app.post('/accounts/:accountId/payment-methods', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const { accountId } = request.params as { accountId: string }
|
||||
const parsed = PaymentMethodCreateSchema.safeParse({ ...(request.body as object), accountId })
|
||||
if (!parsed.success) {
|
||||
return reply.status(400).send({ error: { message: 'Validation failed', details: parsed.error.flatten(), statusCode: 400 } })
|
||||
}
|
||||
const method = await PaymentMethodService.create(app.db, request.companyId, parsed.data)
|
||||
return reply.status(201).send(method)
|
||||
})
|
||||
|
||||
app.get('/accounts/:accountId/payment-methods', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const { accountId } = request.params as { accountId: string }
|
||||
const methods = await PaymentMethodService.listByAccount(app.db, request.companyId, accountId)
|
||||
return reply.send({ data: methods })
|
||||
})
|
||||
|
||||
app.get('/payment-methods/:id', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const method = await PaymentMethodService.getById(app.db, request.companyId, id)
|
||||
if (!method) return reply.status(404).send({ error: { message: 'Payment method not found', statusCode: 404 } })
|
||||
return reply.send(method)
|
||||
})
|
||||
|
||||
app.patch('/payment-methods/:id', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const parsed = PaymentMethodUpdateSchema.safeParse(request.body)
|
||||
if (!parsed.success) {
|
||||
return reply.status(400).send({ error: { message: 'Validation failed', details: parsed.error.flatten(), statusCode: 400 } })
|
||||
}
|
||||
const method = await PaymentMethodService.update(app.db, request.companyId, id, parsed.data)
|
||||
if (!method) return reply.status(404).send({ error: { message: 'Payment method not found', statusCode: 404 } })
|
||||
return reply.send(method)
|
||||
})
|
||||
|
||||
app.delete('/payment-methods/:id', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const method = await PaymentMethodService.delete(app.db, request.companyId, id)
|
||||
if (!method) return reply.status(404).send({ error: { message: 'Payment method not found', statusCode: 404 } })
|
||||
return reply.send(method)
|
||||
})
|
||||
|
||||
// --- Tax Exemptions ---
|
||||
|
||||
app.post('/accounts/:accountId/tax-exemptions', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const { accountId } = request.params as { accountId: string }
|
||||
const parsed = TaxExemptionCreateSchema.safeParse({ ...(request.body as object), accountId })
|
||||
if (!parsed.success) {
|
||||
return reply.status(400).send({ error: { message: 'Validation failed', details: parsed.error.flatten(), statusCode: 400 } })
|
||||
}
|
||||
const exemption = await TaxExemptionService.create(app.db, request.companyId, parsed.data)
|
||||
return reply.status(201).send(exemption)
|
||||
})
|
||||
|
||||
app.get('/accounts/:accountId/tax-exemptions', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const { accountId } = request.params as { accountId: string }
|
||||
const exemptions = await TaxExemptionService.listByAccount(app.db, request.companyId, accountId)
|
||||
return reply.send({ data: exemptions })
|
||||
})
|
||||
|
||||
app.get('/tax-exemptions/:id', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const exemption = await TaxExemptionService.getById(app.db, request.companyId, id)
|
||||
if (!exemption) return reply.status(404).send({ error: { message: 'Tax exemption not found', statusCode: 404 } })
|
||||
return reply.send(exemption)
|
||||
})
|
||||
|
||||
app.patch('/tax-exemptions/:id', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const parsed = TaxExemptionUpdateSchema.safeParse(request.body)
|
||||
if (!parsed.success) {
|
||||
return reply.status(400).send({ error: { message: 'Validation failed', details: parsed.error.flatten(), statusCode: 400 } })
|
||||
}
|
||||
const exemption = await TaxExemptionService.update(app.db, request.companyId, id, parsed.data)
|
||||
if (!exemption) return reply.status(404).send({ error: { message: 'Tax exemption not found', statusCode: 404 } })
|
||||
return reply.send(exemption)
|
||||
})
|
||||
|
||||
app.post('/tax-exemptions/:id/approve', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const exemption = await TaxExemptionService.approve(app.db, request.companyId, id, request.user.id)
|
||||
if (!exemption) return reply.status(404).send({ error: { message: 'Tax exemption not found', statusCode: 404 } })
|
||||
return reply.send(exemption)
|
||||
})
|
||||
|
||||
app.post('/tax-exemptions/:id/revoke', { preHandler: [app.authenticate] }, async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const { reason } = (request.body as { reason?: string }) ?? {}
|
||||
if (!reason) {
|
||||
return reply.status(400).send({ error: { message: 'Reason is required to revoke a tax exemption', statusCode: 400 } })
|
||||
}
|
||||
const exemption = await TaxExemptionService.revoke(app.db, request.companyId, id, request.user.id, reason)
|
||||
if (!exemption) return reply.status(404).send({ error: { message: 'Tax exemption not found', statusCode: 404 } })
|
||||
return reply.send(exemption)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user