feat(commissions): add customer records and kanban workflow baseline
This commit is contained in:
64
packages/db/src/commissions.test.ts
Normal file
64
packages/db/src/commissions.test.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest"
|
||||
|
||||
const { mockDb } = vi.hoisted(() => ({
|
||||
mockDb: {
|
||||
customer: {
|
||||
create: vi.fn(),
|
||||
findMany: vi.fn(),
|
||||
},
|
||||
commission: {
|
||||
create: vi.fn(),
|
||||
findMany: vi.fn(),
|
||||
update: vi.fn(),
|
||||
},
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock("./client", () => ({
|
||||
db: mockDb,
|
||||
}))
|
||||
|
||||
import { createCommission, createCustomer, updateCommissionStatus } from "./commissions"
|
||||
|
||||
describe("commissions service", () => {
|
||||
beforeEach(() => {
|
||||
for (const value of Object.values(mockDb)) {
|
||||
for (const fn of Object.values(value)) {
|
||||
if (typeof fn === "function") {
|
||||
fn.mockReset()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
it("creates customer and commission payloads", async () => {
|
||||
mockDb.customer.create.mockResolvedValue({ id: "customer-1" })
|
||||
mockDb.commission.create.mockResolvedValue({ id: "commission-1" })
|
||||
|
||||
await createCustomer({
|
||||
name: "Ada Lovelace",
|
||||
email: "ada@example.com",
|
||||
isRecurring: true,
|
||||
})
|
||||
|
||||
await createCommission({
|
||||
title: "Portrait Request",
|
||||
status: "new",
|
||||
customerId: "550e8400-e29b-41d4-a716-446655440000",
|
||||
})
|
||||
|
||||
expect(mockDb.customer.create).toHaveBeenCalledTimes(1)
|
||||
expect(mockDb.commission.create).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it("updates commission status", async () => {
|
||||
mockDb.commission.update.mockResolvedValue({ id: "commission-1", status: "done" })
|
||||
|
||||
await updateCommissionStatus({
|
||||
id: "550e8400-e29b-41d4-a716-446655440001",
|
||||
status: "done",
|
||||
})
|
||||
|
||||
expect(mockDb.commission.update).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
66
packages/db/src/commissions.ts
Normal file
66
packages/db/src/commissions.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import {
|
||||
commissionStatusSchema,
|
||||
createCommissionInputSchema,
|
||||
createCustomerInputSchema,
|
||||
updateCommissionStatusInputSchema,
|
||||
} from "@cms/content"
|
||||
|
||||
import { db } from "./client"
|
||||
|
||||
export const commissionKanbanOrder = commissionStatusSchema.options
|
||||
|
||||
export async function listCustomers(limit = 200) {
|
||||
return db.customer.findMany({
|
||||
orderBy: [{ updatedAt: "desc" }],
|
||||
take: limit,
|
||||
})
|
||||
}
|
||||
|
||||
export async function createCustomer(input: unknown) {
|
||||
const payload = createCustomerInputSchema.parse(input)
|
||||
|
||||
return db.customer.create({
|
||||
data: payload,
|
||||
})
|
||||
}
|
||||
|
||||
export async function listCommissions(limit = 300) {
|
||||
return db.commission.findMany({
|
||||
orderBy: [{ updatedAt: "desc" }],
|
||||
take: limit,
|
||||
include: {
|
||||
customer: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
isRecurring: true,
|
||||
},
|
||||
},
|
||||
assignedUser: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
username: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function createCommission(input: unknown) {
|
||||
const payload = createCommissionInputSchema.parse(input)
|
||||
|
||||
return db.commission.create({
|
||||
data: payload,
|
||||
})
|
||||
}
|
||||
|
||||
export async function updateCommissionStatus(input: unknown) {
|
||||
const payload = updateCommissionStatusInputSchema.parse(input)
|
||||
|
||||
return db.commission.update({
|
||||
where: { id: payload.id },
|
||||
data: { status: payload.status },
|
||||
})
|
||||
}
|
||||
@@ -1,4 +1,12 @@
|
||||
export { db } from "./client"
|
||||
export {
|
||||
commissionKanbanOrder,
|
||||
createCommission,
|
||||
createCustomer,
|
||||
listCommissions,
|
||||
listCustomers,
|
||||
updateCommissionStatus,
|
||||
} from "./commissions"
|
||||
export {
|
||||
attachArtworkRendition,
|
||||
createAlbum,
|
||||
|
||||
Reference in New Issue
Block a user