| 1 | import { schema } from '@ghost/db'; |
| 2 | import { and, eq |
| 3 | |
| 4 | /** Who pays for a repository's stored files: its organization, or its owner when it belongs to no organization. */ |
| 5 | export type StorageAccount |
| 6 | |
| 7 | export function storageAccountOf(repository |
| 8 | ownerId: string |
| 9 | organizationId: string |
| 10 | }): StorageAccount { |
| 11 | return repository.organizationId |
| 12 | ? { organizationId: repository |
| 13 | : { userId: repository |
| 14 | } |
| 15 | |
| 16 | /** Which of an account's limits bytes count against. Forks have their own, since people fork large repositories they never push to. */ |
| 17 | export type StorageKind |
| 18 | |
| 19 | export type RepositoryStorageKind |
| 20 | |
| 21 | /** What a repository's files are billed as: everything a fork holds counts against the fork limit, and anything else against `kind`. */ |
| 22 | export function billedKindOf<Kind |
| 23 | repository: { |
| 24 | kind: Kind |
| 25 | ): Kind | |
| 26 | return repository.parentRepositoryId |
| 27 | } |
| 28 | |
| 29 | /** Repositories of `kind` whose files count against `account`. */ |
| 30 | export function billedTo(account |
| 31 | const ofKind = |
| 32 | kind === 'fork' |
| 33 | ? isNotNull(schema. |
| 34 | : isNull(schema. |
| 35 | return 'organizationId' in |
| 36 | ? and(eq( |
| 37 | : and( |
| 38 | eq(schema.repository |
| 39 | isNull(schema.repository |
| 40 | ofKind, |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | export function storageAccountKey(account |
| 45 | return 'organizationId' in |
| 46 | ? `organization:${account. |
| 47 | : `user:${account. |
| 48 | } |