@@ -4,14 +4,17 @@ import slugify from "slugify";
44
55import { builder } from "~/builder" ;
66import {
7+ insertUserDataSchema ,
78 insertUsersSchema ,
89 PronounsEnum ,
910 selectUsersSchema ,
11+ updateUserDataSchema ,
1012 updateUsersSchema ,
1113 userDataSchema ,
1214 usersSchema ,
1315 usersToCommunitiesSchema ,
1416} from "~/datasources/db/schema" ;
17+ import { applicationError , ServiceErrors } from "~/errors" ;
1518import { UserRef } from "~/schema/shared/refs" ;
1619import { pronounsEnum } from "~/schema/user/types" ;
1720import { usersFetcher } from "~/schema/user/userFetcher" ;
@@ -182,51 +185,60 @@ builder.mutationField("updateMyUserData", (t) =>
182185 input : t . arg ( { type : updateUserDataInput , required : true } ) ,
183186 } ,
184187 resolve : async ( root , { input } , ctx ) => {
185- try {
186- const {
187- countryOfResidence,
188- city,
189- worksInOrganization,
190- organizationName,
191- roleInOrganization,
192- } = input ;
188+ const {
189+ countryOfResidence,
190+ city,
191+ worksInOrganization,
192+ organizationName,
193+ roleInOrganization,
194+ } = input ;
193195
194- const USER = ctx . USER ;
196+ const USER = ctx . USER ;
195197
196- if ( ! USER ) {
197- throw new Error ( "User not found" ) ;
198- }
198+ if ( ! USER ) {
199+ throw new Error ( "User not found" ) ;
200+ }
201+
202+ const userData = insertUserDataSchema . parse ( {
203+ userId : USER . id ,
204+ countryOfResidence,
205+ city,
206+ worksInOrganization,
207+ organizationName,
208+ roleInOrganization,
209+ } ) ;
199210
200- await ctx . DB . insert ( userDataSchema )
201- . values ( {
202- userId : USER . id ,
211+ const updatedUsers = await ctx . DB . insert ( userDataSchema )
212+ . values ( userData )
213+ . onConflictDoUpdate ( {
214+ target : userDataSchema . userId ,
215+ set : updateUserDataSchema . parse ( {
203216 countryOfResidence,
204217 city,
205218 worksInOrganization,
206219 organizationName,
207220 roleInOrganization,
208- } )
209- . onConflictDoUpdate ( {
210- target : userDataSchema . userId ,
211- set : {
212- countryOfResidence,
213- city,
214- worksInOrganization,
215- organizationName,
216- roleInOrganization,
217- } ,
218- } ) ;
219-
220- const user = await ctx . DB . query . usersSchema . findFirst ( {
221- where : ( u , { eq } ) => eq ( u . id , USER . id ) ,
222- } ) ;
221+ } ) ,
222+ } )
223+ . returning ( ) ;
224+ const updatedUser = updatedUsers [ 0 ] ;
223225
224- return selectUsersSchema . parse ( user ) ;
225- } catch ( e ) {
226- throw new GraphQLError (
227- e instanceof Error ? e . message : "Unknown error" ,
226+ if ( ! updatedUser ) {
227+ throw applicationError (
228+ "Could not update the user information" ,
229+ ServiceErrors . NOT_FOUND ,
230+ ctx . logger ,
228231 ) ;
229232 }
233+
234+ const user = await usersFetcher . searchUsers ( {
235+ DB : ctx . DB ,
236+ search : {
237+ userIds : [ USER . id ] ,
238+ } ,
239+ } ) ;
240+
241+ return selectUsersSchema . parse ( user [ 0 ] ) ;
230242 } ,
231243 } ) ,
232244) ;
0 commit comments