|
| 1 | +import { |
| 2 | + assertSuccess, |
| 3 | + type ManagementApiClient, |
| 4 | +} from './management-api/index.js'; |
| 5 | + |
| 6 | +export const PROJECT_COST_MONTHLY = 10; |
| 7 | +export const BRANCH_COST_HOURLY = 0.01344; |
| 8 | + |
| 9 | +export type ProjectCost = { |
| 10 | + type: 'project'; |
| 11 | + recurrence: 'monthly'; |
| 12 | + amount: number; |
| 13 | +}; |
| 14 | + |
| 15 | +export type BranchCost = { |
| 16 | + type: 'branch'; |
| 17 | + recurrence: 'hourly'; |
| 18 | + amount: number; |
| 19 | +}; |
| 20 | + |
| 21 | +export type Cost = ProjectCost | BranchCost; |
| 22 | + |
| 23 | +/** |
| 24 | + * Gets the cost of the next project in an organization. |
| 25 | + */ |
| 26 | +export async function getNextProjectCost( |
| 27 | + managementApiClient: ManagementApiClient, |
| 28 | + orgId: string |
| 29 | +): Promise<Cost> { |
| 30 | + const orgResponse = await managementApiClient.GET( |
| 31 | + '/v1/organizations/{slug}', |
| 32 | + { |
| 33 | + params: { |
| 34 | + path: { |
| 35 | + slug: orgId, |
| 36 | + }, |
| 37 | + }, |
| 38 | + } |
| 39 | + ); |
| 40 | + |
| 41 | + assertSuccess(orgResponse, 'Failed to fetch organization'); |
| 42 | + |
| 43 | + const projectsResponse = await managementApiClient.GET('/v1/projects'); |
| 44 | + |
| 45 | + assertSuccess(projectsResponse, 'Failed to fetch projects'); |
| 46 | + |
| 47 | + const org = orgResponse.data; |
| 48 | + const activeProjects = projectsResponse.data.filter( |
| 49 | + (project) => |
| 50 | + project.organization_id === orgId && |
| 51 | + !['INACTIVE', 'GOING_DOWN', 'REMOVED'].includes(project.status) |
| 52 | + ); |
| 53 | + |
| 54 | + let amount = 0; |
| 55 | + |
| 56 | + if (org.plan !== 'free') { |
| 57 | + // If the organization is on a paid plan, the first project is included |
| 58 | + if (activeProjects.length > 0) { |
| 59 | + amount = PROJECT_COST_MONTHLY; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return { type: 'project', recurrence: 'monthly', amount }; |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Gets the cost for a database branch. |
| 68 | + */ |
| 69 | +export function getBranchCost(): Cost { |
| 70 | + return { type: 'branch', recurrence: 'hourly', amount: BRANCH_COST_HOURLY }; |
| 71 | +} |
0 commit comments