@capgo/cli 5.0.0-alpha.3 → 5.0.0-alpha.7

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
@@ -18,6 +18,7 @@ export const baseKeyPub = `${baseKey}.pub`
18
18
  export const defaultHost = 'https://capgo.app'
19
19
  export const defaultApiHost = 'https://api.capgo.app'
20
20
  export const defaultHostWeb = 'https://web.capgo.app'
21
+ export const EMPTY_UUID = '00000000-0000-0000-0000-000000000000'
21
22
 
22
23
  export const regexSemver = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/
23
24
  export const formatError = (error: any) => error ? `\n${prettyjson.render(error)}` : ''
@@ -133,23 +134,23 @@ export async function isGoodPlan(supabase: SupabaseClient<Database>, userId: str
133
134
  return data || false
134
135
  }
135
136
 
136
- export async function isPaying(supabase: SupabaseClient<Database>, userId: string): Promise<boolean> {
137
+ export async function isPayingOrg(supabase: SupabaseClient<Database>, orgId: string): Promise<boolean> {
137
138
  const { data } = await supabase
138
- .rpc('is_paying', { userid: userId })
139
+ .rpc('is_paying_org', { orgid: orgId })
139
140
  .single()
140
141
  return data || false
141
142
  }
142
143
 
143
- export async function isTrial(supabase: SupabaseClient<Database>, userId: string): Promise<number> {
144
+ export async function isTrialOrg(supabase: SupabaseClient<Database>, orgId: string): Promise<number> {
144
145
  const { data } = await supabase
145
- .rpc('is_trial', { userid: userId })
146
+ .rpc('is_trial_org', { orgid: orgId })
146
147
  .single()
147
148
  return data || 0
148
149
  }
149
150
 
150
- export async function isAllowedAction(supabase: SupabaseClient<Database>, userId: string): Promise<boolean> {
151
+ export async function isAllowedActionOrg(supabase: SupabaseClient<Database>, orgId: string): Promise<boolean> {
151
152
  const { data } = await supabase
152
- .rpc('is_allowed_action_user', { userid: userId })
153
+ .rpc('is_allowed_action_org', { orgid: orgId })
153
154
  .single()
154
155
  return !!data
155
156
  }
@@ -192,7 +193,7 @@ export enum OrganizationPerm {
192
193
  upload = 2,
193
194
  write = 3,
194
195
  admin = 4,
195
- owner = 5,
196
+ super_admin = 5,
196
197
  }
197
198
 
198
199
  export const hasOrganizationPerm = (perm: OrganizationPerm, required: OrganizationPerm): boolean => (perm as number) >= (required as number)
@@ -234,7 +235,7 @@ export async function isAllowedAppOrg(supabase: SupabaseClient<Database>, apikey
234
235
  break
235
236
  }
236
237
  case 'perm_owner': {
237
- perm = OrganizationPerm.owner
238
+ perm = OrganizationPerm.super_admin
238
239
  break
239
240
  }
240
241
  default: {
@@ -282,10 +283,11 @@ export async function isAllowedAppOrg(supabase: SupabaseClient<Database>, apikey
282
283
  }
283
284
  }
284
285
 
285
- export async function checkPlanValid(supabase: SupabaseClient<Database>, userId: string, apikey: string, appId?: string, warning = true) {
286
+ export async function checkPlanValid(supabase: SupabaseClient<Database>, orgId: string, apikey: string, appId?: string, warning = true) {
286
287
  const config = await getRemoteConfig()
287
288
 
288
- const validPlan = await (appId ? isAllowedActionAppIdApiKey(supabase, appId, apikey) : isAllowedAction(supabase, userId))
289
+ // isAllowedActionAppIdApiKey was updated in the orgs_v3 migration to work with the new system
290
+ const validPlan = await (appId ? isAllowedActionAppIdApiKey(supabase, appId, apikey) : isAllowedActionOrg(supabase, orgId))
289
291
  if (!validPlan) {
290
292
  p.log.error(`You need to upgrade your plan to continue to use capgo.\n Upgrade here: ${config.hostWeb}/dashboard/settings/plans\n`)
291
293
  wait(100)
@@ -296,8 +298,10 @@ export async function checkPlanValid(supabase: SupabaseClient<Database>, userId:
296
298
  wait(500)
297
299
  program.error('')
298
300
  }
299
- const trialDays = await isTrial(supabase, userId)
300
- const ispaying = await isPaying(supabase, userId)
301
+ const [trialDays, ispaying] = await Promise.all([
302
+ isTrialOrg(supabase, orgId),
303
+ isPayingOrg(supabase, orgId),
304
+ ])
301
305
  if (trialDays > 0 && warning && !ispaying)
302
306
  p.log.warn(`WARNING !!\nTrial expires in ${trialDays} days, upgrade here: ${config.hostWeb}/dashboard/settings/plans\n`)
303
307
  }
@@ -427,10 +431,10 @@ export async function updateOrCreateVersion(supabase: SupabaseClient<Database>,
427
431
  .eq('name', update.name)
428
432
  }
429
433
 
430
- export async function uploadUrl(supabase: SupabaseClient<Database>, appId: string, bucketId: string): Promise<string> {
434
+ export async function uploadUrl(supabase: SupabaseClient<Database>, appId: string, name: string): Promise<string> {
431
435
  const data = {
432
436
  app_id: appId,
433
- bucket_id: bucketId,
437
+ name,
434
438
  }
435
439
  try {
436
440
  const pathUploadLink = 'private/upload_link'
@@ -454,7 +458,7 @@ export async function updateOrCreateChannel(supabase: SupabaseClient<Database>,
454
458
  .select('enable_progressive_deploy, secondaryVersionPercentage, secondVersion')
455
459
  .eq('app_id', update.app_id)
456
460
  .eq('name', update.name)
457
- // .eq('created_by', update.created_by)
461
+ // .eq('created_by', update.created_by)
458
462
  .single()
459
463
 
460
464
  if (data && !error) {
@@ -480,7 +484,7 @@ export async function updateOrCreateChannel(supabase: SupabaseClient<Database>,
480
484
  .update(update)
481
485
  .eq('app_id', update.app_id)
482
486
  .eq('name', update.name)
483
- // .eq('created_by', update.created_by)
487
+ // .eq('created_by', update.created_by)
484
488
  .select()
485
489
  .single()
486
490
  }
@@ -518,11 +522,26 @@ export async function verifyUser(supabase: SupabaseClient<Database>, apikey: str
518
522
  return userId
519
523
  }
520
524
 
521
- export async function requireUpdateMetadata(supabase: SupabaseClient<Database>, channel: string): Promise<boolean> {
525
+ export async function getOrganizationId(supabase: SupabaseClient<Database>, appId: string) {
526
+ const { data, error } = await supabase.from('apps')
527
+ .select('owner_org')
528
+ .eq('app_id', appId)
529
+ .single()
530
+
531
+ if (!data || error) {
532
+ p.log.error(`Cannot get organization id for app id ${appId}`)
533
+ formatError(error)
534
+ program.error('')
535
+ }
536
+ return data.owner_org
537
+ }
538
+
539
+ export async function requireUpdateMetadata(supabase: SupabaseClient<Database>, channel: string, appId: string): Promise<boolean> {
522
540
  const { data, error } = await supabase
523
541
  .from('channels')
524
542
  .select('disableAutoUpdate')
525
543
  .eq('name', channel)
544
+ .eq('app_id', appId)
526
545
  .limit(1)
527
546
 
528
547
  if (error) {