All files / src/app/api/user/notification-preferences route.ts

0% Statements 0/156
100% Branches 0/0
0% Functions 0/1
0% Lines 0/156

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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157                                                                                                                                                                                                                                                                                                                         
export const dynamic = "force-dynamic";

import { NextRequest, NextResponse } from 'next/server';
import { Session } from "next-auth";
import { prisma } from "@/lib/prisma";
import { z } from "zod";
import {
  withAuth,
  withErrorHandling,
  successResponse,
  ApiError,
  ApiSuccessResponse,
  ApiErrorResponse } from "@/lib/api";
import { RouteContext } from "@/lib/api/middleware";

// Schema for notification preferences (matching actual Prisma model)
const preferencesSchema = z.object({
  email: z.boolean().optional(),
  push: z.boolean().optional(),
  sms: z.boolean().optional(),
  orders: z.boolean().optional(),
  reviews: z.boolean().optional(),
  stock: z.boolean().optional(),
  promotions: z.boolean().optional()});

export type NotificationPreferences = z.infer<typeof preferencesSchema>;

// Default preferences for new users
const defaultPreferences: NotificationPreferences = {
  email: true,
  push: true,
  sms: false,
  orders: true,
  reviews: true,
  stock: true,
  promotions: false};

/**
 * GET /api/user/notification-preferences
 * Get current user's notification preferences
 */
async function handleGet(
  _request: NextRequest,
  _context: RouteContext | undefined,
  session: Session
): Promise<NextResponse<ApiSuccessResponse<NotificationPreferences> | ApiErrorResponse>> {
  const user = await prisma.user.findUnique({
    where: { email: session.user.email! },
    include: { notificationPreferences: true }});

  if (!user) {
    throw ApiError.notFound("User");
  }

  // Merge with defaults to ensure all fields are present
  const userPrefs = user.notificationPreferences;
  const preferences: NotificationPreferences = {
    email: userPrefs?.email ?? defaultPreferences.email,
    push: userPrefs?.push ?? defaultPreferences.push,
    sms: userPrefs?.sms ?? defaultPreferences.sms,
    orders: userPrefs?.orders ?? defaultPreferences.orders,
    reviews: userPrefs?.reviews ?? defaultPreferences.reviews,
    stock: userPrefs?.stock ?? defaultPreferences.stock,
    promotions: userPrefs?.promotions ?? defaultPreferences.promotions};

  return successResponse(preferences);
}

export const GET = withErrorHandling(withAuth(handleGet));

/**
 * PUT /api/user/notification-preferences
 * Update user's notification preferences
 */
async function handlePut(
  request: NextRequest,
  _context: RouteContext | undefined,
  session: Session
): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
  const body = await request.json();
  const validationResult = preferencesSchema.safeParse(body);

  if (!validationResult.success) {
    throw ApiError.validation("Invalid preferences", validationResult.error.issues);
  }

  const user = await prisma.user.findUnique({
    where: { email: session.user.email! },
    select: { id: true }});

  if (!user) {
    throw ApiError.notFound("User");
  }

  // Upsert notification preferences
  const updatedPreferences = await prisma.notificationPreference.upsert({
    where: { userId: user.id },
    create: {
      userId: user.id,
      ...defaultPreferences,
      ...validationResult.data},
    update: validationResult.data});

  return successResponse({
    ...updatedPreferences,
    message: "Notification preferences updated successfully"});
}

export const PUT = withErrorHandling(withAuth(handlePut));

/**
 * PATCH /api/user/notification-preferences
 * Toggle a single notification preference
 */
async function handlePatch(
  request: NextRequest,
  _context: RouteContext | undefined,
  session: Session
): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
  const body = await request.json();
  const { key, value } = body;

  if (!key || typeof value !== "boolean") {
    throw ApiError.badRequest("Invalid request. Provide key and boolean value.");
  }

  // Validate key
  const validKeys = Object.keys(defaultPreferences);
  if (!validKeys.includes(key)) {
    throw ApiError.badRequest(`Invalid preference key. Valid keys: ${validKeys.join(", ")}`);
  }

  const user = await prisma.user.findUnique({
    where: { email: session.user.email! },
    select: { id: true }});

  if (!user) {
    throw ApiError.notFound("User");
  }

  // Upsert with the single field update
  const updatedPreferences = await prisma.notificationPreference.upsert({
    where: { userId: user.id },
    create: {
      userId: user.id,
      ...defaultPreferences,
      [key]: value},
    update: {
      [key]: value}});

  return successResponse({
    ...updatedPreferences,
    message: `${key} preference updated to ${value}`});
}

export const PATCH = withErrorHandling(withAuth(handlePatch));