@@ -3,6 +3,8 @@ const { PrismaClient } = Prisma;
33
44const prisma = new PrismaClient ( ) ;
55
6+ // console.log("resolver_repo.ts", Prisma.RepoInclude);
7+
68async function ensurePodAccess ( { id, userId } ) {
79 let pod = await prisma . pod . findFirst ( {
810 where : { id } ,
@@ -30,7 +32,9 @@ async function ensurePodAccess({ id, userId }) {
3032 // is created on server, which is a time sequence bug
3133 throw new Error ( "Pod not exists." ) ;
3234 }
33- if ( pod . repo . owner . id !== userId ) {
35+ // public repo can be accessed by everyone
36+ // if the user is the owner or one of the collaborators, then it is ok
37+ if ( ! pod . repo . public && pod . repo . owner . id !== userId && pod . repo . collaboratorIds . indexOf ( userId ) === - 1 ) {
3438 throw new Error ( "You do not have access to this pod." ) ;
3539 }
3640}
@@ -57,12 +61,27 @@ export async function myRepos(_, __, { userId }) {
5761 return repos ;
5862}
5963
60- export async function repo ( _ , { id } , { userId } ) {
64+ export async function myCollabRepos ( _ , __ , { userId} ) {
6165 if ( ! userId ) throw Error ( "Unauthenticated" ) ;
62- const repo = await prisma . repo . findFirst ( {
66+ // console.log("myCollabRepos", userId);
67+ const repos = await prisma . repo . findMany ( {
6368 where : {
64- id,
65- owner : { id : userId } ,
69+ public : false ,
70+ collaboratorIds : {
71+ has :userId ,
72+ } ,
73+ } ,
74+ } ) ;
75+ return repos ;
76+ }
77+
78+ export async function repo ( _ , { id } , { userId } ) {
79+ const repo = await prisma . repo . findFirst ( {
80+ where : { OR : [
81+ { id, public : true } ,
82+ { id, owner : { id : userId ! } } ,
83+ { id, collaboratorIds : { has : userId ! } } ,
84+ ]
6685 } ,
6786 include : {
6887 owner : true ,
@@ -77,7 +96,6 @@ export async function repo(_, { id }, { userId }) {
7796 } ,
7897 } ,
7998 } ) ;
80- // console.log("Returning repo", repo);
8199 return repo ;
82100}
83101
@@ -90,12 +108,13 @@ export async function pod(_, { id }) {
90108 } ) ;
91109}
92110
93- export async function createRepo ( _ , { id, name } , { userId } ) {
111+ export async function createRepo ( _ , { id, name, isPublic } , { userId } ) {
94112 if ( ! userId ) throw Error ( "Unauthenticated" ) ;
95113 const repo = await prisma . repo . create ( {
96114 data : {
97115 id,
98116 name,
117+ public : isPublic ,
99118 owner : {
100119 connect : {
101120 id : userId ,
@@ -141,6 +160,42 @@ export async function deleteRepo(_, { name }, { userId }) {
141160 return true ;
142161}
143162
163+ export async function addCollaborator ( _ , { repoId, email } , { userId} ) {
164+ // make sure the repo is writable by this user
165+ if ( ! userId ) throw new Error ( "Not authenticated." ) ;
166+ // 1. find the repo
167+ const repo = await prisma . repo . findFirst ( {
168+ where : {
169+ id : repoId ,
170+ owner : { id : userId } ,
171+ } ,
172+ } ) ;
173+ if ( ! repo ) throw new Error ( "Repo not found or you are not the owner." ) ;
174+ if ( repo . public ) throw new Error ( "Public repo cannot have collaborators." ) ;
175+ // 2. find the user
176+ const user = await prisma . user . findFirst ( {
177+ where : {
178+ email,
179+ }
180+ } ) ;
181+ if ( ! user ) throw new Error ( "User not found" ) ;
182+ if ( user . id === userId ) throw new Error ( "You are already the owner." ) ;
183+ if ( repo . collaboratorIds . indexOf ( user . id ) !== - 1 ) throw new Error ( "The user is already a collaborator." ) ;
184+ // 3. add the user to the repo
185+ const res = await prisma . repo . update ( {
186+ where : {
187+ id : repoId ,
188+ } ,
189+ data : {
190+ // public: false,
191+ // name: "test",
192+ collaboratorIds : { push : user . id } ,
193+ }
194+ } )
195+ // console.log(res.collaboratorIds);
196+ return true ;
197+ }
198+
144199export async function addPod ( _ , { repoId, parent, index, input } , { userId } ) {
145200 // make sure the repo is writable by this user
146201 if ( ! userId ) throw new Error ( "Not authenticated." ) ;
@@ -272,4 +327,4 @@ export async function deletePod(_, { id, toDelete }, { userId }) {
272327 } ,
273328 } ) ;
274329 return true ;
275- }
330+ }
0 commit comments