@chatarmin/os 1.8.8 → 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.
Files changed (37) hide show
  1. package/README.md +63 -4
  2. package/dist/billing/client.d.ts.map +1 -1
  3. package/dist/billing/client.js +1 -0
  4. package/dist/billing/index.d.ts +2 -1
  5. package/dist/billing/index.d.ts.map +1 -1
  6. package/dist/billing/read-types.d.ts +1 -1
  7. package/dist/billing/read-types.d.ts.map +1 -1
  8. package/dist/billing/types.d.ts +11 -2
  9. package/dist/billing/types.d.ts.map +1 -1
  10. package/dist/billing/usage-breakdown-types.d.ts +50 -0
  11. package/dist/billing/usage-breakdown-types.d.ts.map +1 -1
  12. package/dist/channels/client.d.ts.map +1 -1
  13. package/dist/channels/client.js +15 -1
  14. package/dist/channels/index.d.ts +1 -1
  15. package/dist/channels/index.d.ts.map +1 -1
  16. package/dist/channels/types.d.ts +44 -5
  17. package/dist/channels/types.d.ts.map +1 -1
  18. package/dist/companies/client.d.ts.map +1 -1
  19. package/dist/companies/client.js +4 -0
  20. package/dist/companies/index.d.ts +1 -1
  21. package/dist/companies/index.d.ts.map +1 -1
  22. package/dist/companies/types.d.ts +70 -4
  23. package/dist/companies/types.d.ts.map +1 -1
  24. package/dist/features/index.d.ts +1 -1
  25. package/dist/features/index.d.ts.map +1 -1
  26. package/dist/features/types.d.ts +17 -0
  27. package/dist/features/types.d.ts.map +1 -1
  28. package/dist/index.d.ts +59 -109
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +33 -100
  31. package/dist/onboard-flow.d.ts +74 -0
  32. package/dist/onboard-flow.d.ts.map +1 -0
  33. package/dist/onboard-flow.js +118 -0
  34. package/dist/onboard-types.d.ts +99 -0
  35. package/dist/onboard-types.d.ts.map +1 -0
  36. package/dist/onboard-types.js +1 -0
  37. package/package.json +1 -1
@@ -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
+ }
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Input for the unified onboarding flow.
3
+ */
4
+ export interface OnboardInput {
5
+ /** Your product's unique organization ID for this customer */
6
+ externalOrgId: string;
7
+ /** Display label for the product link (e.g., "Chatarmin CX", "Shopify") */
8
+ label: string;
9
+ /**
10
+ * Company details used when creating a new company via onboard().
11
+ */
12
+ hints?: {
13
+ /** Company name for new companies */
14
+ companyName?: string;
15
+ /** Domain for new companies */
16
+ domain?: string;
17
+ };
18
+ /**
19
+ * Stripe checkout session ID to claim.
20
+ * Use when user completed Stripe checkout in your product.
21
+ * @example "cs_test_xxxxxxxxxxxxx"
22
+ */
23
+ checkoutSessionId?: string;
24
+ /**
25
+ * Existing Stripe subscription ID to link.
26
+ * Use when you create subscriptions on your product's Stripe account.
27
+ * @example "sub_xxxxxxxxxxxxx"
28
+ */
29
+ stripeSubscriptionId?: string;
30
+ /**
31
+ * Stripe customer ID.
32
+ * Will be fetched from subscription if not provided.
33
+ * @example "cus_xxxxxxxxxxxxx"
34
+ */
35
+ stripeCustomerId?: string;
36
+ /**
37
+ * Tier code for onboard billing setup (`free`) or claim/link flows.
38
+ * @example "free" | "pro" | "enterprise"
39
+ */
40
+ tierCode?: string;
41
+ /**
42
+ * Billing identity overrides for free-tier onboard billing setup.
43
+ */
44
+ billing?: {
45
+ name?: string;
46
+ email?: string;
47
+ currency?: string;
48
+ trialPeriodDays?: number | null;
49
+ };
50
+ /** Primary contact email for new companies */
51
+ contactEmail?: string;
52
+ /** Primary contact name for new companies */
53
+ contactName?: string;
54
+ /**
55
+ * Historical created_at timestamp for the company.
56
+ * Only used when creating new companies (not for existing matches).
57
+ * @example "2024-01-15T10:30:00.000Z"
58
+ */
59
+ createdAt?: string;
60
+ }
61
+ /**
62
+ * Result from the onboarding flow.
63
+ */
64
+ export interface OnboardResult {
65
+ /** The company ID (either existing or newly created) */
66
+ companyId: string;
67
+ /** The company name */
68
+ companyName: string;
69
+ /**
70
+ * How the company was linked:
71
+ * - `already_linked`: Company was already linked to your product
72
+ * - `created`: New company was created
73
+ */
74
+ linkStatus: 'already_linked' | 'created';
75
+ /** Billing status when checkoutSessionId or stripeSubscriptionId was provided */
76
+ billingStatus?: {
77
+ /** Billing profile ID when created or reused */
78
+ billingProfileId?: string;
79
+ /** Stripe customer ID when a billing profile exists */
80
+ stripeCustomerId?: string;
81
+ /** Subscription ID if created/claimed */
82
+ subscriptionId?: string;
83
+ /** True if subscription was claimed from checkout */
84
+ claimed?: boolean;
85
+ /** True if subscription was linked */
86
+ linked?: boolean;
87
+ /** True if tier features were applied */
88
+ tierApplied?: boolean;
89
+ /** Number of feature access rows upserted */
90
+ featuresApplied?: number;
91
+ /** Tier code that was applied */
92
+ tierCode?: string;
93
+ /** True if a new subscription was created during onboard */
94
+ subscriptionCreated?: boolean;
95
+ /** Error message if billing failed (company still linked) */
96
+ error?: string;
97
+ };
98
+ }
99
+ //# sourceMappingURL=onboard-types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"onboard-types.d.ts","sourceRoot":"","sources":["../src/onboard-types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,8DAA8D;IAC9D,aAAa,EAAE,MAAM,CAAA;IAErB,2EAA2E;IAC3E,KAAK,EAAE,MAAM,CAAA;IAEb;;OAEG;IACH,KAAK,CAAC,EAAE;QACN,qCAAqC;QACrC,WAAW,CAAC,EAAE,MAAM,CAAA;QACpB,+BAA+B;QAC/B,MAAM,CAAC,EAAE,MAAM,CAAA;KAChB,CAAA;IAED;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAE1B;;;;OAIG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAA;IAE7B;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAA;IAEzB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;IAEjB;;OAEG;IACH,OAAO,CAAC,EAAE;QACR,IAAI,CAAC,EAAE,MAAM,CAAA;QACb,KAAK,CAAC,EAAE,MAAM,CAAA;QACd,QAAQ,CAAC,EAAE,MAAM,CAAA;QACjB,eAAe,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;KAChC,CAAA;IAED,8CAA8C;IAC9C,YAAY,CAAC,EAAE,MAAM,CAAA;IAErB,6CAA6C;IAC7C,WAAW,CAAC,EAAE,MAAM,CAAA;IAEpB;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,wDAAwD;IACxD,SAAS,EAAE,MAAM,CAAA;IAEjB,uBAAuB;IACvB,WAAW,EAAE,MAAM,CAAA;IAEnB;;;;OAIG;IACH,UAAU,EAAE,gBAAgB,GAAG,SAAS,CAAA;IAExC,iFAAiF;IACjF,aAAa,CAAC,EAAE;QACd,gDAAgD;QAChD,gBAAgB,CAAC,EAAE,MAAM,CAAA;QACzB,uDAAuD;QACvD,gBAAgB,CAAC,EAAE,MAAM,CAAA;QACzB,yCAAyC;QACzC,cAAc,CAAC,EAAE,MAAM,CAAA;QACvB,qDAAqD;QACrD,OAAO,CAAC,EAAE,OAAO,CAAA;QACjB,sCAAsC;QACtC,MAAM,CAAC,EAAE,OAAO,CAAA;QAChB,yCAAyC;QACzC,WAAW,CAAC,EAAE,OAAO,CAAA;QACrB,6CAA6C;QAC7C,eAAe,CAAC,EAAE,MAAM,CAAA;QACxB,iCAAiC;QACjC,QAAQ,CAAC,EAAE,MAAM,CAAA;QACjB,4DAA4D;QAC5D,mBAAmB,CAAC,EAAE,OAAO,CAAA;QAC7B,6DAA6D;QAC7D,KAAK,CAAC,EAAE,MAAM,CAAA;KACf,CAAA;CACF"}
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chatarmin/os",
3
- "version": "1.8.8",
3
+ "version": "1.13.0",
4
4
  "description": "Type-safe SDK for ChatarminOS - Customer & Subscription Management",
5
5
  "type": "module",
6
6
  "publishConfig": {