@@ -6,73 +6,77 @@ import { z } from 'zod';
66import { createTRPCRouter , protectedProcedure } from '../trpc' ;
77
88const preferencesSchema = z . object ( {
9- timezone : z . string ( ) . optional ( ) ,
10- dateFormat : z . string ( ) . optional ( ) ,
11- timeFormat : z . string ( ) . optional ( ) ,
9+ timezone : z . string ( ) . optional ( ) ,
10+ dateFormat : z . string ( ) . optional ( ) ,
11+ timeFormat : z . string ( ) . optional ( ) ,
1212} ) ;
1313
1414export const preferencesRouter = createTRPCRouter ( {
15- getUserPreferences : protectedProcedure . query ( async ( { ctx } ) => {
16- const user = ctx . user ;
17- if ( ! user ) throw new TRPCError ( { code : 'UNAUTHORIZED' } ) ;
15+ getUserPreferences : protectedProcedure . query ( async ( { ctx } ) => {
16+ const user = ctx . user ;
17+ if ( ! user ) {
18+ throw new TRPCError ( { code : 'UNAUTHORIZED' } ) ;
19+ }
1820
19- let preferences = await ctx . db . query . userPreferences . findFirst ( {
20- where : eq ( userPreferences . userId , user . id ) ,
21- } ) ;
21+ let preferences = await ctx . db . query . userPreferences . findFirst ( {
22+ where : eq ( userPreferences . userId , user . id ) ,
23+ } ) ;
2224
23- if ( ! preferences ) {
24- const inserted = await ctx . db
25- . insert ( userPreferences )
26- . values ( {
27- id : nanoid ( ) ,
28- userId : user . id ,
29- timezone : 'auto' ,
30- dateFormat : 'MMM D, YYYY' ,
31- timeFormat : 'h:mm a' ,
32- updatedAt : new Date ( ) . toISOString ( ) ,
33- } as any )
34- . returning ( ) ;
35- preferences = inserted [ 0 ] ;
36- }
37- return preferences ;
38- } ) ,
25+ if ( ! preferences ) {
26+ const inserted = await ctx . db
27+ . insert ( userPreferences )
28+ . values ( {
29+ id : nanoid ( ) ,
30+ userId : user . id ,
31+ timezone : 'auto' ,
32+ dateFormat : 'MMM D, YYYY' ,
33+ timeFormat : 'h:mm a' ,
34+ updatedAt : new Date ( ) . toISOString ( ) ,
35+ } as any )
36+ . returning ( ) ;
37+ preferences = inserted [ 0 ] ;
38+ }
39+ return preferences ;
40+ } ) ,
3941
40- updateUserPreferences : protectedProcedure
41- . input ( preferencesSchema )
42- . mutation ( async ( { ctx, input } ) => {
43- const user = ctx . user ;
44- if ( ! user ) throw new TRPCError ( { code : 'UNAUTHORIZED' } ) ;
42+ updateUserPreferences : protectedProcedure
43+ . input ( preferencesSchema )
44+ . mutation ( async ( { ctx, input } ) => {
45+ const user = ctx . user ;
46+ if ( ! user ) {
47+ throw new TRPCError ( { code : 'UNAUTHORIZED' } ) ;
48+ }
4549
46- let preferences = await ctx . db . query . userPreferences . findFirst ( {
47- where : eq ( userPreferences . userId , user . id ) ,
48- } ) ;
50+ let preferences = await ctx . db . query . userPreferences . findFirst ( {
51+ where : eq ( userPreferences . userId , user . id ) ,
52+ } ) ;
4953
50- if ( preferences ) {
51- const updated = await ctx . db
52- . update ( userPreferences )
53- . set ( {
54- timezone : input . timezone || preferences . timezone ,
55- dateFormat : input . dateFormat || preferences . dateFormat ,
56- timeFormat : input . timeFormat || preferences . timeFormat ,
57- updatedAt : new Date ( ) . toISOString ( ) ,
58- } as any )
59- . where ( eq ( userPreferences . userId , user . id ) )
60- . returning ( ) ;
61- preferences = updated [ 0 ] ;
62- } else {
63- const inserted = await ctx . db
64- . insert ( userPreferences )
65- . values ( {
66- id : nanoid ( ) ,
67- userId : user . id ,
68- timezone : input . timezone || 'auto' ,
69- dateFormat : input . dateFormat || 'MMM D, YYYY' ,
70- timeFormat : input . timeFormat || 'h:mm a' ,
71- updatedAt : new Date ( ) . toISOString ( ) ,
72- } as any )
73- . returning ( ) ;
74- preferences = inserted [ 0 ] ;
75- }
76- return preferences ;
77- } ) ,
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+ } as any )
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+ } as any )
77+ . returning ( ) ;
78+ preferences = inserted [ 0 ] ;
79+ }
80+ return preferences ;
81+ } ) ,
7882} ) ;
0 commit comments