@chatarmin/os 1.8.9 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -4
- package/dist/billing/client.d.ts.map +1 -1
- package/dist/billing/client.js +1 -0
- package/dist/billing/index.d.ts +2 -1
- package/dist/billing/index.d.ts.map +1 -1
- package/dist/billing/types.d.ts +11 -2
- package/dist/billing/types.d.ts.map +1 -1
- package/dist/billing/usage-breakdown-types.d.ts +50 -0
- package/dist/billing/usage-breakdown-types.d.ts.map +1 -1
- package/dist/companies/client.d.ts.map +1 -1
- package/dist/companies/client.js +4 -0
- package/dist/companies/index.d.ts +1 -1
- package/dist/companies/index.d.ts.map +1 -1
- package/dist/companies/types.d.ts +70 -4
- package/dist/companies/types.d.ts.map +1 -1
- package/dist/features/index.d.ts +1 -1
- package/dist/features/index.d.ts.map +1 -1
- package/dist/features/types.d.ts +17 -0
- package/dist/features/types.d.ts.map +1 -1
- package/dist/index.d.ts +59 -109
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +33 -100
- package/dist/onboard-flow.d.ts +74 -0
- package/dist/onboard-flow.d.ts.map +1 -0
- package/dist/onboard-flow.js +118 -0
- package/dist/onboard-types.d.ts +99 -0
- package/dist/onboard-types.d.ts.map +1 -0
- package/dist/onboard-types.js +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -3,12 +3,13 @@ import { type CompaniesClient } from "./companies/index.js";
|
|
|
3
3
|
import { type ChannelsClient } from "./channels/index.js";
|
|
4
4
|
import { type BillingClient } from "./billing/index.js";
|
|
5
5
|
import { type FeaturesClient } from "./features/index.js";
|
|
6
|
+
import type { OnboardInput, OnboardResult } from "./onboard-types.js";
|
|
6
7
|
export { webhooks, constructProductWebhookEvent, generateTestWebhookHeaders, OsWebhookSignatureVerificationError, WEBHOOK_ID_HEADER, WEBHOOK_SIGNATURE_HEADER, WEBHOOK_TIMESTAMP_HEADER, } from "./webhooks/index.js";
|
|
7
8
|
export type { ConstructProductWebhookEventOptions, ProductWebhookEventPayload, ProductWebhookHeaders, VerifiedProductWebhookEvent, } from "./webhooks/index.js";
|
|
8
|
-
export type { CompaniesClient, CompanyAssignee, CompanyAssignees, CreateCompanyInput, CreateCompanyResult, GetCompanyInput, LinkedCompany, ListCompaniesInput, ListCompaniesResult, ListLinkedCompaniesInput, ListLinkedCompaniesResult, ProductCompany, UpdateCompanyResult, } from "./companies/index.js";
|
|
9
|
+
export type { CompaniesClient, CompanyAssignee, CompanyAssignees, CreateCompanyInput, CreateCompanyResult, GetCompanyInput, LinkedCompany, ListCompaniesInput, ListCompaniesResult, ListLinkedCompaniesInput, ListLinkedCompaniesResult, ProductCompany, ApplyFreeOnboardTierInput, ApplyFreeOnboardTierResult, SetupOnboardBillingInput, SetupOnboardBillingResult, UpdateCompanyResult, } from "./companies/index.js";
|
|
9
10
|
export type { ChannelLinkType, ChannelsClient, LinkChannelInput, LinkChannelResult, LinkThreadInput, LinkThreadResult, UpdateChannelLinkInput, UpdateChannelLinkResult, } from "./channels/index.js";
|
|
10
11
|
export type { BillingClient, BillingInvoiceSummary, BillingOverview, BillingProfileSummary, BillingSubscriptionSummary, ClaimCheckoutInput, ClaimCheckoutResult, CreateBillingPortalSessionInput, EnsureFeatureSubscriptionInput, EnsureFeatureSubscriptionResult, GetBillingOverviewInput, GetUsageInput, LinkSubscriptionInput, LinkSubscriptionResult, ListBillingInvoicesOptions, PeriodOffsetRange, RelinkBillingProfileInput, ResolveInvoicePaymentUrlInput, SetUsageInput, SyncCompanyFromStripeInput, SyncCompanyFromStripeResult, UsageAggregate, UsageCycleBucket, UsageCycleSummary, UsagePoint, UsageQueryResult, UsageRecord, UsageSubscriptionItem, UsageResult, } from "./billing/index.js";
|
|
11
|
-
export type { ExternalOrgFeatureAccessItem, FeatureAccessByExternalOrgIdResult, FeatureAccessItem, FeatureBillingDetails, FeatureCheckByExternalOrgIdResult, FeatureCheckInput, FeatureCheckResult, FeatureSetAccessInput, FeatureSetAccessResult, FeaturesClient, ListFeatureAccessInput, } from "./features/index.js";
|
|
12
|
+
export type { ExternalOrgFeatureAccessItem, FeatureAccessByExternalOrgIdResult, FeatureAccessItem, FeatureAccessMode, FeatureBillingDetails, FeatureCheckByExternalOrgIdResult, FeatureCheckInput, FeatureCheckResult, FeatureResolvedAccess, FeatureResolvedAccessSource, FeatureSetAccessInput, FeatureSetAccessResult, FeaturesClient, ListFeatureAccessInput, } from "./features/index.js";
|
|
12
13
|
/**
|
|
13
14
|
* Configuration options for the ChatarminOS SDK client.
|
|
14
15
|
*/
|
|
@@ -101,86 +102,7 @@ export interface BulkContactResult {
|
|
|
101
102
|
isNew: boolean;
|
|
102
103
|
}>;
|
|
103
104
|
}
|
|
104
|
-
|
|
105
|
-
* Input for the unified onboarding flow.
|
|
106
|
-
*/
|
|
107
|
-
export interface OnboardInput {
|
|
108
|
-
/** Your product's unique organization ID for this customer */
|
|
109
|
-
externalOrgId: string;
|
|
110
|
-
/** Display label for the product link (e.g., "Chatarmin CX", "Shopify") */
|
|
111
|
-
label: string;
|
|
112
|
-
/**
|
|
113
|
-
* Company details used when creating a new company via onboard().
|
|
114
|
-
*/
|
|
115
|
-
hints?: {
|
|
116
|
-
/** Company name for new companies */
|
|
117
|
-
companyName?: string;
|
|
118
|
-
/** Domain for new companies */
|
|
119
|
-
domain?: string;
|
|
120
|
-
};
|
|
121
|
-
/**
|
|
122
|
-
* Stripe checkout session ID to claim.
|
|
123
|
-
* Use when user completed Stripe checkout in your product.
|
|
124
|
-
* @example "cs_test_xxxxxxxxxxxxx"
|
|
125
|
-
*/
|
|
126
|
-
checkoutSessionId?: string;
|
|
127
|
-
/**
|
|
128
|
-
* Existing Stripe subscription ID to link.
|
|
129
|
-
* Use when you create subscriptions on your product's Stripe account.
|
|
130
|
-
* @example "sub_xxxxxxxxxxxxx"
|
|
131
|
-
*/
|
|
132
|
-
stripeSubscriptionId?: string;
|
|
133
|
-
/**
|
|
134
|
-
* Stripe customer ID.
|
|
135
|
-
* Will be fetched from subscription if not provided.
|
|
136
|
-
* @example "cus_xxxxxxxxxxxxx"
|
|
137
|
-
*/
|
|
138
|
-
stripeCustomerId?: string;
|
|
139
|
-
/**
|
|
140
|
-
* Tier code to apply features from.
|
|
141
|
-
* Use for free tier signups or to tag linked subscriptions.
|
|
142
|
-
* @example "free" | "pro" | "enterprise"
|
|
143
|
-
*/
|
|
144
|
-
tierCode?: string;
|
|
145
|
-
/** Primary contact email for new companies */
|
|
146
|
-
contactEmail?: string;
|
|
147
|
-
/** Primary contact name for new companies */
|
|
148
|
-
contactName?: string;
|
|
149
|
-
/**
|
|
150
|
-
* Historical created_at timestamp for the company.
|
|
151
|
-
* Only used when creating new companies (not for existing matches).
|
|
152
|
-
* @example "2024-01-15T10:30:00.000Z"
|
|
153
|
-
*/
|
|
154
|
-
createdAt?: string;
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Result from the onboarding flow.
|
|
158
|
-
*/
|
|
159
|
-
export interface OnboardResult {
|
|
160
|
-
/** The company ID (either existing or newly created) */
|
|
161
|
-
companyId: string;
|
|
162
|
-
/** The company name */
|
|
163
|
-
companyName: string;
|
|
164
|
-
/**
|
|
165
|
-
* How the company was linked:
|
|
166
|
-
* - `already_linked`: Company was already linked to your product
|
|
167
|
-
* - `created`: New company was created
|
|
168
|
-
*/
|
|
169
|
-
linkStatus: "already_linked" | "created";
|
|
170
|
-
/** Billing status (only present if billing was configured) */
|
|
171
|
-
billingStatus?: {
|
|
172
|
-
/** Subscription ID if created/claimed */
|
|
173
|
-
subscriptionId?: string;
|
|
174
|
-
/** True if subscription was claimed from checkout */
|
|
175
|
-
claimed?: boolean;
|
|
176
|
-
/** True if subscription was linked */
|
|
177
|
-
linked?: boolean;
|
|
178
|
-
/** True if tier features were applied */
|
|
179
|
-
tierApplied?: boolean;
|
|
180
|
-
/** Error message if billing failed (company still linked) */
|
|
181
|
-
error?: string;
|
|
182
|
-
};
|
|
183
|
-
}
|
|
105
|
+
export type { OnboardInput, OnboardResult } from './onboard-types.js';
|
|
184
106
|
/**
|
|
185
107
|
* ChatarminOS SDK Client
|
|
186
108
|
*
|
|
@@ -341,6 +263,7 @@ export declare class ChatarminOS {
|
|
|
341
263
|
name: string | null;
|
|
342
264
|
role: string | null;
|
|
343
265
|
short_id: string | null;
|
|
266
|
+
custom_fields: unknown;
|
|
344
267
|
is_primary: boolean;
|
|
345
268
|
avatar_url: string | null;
|
|
346
269
|
}>;
|
|
@@ -395,9 +318,43 @@ export declare class ChatarminOS {
|
|
|
395
318
|
name: string | null;
|
|
396
319
|
role: string | null;
|
|
397
320
|
short_id: string | null;
|
|
321
|
+
custom_fields: unknown;
|
|
398
322
|
is_primary: boolean;
|
|
399
323
|
avatar_url: string | null;
|
|
400
324
|
}[]>;
|
|
325
|
+
/**
|
|
326
|
+
* Update a contact linked to a company. Identify it by `contactId` or `email`.
|
|
327
|
+
*/
|
|
328
|
+
update: (input: {
|
|
329
|
+
companyId: string;
|
|
330
|
+
contactId?: string;
|
|
331
|
+
email?: string;
|
|
332
|
+
name?: string;
|
|
333
|
+
role?: string;
|
|
334
|
+
isPrimary?: boolean;
|
|
335
|
+
}) => Promise<{
|
|
336
|
+
id: string;
|
|
337
|
+
metadata: unknown;
|
|
338
|
+
created_at: Date;
|
|
339
|
+
updated_at: Date;
|
|
340
|
+
name: string | null;
|
|
341
|
+
role: string | null;
|
|
342
|
+
short_id: string | null;
|
|
343
|
+
custom_fields: unknown;
|
|
344
|
+
is_primary: boolean;
|
|
345
|
+
avatar_url: string | null;
|
|
346
|
+
}>;
|
|
347
|
+
/**
|
|
348
|
+
* Remove a contact from a company. The person is deleted only when no other company uses them.
|
|
349
|
+
*/
|
|
350
|
+
unlink: (input: {
|
|
351
|
+
companyId: string;
|
|
352
|
+
contactId?: string;
|
|
353
|
+
email?: string;
|
|
354
|
+
}) => Promise<{
|
|
355
|
+
unlinked: boolean;
|
|
356
|
+
deleted: boolean;
|
|
357
|
+
}>;
|
|
401
358
|
};
|
|
402
359
|
/**
|
|
403
360
|
* Feature access and entitlement operations.
|
|
@@ -484,12 +441,13 @@ export declare class ChatarminOS {
|
|
|
484
441
|
created_at: Date;
|
|
485
442
|
updated_at: Date;
|
|
486
443
|
sync_status: string;
|
|
444
|
+
custom_fields: unknown;
|
|
445
|
+
label: string | null;
|
|
487
446
|
product_id: string;
|
|
488
447
|
external_org_id: string;
|
|
489
448
|
external_user_id: string | null;
|
|
490
449
|
confidence: number | null;
|
|
491
450
|
link_type: string;
|
|
492
|
-
label: string | null;
|
|
493
451
|
external_display_name: string | null;
|
|
494
452
|
billing_profile_id: string | null;
|
|
495
453
|
}>;
|
|
@@ -856,39 +814,33 @@ export declare class ChatarminOS {
|
|
|
856
814
|
}>;
|
|
857
815
|
};
|
|
858
816
|
/**
|
|
859
|
-
*
|
|
860
|
-
*
|
|
861
|
-
* This is the recommended way to onboard new customers. It handles:
|
|
862
|
-
* 1. Smart company matching/creation
|
|
863
|
-
* 2. Product linking
|
|
864
|
-
* 3. Subscription claiming/linking
|
|
865
|
-
* 4. Tier feature application
|
|
817
|
+
* Create or link a company for your product (fast, non-blocking).
|
|
866
818
|
*
|
|
867
|
-
*
|
|
819
|
+
* Handles company matching/creation, product linking, and free-tier billing setup
|
|
820
|
+
* (billing profile, subscription, feature access) when `tierCode: 'free'`.
|
|
821
|
+
* Paid tiers can use `companies.setupOnboardBilling()` after onboard or inline Stripe IDs.
|
|
822
|
+
* Pass `checkoutSessionId` or `stripeSubscriptionId` to claim/link Stripe inline.
|
|
868
823
|
*
|
|
869
|
-
*
|
|
870
|
-
* 2. **Free signup** - Pass `tierCode` only
|
|
871
|
-
* 3. **You create Stripe subscription** - Pass `stripeSubscriptionId` + `tierCode`
|
|
824
|
+
* Billing failures never throw after the company exists; `billingStatus.error` is set instead.
|
|
872
825
|
*
|
|
873
826
|
* @param input - Onboarding parameters
|
|
874
|
-
* @returns Result with company info and billing status
|
|
827
|
+
* @returns Result with company info and optional billing status
|
|
875
828
|
*
|
|
876
|
-
* @example
|
|
829
|
+
* @example CX free signup
|
|
877
830
|
* ```typescript
|
|
878
831
|
* const result = await os.onboard({
|
|
879
832
|
* externalOrgId: 'org_abc123',
|
|
880
|
-
* label: '
|
|
881
|
-
* hints: {
|
|
882
|
-
*
|
|
883
|
-
*
|
|
884
|
-
*
|
|
885
|
-
* checkoutSessionId: 'cs_test_xxx' // From Stripe callback
|
|
833
|
+
* label: 'Chatarmin CX',
|
|
834
|
+
* hints: { companyName: 'Acme Inc' },
|
|
835
|
+
* contactEmail: 'admin@acme.com',
|
|
836
|
+
* contactName: 'Jane Doe',
|
|
837
|
+
* tierCode: 'free',
|
|
886
838
|
* })
|
|
887
839
|
*
|
|
888
|
-
* // result.billingStatus.
|
|
840
|
+
* // result.billingStatus.tierApplied === true (no Stripe on signup path)
|
|
889
841
|
* ```
|
|
890
842
|
*
|
|
891
|
-
* @example Scenario
|
|
843
|
+
* @example Scenario 1: User completed Stripe checkout
|
|
892
844
|
* ```typescript
|
|
893
845
|
* const result = await os.onboard({
|
|
894
846
|
* externalOrgId: 'org_abc123',
|
|
@@ -897,15 +849,13 @@ export declare class ChatarminOS {
|
|
|
897
849
|
* companyName: 'Acme Inc',
|
|
898
850
|
* domain: 'acme.com'
|
|
899
851
|
* },
|
|
900
|
-
*
|
|
901
|
-
* contactEmail: 'admin@acme.com',
|
|
902
|
-
* contactName: 'John Doe'
|
|
852
|
+
* checkoutSessionId: 'cs_test_xxx' // From Stripe callback
|
|
903
853
|
* })
|
|
904
854
|
*
|
|
905
|
-
* // result.billingStatus.
|
|
855
|
+
* // result.billingStatus.claimed = true
|
|
906
856
|
* ```
|
|
907
857
|
*
|
|
908
|
-
* @example Scenario
|
|
858
|
+
* @example Scenario 2: You create Stripe subscription
|
|
909
859
|
* ```typescript
|
|
910
860
|
* // First, get tier prices
|
|
911
861
|
* const tier = await os.tiers.get('pro')
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
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;;;;;;;;;;;;;;;;;WA6BG;2BACgB,MAAM,WAAW,cAAc;;;;;QASlD;;;;;;;;;;;;;;;;;;;WAmBG;;;;;;;;;;;;;;;;;;;;QAMH;;;;;;;;;;;;;;;;;;;;;;;WAuBG;4BACiB,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAK7B;IAMD;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,IAAI,KAAK;QAIL;;;;;;;;;;;WAWG;;;;;;;;;;;;;;;;;;;;QAMH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAsCG;wBACa,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;QAKtB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WA8BG;0BACe,MAAM,WAAW,cAAc;;;;;MASpD;IAMD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiFG;IACG,OAAO,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;CAS3D;AAGD,YAAY,EAAE,SAAS,EAAE,CAAA;AAGzB,eAAe,WAAW,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ import { createChannelsClient, } from "./channels/index.js";
|
|
|
5
5
|
import { createBillingClient } from "./billing/index.js";
|
|
6
6
|
import { createFeaturesClient } from "./features/index.js";
|
|
7
7
|
import { webhooks } from "./webhooks/index.js";
|
|
8
|
+
import { runOnboardFlow } from "./onboard-flow.js";
|
|
8
9
|
export { webhooks, constructProductWebhookEvent, generateTestWebhookHeaders, OsWebhookSignatureVerificationError, WEBHOOK_ID_HEADER, WEBHOOK_SIGNATURE_HEADER, WEBHOOK_TIMESTAMP_HEADER, } from "./webhooks/index.js";
|
|
9
10
|
const INSPECT_SYMBOL = Symbol.for("nodejs.util.inspect.custom");
|
|
10
11
|
export class ChatarminOSError extends Error {
|
|
@@ -302,6 +303,14 @@ export class ChatarminOS {
|
|
|
302
303
|
* ```
|
|
303
304
|
*/
|
|
304
305
|
list: (companyId, primaryOnly = false) => call("contacts.list", () => client.contacts.listForCompany.query({ companyId, primaryOnly })),
|
|
306
|
+
/**
|
|
307
|
+
* Update a contact linked to a company. Identify it by `contactId` or `email`.
|
|
308
|
+
*/
|
|
309
|
+
update: (input) => call("contacts.update", () => client.contacts.updateForCompany.mutate(input)),
|
|
310
|
+
/**
|
|
311
|
+
* Remove a contact from a company. The person is deleted only when no other company uses them.
|
|
312
|
+
*/
|
|
313
|
+
unlink: (input) => call("contacts.unlink", () => client.contacts.unlinkFromCompany.mutate(input)),
|
|
305
314
|
};
|
|
306
315
|
}
|
|
307
316
|
// ==========================================================================
|
|
@@ -663,39 +672,33 @@ export class ChatarminOS {
|
|
|
663
672
|
// Onboarding
|
|
664
673
|
// ==========================================================================
|
|
665
674
|
/**
|
|
666
|
-
*
|
|
667
|
-
*
|
|
668
|
-
* This is the recommended way to onboard new customers. It handles:
|
|
669
|
-
* 1. Smart company matching/creation
|
|
670
|
-
* 2. Product linking
|
|
671
|
-
* 3. Subscription claiming/linking
|
|
672
|
-
* 4. Tier feature application
|
|
675
|
+
* Create or link a company for your product (fast, non-blocking).
|
|
673
676
|
*
|
|
674
|
-
*
|
|
677
|
+
* Handles company matching/creation, product linking, and free-tier billing setup
|
|
678
|
+
* (billing profile, subscription, feature access) when `tierCode: 'free'`.
|
|
679
|
+
* Paid tiers can use `companies.setupOnboardBilling()` after onboard or inline Stripe IDs.
|
|
680
|
+
* Pass `checkoutSessionId` or `stripeSubscriptionId` to claim/link Stripe inline.
|
|
675
681
|
*
|
|
676
|
-
*
|
|
677
|
-
* 2. **Free signup** - Pass `tierCode` only
|
|
678
|
-
* 3. **You create Stripe subscription** - Pass `stripeSubscriptionId` + `tierCode`
|
|
682
|
+
* Billing failures never throw after the company exists; `billingStatus.error` is set instead.
|
|
679
683
|
*
|
|
680
684
|
* @param input - Onboarding parameters
|
|
681
|
-
* @returns Result with company info and billing status
|
|
685
|
+
* @returns Result with company info and optional billing status
|
|
682
686
|
*
|
|
683
|
-
* @example
|
|
687
|
+
* @example CX free signup
|
|
684
688
|
* ```typescript
|
|
685
689
|
* const result = await os.onboard({
|
|
686
690
|
* externalOrgId: 'org_abc123',
|
|
687
|
-
* label: '
|
|
688
|
-
* hints: {
|
|
689
|
-
*
|
|
690
|
-
*
|
|
691
|
-
*
|
|
692
|
-
* checkoutSessionId: 'cs_test_xxx' // From Stripe callback
|
|
691
|
+
* label: 'Chatarmin CX',
|
|
692
|
+
* hints: { companyName: 'Acme Inc' },
|
|
693
|
+
* contactEmail: 'admin@acme.com',
|
|
694
|
+
* contactName: 'Jane Doe',
|
|
695
|
+
* tierCode: 'free',
|
|
693
696
|
* })
|
|
694
697
|
*
|
|
695
|
-
* // result.billingStatus.
|
|
698
|
+
* // result.billingStatus.tierApplied === true (no Stripe on signup path)
|
|
696
699
|
* ```
|
|
697
700
|
*
|
|
698
|
-
* @example Scenario
|
|
701
|
+
* @example Scenario 1: User completed Stripe checkout
|
|
699
702
|
* ```typescript
|
|
700
703
|
* const result = await os.onboard({
|
|
701
704
|
* externalOrgId: 'org_abc123',
|
|
@@ -704,15 +707,13 @@ export class ChatarminOS {
|
|
|
704
707
|
* companyName: 'Acme Inc',
|
|
705
708
|
* domain: 'acme.com'
|
|
706
709
|
* },
|
|
707
|
-
*
|
|
708
|
-
* contactEmail: 'admin@acme.com',
|
|
709
|
-
* contactName: 'John Doe'
|
|
710
|
+
* checkoutSessionId: 'cs_test_xxx' // From Stripe callback
|
|
710
711
|
* })
|
|
711
712
|
*
|
|
712
|
-
* // result.billingStatus.
|
|
713
|
+
* // result.billingStatus.claimed = true
|
|
713
714
|
* ```
|
|
714
715
|
*
|
|
715
|
-
* @example Scenario
|
|
716
|
+
* @example Scenario 2: You create Stripe subscription
|
|
716
717
|
* ```typescript
|
|
717
718
|
* // First, get tier prices
|
|
718
719
|
* const tier = await os.tiers.get('pro')
|
|
@@ -753,81 +754,13 @@ export class ChatarminOS {
|
|
|
753
754
|
* ```
|
|
754
755
|
*/
|
|
755
756
|
async onboard(input) {
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
757
|
+
return runOnboardFlow({
|
|
758
|
+
client: this.client,
|
|
759
|
+
billing: this.billing,
|
|
760
|
+
call: this.call.bind(this),
|
|
761
|
+
companies: this.companies,
|
|
762
|
+
input,
|
|
761
763
|
});
|
|
762
|
-
if (existing) {
|
|
763
|
-
companyId = existing.id;
|
|
764
|
-
companyName = existing.name;
|
|
765
|
-
linkStatus = "already_linked";
|
|
766
|
-
}
|
|
767
|
-
else {
|
|
768
|
-
const newCompany = await this.companies.create({
|
|
769
|
-
name: input.hints?.companyName ?? `Company ${input.externalOrgId}`,
|
|
770
|
-
domain: input.hints?.domain,
|
|
771
|
-
externalOrgId: input.externalOrgId,
|
|
772
|
-
label: input.label,
|
|
773
|
-
contactEmail: input.contactEmail,
|
|
774
|
-
contactName: input.contactName,
|
|
775
|
-
createdAt: input.createdAt,
|
|
776
|
-
});
|
|
777
|
-
companyId = newCompany.id;
|
|
778
|
-
companyName = newCompany.name;
|
|
779
|
-
linkStatus = "created";
|
|
780
|
-
}
|
|
781
|
-
// Step 2: Handle billing (non-fatal - don't block company linking)
|
|
782
|
-
let billingStatus;
|
|
783
|
-
try {
|
|
784
|
-
if (input.checkoutSessionId) {
|
|
785
|
-
// Claim subscription from checkout
|
|
786
|
-
const claimResult = await this.billing.claimCheckout({
|
|
787
|
-
companyId,
|
|
788
|
-
checkoutSessionId: input.checkoutSessionId,
|
|
789
|
-
});
|
|
790
|
-
billingStatus = {
|
|
791
|
-
subscriptionId: claimResult.subscriptionId,
|
|
792
|
-
claimed: !claimResult.alreadyClaimed,
|
|
793
|
-
};
|
|
794
|
-
}
|
|
795
|
-
else if (input.stripeSubscriptionId) {
|
|
796
|
-
// Link existing subscription
|
|
797
|
-
const linkResult = await this.billing.linkSubscription({
|
|
798
|
-
companyId,
|
|
799
|
-
stripeSubscriptionId: input.stripeSubscriptionId,
|
|
800
|
-
stripeCustomerId: input.stripeCustomerId,
|
|
801
|
-
tierCode: input.tierCode,
|
|
802
|
-
});
|
|
803
|
-
billingStatus = {
|
|
804
|
-
subscriptionId: linkResult.subscriptionId,
|
|
805
|
-
linked: !linkResult.alreadyLinked,
|
|
806
|
-
tierApplied: linkResult.tierApplied,
|
|
807
|
-
};
|
|
808
|
-
}
|
|
809
|
-
else if (input.tierCode) {
|
|
810
|
-
// Just apply tier features (no Stripe)
|
|
811
|
-
const applyResult = await this.tiers.apply(input.tierCode, {
|
|
812
|
-
companyId,
|
|
813
|
-
});
|
|
814
|
-
billingStatus = {
|
|
815
|
-
tierApplied: applyResult.featuresApplied > 0,
|
|
816
|
-
};
|
|
817
|
-
}
|
|
818
|
-
}
|
|
819
|
-
catch (err) {
|
|
820
|
-
// Billing failed but company is still linked - capture error
|
|
821
|
-
billingStatus = {
|
|
822
|
-
error: err instanceof Error ? err.message : String(err),
|
|
823
|
-
};
|
|
824
|
-
}
|
|
825
|
-
return {
|
|
826
|
-
companyId,
|
|
827
|
-
companyName,
|
|
828
|
-
linkStatus,
|
|
829
|
-
billingStatus,
|
|
830
|
-
};
|
|
831
764
|
}
|
|
832
765
|
}
|
|
833
766
|
// Default export
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { createTRPCClient } from '@trpc/client';
|
|
2
|
+
import type { AppRouter } from '@chatarmin/api/trpc-types';
|
|
3
|
+
import type { OnboardInput, OnboardResult } from './onboard-types.js';
|
|
4
|
+
type SdkClient = ReturnType<typeof createTRPCClient<AppRouter>>;
|
|
5
|
+
type BillingClient = {
|
|
6
|
+
claimCheckout: (input: {
|
|
7
|
+
companyId: string;
|
|
8
|
+
checkoutSessionId: string;
|
|
9
|
+
externalOrgId?: string;
|
|
10
|
+
productLinkId?: string;
|
|
11
|
+
}) => Promise<{
|
|
12
|
+
subscriptionId: string;
|
|
13
|
+
alreadyClaimed?: boolean;
|
|
14
|
+
}>;
|
|
15
|
+
linkSubscription: (input: {
|
|
16
|
+
companyId: string;
|
|
17
|
+
stripeSubscriptionId: string;
|
|
18
|
+
stripeCustomerId?: string;
|
|
19
|
+
tierCode?: string;
|
|
20
|
+
externalOrgId?: string;
|
|
21
|
+
productLinkId?: string;
|
|
22
|
+
}) => Promise<{
|
|
23
|
+
subscriptionId: string;
|
|
24
|
+
alreadyLinked?: boolean;
|
|
25
|
+
tierApplied?: boolean;
|
|
26
|
+
featuresApplied?: number;
|
|
27
|
+
}>;
|
|
28
|
+
};
|
|
29
|
+
type CompaniesClient = {
|
|
30
|
+
setupOnboardBilling: (input: {
|
|
31
|
+
companyId: string;
|
|
32
|
+
externalOrgId?: string;
|
|
33
|
+
tierCode?: string;
|
|
34
|
+
name: string;
|
|
35
|
+
email?: string;
|
|
36
|
+
currency?: string;
|
|
37
|
+
trialPeriodDays?: number | null;
|
|
38
|
+
}) => Promise<{
|
|
39
|
+
billingProfileId?: string;
|
|
40
|
+
stripeCustomerId?: string;
|
|
41
|
+
subscriptionId?: string;
|
|
42
|
+
tierCode: string;
|
|
43
|
+
featuresApplied: number;
|
|
44
|
+
subscriptionCreated: boolean;
|
|
45
|
+
}>;
|
|
46
|
+
};
|
|
47
|
+
export declare function runOnboardFlow(params: {
|
|
48
|
+
client: SdkClient;
|
|
49
|
+
billing: BillingClient;
|
|
50
|
+
companies: CompaniesClient & {
|
|
51
|
+
get: (input: {
|
|
52
|
+
externalOrgId: string;
|
|
53
|
+
}) => Promise<{
|
|
54
|
+
id: string;
|
|
55
|
+
name: string;
|
|
56
|
+
} | null>;
|
|
57
|
+
create: (input: {
|
|
58
|
+
name: string;
|
|
59
|
+
domain?: string;
|
|
60
|
+
externalOrgId: string;
|
|
61
|
+
label: string;
|
|
62
|
+
contactEmail?: string;
|
|
63
|
+
contactName?: string;
|
|
64
|
+
createdAt?: string;
|
|
65
|
+
}) => Promise<{
|
|
66
|
+
id: string;
|
|
67
|
+
name: string;
|
|
68
|
+
}>;
|
|
69
|
+
};
|
|
70
|
+
call: <T>(operation: string, action: () => Promise<T>) => Promise<T>;
|
|
71
|
+
input: OnboardInput;
|
|
72
|
+
}): Promise<OnboardResult>;
|
|
73
|
+
export {};
|
|
74
|
+
//# sourceMappingURL=onboard-flow.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"onboard-flow.d.ts","sourceRoot":"","sources":["../src/onboard-flow.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AACpD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAErE,KAAK,SAAS,GAAG,UAAU,CAAC,OAAO,gBAAgB,CAAC,SAAS,CAAC,CAAC,CAAA;AAE/D,KAAK,aAAa,GAAG;IACnB,aAAa,EAAE,CAAC,KAAK,EAAE;QACrB,SAAS,EAAE,MAAM,CAAA;QACjB,iBAAiB,EAAE,MAAM,CAAA;QACzB,aAAa,CAAC,EAAE,MAAM,CAAA;QACtB,aAAa,CAAC,EAAE,MAAM,CAAA;KACvB,KAAK,OAAO,CAAC;QACZ,cAAc,EAAE,MAAM,CAAA;QACtB,cAAc,CAAC,EAAE,OAAO,CAAA;KACzB,CAAC,CAAA;IACF,gBAAgB,EAAE,CAAC,KAAK,EAAE;QACxB,SAAS,EAAE,MAAM,CAAA;QACjB,oBAAoB,EAAE,MAAM,CAAA;QAC5B,gBAAgB,CAAC,EAAE,MAAM,CAAA;QACzB,QAAQ,CAAC,EAAE,MAAM,CAAA;QACjB,aAAa,CAAC,EAAE,MAAM,CAAA;QACtB,aAAa,CAAC,EAAE,MAAM,CAAA;KACvB,KAAK,OAAO,CAAC;QACZ,cAAc,EAAE,MAAM,CAAA;QACtB,aAAa,CAAC,EAAE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A;YACpB,SAAS,CAAC,EAAE,MAAM,CAAA;SACnB,KAAK,OAAO,CAAC;YAAE,EAAE,EAAE,MAAM,CAAC;YAAC,IAAI,EAAE,MAAM,CAAA;SAAE,CAAC,CAAA;KAC5C,CAAA;IACD,IAAI,EAAE,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;IACpE,KAAK,EAAE,YAAY,CAAA;CACpB,GAAG,OAAO,CAAC,aAAa,CAAC,CAyHzB"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
function isFreeSignupTier(tierCode) {
|
|
2
|
+
return tierCode === 'free';
|
|
3
|
+
}
|
|
4
|
+
export async function runOnboardFlow(params) {
|
|
5
|
+
let companyId;
|
|
6
|
+
let companyName;
|
|
7
|
+
let linkStatus;
|
|
8
|
+
const existing = await params.companies.get({
|
|
9
|
+
externalOrgId: params.input.externalOrgId,
|
|
10
|
+
});
|
|
11
|
+
if (existing) {
|
|
12
|
+
companyId = existing.id;
|
|
13
|
+
companyName = existing.name;
|
|
14
|
+
linkStatus = 'already_linked';
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
const newCompany = await params.companies.create({
|
|
18
|
+
name: params.input.hints?.companyName ?? `Company ${params.input.externalOrgId}`,
|
|
19
|
+
domain: params.input.hints?.domain,
|
|
20
|
+
externalOrgId: params.input.externalOrgId,
|
|
21
|
+
label: params.input.label,
|
|
22
|
+
contactEmail: params.input.contactEmail,
|
|
23
|
+
contactName: params.input.contactName,
|
|
24
|
+
createdAt: params.input.createdAt,
|
|
25
|
+
});
|
|
26
|
+
companyId = newCompany.id;
|
|
27
|
+
companyName = newCompany.name;
|
|
28
|
+
linkStatus = 'created';
|
|
29
|
+
}
|
|
30
|
+
const hasExplicitBilling = Boolean(params.input.checkoutSessionId) || Boolean(params.input.stripeSubscriptionId);
|
|
31
|
+
if (hasExplicitBilling) {
|
|
32
|
+
let billingStatus;
|
|
33
|
+
try {
|
|
34
|
+
if (params.input.checkoutSessionId) {
|
|
35
|
+
const claimResult = await params.billing.claimCheckout({
|
|
36
|
+
companyId,
|
|
37
|
+
checkoutSessionId: params.input.checkoutSessionId,
|
|
38
|
+
externalOrgId: params.input.externalOrgId,
|
|
39
|
+
});
|
|
40
|
+
billingStatus = {
|
|
41
|
+
subscriptionId: claimResult.subscriptionId,
|
|
42
|
+
claimed: !claimResult.alreadyClaimed,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
const linkResult = await params.billing.linkSubscription({
|
|
47
|
+
companyId,
|
|
48
|
+
stripeSubscriptionId: params.input.stripeSubscriptionId,
|
|
49
|
+
stripeCustomerId: params.input.stripeCustomerId,
|
|
50
|
+
tierCode: params.input.tierCode,
|
|
51
|
+
externalOrgId: params.input.externalOrgId,
|
|
52
|
+
});
|
|
53
|
+
billingStatus = {
|
|
54
|
+
subscriptionId: linkResult.subscriptionId,
|
|
55
|
+
linked: !linkResult.alreadyLinked,
|
|
56
|
+
tierApplied: linkResult.tierApplied,
|
|
57
|
+
featuresApplied: linkResult.featuresApplied,
|
|
58
|
+
tierCode: params.input.tierCode,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
billingStatus = {
|
|
64
|
+
error: err instanceof Error ? err.message : String(err),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
companyId,
|
|
69
|
+
companyName,
|
|
70
|
+
linkStatus,
|
|
71
|
+
billingStatus,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
if (isFreeSignupTier(params.input.tierCode) && linkStatus === 'created') {
|
|
75
|
+
try {
|
|
76
|
+
const setupResult = await params.companies.setupOnboardBilling({
|
|
77
|
+
companyId,
|
|
78
|
+
externalOrgId: params.input.externalOrgId,
|
|
79
|
+
tierCode: params.input.tierCode,
|
|
80
|
+
name: params.input.billing?.name ??
|
|
81
|
+
params.input.hints?.companyName ??
|
|
82
|
+
companyName,
|
|
83
|
+
email: params.input.billing?.email ?? params.input.contactEmail,
|
|
84
|
+
currency: params.input.billing?.currency,
|
|
85
|
+
trialPeriodDays: params.input.billing?.trialPeriodDays,
|
|
86
|
+
});
|
|
87
|
+
return {
|
|
88
|
+
companyId,
|
|
89
|
+
companyName,
|
|
90
|
+
linkStatus,
|
|
91
|
+
billingStatus: {
|
|
92
|
+
billingProfileId: setupResult.billingProfileId,
|
|
93
|
+
stripeCustomerId: setupResult.stripeCustomerId,
|
|
94
|
+
subscriptionId: setupResult.subscriptionId,
|
|
95
|
+
tierApplied: setupResult.featuresApplied > 0,
|
|
96
|
+
featuresApplied: setupResult.featuresApplied,
|
|
97
|
+
tierCode: setupResult.tierCode,
|
|
98
|
+
subscriptionCreated: setupResult.subscriptionCreated,
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
return {
|
|
104
|
+
companyId,
|
|
105
|
+
companyName,
|
|
106
|
+
linkStatus,
|
|
107
|
+
billingStatus: {
|
|
108
|
+
error: err instanceof Error ? err.message : String(err),
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
companyId,
|
|
115
|
+
companyName,
|
|
116
|
+
linkStatus,
|
|
117
|
+
};
|
|
118
|
+
}
|