Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | export const dynamic = "force-dynamic"; import { NextRequest, NextResponse } from 'next/server'; import { } from "next-auth"; import { prisma } from "@/lib/prisma"; import { z } from "zod"; import { withAdmin, withErrorHandling, successResponse, ApiError, ApiSuccessResponse, ApiErrorResponse } from "@/lib/api"; import { } from "@/lib/api/middleware"; const UpdateSettingsSchema = z.object({ defaultReferrerReward: z .object({ type: z.enum(["points", "credit", "discount"]), value: z.number(), discountType: z.enum(["percentage", "fixed"]).optional() }) .optional(), defaultRefereeReward: z .object({ type: z.enum(["points", "credit", "discount"]), value: z.number(), discountType: z.enum(["percentage", "fixed"]).optional() }) .optional(), minPurchase: z.number().nullable().optional(), isActive: z.boolean().optional() }); /** * GET /api/admin/referrals/settings * Get current referral program settings (default/active program) */ async function handleGet(): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { // Get the active/default referral program let program = await prisma.referralProgram.findFirst({ where: { isActive: true }, orderBy: { createdAt: "desc" } }); // If no active program, get any program if (!program) { program = await prisma.referralProgram.findFirst({ orderBy: { createdAt: "desc" } }); } if (!program) { // Return default settings if no program exists return successResponse({ id: null, name: "Default Program", defaultReferrerReward: { type: "points", value: 500 }, defaultRefereeReward: { type: "discount", discountType: "percentage", value: 10 }, minPurchase: null, isActive: false, hasProgram: false }); } return successResponse({ id: program.id, name: program.name, defaultReferrerReward: program.referrerReward, defaultRefereeReward: program.refereeReward, minPurchase: program.minPurchase, isActive: program.isActive, hasProgram: true, createdAt: program.createdAt, updatedAt: program.updatedAt }); } /** * PATCH /api/admin/referrals/settings * Update referral program settings */ async function handlePatch(request: NextRequest): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const body = await request.json(); const result = UpdateSettingsSchema.safeParse(body); if (!result.success) { throw ApiError.validation("Invalid settings data", result.error.issues); } const validatedData = result.data; // Get or create default program let program = await prisma.referralProgram.findFirst({ where: { isActive: true }, orderBy: { createdAt: "desc" } }); if (!program) { // Create a default program program = await prisma.referralProgram.create({ data: { name: "Default Referral Program", referrerReward: validatedData.defaultReferrerReward || { type: "points", value: 500 }, refereeReward: validatedData.defaultRefereeReward || { type: "discount", discountType: "percentage", value: 10 }, minPurchase: validatedData.minPurchase, isActive: validatedData.isActive ?? true } }); } else { // Update existing program program = await prisma.referralProgram.update({ where: { id: program.id }, data: { referrerReward: validatedData.defaultReferrerReward, refereeReward: validatedData.defaultRefereeReward, minPurchase: validatedData.minPurchase, isActive: validatedData.isActive } }); } return successResponse({ id: program.id, name: program.name, defaultReferrerReward: program.referrerReward, defaultRefereeReward: program.refereeReward, minPurchase: program.minPurchase, isActive: program.isActive }); } export const GET = withErrorHandling(withAdmin(handleGet)); export const PATCH = withErrorHandling(withAdmin(handlePatch)); |