@@ -8,34 +8,32 @@ const { PrismaClient } = Prisma;
88
99const prisma = new PrismaClient ( ) ;
1010
11- // console.log("resolver_repo.ts", Prisma.RepoInclude);
12-
13- function checkWriteAccess ( { repo, userId } ) {
14- console . log ( repo , userId ) ;
15- if ( repo . userId !== userId && repo . collaboratorIds . indexOf ( userId ) === - 1 ) {
16- throw new Error ( "You do not have write access to this repo." ) ;
11+ async function ensureRepoEditAccess ( { repoId, userId } ) {
12+ let repo = await prisma . repo . findFirst ( {
13+ where : {
14+ id : repoId ,
15+ OR : [
16+ { owner : { id : userId || "undefined" } } ,
17+ { collaborators : { some : { id : userId || "undefined" } } } ,
18+ ] ,
19+ } ,
20+ } ) ;
21+ if ( ! repo ) {
22+ // this might be caused by creating a pod and update it too soon before it
23+ // is created on server, which is a time sequence bug
24+ throw new Error ( "Repo not exists." ) ;
1725 }
1826}
1927
20- async function ensurePodAccess ( { id, userId, read = true } ) {
28+ async function ensurePodEditAccess ( { id, userId } ) {
2129 let pod = await prisma . pod . findFirst ( {
22- where : { id } ,
23- // HEBI: select is used to select a subset of fields
24- // select: {
25- // repo: {
26- // select: {
27- // owner: true,
28- // },
29- // },
30- // },
31- // HEBI: include is used to include additional fields
32- // Both include and select can go through relations, but they cannot be used
33- // at the same time.
34- include : {
30+ where : {
31+ id,
3532 repo : {
36- include : {
37- owner : true ,
38- } ,
33+ OR : [
34+ { owner : { id : userId || "undefined" } } ,
35+ { collaborators : { some : { id : userId || "undefined" } } } ,
36+ ] ,
3937 } ,
4038 } ,
4139 } ) ;
@@ -44,16 +42,6 @@ async function ensurePodAccess({ id, userId, read = true }) {
4442 // is created on server, which is a time sequence bug
4543 throw new Error ( "Pod not exists." ) ;
4644 }
47- // public repo can be accessed by everyone
48- // if the user is the owner or one of the collaborators, then it is ok
49- if (
50- pod . repo . owner . id !== userId &&
51- pod . repo . collaboratorIds . indexOf ( userId ) === - 1
52- ) {
53- if ( ! read || ! pod . repo . public ) {
54- throw new Error ( "You do not have access to this pod." ) ;
55- }
56- }
5745}
5846
5947export async function repos ( ) {
@@ -80,11 +68,10 @@ export async function myRepos(_, __, { userId }) {
8068
8169export async function myCollabRepos ( _ , __ , { userId } ) {
8270 if ( ! userId ) throw Error ( "Unauthenticated" ) ;
83- // console.log("myCollabRepos", userId);
8471 const repos = await prisma . repo . findMany ( {
8572 where : {
86- collaboratorIds : {
87- has : userId ,
73+ collaborators : {
74+ some : { id : userId } ,
8875 } ,
8976 } ,
9077 } ) ;
@@ -98,11 +85,12 @@ export async function repo(_, { id }, { userId }) {
9885 OR : [
9986 { id, public : true } ,
10087 { id, owner : { id : userId || "undefined" } } ,
101- { id, collaboratorIds : { has : userId || "undefined" } } ,
88+ { id, collaborators : { some : { id : userId || "undefined" } } } ,
10289 ] ,
10390 } ,
10491 include : {
10592 owner : true ,
93+ collaborators : true ,
10694 pods : {
10795 include : {
10896 children : true ,
@@ -210,25 +198,28 @@ export async function addCollaborator(_, { repoId, email }, { userId }) {
210198 id : repoId ,
211199 owner : { id : userId } ,
212200 } ,
201+ include : {
202+ collaborators : true ,
203+ } ,
213204 } ) ;
214205 if ( ! repo ) throw new Error ( "Repo not found or you are not the owner." ) ;
215206 // 2. find the user
216- const user = await prisma . user . findFirst ( {
207+ const other = await prisma . user . findFirst ( {
217208 where : {
218209 email,
219210 } ,
220211 } ) ;
221- if ( ! user ) throw new Error ( "User not found" ) ;
222- if ( user . id === userId ) throw new Error ( "You are already the owner." ) ;
223- if ( repo . collaboratorIds . indexOf ( user . id ) !== - 1 )
212+ if ( ! other ) throw new Error ( "User not found" ) ;
213+ if ( other . id === userId ) throw new Error ( "You are already the owner." ) ;
214+ if ( repo . collaborators . findIndex ( ( user ) => user . id === other . id ) !== - 1 )
224215 throw new Error ( "The user is already a collaborator." ) ;
225216 // 3. add the user to the repo
226217 const res = await prisma . repo . update ( {
227218 where : {
228219 id : repoId ,
229220 } ,
230221 data : {
231- collaboratorIds : { push : user . id } ,
222+ collaborators : { connect : { id : other . id } } ,
232223 } ,
233224 } ) ;
234225 return true ;
@@ -237,20 +228,13 @@ export async function addCollaborator(_, { repoId, email }, { userId }) {
237228export async function addPod ( _ , { repoId, parent, index, input } , { userId } ) {
238229 // make sure the repo is writable by this user
239230 if ( ! userId ) throw new Error ( "Not authenticated." ) ;
240- // 1. find the repo
241- const repo = await prisma . repo . findFirst ( {
242- where : {
243- id : repoId ,
244- } ,
245- } ) ;
246- if ( ! repo ) throw new Error ( "Repo not found" ) ;
231+ await ensureRepoEditAccess ( { repoId, userId } ) ;
247232
248- checkWriteAccess ( { repo, userId } ) ;
249233 // update all other records
250234 await prisma . pod . updateMany ( {
251235 where : {
252236 repo : {
253- id : repo . id ,
237+ id : repoId ,
254238 } ,
255239 index : {
256240 gte : index ,
@@ -279,7 +263,7 @@ export async function addPod(_, { repoId, parent, index, input }, { userId }) {
279263 index,
280264 repo : {
281265 connect : {
282- id : repo . id ,
266+ id : repoId ,
283267 } ,
284268 } ,
285269 parent :
@@ -298,7 +282,7 @@ export async function addPod(_, { repoId, parent, index, input }, { userId }) {
298282
299283export async function updatePod ( _ , { id, input } , { userId } ) {
300284 if ( ! userId ) throw new Error ( "Not authenticated." ) ;
301- await ensurePodAccess ( { id, userId, read : false } ) ;
285+ await ensurePodEditAccess ( { id, userId } ) ;
302286 const pod = await prisma . pod . update ( {
303287 where : {
304288 id,
@@ -318,13 +302,12 @@ export async function updatePod(_, { id, input }, { userId }) {
318302 } ,
319303 } ,
320304 } ) ;
321- console . log ( "Updated pod" , pod ) ;
322305 return true ;
323306}
324307
325308export async function deletePod ( _ , { id, toDelete } , { userId } ) {
326309 if ( ! userId ) throw new Error ( "Not authenticated." ) ;
327- await ensurePodAccess ( { id, userId, read : false } ) ;
310+ await ensurePodEditAccess ( { id, userId } ) ;
328311 // find all children of this ID
329312 // FIXME how to ensure atomic
330313 // 1. find the parent of this node
0 commit comments