This commit is contained in:
Your Name committed 2026-06-07 17:17:01 +07:00
1 parent d325cd8763
commit c6bf2f70ac
45 files changed
+5195 -338

No files matched your search

+46 -2
View File
@@ -1,9 +1,21 @@
import { Elysia } from "elysia";
import { Elysia, t } from "elysia";
import prisma from "../lib/prisma";
export const customersRoute = new Elysia({ prefix: "/customers" })
.get("/", async () => {
.get("/", async ({ query }) => {
const search = query.search as string | undefined;
const whereClause = search ? {
OR: [
{ displayName: { contains: search, mode: "insensitive" as any } },
{ email: { contains: search, mode: "insensitive" as any } },
{ phone: { contains: search, mode: "insensitive" as any } },
{ lineUserId: { contains: search, mode: "insensitive" as any } }
]
} : {};
return await prisma.customer.findMany({
where: whereClause,
orderBy: { createdAt: 'desc' },
include: {
_count: {
@@ -22,4 +34,36 @@ export const customersRoute = new Elysia({ prefix: "/customers" })
return { error: "Customer not found" };
}
return customer;
})
.patch("/:id/points", async ({ params: { id }, body, set }) => {
try {
const { pointsDelta, reason } = body as { pointsDelta: number, reason?: string };
const customer = await prisma.customer.findUnique({
where: { id: Number(id) }
});
if (!customer) {
set.status = 404;
return { error: "Customer not found" };
}
const newPoints = Math.max(0, customer.points + pointsDelta);
const updatedCustomer = await prisma.customer.update({
where: { id: Number(id) },
data: { points: newPoints }
});
return updatedCustomer;
} catch (err) {
console.error("[Customers] Adjust Points Error:", err);
set.status = 500;
return { error: "Failed to adjust points" };
}
}, {
body: t.Object({
pointsDelta: t.Number(),
reason: t.Optional(t.String())
})
});