650 lines
23 KiB
TypeScript
650 lines
23 KiB
TypeScript
import {
|
|
commissionKanbanOrder,
|
|
createCommission,
|
|
createCustomer,
|
|
db,
|
|
listArtworks,
|
|
listCommissions,
|
|
listCustomers,
|
|
updateCommission,
|
|
updateCommissionStatus,
|
|
} from "@cms/db"
|
|
import { Button } from "@cms/ui/button"
|
|
import { revalidatePath } from "next/cache"
|
|
import { redirect } from "next/navigation"
|
|
|
|
import { AdminShell } from "@/components/admin-shell"
|
|
import { requirePermissionForRoute } from "@/lib/route-guards"
|
|
|
|
export const dynamic = "force-dynamic"
|
|
|
|
type SearchParamsInput = Record<string, string | string[] | undefined>
|
|
|
|
function readFirstValue(value: string | string[] | undefined): string | null {
|
|
if (Array.isArray(value)) {
|
|
return value[0] ?? null
|
|
}
|
|
|
|
return value ?? null
|
|
}
|
|
|
|
function readInputString(formData: FormData, field: string): string {
|
|
const value = formData.get(field)
|
|
return typeof value === "string" ? value.trim() : ""
|
|
}
|
|
|
|
function readNullableString(formData: FormData, field: string): string | null {
|
|
const value = readInputString(formData, field)
|
|
return value.length > 0 ? value : null
|
|
}
|
|
|
|
function readNullableNumber(formData: FormData, field: string): number | null {
|
|
const value = readInputString(formData, field)
|
|
|
|
if (!value) {
|
|
return null
|
|
}
|
|
|
|
const parsed = Number.parseFloat(value)
|
|
|
|
if (!Number.isFinite(parsed)) {
|
|
return null
|
|
}
|
|
|
|
return parsed
|
|
}
|
|
|
|
function readNullableDate(formData: FormData, field: string): Date | null {
|
|
const value = readInputString(formData, field)
|
|
|
|
if (!value) {
|
|
return null
|
|
}
|
|
|
|
const parsed = new Date(value)
|
|
|
|
if (Number.isNaN(parsed.getTime())) {
|
|
return null
|
|
}
|
|
|
|
return parsed
|
|
}
|
|
|
|
function readUuidList(formData: FormData, field: string): string[] {
|
|
const raw = readInputString(formData, field)
|
|
|
|
if (!raw) {
|
|
return []
|
|
}
|
|
|
|
return raw
|
|
.split(",")
|
|
.map((entry) => entry.trim())
|
|
.filter((entry) => entry.length > 0)
|
|
}
|
|
|
|
function redirectWithState(params: { notice?: string; error?: string }) {
|
|
const query = new URLSearchParams()
|
|
|
|
if (params.notice) {
|
|
query.set("notice", params.notice)
|
|
}
|
|
|
|
if (params.error) {
|
|
query.set("error", params.error)
|
|
}
|
|
|
|
const value = query.toString()
|
|
redirect(value ? `/commissions?${value}` : "/commissions")
|
|
}
|
|
|
|
async function createCustomerAction(formData: FormData) {
|
|
"use server"
|
|
|
|
await requirePermissionForRoute({
|
|
nextPath: "/commissions",
|
|
permission: "commissions:write",
|
|
scope: "own",
|
|
})
|
|
|
|
try {
|
|
await createCustomer({
|
|
name: readInputString(formData, "name"),
|
|
email: readNullableString(formData, "email"),
|
|
phone: readNullableString(formData, "phone"),
|
|
instagram: readNullableString(formData, "instagram"),
|
|
notes: readNullableString(formData, "notes"),
|
|
isRecurring: readInputString(formData, "isRecurring") === "true",
|
|
})
|
|
} catch {
|
|
redirectWithState({ error: "Failed to create customer." })
|
|
}
|
|
|
|
revalidatePath("/commissions")
|
|
redirectWithState({ notice: "Customer created." })
|
|
}
|
|
|
|
async function createCommissionAction(formData: FormData) {
|
|
"use server"
|
|
|
|
await requirePermissionForRoute({
|
|
nextPath: "/commissions",
|
|
permission: "commissions:write",
|
|
scope: "own",
|
|
})
|
|
|
|
try {
|
|
await createCommission({
|
|
title: readInputString(formData, "title"),
|
|
description: readNullableString(formData, "description"),
|
|
status: readInputString(formData, "status"),
|
|
customerId: readNullableString(formData, "customerId"),
|
|
assignedUserId: readNullableString(formData, "assignedUserId"),
|
|
linkedArtworkIds: readUuidList(formData, "linkedArtworkIds"),
|
|
budgetMin: readNullableNumber(formData, "budgetMin"),
|
|
budgetMax: readNullableNumber(formData, "budgetMax"),
|
|
dueAt: readNullableDate(formData, "dueAt"),
|
|
})
|
|
} catch {
|
|
redirectWithState({ error: "Failed to create commission." })
|
|
}
|
|
|
|
revalidatePath("/commissions")
|
|
redirectWithState({ notice: "Commission created." })
|
|
}
|
|
|
|
async function updateCommissionAction(formData: FormData) {
|
|
"use server"
|
|
|
|
await requirePermissionForRoute({
|
|
nextPath: "/commissions",
|
|
permission: "commissions:write",
|
|
scope: "own",
|
|
})
|
|
|
|
try {
|
|
await updateCommission({
|
|
id: readInputString(formData, "id"),
|
|
title: readInputString(formData, "title"),
|
|
description: readNullableString(formData, "description"),
|
|
customerId: readNullableString(formData, "customerId"),
|
|
assignedUserId: readNullableString(formData, "assignedUserId"),
|
|
linkedArtworkIds: readUuidList(formData, "linkedArtworkIds"),
|
|
budgetMin: readNullableNumber(formData, "budgetMin"),
|
|
budgetMax: readNullableNumber(formData, "budgetMax"),
|
|
dueAt: readNullableDate(formData, "dueAt"),
|
|
})
|
|
} catch {
|
|
redirectWithState({ error: "Failed to update commission details." })
|
|
}
|
|
|
|
revalidatePath("/commissions")
|
|
redirectWithState({ notice: "Commission updated." })
|
|
}
|
|
|
|
async function updateCommissionStatusAction(formData: FormData) {
|
|
"use server"
|
|
|
|
await requirePermissionForRoute({
|
|
nextPath: "/commissions",
|
|
permission: "commissions:transition",
|
|
scope: "own",
|
|
})
|
|
|
|
try {
|
|
await updateCommissionStatus({
|
|
id: readInputString(formData, "id"),
|
|
status: readInputString(formData, "status"),
|
|
})
|
|
} catch {
|
|
redirectWithState({ error: "Failed to transition commission." })
|
|
}
|
|
|
|
revalidatePath("/commissions")
|
|
redirectWithState({ notice: "Commission status updated." })
|
|
}
|
|
|
|
function formatDate(value: Date | null) {
|
|
if (!value) {
|
|
return "-"
|
|
}
|
|
|
|
return value.toLocaleDateString("en-US")
|
|
}
|
|
|
|
function formatDateInput(value: Date | null) {
|
|
if (!value) {
|
|
return ""
|
|
}
|
|
|
|
return value.toISOString().slice(0, 10)
|
|
}
|
|
|
|
export default async function CommissionsManagementPage({
|
|
searchParams,
|
|
}: {
|
|
searchParams: Promise<SearchParamsInput>
|
|
}) {
|
|
const role = await requirePermissionForRoute({
|
|
nextPath: "/commissions",
|
|
permission: "commissions:read",
|
|
scope: "own",
|
|
})
|
|
|
|
const [resolvedSearchParams, customers, commissions, assignees, artworks] = await Promise.all([
|
|
searchParams,
|
|
listCustomers(200),
|
|
listCommissions(300),
|
|
db.user.findMany({
|
|
where: {
|
|
isBanned: false,
|
|
},
|
|
orderBy: [{ createdAt: "asc" }],
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
username: true,
|
|
},
|
|
}),
|
|
listArtworks(300),
|
|
])
|
|
|
|
const notice = readFirstValue(resolvedSearchParams.notice)
|
|
const error = readFirstValue(resolvedSearchParams.error)
|
|
|
|
return (
|
|
<AdminShell
|
|
role={role}
|
|
activePath="/commissions"
|
|
badge="Admin App"
|
|
title="Commissions"
|
|
description="Manage customers and commission requests with kanban-style status transitions."
|
|
>
|
|
{notice ? (
|
|
<section className="rounded-xl border border-emerald-300 bg-emerald-50 px-4 py-3 text-sm text-emerald-800">
|
|
{notice}
|
|
</section>
|
|
) : null}
|
|
|
|
{error ? (
|
|
<section className="rounded-xl border border-red-300 bg-red-50 px-4 py-3 text-sm text-red-800">
|
|
{error}
|
|
</section>
|
|
) : null}
|
|
|
|
<section className="grid gap-4 xl:grid-cols-2">
|
|
<article className="rounded-xl border border-neutral-200 p-6">
|
|
<h2 className="text-xl font-medium">Create Customer</h2>
|
|
<form action={createCustomerAction} className="mt-4 space-y-3">
|
|
<label className="space-y-1">
|
|
<span className="text-xs text-neutral-600">Name</span>
|
|
<input
|
|
name="name"
|
|
required
|
|
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
|
/>
|
|
</label>
|
|
<div className="grid gap-3 md:grid-cols-2">
|
|
<label className="space-y-1">
|
|
<span className="text-xs text-neutral-600">Email</span>
|
|
<input
|
|
name="email"
|
|
type="email"
|
|
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
|
/>
|
|
</label>
|
|
<label className="space-y-1">
|
|
<span className="text-xs text-neutral-600">Phone</span>
|
|
<input
|
|
name="phone"
|
|
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
|
/>
|
|
</label>
|
|
</div>
|
|
<label className="space-y-1">
|
|
<span className="text-xs text-neutral-600">Instagram</span>
|
|
<input
|
|
name="instagram"
|
|
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
|
/>
|
|
</label>
|
|
<label className="space-y-1">
|
|
<span className="text-xs text-neutral-600">Notes</span>
|
|
<textarea
|
|
name="notes"
|
|
rows={3}
|
|
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
|
/>
|
|
</label>
|
|
<label className="inline-flex items-center gap-2 text-sm text-neutral-700">
|
|
<input name="isRecurring" type="checkbox" value="true" className="size-4" />
|
|
Recurring customer
|
|
</label>
|
|
<Button type="submit">Create customer</Button>
|
|
</form>
|
|
</article>
|
|
|
|
<article className="rounded-xl border border-neutral-200 p-6">
|
|
<h2 className="text-xl font-medium">Create Commission</h2>
|
|
<form action={createCommissionAction} className="mt-4 space-y-3">
|
|
<label className="space-y-1">
|
|
<span className="text-xs text-neutral-600">Title</span>
|
|
<input
|
|
name="title"
|
|
required
|
|
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
|
/>
|
|
</label>
|
|
<label className="space-y-1">
|
|
<span className="text-xs text-neutral-600">Description</span>
|
|
<textarea
|
|
name="description"
|
|
rows={3}
|
|
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
|
/>
|
|
</label>
|
|
<div className="grid gap-3 md:grid-cols-2">
|
|
<label className="space-y-1">
|
|
<span className="text-xs text-neutral-600">Status</span>
|
|
<select
|
|
name="status"
|
|
defaultValue="new"
|
|
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
|
>
|
|
{commissionKanbanOrder.map((status) => (
|
|
<option key={status} value={status}>
|
|
{status}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<label className="space-y-1">
|
|
<span className="text-xs text-neutral-600">Customer</span>
|
|
<select
|
|
name="customerId"
|
|
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
|
>
|
|
<option value="">(none)</option>
|
|
{customers.map((customer) => (
|
|
<option key={customer.id} value={customer.id}>
|
|
{customer.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
</div>
|
|
<div className="grid gap-3 md:grid-cols-3">
|
|
<label className="space-y-1">
|
|
<span className="text-xs text-neutral-600">Assigned user</span>
|
|
<select
|
|
name="assignedUserId"
|
|
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
|
>
|
|
<option value="">(none)</option>
|
|
{assignees.map((assignee) => (
|
|
<option key={assignee.id} value={assignee.id}>
|
|
{assignee.name} @{assignee.username ?? "no-user"}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<label className="space-y-1">
|
|
<span className="text-xs text-neutral-600">Budget min</span>
|
|
<input
|
|
name="budgetMin"
|
|
type="number"
|
|
min={0}
|
|
step="0.01"
|
|
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
|
/>
|
|
</label>
|
|
<label className="space-y-1">
|
|
<span className="text-xs text-neutral-600">Budget max</span>
|
|
<input
|
|
name="budgetMax"
|
|
type="number"
|
|
min={0}
|
|
step="0.01"
|
|
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
|
/>
|
|
</label>
|
|
</div>
|
|
<label className="space-y-1">
|
|
<span className="text-xs text-neutral-600">Due date</span>
|
|
<input
|
|
name="dueAt"
|
|
type="date"
|
|
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
|
/>
|
|
</label>
|
|
<label className="space-y-1">
|
|
<span className="text-xs text-neutral-600">Linked artwork IDs (comma separated)</span>
|
|
<textarea
|
|
name="linkedArtworkIds"
|
|
rows={2}
|
|
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
|
/>
|
|
</label>
|
|
<Button type="submit">Create commission</Button>
|
|
</form>
|
|
</article>
|
|
</section>
|
|
|
|
<section className="space-y-4">
|
|
<h2 className="text-xl font-medium">Kanban Board</h2>
|
|
<div className="grid gap-3 xl:grid-cols-6">
|
|
{commissionKanbanOrder.map((status) => {
|
|
const items = commissions.filter((commission) => commission.status === status)
|
|
|
|
return (
|
|
<article
|
|
key={status}
|
|
className="rounded-xl border border-neutral-200 bg-neutral-50 p-3"
|
|
>
|
|
<header className="mb-3 flex items-center justify-between">
|
|
<h3 className="text-sm font-semibold uppercase tracking-wide text-neutral-700">
|
|
{status}
|
|
</h3>
|
|
<span className="text-xs text-neutral-500">{items.length}</span>
|
|
</header>
|
|
|
|
<div className="space-y-2">
|
|
{items.length === 0 ? (
|
|
<p className="text-xs text-neutral-500">No commissions</p>
|
|
) : (
|
|
items.map((commission) => (
|
|
<form
|
|
key={commission.id}
|
|
action={updateCommissionStatusAction}
|
|
className="rounded border border-neutral-200 bg-white p-2"
|
|
>
|
|
<input type="hidden" name="id" value={commission.id} />
|
|
<div className="space-y-1">
|
|
<p className="text-sm font-medium">{commission.title}</p>
|
|
<p className="text-xs text-neutral-600">
|
|
{commission.customer?.name ?? "No customer"}
|
|
</p>
|
|
<p className="text-xs text-neutral-500">
|
|
Assignee: {commission.assignedUser?.name ?? "none"}
|
|
</p>
|
|
<p className="text-xs text-neutral-500">
|
|
Due: {formatDate(commission.dueAt)}
|
|
</p>
|
|
</div>
|
|
<div className="mt-2 flex items-center gap-2">
|
|
<select
|
|
name="status"
|
|
defaultValue={commission.status}
|
|
className="w-full rounded border border-neutral-300 px-2 py-1 text-xs"
|
|
>
|
|
{commissionKanbanOrder.map((value) => (
|
|
<option key={value} value={value}>
|
|
{value}
|
|
</option>
|
|
))}
|
|
</select>
|
|
<button
|
|
type="submit"
|
|
className="rounded border border-neutral-300 px-2 py-1 text-xs"
|
|
>
|
|
Move
|
|
</button>
|
|
</div>
|
|
<details className="mt-2 rounded border border-neutral-200 p-2 text-xs">
|
|
<summary className="cursor-pointer text-neutral-700">
|
|
Edit details
|
|
</summary>
|
|
<form action={updateCommissionAction} className="mt-2 space-y-2">
|
|
<input type="hidden" name="id" value={commission.id} />
|
|
<input
|
|
name="title"
|
|
defaultValue={commission.title}
|
|
className="w-full rounded border border-neutral-300 px-2 py-1"
|
|
/>
|
|
<textarea
|
|
name="description"
|
|
rows={2}
|
|
defaultValue={commission.description ?? ""}
|
|
className="w-full rounded border border-neutral-300 px-2 py-1"
|
|
/>
|
|
<select
|
|
name="customerId"
|
|
defaultValue={commission.customerId ?? ""}
|
|
className="w-full rounded border border-neutral-300 px-2 py-1"
|
|
>
|
|
<option value="">(no customer)</option>
|
|
{customers.map((customer) => (
|
|
<option
|
|
key={`${commission.id}-customer-${customer.id}`}
|
|
value={customer.id}
|
|
>
|
|
{customer.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
<select
|
|
name="assignedUserId"
|
|
defaultValue={commission.assignedUserId ?? ""}
|
|
className="w-full rounded border border-neutral-300 px-2 py-1"
|
|
>
|
|
<option value="">(no assignee)</option>
|
|
{assignees.map((assignee) => (
|
|
<option
|
|
key={`${commission.id}-assignee-${assignee.id}`}
|
|
value={assignee.id}
|
|
>
|
|
{assignee.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
<div className="grid grid-cols-2 gap-2">
|
|
<input
|
|
name="budgetMin"
|
|
type="number"
|
|
min={0}
|
|
step="0.01"
|
|
defaultValue={commission.budgetMin ?? ""}
|
|
placeholder="Budget min"
|
|
className="rounded border border-neutral-300 px-2 py-1"
|
|
/>
|
|
<input
|
|
name="budgetMax"
|
|
type="number"
|
|
min={0}
|
|
step="0.01"
|
|
defaultValue={commission.budgetMax ?? ""}
|
|
placeholder="Budget max"
|
|
className="rounded border border-neutral-300 px-2 py-1"
|
|
/>
|
|
</div>
|
|
<input
|
|
name="dueAt"
|
|
type="date"
|
|
defaultValue={formatDateInput(commission.dueAt)}
|
|
className="w-full rounded border border-neutral-300 px-2 py-1"
|
|
/>
|
|
<textarea
|
|
name="linkedArtworkIds"
|
|
rows={2}
|
|
defaultValue={commission.linkedArtworkIds.join(",")}
|
|
placeholder="Artwork IDs"
|
|
className="w-full rounded border border-neutral-300 px-2 py-1"
|
|
/>
|
|
<button
|
|
type="submit"
|
|
className="rounded border border-neutral-300 px-2 py-1 text-xs"
|
|
>
|
|
Save details
|
|
</button>
|
|
</form>
|
|
</details>
|
|
{commission.linkedArtworkIds.length > 0 ? (
|
|
<p className="mt-2 text-[11px] text-neutral-500">
|
|
Linked artworks: {commission.linkedArtworkIds.length}
|
|
</p>
|
|
) : null}
|
|
</form>
|
|
))
|
|
)}
|
|
</div>
|
|
</article>
|
|
)
|
|
})}
|
|
</div>
|
|
</section>
|
|
|
|
<section className="rounded-xl border border-neutral-200 p-6">
|
|
<h2 className="text-xl font-medium">Customers</h2>
|
|
<div className="mt-4 overflow-x-auto">
|
|
<table className="min-w-full text-left text-sm">
|
|
<thead className="text-xs uppercase tracking-wide text-neutral-500">
|
|
<tr>
|
|
<th className="py-2 pr-4">Name</th>
|
|
<th className="py-2 pr-4">Email</th>
|
|
<th className="py-2 pr-4">Phone</th>
|
|
<th className="py-2 pr-4">Recurring</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{customers.length === 0 ? (
|
|
<tr>
|
|
<td className="py-3 text-neutral-500" colSpan={4}>
|
|
No customers yet.
|
|
</td>
|
|
</tr>
|
|
) : (
|
|
customers.map((customer) => (
|
|
<tr key={customer.id} className="border-t border-neutral-200">
|
|
<td className="py-3 pr-4">{customer.name}</td>
|
|
<td className="py-3 pr-4 text-neutral-600">{customer.email ?? "-"}</td>
|
|
<td className="py-3 pr-4 text-neutral-600">{customer.phone ?? "-"}</td>
|
|
<td className="py-3 pr-4">{customer.isRecurring ? "yes" : "no"}</td>
|
|
</tr>
|
|
))
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</section>
|
|
|
|
<section className="rounded-xl border border-neutral-200 p-6">
|
|
<h2 className="text-xl font-medium">Artwork Reference</h2>
|
|
<p className="mt-1 text-sm text-neutral-600">
|
|
Use these IDs when linking artworks to commissions.
|
|
</p>
|
|
<div className="mt-3 max-h-64 overflow-auto rounded border border-neutral-200 p-3 text-xs">
|
|
{artworks.length === 0 ? (
|
|
<p className="text-neutral-500">No artworks available.</p>
|
|
) : (
|
|
artworks.map((artwork) => (
|
|
<p key={artwork.id} className="font-mono text-neutral-700">
|
|
{artwork.id} - {artwork.title}
|
|
</p>
|
|
))
|
|
)}
|
|
</div>
|
|
</section>
|
|
</AdminShell>
|
|
)
|
|
}
|