import type { Selectable } from 'kysely'
import type * as Db from '../Db.js'
import type * as db_Schema from '../Schema.js'
/** Columns of the `enabled_billing_sources` table, derived from `Schema.EnabledBillingSource`. */
export type Table = db_Schema.EnabledBillingSource
/** A stored enabled billing-source row. */
export type Record = Selectable
/** An enabled billing-source type. */
export type Source = Record['source']
/** Billing-source types in canonical display order. */
export const sources = ['stripe', 'tempo'] as const satisfies readonly Source[]
/**
* Enables a billing-source type for an organization.
*
* @param db - The database.
* @param input - The source to enable.
* @returns The stored row, or `undefined` when already enabled.
*/
export function add(db: Db.Db, input: add.Input): Promise {
return db.kysely
.insertInto('enabled_billing_sources')
.values({
createdAt: new Date().toISOString(),
createdBy: input.createdBy,
orgId: input.orgId,
source: input.source,
})
.onConflict((oc) => oc.columns(['orgId', 'source']).doNothing())
.returningAll()
.executeTakeFirst()
}
export declare namespace add {
/** Fields accepted when enabling a billing source. */
type Input = {
/** Identity enabling the source. */
createdBy: string
/** Organization id (`org_…`) the source is enabled for. */
orgId: string
/** Billing-source type to enable. */
source: Source
}
}
/**
* Lists an organization's enabled billing sources in canonical order.
*
* @param db - The database.
* @param orgId - The organization id (`org_…`).
* @returns The enabled sources.
*/
export function listByOrg(db: Db.Db, orgId: string): Promise {
return db.kysely
.selectFrom('enabled_billing_sources')
.selectAll()
.where('orgId', '=', orgId)
.orderBy('source', 'asc')
.execute()
}
/**
* Removes one enabled billing source from an organization.
*
* @param db - The database.
* @param input - The source to remove.
* @returns The removed row, or `undefined` when absent.
*/
export function remove(db: Db.Db, input: remove.Input): Promise {
return db.kysely
.deleteFrom('enabled_billing_sources')
.where('orgId', '=', input.orgId)
.where('source', '=', input.source)
.returningAll()
.executeTakeFirst()
}
export declare namespace remove {
/** Fields identifying one enabled billing source. */
type Input = {
/** Organization id (`org_…`) the source is enabled for. */
orgId: string
/** Billing-source type to remove. */
source: Source
}
}
/**
* Checks whether a billing-source type is enabled for an organization.
*
* @param db - The database.
* @param input - The source to check.
* @returns Whether the source is enabled.
*/
export async function isEnabled(db: Db.Db, input: isEnabled.Input): Promise {
const record = await db.kysely
.selectFrom('enabled_billing_sources')
.select('source')
.where('orgId', '=', input.orgId)
.where('source', '=', input.source)
.executeTakeFirst()
return record !== undefined
}
export declare namespace isEnabled {
/** Fields identifying one enabled billing source. */
type Input = {
/** Organization id (`org_…`) the source is enabled for. */
orgId: string
/** Billing-source type to check. */
source: Source
}
}