11import { eq } from "drizzle-orm" ;
22import { GraphQLError } from "graphql" ;
33
4+ import { createAuthToken } from "~/authn" ;
45import { builder } from "~/builder" ;
56import {
67 PronounsEnum ,
@@ -9,6 +10,7 @@ import {
910 usersSchema ,
1011 usersToCommunitiesSchema ,
1112} from "~/datasources/db/schema" ;
13+ import { applicationError , ServiceErrors } from "~/errors" ;
1214import { UserRef } from "~/schema/shared/refs" ;
1315import { pronounsEnum } from "~/schema/user/types" ;
1416import {
@@ -156,3 +158,65 @@ builder.mutationField("updateUserRoleInCommunity", (t) =>
156158 } ,
157159 } ) ,
158160) ;
161+
162+ const retoolToken = builder . inputType ( "retoolToken" , {
163+ fields : ( t ) => ( {
164+ userEmail : t . string ( { required : true } ) ,
165+ authToken : t . string ( { required : true } ) ,
166+ } ) ,
167+ } ) ;
168+
169+ export const TokenRef = builder . objectRef < {
170+ token : string ;
171+ } > ( "TokenRef" ) ;
172+
173+ builder . mutationField ( "retoolToken" , ( t ) =>
174+ t . field ( {
175+ description : "Update a user role" ,
176+ type : TokenRef ,
177+ deprecationReason : "Not enabled" ,
178+ nullable : false ,
179+ args : {
180+ input : t . arg ( { type : retoolToken , required : true } ) ,
181+ } ,
182+ resolve : async ( root , { input } , ctx ) => {
183+ try {
184+ const { userEmail, authToken } = input ;
185+
186+ if ( authToken !== ctx . RETOOL_AUTHENTICATION_TOKEN ) {
187+ throw new Error ( "Not authorized" ) ;
188+ }
189+
190+ const user = await ctx . DB . query . usersSchema . findFirst ( {
191+ where : ( u , { eq, and } ) =>
192+ and (
193+ eq ( u . email , userEmail . trim ( ) . toLocaleLowerCase ( ) ) ,
194+ eq ( u . isRetoolEnabled , true ) ,
195+ eq ( u . isSuperAdmin , true ) ,
196+ ) ,
197+ } ) ;
198+
199+ if ( ! user ) {
200+ throw new Error ( "Not authorized" ) ;
201+ }
202+
203+ const selectedUser = selectUsersSchema . parse ( user ) ;
204+
205+ const token = await createAuthToken (
206+ selectedUser ,
207+ ctx . SUPABASE_JWT_ENCODER ,
208+ ) ;
209+
210+ return {
211+ token,
212+ } ;
213+ } catch ( e ) {
214+ throw applicationError (
215+ "Not authorized" ,
216+ ServiceErrors . FORBIDDEN ,
217+ ctx . logger ,
218+ ) ;
219+ }
220+ } ,
221+ } ) ,
222+ ) ;
0 commit comments