77 updateStudentInfo ,
88 updateDriverShifts ,
99 updatePreferredName ,
10+ updateNotificationSettings ,
1011} from "@/server/db/actions/UserAction" ;
1112import { findInvalidAccommodations } from "@/server/db/actions/AccommodationAction" ;
1213import { HTTP_STATUS_CODE } from "@/utils/consts" ;
@@ -94,6 +95,26 @@ const driverShiftsPatchSchema = z
9495 } )
9596 . strict ( ) ;
9697
98+ const notificationSettingsPatchSchema = z
99+ . object ( {
100+ settings : z
101+ . object ( {
102+ notifications : z
103+ . object ( {
104+ dailySummary : z . boolean ( ) . optional ( ) ,
105+ driverAssigned : z . boolean ( ) . optional ( ) ,
106+ driverEnRoute : z . boolean ( ) . optional ( ) ,
107+ rideCancelled : z . boolean ( ) . optional ( ) ,
108+ rideAssigned : z . boolean ( ) . optional ( ) ,
109+ rideCompleted : z . boolean ( ) . optional ( ) ,
110+ } )
111+ . optional ( ) ,
112+ } )
113+ . passthrough ( )
114+ . optional ( ) ,
115+ } )
116+ . passthrough ( ) ;
117+
97118export async function PATCH (
98119 req : NextRequest ,
99120 { params } : { params : Promise < { id : string } > } ,
@@ -149,9 +170,33 @@ export async function PATCH(
149170 }
150171
151172 try {
173+ const parsedNotif = notificationSettingsPatchSchema . safeParse ( body ) ;
174+ let notificationUpdated ;
175+ if ( parsedNotif . success && parsedNotif . data . settings ?. notifications ) {
176+ notificationUpdated = await updateNotificationSettings ( id , {
177+ notifications : parsedNotif . data . settings . notifications ,
178+ } ) ;
179+
180+ if ( ! notificationUpdated ) {
181+ return NextResponse . json (
182+ { error : "Update failed" } ,
183+ { status : HTTP_STATUS_CODE . INTERNAL_SERVER_ERROR } ,
184+ ) ;
185+ }
186+ }
187+
152188 let updated ;
189+ const bodyWithoutSettings =
190+ typeof body === "object" && body !== null
191+ ? Object . fromEntries (
192+ Object . entries ( body as Record < string , unknown > ) . filter (
193+ ( [ key ] ) => key !== "settings" ,
194+ ) ,
195+ )
196+ : { } ;
197+
153198 if ( user . type === "Student" ) {
154- const parsed = studentInfoPatchSchema . safeParse ( body ) ;
199+ const parsed = studentInfoPatchSchema . safeParse ( bodyWithoutSettings ) ;
155200 if ( ! parsed . success ) {
156201 return NextResponse . json ( parsed . error . format ( ) , {
157202 status : HTTP_STATUS_CODE . BAD_REQUEST ,
@@ -200,24 +245,26 @@ export async function PATCH(
200245 }
201246 } else if ( user . type === "Driver" ) {
202247 // For drivers, only admins can update shifts
203- if ( ! isAdmin ) {
248+ if ( ! isAdmin && bodyWithoutSettings . shifts !== undefined ) {
204249 return NextResponse . json (
205250 { error : "Only admins can update driver shifts" } ,
206251 { status : HTTP_STATUS_CODE . FORBIDDEN } ,
207252 ) ;
208253 }
209- const parsed = driverShiftsPatchSchema . safeParse ( body ) ;
254+ const parsed = driverShiftsPatchSchema . safeParse ( bodyWithoutSettings ) ;
210255 if ( ! parsed . success ) {
211256 return NextResponse . json ( parsed . error . format ( ) , {
212257 status : HTTP_STATUS_CODE . BAD_REQUEST ,
213258 } ) ;
214259 }
215- updated = await updateDriverShifts ( id , parsed . data . shifts ?? [ ] ) ;
216- if ( ! updated ) {
217- return NextResponse . json (
218- { error : "User not found or update failed" } ,
219- { status : HTTP_STATUS_CODE . NOT_FOUND } ,
220- ) ;
260+ if ( parsed . data . shifts !== undefined ) {
261+ updated = await updateDriverShifts ( id , parsed . data . shifts ?? [ ] ) ;
262+ if ( ! updated ) {
263+ return NextResponse . json (
264+ { error : "User not found or update failed" } ,
265+ { status : HTTP_STATUS_CODE . NOT_FOUND } ,
266+ ) ;
267+ }
221268 }
222269 } else {
223270 return NextResponse . json (
@@ -226,9 +273,18 @@ export async function PATCH(
226273 ) ;
227274 }
228275
229- const userObj = updated as Record < string , unknown > & {
230- _id : { toString ( ) : string } ;
231- } ;
276+ const finalUser = ( updated ?? notificationUpdated ) as
277+ | ( Record < string , unknown > & { _id : { toString ( ) : string } } )
278+ | null ;
279+
280+ if ( ! finalUser ) {
281+ return NextResponse . json (
282+ { error : "User update failed" } ,
283+ { status : HTTP_STATUS_CODE . BAD_REQUEST } ,
284+ ) ;
285+ }
286+
287+ const userObj = finalUser ;
232288 return NextResponse . json (
233289 { ...userObj , _id : userObj . _id . toString ( ) } ,
234290 { status : HTTP_STATUS_CODE . OK } ,
0 commit comments