1- import { project } from "../db-schema" ;
1+ import { project , user } from "../db-schema" ;
22import { builder , notEmpty } from "./utils" ;
33import { v4 as uuidv4 } from "uuid" ;
44import { ProjectConfigurationSchema , SUPPORTED_CHAINS } from "utils" ;
@@ -24,6 +24,7 @@ builder.node(Project, {
2424 ProjectConfigurationSchema . parse ( obj . configuration ) . chain ,
2525 } ) ,
2626 } ) ,
27+
2728 loadOne : ( id , { db } ) =>
2829 db
2930 . select ( )
@@ -42,6 +43,9 @@ builder.queryField("projects", (t) => {
4243 return t . connection ( {
4344 type : Project ,
4445 description : "List of projects" ,
46+ authScopes : {
47+ isAuthenticated : true ,
48+ } ,
4549 resolve : async ( _parent , { first, after } , { db } ) => {
4650 const limit = first ?? 10 ;
4751
@@ -123,18 +127,26 @@ builder.relayMutationField(
123127 } ) ,
124128 } ,
125129 {
130+ authScopes : {
131+ isAuthenticated : true ,
132+ } ,
126133 resolve : async (
127134 _parent ,
128135 { input } ,
129136 {
130137 db,
138+ authUserId,
131139 svix,
132140 SVIX_TOKEN ,
133141 SF_TOKEN ,
134142 GUILD_ADMIN_TOKEN ,
135143 SUBSTREAM_LISTENER_HOST ,
136144 } ,
137145 ) => {
146+ if ( ! authUserId ) {
147+ throw new Error ( "User not authenticated" ) ;
148+ }
149+
138150 const configuration = await ProjectConfigurationSchema . safeParseAsync ( {
139151 ...input ,
140152 webhookUrl : input . webhookUrl ?. toString ( ) ,
@@ -183,14 +195,24 @@ builder.relayMutationField(
183195 throw new Error ( "Failed to register webhook" ) ;
184196 }
185197
198+ // For now we just have one organization that is user's default.
199+ // TODO: rework this in future to handle multiple organizations
200+ const org = await db . query . usersToOrgs . findFirst ( {
201+ where : eq ( user . id , authUserId ) ,
202+ } ) ;
203+
204+ if ( ! org ) {
205+ throw new Error ( "User is not part of any organization" ) ;
206+ }
207+
186208 const createdProj = await db
187209 . insert ( project )
188210 . values ( {
189211 name : input . name ,
190212 configuration : configuration . data ,
191- creator : 1 , // TODO: get from the context user
213+ creator : authUserId ,
192214 id,
193- organization : 1 , // TODO: get from the context user
215+ organization : org . orgId ,
194216 } )
195217 . returning ( ) ;
196218
0 commit comments