11import { ConnectError } from '@connectrpc/connect' ;
22
3- // The server reports why an organization cannot be deleted as a list of
4- // blockers, each with a machine-readable type. These are the types the
5- // server knows today, mapped to a short instruction the user can act on.
6- // The wording must stay in line with the server behavior: a paid
7- // subscription must be downgraded, unpaid invoices must be paid, and a
8- // token debt is settled through support.
9- const BLOCKER_INSTRUCTIONS : Record < string , ( count : number ) => string > = {
10- ACTIVE_SUBSCRIPTION : ( ) =>
11- 'Please downgrade the subscription to the standard plan' ,
12- UNPAID_INVOICE : count =>
3+ // Words that come from the host app: what it calls an organization
4+ // and what it calls its base plan.
5+ export interface BlockerWording {
6+ organizationLabel : string ;
7+ basePlanTitle ?: string ;
8+ }
9+
10+ const DEFAULT_WORDING : BlockerWording = {
11+ organizationLabel : 'organization'
12+ } ;
13+
14+ // One sentence per blocker type the server can return.
15+ const BLOCKER_INSTRUCTIONS : Record <
16+ string ,
17+ ( count : number , wording : BlockerWording ) => string
18+ > = {
19+ ACTIVE_SUBSCRIPTION : ( _ , { organizationLabel, basePlanTitle } ) =>
20+ `Downgrade your current subscription and switch to the ${
21+ basePlanTitle || 'standard plan'
22+ } before deleting this ${ organizationLabel } .`,
23+ UNPAID_INVOICE : ( count , { organizationLabel } ) =>
1324 count > 1
14- ? `Please pay the ${ count } open invoices from the billing page `
15- : ' Please pay the open invoice from the billing page' ,
16- NEGATIVE_TOKEN_BALANCE : ( ) =>
17- 'Your token balance is negative. Please add tokens from the Tokens page to bring it back up, or pay the invoice when it arrives, and then you can delete'
25+ ? `Please pay your ${ count } outstanding invoices before deleting this ${ organizationLabel } . `
26+ : ` Please pay your outstanding invoice before deleting this ${ organizationLabel } .` ,
27+ NEGATIVE_TOKEN_BALANCE : ( _ , { organizationLabel } ) =>
28+ ` Please purchase enough tokens to clear your outstanding token balance before deleting this ${ organizationLabel } .`
1829} ;
1930
20- // Shown when the server reports a blocker kind this version does not know,
21- // or when the error could not be read at all.
31+ // Used for blocker types this version does not know.
2232export const GENERIC_DELETE_BLOCKED_MESSAGE =
2333 'Something is blocking the delete right now. Please try again later or contact support.' ;
2434
25- // instructionLines turns a list of blockers into one instruction per kind
26- // of blocker. Blockers of the same kind are counted so the instruction can
27- // say "the 2 open invoices". A kind without a known instruction becomes the
28- // generic message, once.
29- export function instructionLines ( blockers : { type : string } [ ] ) : string [ ] {
35+ // Returns one sentence per blocker type. Same-type blockers are counted.
36+ export function instructionLines (
37+ blockers : { type : string } [ ] ,
38+ wording : BlockerWording = DEFAULT_WORDING
39+ ) : string [ ] {
3040 const counts = new Map < string , number > ( ) ;
3141 for ( const blocker of blockers ) {
3242 counts . set ( blocker . type , ( counts . get ( blocker . type ) ?? 0 ) + 1 ) ;
@@ -36,7 +46,7 @@ export function instructionLines(blockers: { type: string }[]): string[] {
3646 for ( const [ type , count ] of counts ) {
3747 const instruction = BLOCKER_INSTRUCTIONS [ type ] ;
3848 if ( instruction ) {
39- lines . push ( instruction ( count ) ) ;
49+ lines . push ( instruction ( count , wording ) ) ;
4050 } else {
4151 hasUnknown = true ;
4252 }
@@ -47,13 +57,14 @@ export function instructionLines(blockers: { type: string }[]): string[] {
4757 return lines ;
4858}
4959
50- // deleteBlockedDescription reads the blockers out of a failed_precondition
51- // error from DeleteOrganization and returns the instructions as one string.
52- // The server attaches them as a google.rpc.PreconditionFailure detail; over
53- // the Connect JSON protocol that detail arrives with a ready-made JSON copy
54- // in its debug field. When the error carries nothing readable, the generic
55- // message is returned, so the raw server text is never shown.
56- export function deleteBlockedDescription ( err : ConnectError ) : string {
60+ // Reads the blockers from a failed_precondition error and returns the
61+ // sentences as one string. The server sends them as a
62+ // google.rpc.PreconditionFailure detail, which Connect exposes in the
63+ // detail's debug field.
64+ export function deleteBlockedDescription (
65+ err : ConnectError ,
66+ wording : BlockerWording = DEFAULT_WORDING
67+ ) : string {
5768 for ( const detail of err . details ) {
5869 if ( ! ( 'type' in detail ) || detail . type !== 'google.rpc.PreconditionFailure' ) {
5970 continue ;
@@ -73,9 +84,9 @@ export function deleteBlockedDescription(err: ConnectError): string {
7384 violation !== null &&
7485 typeof ( violation as { type ?: unknown } ) . type === 'string'
7586 ) ;
76- const lines = instructionLines ( blockers ) ;
87+ const lines = instructionLines ( blockers , wording ) ;
7788 if ( lines . length > 0 ) {
78- return lines . join ( '. ' ) ;
89+ return lines . join ( ' ' ) ;
7990 }
8091 }
8192 return GENERIC_DELETE_BLOCKED_MESSAGE ;
0 commit comments