Add accounts, members, and processor-agnostic payment linking
- account table (billing entity, soft-delete, company-scoped) - member table (people on an account, is_minor from DOB) - account_processor_link table (maps accounts to any payment processor — stripe, global_payments — instead of stripe_customer_id directly on account) - Full CRUD routes + search (name, email, phone, account_number) - Member routes nested under accounts with isMinor auto-calculation - Zod validation schemas in @forte/shared - 19 tests passing
This commit is contained in:
148
packages/backend/src/routes/v1/accounts.ts
Normal file
148
packages/backend/src/routes/v1/accounts.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
import type { FastifyPluginAsync } from 'fastify'
|
||||
import {
|
||||
AccountCreateSchema,
|
||||
AccountUpdateSchema,
|
||||
MemberCreateSchema,
|
||||
MemberUpdateSchema,
|
||||
AccountSearchSchema,
|
||||
} from '@forte/shared/schemas'
|
||||
import { AccountService, MemberService } from '../../services/account.service.js'
|
||||
|
||||
export const accountRoutes: FastifyPluginAsync = async (app) => {
|
||||
// --- Accounts ---
|
||||
|
||||
app.post(
|
||||
'/accounts',
|
||||
{ preHandler: [app.authenticate] },
|
||||
async (request, reply) => {
|
||||
const parsed = AccountCreateSchema.safeParse(request.body)
|
||||
if (!parsed.success) {
|
||||
return reply.status(400).send({ error: { message: 'Validation failed', details: parsed.error.flatten(), statusCode: 400 } })
|
||||
}
|
||||
const account = await AccountService.create(app.db, request.companyId, parsed.data)
|
||||
return reply.status(201).send(account)
|
||||
},
|
||||
)
|
||||
|
||||
app.get(
|
||||
'/accounts',
|
||||
{ preHandler: [app.authenticate] },
|
||||
async (request, reply) => {
|
||||
const accounts = await AccountService.list(app.db, request.companyId)
|
||||
return reply.send(accounts)
|
||||
},
|
||||
)
|
||||
|
||||
app.get(
|
||||
'/accounts/search',
|
||||
{ preHandler: [app.authenticate] },
|
||||
async (request, reply) => {
|
||||
const parsed = AccountSearchSchema.safeParse(request.query)
|
||||
if (!parsed.success) {
|
||||
return reply.status(400).send({ error: { message: 'Query parameter q is required', statusCode: 400 } })
|
||||
}
|
||||
const results = await AccountService.search(app.db, request.companyId, parsed.data.q)
|
||||
return reply.send(results)
|
||||
},
|
||||
)
|
||||
|
||||
app.get(
|
||||
'/accounts/:id',
|
||||
{ preHandler: [app.authenticate] },
|
||||
async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const account = await AccountService.getById(app.db, request.companyId, id)
|
||||
if (!account) return reply.status(404).send({ error: { message: 'Account not found', statusCode: 404 } })
|
||||
return reply.send(account)
|
||||
},
|
||||
)
|
||||
|
||||
app.patch(
|
||||
'/accounts/:id',
|
||||
{ preHandler: [app.authenticate] },
|
||||
async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const parsed = AccountUpdateSchema.safeParse(request.body)
|
||||
if (!parsed.success) {
|
||||
return reply.status(400).send({ error: { message: 'Validation failed', details: parsed.error.flatten(), statusCode: 400 } })
|
||||
}
|
||||
const account = await AccountService.update(app.db, request.companyId, id, parsed.data)
|
||||
if (!account) return reply.status(404).send({ error: { message: 'Account not found', statusCode: 404 } })
|
||||
return reply.send(account)
|
||||
},
|
||||
)
|
||||
|
||||
app.delete(
|
||||
'/accounts/:id',
|
||||
{ preHandler: [app.authenticate] },
|
||||
async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const account = await AccountService.softDelete(app.db, request.companyId, id)
|
||||
if (!account) return reply.status(404).send({ error: { message: 'Account not found', statusCode: 404 } })
|
||||
return reply.send(account)
|
||||
},
|
||||
)
|
||||
|
||||
// --- Members ---
|
||||
|
||||
app.post(
|
||||
'/accounts/:accountId/members',
|
||||
{ preHandler: [app.authenticate] },
|
||||
async (request, reply) => {
|
||||
const { accountId } = request.params as { accountId: string }
|
||||
const parsed = MemberCreateSchema.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 member = await MemberService.create(app.db, request.companyId, parsed.data)
|
||||
return reply.status(201).send(member)
|
||||
},
|
||||
)
|
||||
|
||||
app.get(
|
||||
'/accounts/:accountId/members',
|
||||
{ preHandler: [app.authenticate] },
|
||||
async (request, reply) => {
|
||||
const { accountId } = request.params as { accountId: string }
|
||||
const membersList = await MemberService.listByAccount(app.db, request.companyId, accountId)
|
||||
return reply.send(membersList)
|
||||
},
|
||||
)
|
||||
|
||||
app.get(
|
||||
'/members/:id',
|
||||
{ preHandler: [app.authenticate] },
|
||||
async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const member = await MemberService.getById(app.db, request.companyId, id)
|
||||
if (!member) return reply.status(404).send({ error: { message: 'Member not found', statusCode: 404 } })
|
||||
return reply.send(member)
|
||||
},
|
||||
)
|
||||
|
||||
app.patch(
|
||||
'/members/:id',
|
||||
{ preHandler: [app.authenticate] },
|
||||
async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const parsed = MemberUpdateSchema.safeParse(request.body)
|
||||
if (!parsed.success) {
|
||||
return reply.status(400).send({ error: { message: 'Validation failed', details: parsed.error.flatten(), statusCode: 400 } })
|
||||
}
|
||||
const member = await MemberService.update(app.db, request.companyId, id, parsed.data)
|
||||
if (!member) return reply.status(404).send({ error: { message: 'Member not found', statusCode: 404 } })
|
||||
return reply.send(member)
|
||||
},
|
||||
)
|
||||
|
||||
app.delete(
|
||||
'/members/:id',
|
||||
{ preHandler: [app.authenticate] },
|
||||
async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const member = await MemberService.delete(app.db, request.companyId, id)
|
||||
if (!member) return reply.status(404).send({ error: { message: 'Member not found', statusCode: 404 } })
|
||||
return reply.send(member)
|
||||
},
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user