Files
accounted/extensions/general/invoice-inbox/lib/inbox-provisioning.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

44 lines
1.4 KiB
TypeScript

import type { SupabaseClient } from '@supabase/supabase-js'
import type { CompanyInbox } from '@/types'
export function composeInboxAddress(localPart: string, domain: string): string {
return `${localPart}@${domain}`
}
export async function getActiveInbox(
supabase: SupabaseClient,
companyId: string
): Promise<CompanyInbox | null> {
const { data, error } = await supabase
.from('company_inboxes')
.select('*')
.eq('company_id', companyId)
.eq('status', 'active')
.maybeSingle()
if (error) throw new Error(`Failed to load inbox: ${error.message}`)
return (data as CompanyInbox | null) ?? null
}
// Rotate the company's inbox address. Delegates to the rotate_company_inbox
// RPC so the three steps (deprecate, generate, insert) run inside a single
// Postgres transaction; a failure on any step rolls the whole thing back
// and the company is never left without an active inbox.
export async function rotateCompanyInbox(
supabase: SupabaseClient,
companyId: string
): Promise<CompanyInbox> {
const { data, error } = await supabase
.rpc('rotate_company_inbox', { p_company_id: companyId })
if (error || !data) {
throw new Error(`Failed to rotate inbox: ${error?.message ?? 'no data'}`)
}
// The RPC returns a single row (SETOF company_inboxes).
const row = Array.isArray(data) ? data[0] : data
if (!row) throw new Error('Failed to rotate inbox: RPC returned no row')
return row as CompanyInbox
}