@capgo/cli 3.14.55 → 3.14.58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.ts CHANGED
@@ -150,6 +150,106 @@ export const isAllowedApp = async (supabase: SupabaseClient<Database>, apikey: s
150
150
  return !!data
151
151
  }
152
152
 
153
+ export enum OrganizationPerm {
154
+ 'none' = 0,
155
+ 'read' = 1,
156
+ 'upload' = 2,
157
+ 'write' = 3,
158
+ 'admin' = 4,
159
+ 'owner' = 5,
160
+ }
161
+
162
+ export const hasOrganizationPerm = (perm: OrganizationPerm, required: OrganizationPerm): boolean => (perm as number) >= (required as number)
163
+
164
+ export const isAllowedAppOrg = async (
165
+ supabase: SupabaseClient<Database>,
166
+ apikey: string,
167
+ appId: string,
168
+ ): Promise<{ okay: true, data: OrganizationPerm } | { okay: false, error: 'INVALID_APIKEY' | 'NO_APP' | 'NO_ORG' }> => {
169
+ const { data, error } = await supabase
170
+ .rpc('get_org_perm_for_apikey', { apikey, app_id: appId })
171
+ .single()
172
+
173
+ if (error) {
174
+ p.log.error('Cannot get permissions for organization!')
175
+ console.error(error)
176
+ process.exit(1)
177
+ }
178
+
179
+ const ok = (data as string).includes('perm')
180
+ if (ok) {
181
+ let perm = null as (OrganizationPerm | null)
182
+
183
+ switch (data as string) {
184
+ case 'perm_none': {
185
+ perm = OrganizationPerm.none
186
+ break;
187
+ }
188
+ case 'perm_read': {
189
+ perm = OrganizationPerm.read
190
+ break;
191
+ }
192
+ case 'perm_upload': {
193
+ perm = OrganizationPerm.upload
194
+ break;
195
+ }
196
+ case 'perm_write': {
197
+ perm = OrganizationPerm.write
198
+ break;
199
+ }
200
+ case 'perm_admin': {
201
+ perm = OrganizationPerm.admin
202
+ break;
203
+ }
204
+ case 'perm_owner': {
205
+ perm = OrganizationPerm.owner
206
+ break;
207
+ }
208
+ default: {
209
+ if ((data as string).includes('invite')) {
210
+ p.log.info('Please accept/deny the organization invitation before trying to access the app')
211
+ process.exit(1)
212
+ }
213
+
214
+ p.log.error(`Invalid output when fetching organization permission. Response: ${data}`)
215
+ process.exit(1)
216
+ }
217
+ }
218
+
219
+ return {
220
+ okay: true,
221
+ data: perm
222
+ }
223
+ }
224
+
225
+ // This means that something went wrong here
226
+ let functionError = null as 'INVALID_APIKEY' | 'NO_APP' | 'NO_ORG' | null
227
+
228
+ switch (data as string) {
229
+ case 'INVALID_APIKEY': {
230
+ functionError = 'INVALID_APIKEY'
231
+ break
232
+ }
233
+ case 'NO_APP': {
234
+ functionError = 'NO_APP'
235
+ break
236
+ }
237
+ case 'NO_ORG': {
238
+ functionError = 'NO_ORG'
239
+ break
240
+ }
241
+ default: {
242
+ p.log.error(`Invalid error when fetching organization permission. Response: ${data}`)
243
+ process.exit(1)
244
+ }
245
+ }
246
+
247
+ return {
248
+ okay: false,
249
+ error: functionError
250
+ }
251
+ }
252
+
153
253
  export const checkPlanValid = async (supabase: SupabaseClient<Database>, userId: string, warning = true) => {
154
254
  const config = await getRemoteConfig()
155
255
  const validPlan = await isAllowedAction(supabase, userId)
@@ -309,7 +409,7 @@ export const updateOrCreateChannel = async (supabase: SupabaseClient<Database>,
309
409
  .select('enable_progressive_deploy, secondaryVersionPercentage, secondVersion')
310
410
  .eq('app_id', update.app_id)
311
411
  .eq('name', update.name)
312
- .eq('created_by', update.created_by)
412
+ // .eq('created_by', update.created_by)
313
413
  .single()
314
414
 
315
415
  if (data && !error) {
@@ -335,7 +435,7 @@ export const updateOrCreateChannel = async (supabase: SupabaseClient<Database>,
335
435
  .update(update)
336
436
  .eq('app_id', update.app_id)
337
437
  .eq('name', update.name)
338
- .eq('created_by', update.created_by)
438
+ // .eq('created_by', update.created_by)
339
439
  .select()
340
440
  .single()
341
441
  }