Add request single page
This commit is contained in:
53
src/actions/commissions/requests/getCommissionRequestById.ts
Normal file
53
src/actions/commissions/requests/getCommissionRequestById.ts
Normal file
@ -0,0 +1,53 @@
|
||||
"use server";
|
||||
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { calculatePriceRange, PriceSource } from "@/utils/commissionPricing";
|
||||
|
||||
export async function getCommissionRequestById(id: string) {
|
||||
const req = await prisma.commissionRequest.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
type: { select: { id: true, name: true } },
|
||||
option: { select: { id: true, name: true } },
|
||||
extras: { select: { id: true, name: true } },
|
||||
files: {
|
||||
orderBy: { createdAt: "asc" },
|
||||
select: {
|
||||
id: true,
|
||||
createdAt: true,
|
||||
originalFile: true,
|
||||
fileType: true,
|
||||
fileSize: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!req) return null;
|
||||
|
||||
const baseSource: PriceSource | undefined =
|
||||
req.typeId && req.optionId
|
||||
? await prisma.commissionTypeOption.findUnique({
|
||||
where: { typeId_optionId: { typeId: req.typeId, optionId: req.optionId } },
|
||||
select: { price: true, pricePercent: true, priceRange: true },
|
||||
}) ?? undefined
|
||||
: undefined;
|
||||
|
||||
const extrasSources: PriceSource[] =
|
||||
req.typeId && req.extras.length
|
||||
? await prisma.commissionTypeExtra.findMany({
|
||||
where: {
|
||||
typeId: req.typeId,
|
||||
extraId: { in: req.extras.map((e) => e.id) },
|
||||
},
|
||||
select: { price: true, pricePercent: true, priceRange: true },
|
||||
})
|
||||
: [];
|
||||
|
||||
const [min, max] = calculatePriceRange(baseSource, extrasSources);
|
||||
|
||||
return {
|
||||
...req,
|
||||
priceEstimate: { min, max },
|
||||
};
|
||||
}
|
||||
31
src/actions/commissions/requests/updateCommissionRequest.ts
Normal file
31
src/actions/commissions/requests/updateCommissionRequest.ts
Normal file
@ -0,0 +1,31 @@
|
||||
"use server";
|
||||
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { commissionStatusSchema } from "@/schemas/commissions/tableSchema";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
const updateSchema = z.object({
|
||||
id: z.string().min(1),
|
||||
status: commissionStatusSchema,
|
||||
customerName: z.string().min(1).max(200),
|
||||
customerEmail: z.string().email().max(320),
|
||||
customerSocials: z.string().max(2000).optional().nullable(),
|
||||
message: z.string().min(1).max(20_000),
|
||||
});
|
||||
|
||||
export async function updateCommissionRequest(input: z.infer<typeof updateSchema>) {
|
||||
const data = updateSchema.parse(input);
|
||||
|
||||
await prisma.commissionRequest.update({
|
||||
where: { id: data.id },
|
||||
data: {
|
||||
status: data.status,
|
||||
customerName: data.customerName,
|
||||
customerEmail: data.customerEmail,
|
||||
customerSocials: data.customerSocials ?? null,
|
||||
message: data.message,
|
||||
},
|
||||
});
|
||||
|
||||
return { ok: true };
|
||||
}
|
||||
Reference in New Issue
Block a user