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:
302
packages/backend/src/routes/v1/accounts.test.ts
Normal file
302
packages/backend/src/routes/v1/accounts.test.ts
Normal file
@@ -0,0 +1,302 @@
|
||||
import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'bun:test'
|
||||
import type { FastifyInstance } from 'fastify'
|
||||
import {
|
||||
createTestApp,
|
||||
cleanDb,
|
||||
seedTestCompany,
|
||||
registerAndLogin,
|
||||
TEST_COMPANY_ID,
|
||||
} from '../../test/helpers.js'
|
||||
|
||||
describe('Account routes', () => {
|
||||
let app: FastifyInstance
|
||||
let token: string
|
||||
|
||||
beforeAll(async () => {
|
||||
app = await createTestApp()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await cleanDb(app)
|
||||
await seedTestCompany(app)
|
||||
const auth = await registerAndLogin(app)
|
||||
token = auth.token
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close()
|
||||
})
|
||||
|
||||
describe('POST /v1/accounts', () => {
|
||||
it('creates an account', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/accounts',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: {
|
||||
name: 'Smith Family',
|
||||
email: 'smith@example.com',
|
||||
phone: '512-555-1234',
|
||||
billingMode: 'consolidated',
|
||||
},
|
||||
})
|
||||
|
||||
expect(response.statusCode).toBe(201)
|
||||
const body = response.json()
|
||||
expect(body.name).toBe('Smith Family')
|
||||
expect(body.email).toBe('smith@example.com')
|
||||
expect(body.companyId).toBe(TEST_COMPANY_ID)
|
||||
expect(body.billingMode).toBe('consolidated')
|
||||
})
|
||||
|
||||
it('rejects missing name', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/accounts',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { email: 'noname@test.com' },
|
||||
})
|
||||
|
||||
expect(response.statusCode).toBe(400)
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /v1/accounts', () => {
|
||||
it('lists accounts for the company', async () => {
|
||||
await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/accounts',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { name: 'Account One' },
|
||||
})
|
||||
await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/accounts',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { name: 'Account Two' },
|
||||
})
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/v1/accounts',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
})
|
||||
|
||||
expect(response.statusCode).toBe(200)
|
||||
const body = response.json()
|
||||
expect(body.length).toBe(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /v1/accounts/search', () => {
|
||||
it('searches by name', async () => {
|
||||
await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/accounts',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { name: 'Johnson Family', phone: '555-9999' },
|
||||
})
|
||||
await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/accounts',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { name: 'Williams Music' },
|
||||
})
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/v1/accounts/search?q=johnson',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
})
|
||||
|
||||
expect(response.statusCode).toBe(200)
|
||||
const body = response.json()
|
||||
expect(body.length).toBe(1)
|
||||
expect(body[0].name).toBe('Johnson Family')
|
||||
})
|
||||
|
||||
it('searches by phone', async () => {
|
||||
await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/accounts',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { name: 'Phone Test', phone: '512-867-5309' },
|
||||
})
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/v1/accounts/search?q=867-5309',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
})
|
||||
|
||||
expect(response.statusCode).toBe(200)
|
||||
expect(response.json().length).toBe(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('PATCH /v1/accounts/:id', () => {
|
||||
it('updates an account', async () => {
|
||||
const createRes = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/accounts',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { name: 'Original Name' },
|
||||
})
|
||||
const id = createRes.json().id
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'PATCH',
|
||||
url: `/v1/accounts/${id}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { name: 'Updated Name' },
|
||||
})
|
||||
|
||||
expect(response.statusCode).toBe(200)
|
||||
expect(response.json().name).toBe('Updated Name')
|
||||
})
|
||||
})
|
||||
|
||||
describe('DELETE /v1/accounts/:id', () => {
|
||||
it('soft-deletes an account', async () => {
|
||||
const createRes = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/accounts',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { name: 'To Delete' },
|
||||
})
|
||||
const id = createRes.json().id
|
||||
|
||||
const delRes = await app.inject({
|
||||
method: 'DELETE',
|
||||
url: `/v1/accounts/${id}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
})
|
||||
expect(delRes.statusCode).toBe(200)
|
||||
expect(delRes.json().isActive).toBe(false)
|
||||
|
||||
// Should not appear in list
|
||||
const listRes = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/v1/accounts',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
})
|
||||
expect(listRes.json().length).toBe(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('Member routes', () => {
|
||||
let app: FastifyInstance
|
||||
let token: string
|
||||
let accountId: string
|
||||
|
||||
beforeAll(async () => {
|
||||
app = await createTestApp()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await cleanDb(app)
|
||||
await seedTestCompany(app)
|
||||
const auth = await registerAndLogin(app, { email: `member-test-${Date.now()}@test.com` })
|
||||
token = auth.token
|
||||
|
||||
const accountRes = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/accounts',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { name: 'Test Family' },
|
||||
})
|
||||
accountId = accountRes.json().id
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close()
|
||||
})
|
||||
|
||||
describe('POST /v1/accounts/:accountId/members', () => {
|
||||
it('creates a member', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: `/v1/accounts/${accountId}/members`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: {
|
||||
firstName: 'Emma',
|
||||
lastName: 'Chen',
|
||||
dateOfBirth: '2015-03-15',
|
||||
email: 'emma@test.com',
|
||||
},
|
||||
})
|
||||
|
||||
expect(response.statusCode).toBe(201)
|
||||
const body = response.json()
|
||||
expect(body.firstName).toBe('Emma')
|
||||
expect(body.lastName).toBe('Chen')
|
||||
expect(body.isMinor).toBe(true)
|
||||
expect(body.accountId).toBe(accountId)
|
||||
})
|
||||
|
||||
it('marks adult members as not minor', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: `/v1/accounts/${accountId}/members`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: {
|
||||
firstName: 'John',
|
||||
lastName: 'Chen',
|
||||
dateOfBirth: '1985-06-20',
|
||||
},
|
||||
})
|
||||
|
||||
expect(response.statusCode).toBe(201)
|
||||
expect(response.json().isMinor).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /v1/accounts/:accountId/members', () => {
|
||||
it('lists members for an account', async () => {
|
||||
await app.inject({
|
||||
method: 'POST',
|
||||
url: `/v1/accounts/${accountId}/members`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { firstName: 'Child', lastName: 'One' },
|
||||
})
|
||||
await app.inject({
|
||||
method: 'POST',
|
||||
url: `/v1/accounts/${accountId}/members`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { firstName: 'Child', lastName: 'Two' },
|
||||
})
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: `/v1/accounts/${accountId}/members`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
})
|
||||
|
||||
expect(response.statusCode).toBe(200)
|
||||
expect(response.json().length).toBe(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe('PATCH /members/:id', () => {
|
||||
it('updates a member and recalculates isMinor', async () => {
|
||||
const createRes = await app.inject({
|
||||
method: 'POST',
|
||||
url: `/v1/accounts/${accountId}/members`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { firstName: 'Kid', lastName: 'Test', dateOfBirth: '2015-01-01' },
|
||||
})
|
||||
const memberId = createRes.json().id
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'PATCH',
|
||||
url: `/v1/members/${memberId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { dateOfBirth: '1980-01-01' },
|
||||
})
|
||||
|
||||
expect(response.statusCode).toBe(200)
|
||||
expect(response.json().isMinor).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
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