11import { userPreferences } from '@databuddy/db' ;
2- import { TRPCError } from '@trpc/server' ;
32import { eq } from 'drizzle-orm' ;
43import { nanoid } from 'nanoid' ;
54import { z } from 'zod' ;
@@ -11,26 +10,25 @@ const preferencesSchema = z.object({
1110 timeFormat : z . string ( ) . optional ( ) ,
1211} ) ;
1312
13+ const defaultPreferences = {
14+ timezone : 'auto' ,
15+ dateFormat : 'MMM D, YYYY' ,
16+ timeFormat : 'h:mm a' ,
17+ } as const ;
18+
1419export const preferencesRouter = createTRPCRouter ( {
1520 getUserPreferences : protectedProcedure . query ( async ( { ctx } ) => {
16- const user = ctx . user ;
17- if ( ! user ) {
18- throw new TRPCError ( { code : 'UNAUTHORIZED' } ) ;
19- }
20-
2121 let preferences = await ctx . db . query . userPreferences . findFirst ( {
22- where : eq ( userPreferences . userId , user . id ) ,
22+ where : eq ( userPreferences . userId , ctx . user . id ) ,
2323 } ) ;
2424
2525 if ( ! preferences ) {
2626 const inserted = await ctx . db
2727 . insert ( userPreferences )
2828 . values ( {
2929 id : nanoid ( ) ,
30- userId : user . id ,
31- timezone : 'auto' ,
32- dateFormat : 'MMM D, YYYY' ,
33- timeFormat : 'h:mm a' ,
30+ userId : ctx . user . id ,
31+ ...defaultPreferences ,
3432 updatedAt : new Date ( ) . toISOString ( ) ,
3533 } )
3634 . returning ( ) ;
@@ -42,41 +40,29 @@ export const preferencesRouter = createTRPCRouter({
4240 updateUserPreferences : protectedProcedure
4341 . input ( preferencesSchema )
4442 . mutation ( async ( { ctx, input } ) => {
45- const user = ctx . user ;
46- if ( ! user ) {
47- throw new TRPCError ( { code : 'UNAUTHORIZED' } ) ;
48- }
43+ const now = new Date ( ) . toISOString ( ) ;
4944
50- let preferences = await ctx . db . query . userPreferences . findFirst ( {
51- where : eq ( userPreferences . userId , user . id ) ,
52- } ) ;
45+ const result = await ctx . db
46+ . insert ( userPreferences )
47+ . values ( {
48+ id : nanoid ( ) ,
49+ userId : ctx . user . id ,
50+ timezone : input . timezone ?? defaultPreferences . timezone ,
51+ dateFormat : input . dateFormat ?? defaultPreferences . dateFormat ,
52+ timeFormat : input . timeFormat ?? defaultPreferences . timeFormat ,
53+ updatedAt : now ,
54+ } )
55+ . onConflictDoUpdate ( {
56+ target : userPreferences . userId ,
57+ set : {
58+ timezone : input . timezone ,
59+ dateFormat : input . dateFormat ,
60+ timeFormat : input . timeFormat ,
61+ updatedAt : now ,
62+ } ,
63+ } )
64+ . returning ( ) ;
5365
54- if ( preferences ) {
55- const updated = await ctx . db
56- . update ( userPreferences )
57- . set ( {
58- timezone : input . timezone || preferences . timezone ,
59- dateFormat : input . dateFormat || preferences . dateFormat ,
60- timeFormat : input . timeFormat || preferences . timeFormat ,
61- updatedAt : new Date ( ) . toISOString ( ) ,
62- } )
63- . where ( eq ( userPreferences . userId , user . id ) )
64- . returning ( ) ;
65- preferences = updated [ 0 ] ;
66- } else {
67- const inserted = await ctx . db
68- . insert ( userPreferences )
69- . values ( {
70- id : nanoid ( ) ,
71- userId : user . id ,
72- timezone : input . timezone || 'auto' ,
73- dateFormat : input . dateFormat || 'MMM D, YYYY' ,
74- timeFormat : input . timeFormat || 'h:mm a' ,
75- updatedAt : new Date ( ) . toISOString ( ) ,
76- } )
77- . returning ( ) ;
78- preferences = inserted [ 0 ] ;
79- }
80- return preferences ;
66+ return result [ 0 ] ;
8167 } ) ,
8268} ) ;
0 commit comments