@wipperoz/wipperoz-core 1.1.24 → 1.2.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.
@@ -1,5 +1,6 @@
1
1
  export * from './billing.interface';
2
2
  export * from './billingConfig.interface';
3
3
  export * from './creditOptionDisplay.interface';
4
+ export * from './product.interface';
4
5
  export * from './topUpIntentResult.interface';
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/billing/interfaces/billing/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iCAAiC,CAAC;AAChD,cAAc,+BAA+B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/billing/interfaces/billing/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iCAAiC,CAAC;AAChD,cAAc,qBAAqB,CAAC;AACpC,cAAc,+BAA+B,CAAC"}
@@ -4,5 +4,6 @@ const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./billing.interface"), exports);
5
5
  tslib_1.__exportStar(require("./billingConfig.interface"), exports);
6
6
  tslib_1.__exportStar(require("./creditOptionDisplay.interface"), exports);
7
+ tslib_1.__exportStar(require("./product.interface"), exports);
7
8
  tslib_1.__exportStar(require("./topUpIntentResult.interface"), exports);
8
9
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/billing/interfaces/billing/index.ts"],"names":[],"mappings":";;;AAAA,8DAAoC;AACpC,oEAA0C;AAC1C,0EAAgD;AAChD,wEAA8C"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/billing/interfaces/billing/index.ts"],"names":[],"mappings":";;;AAAA,8DAAoC;AACpC,oEAA0C;AAC1C,0EAAgD;AAChD,8DAAoC;AACpC,wEAA8C"}
@@ -0,0 +1,98 @@
1
+ import { CurrencyEnum } from '@wipperoz/common-entities-account/dist/src';
2
+ export interface ProductPriceConfig {
3
+ amount: number;
4
+ stripePriceId?: string;
5
+ includesGst?: boolean;
6
+ gstAmount?: number;
7
+ }
8
+ export interface ProductDiscount {
9
+ enabled: boolean;
10
+ originalAmount: number;
11
+ reason?: string;
12
+ }
13
+ /**
14
+ * Product represents a purchasable credit package.
15
+ * This replaces the legacy TopUpOption interface and includes Stripe sync.
16
+ */
17
+ export interface Product {
18
+ productId: string;
19
+ productName: string;
20
+ credits: number;
21
+ stripeProductId?: string;
22
+ prices: Partial<Record<CurrencyEnum, ProductPriceConfig>>;
23
+ enabled: boolean;
24
+ sortOrder: number;
25
+ /** Display label shown in UI (e.g., "100 Credits") */
26
+ label?: string;
27
+ /** Longer description for the product */
28
+ description?: string;
29
+ /** Badge to highlight the product (e.g., "Popular", "Best Value") */
30
+ badge?: 'Popular' | 'Best Value' | 'New' | null;
31
+ /** Discount configuration for promotional pricing */
32
+ discount?: ProductDiscount;
33
+ createdAt: number;
34
+ updatedAt: number;
35
+ createdBy: string;
36
+ updatedBy: string;
37
+ }
38
+ export interface CreateProductInput {
39
+ productId: string;
40
+ productName: string;
41
+ credits: number;
42
+ prices: Partial<Record<CurrencyEnum, {
43
+ amount: number;
44
+ includesGst?: boolean;
45
+ }>>;
46
+ enabled?: boolean;
47
+ sortOrder?: number;
48
+ label?: string;
49
+ description?: string;
50
+ badge?: 'Popular' | 'Best Value' | 'New' | null;
51
+ discount?: ProductDiscount;
52
+ }
53
+ export interface UpdateProductInput {
54
+ productName?: string;
55
+ credits?: number;
56
+ prices?: Partial<Record<CurrencyEnum, {
57
+ amount: number;
58
+ includesGst?: boolean;
59
+ }>>;
60
+ enabled?: boolean;
61
+ sortOrder?: number;
62
+ label?: string;
63
+ description?: string;
64
+ badge?: 'Popular' | 'Best Value' | 'New' | null;
65
+ discount?: ProductDiscount | null;
66
+ }
67
+ /**
68
+ * Product with formatted display values for UI rendering.
69
+ * Currency-specific - call getDisplayProducts(currency) to get these.
70
+ */
71
+ export interface ProductDisplay extends Product {
72
+ /** Formatted price for the user's currency (e.g., "$40.00" or "A$40.00") */
73
+ priceFormatted: string;
74
+ /** Formatted credits (e.g., "400 credits") */
75
+ creditsFormatted: string;
76
+ /** Combined display text (e.g., "400 credits for $40.00") */
77
+ displayText: string;
78
+ /** Savings info if discount is active */
79
+ savings?: {
80
+ amountCents: number;
81
+ amountFormatted: string;
82
+ percentage: number;
83
+ reason?: string;
84
+ };
85
+ /** GST breakdown for AUD pricing */
86
+ gst?: {
87
+ applicable: boolean;
88
+ rate: number;
89
+ amountCents: number;
90
+ amountFormatted: string;
91
+ netAmountCents: number;
92
+ };
93
+ }
94
+ export interface StripeProductSyncResult {
95
+ stripeProductId: string;
96
+ stripePrices: Partial<Record<CurrencyEnum, string>>;
97
+ }
98
+ //# sourceMappingURL=product.interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"product.interface.d.ts","sourceRoot":"","sources":["../../../../src/billing/interfaces/billing/product.interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,4CAA4C,CAAC;AAExE,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,OAAO,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;IACvB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;GAGG;AACH,MAAM,WAAW,OAAO;IACtB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,eAAe,CAAC,EAAE,MAA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AAE,OAAO,CAAC;KACvB,CACF,CACF,CAAC;IACF,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,SAAS,GAAG,YAAY,GAAG,KAAK,GAAG,IAAI,CAAC;IAChD,QAAQ,CAAC,EAAE,eAAe,GAAG,IAAI,CAAC;CACnC;AAED;;;GAGG;AACH,MAAM,WAAW,cAAe,SAAQ,OAAO;IAC7C,4EAA4E;IAC5E,cAAc,EAAE,MAAM,CAAC;IACvB,8CAA8C;IAC9C,gBAAgB,EAAE,MAAM,CAAC;IACzB,6DAA6D;IAC7D,WAAW,EAAE,MAAM,CAAC;IACpB,yCAAyC;IACzC,OAAO,CAAC,EAAE;QACR,WAAW,EAAE,MAAM,CAAC;QACpB,eAAe,EAAE,MAAM,CAAC;QACxB,UAAU,EAAE,MAAM,CAAC;QACnB,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB,CAAC;IACF,oCAAoC;IACpC,GAAG,CAAC,EAAE;QACJ,UAAU,EAAE,OAAO,CAAC;QACpB,IAAI,EAAE,MAAM,CAAC;QACb,WAAW,EAAE,MAAM,CAAC;QACpB,eAAe,EAAE,MAAM,CAAC;QACxB,cAAc,EAAE,MAAM,CAAC;KACxB,CAAC;CACH;AAED,MAAM,WAAW,uBAAuB;IACtC,eAAe,EAAE,MAAM,CAAC;IACxB,YAAY,EAAE,OAAO,CAAC,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC,CAAC;CACrD"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=product.interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"product.interface.js","sourceRoot":"","sources":["../../../../src/billing/interfaces/billing/product.interface.ts"],"names":[],"mappings":""}
@@ -1,6 +1,7 @@
1
1
  export * from './accountCreditManager.service';
2
2
  export * from './billingConfig.service';
3
3
  export * from './costCalculator.service';
4
+ export * from './product.service';
4
5
  export * from './stripePaymentMapper';
5
6
  export * from './topUpOptions.service';
6
7
  export * from './tokenFormatter';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/billing/services/index.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAC;AAC/C,cAAc,yBAAyB,CAAC;AACxC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/billing/services/index.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAC;AAC/C,cAAc,yBAAyB,CAAC;AACxC,cAAc,0BAA0B,CAAC;AACzC,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,kBAAkB,CAAC"}
@@ -4,6 +4,7 @@ const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./accountCreditManager.service"), exports);
5
5
  tslib_1.__exportStar(require("./billingConfig.service"), exports);
6
6
  tslib_1.__exportStar(require("./costCalculator.service"), exports);
7
+ tslib_1.__exportStar(require("./product.service"), exports);
7
8
  tslib_1.__exportStar(require("./stripePaymentMapper"), exports);
8
9
  tslib_1.__exportStar(require("./topUpOptions.service"), exports);
9
10
  tslib_1.__exportStar(require("./tokenFormatter"), exports);
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/billing/services/index.ts"],"names":[],"mappings":";;;AAAA,yEAA+C;AAC/C,kEAAwC;AACxC,mEAAyC;AACzC,gEAAsC;AACtC,iEAAuC;AACvC,2DAAiC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/billing/services/index.ts"],"names":[],"mappings":";;;AAAA,yEAA+C;AAC/C,kEAAwC;AACxC,mEAAyC;AACzC,4DAAkC;AAClC,gEAAsC;AACtC,iEAAuC;AACvC,2DAAiC"}
@@ -0,0 +1,36 @@
1
+ import { DatabaseService } from '@wipperoz/common-utils';
2
+ import { CurrencyEnum } from '@wipperoz/common-entities-account/dist/src';
3
+ import { BillingConfigService } from './billingConfig.service';
4
+ import { Product, CreateProductInput, UpdateProductInput, ProductDisplay, StripeProductSyncResult } from '../interfaces/billing/product.interface';
5
+ export declare class ProductService {
6
+ private db;
7
+ private tableName;
8
+ private billingConfigService;
9
+ constructor(db: DatabaseService, tableName: string, billingConfigService: BillingConfigService);
10
+ private getPkey;
11
+ private getSkey;
12
+ getAll(): Promise<Product[]>;
13
+ getEnabled(): Promise<Product[]>;
14
+ getById(productId: string): Promise<Product | null>;
15
+ getDisplayProducts(currency: CurrencyEnum): Promise<ProductDisplay[]>;
16
+ create(input: CreateProductInput, createdBy: string, stripeSyncResult?: StripeProductSyncResult): Promise<{
17
+ success: boolean;
18
+ product?: Product;
19
+ error?: string;
20
+ }>;
21
+ update(productId: string, input: UpdateProductInput, updatedBy: string, stripeSyncResult?: StripeProductSyncResult): Promise<{
22
+ success: boolean;
23
+ product?: Product;
24
+ error?: string;
25
+ }>;
26
+ delete(productId: string): Promise<{
27
+ success: boolean;
28
+ error?: string;
29
+ }>;
30
+ getProductForCurrency(productId: string, currency: CurrencyEnum): Promise<{
31
+ product: Product;
32
+ stripePriceId: string;
33
+ amountCents: number;
34
+ } | null>;
35
+ }
36
+ //# sourceMappingURL=product.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"product.service.d.ts","sourceRoot":"","sources":["../../../src/billing/services/product.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAC,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAC,YAAY,EAAC,MAAM,4CAA4C,CAAC;AACxE,OAAO,EAAC,oBAAoB,EAAC,MAAM,yBAAyB,CAAC;AAC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BAAqB,CACzB,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,YAAY,GACrB,OAAO,CAAC;QACT,OAAO,EAAE,OAAO,CAAC;QACjB,aAAa,EAAE,MAAM,CAAC;QACtB,WAAW,EAAE,MAAM,CAAC;KACrB,GAAG,IAAI,CAAC;CAaV"}
@@ -0,0 +1,253 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ProductService = void 0;
4
+ const src_1 = require("@wipperoz/common-entities-account/dist/src");
5
+ const GST_RATE = 0.1; // 10% GST for Australia
6
+ class ProductService {
7
+ db;
8
+ tableName;
9
+ billingConfigService;
10
+ constructor(db, tableName, billingConfigService) {
11
+ this.db = db;
12
+ this.tableName = tableName;
13
+ this.billingConfigService = billingConfigService;
14
+ }
15
+ // ==================== KEYS ====================
16
+ getPkey() {
17
+ return 'config';
18
+ }
19
+ getSkey(productId) {
20
+ return `product:${productId}`;
21
+ }
22
+ // ==================== READ ====================
23
+ async getAll() {
24
+ const result = await this.db.query({
25
+ TableName: this.tableName,
26
+ KeyConditionExpression: 'Pkey = :pkey AND begins_with(Skey, :skeyPrefix)',
27
+ ExpressionAttributeValues: {
28
+ ':pkey': this.getPkey(),
29
+ ':skeyPrefix': 'product:',
30
+ },
31
+ });
32
+ const products = (result?.Items ?? []).map(item => item.Details);
33
+ return products.sort((a, b) => a.sortOrder - b.sortOrder);
34
+ }
35
+ async getEnabled() {
36
+ const all = await this.getAll();
37
+ return all.filter(p => p.enabled);
38
+ }
39
+ async getById(productId) {
40
+ const result = await this.db.get({
41
+ TableName: this.tableName,
42
+ Key: {
43
+ Pkey: this.getPkey(),
44
+ Skey: this.getSkey(productId),
45
+ },
46
+ });
47
+ return result?.Item?.Details ?? null;
48
+ }
49
+ async getDisplayProducts(currency) {
50
+ const products = await this.getEnabled();
51
+ const currencySymbol = currency === src_1.CurrencyEnum.AUD ? 'A$' : '$';
52
+ return Promise.all(products.map(async (product) => {
53
+ const priceConfig = product.prices[currency];
54
+ const priceDollars = priceConfig?.amount ?? 0;
55
+ const priceCents = Math.round(priceDollars * 100);
56
+ const gst = await this.billingConfigService.calculateGst(priceCents, currency);
57
+ // Calculate savings if discount is active
58
+ let savings = undefined;
59
+ if (product.discount?.enabled && product.discount.originalAmount > priceDollars) {
60
+ const savingsDollars = product.discount.originalAmount - priceDollars;
61
+ const savingsCents = Math.round(savingsDollars * 100);
62
+ const savingsPercentage = Math.round((savingsDollars / product.discount.originalAmount) * 100);
63
+ savings = {
64
+ amountCents: savingsCents,
65
+ amountFormatted: `${currencySymbol}${savingsDollars.toFixed(2)}`,
66
+ percentage: savingsPercentage,
67
+ reason: product.discount.reason,
68
+ };
69
+ }
70
+ return {
71
+ ...product,
72
+ priceFormatted: `${currencySymbol}${priceDollars.toFixed(2)}`,
73
+ creditsFormatted: `${product.credits.toLocaleString()} credits`,
74
+ displayText: product.label ??
75
+ `${product.credits.toLocaleString()} credits for ${currencySymbol}${priceDollars.toFixed(2)}`,
76
+ savings,
77
+ gst: gst.applicable
78
+ ? {
79
+ ...gst,
80
+ amountFormatted: `${currencySymbol}${(gst.amountCents / 100).toFixed(2)}`,
81
+ }
82
+ : undefined,
83
+ };
84
+ }));
85
+ }
86
+ // ==================== CREATE ====================
87
+ async create(input, createdBy, stripeSyncResult) {
88
+ // Check if already exists
89
+ const existing = await this.getById(input.productId);
90
+ if (existing) {
91
+ return { success: false, error: `Product ${input.productId} already exists` };
92
+ }
93
+ // Get max sort order
94
+ const allProducts = await this.getAll();
95
+ const maxSortOrder = Math.max(0, ...allProducts.map(p => p.sortOrder));
96
+ const now = Date.now();
97
+ // Build prices with Stripe IDs and GST calculation
98
+ const prices = {};
99
+ for (const [currency, priceInput] of Object.entries(input.prices)) {
100
+ const currencyKey = currency;
101
+ if (!priceInput)
102
+ continue;
103
+ let gstAmount;
104
+ if (currencyKey === src_1.CurrencyEnum.AUD && priceInput.includesGst) {
105
+ // Calculate GST amount from GST-inclusive price
106
+ // GST = Price * (GST_RATE / (1 + GST_RATE))
107
+ gstAmount = Number((priceInput.amount * (GST_RATE / (1 + GST_RATE))).toFixed(2));
108
+ }
109
+ prices[currencyKey] = {
110
+ amount: priceInput.amount,
111
+ stripePriceId: stripeSyncResult?.stripePrices[currencyKey],
112
+ includesGst: priceInput.includesGst,
113
+ gstAmount,
114
+ };
115
+ }
116
+ const product = {
117
+ productId: input.productId,
118
+ productName: input.productName,
119
+ credits: input.credits,
120
+ stripeProductId: stripeSyncResult?.stripeProductId,
121
+ prices,
122
+ enabled: input.enabled ?? true,
123
+ sortOrder: input.sortOrder ?? maxSortOrder + 1,
124
+ label: input.label,
125
+ description: input.description,
126
+ badge: input.badge ?? null,
127
+ discount: input.discount,
128
+ createdAt: now,
129
+ updatedAt: now,
130
+ createdBy,
131
+ updatedBy: createdBy,
132
+ };
133
+ try {
134
+ await this.db.create({
135
+ TableName: this.tableName,
136
+ Item: {
137
+ Pkey: this.getPkey(),
138
+ Skey: this.getSkey(input.productId),
139
+ Details: product,
140
+ CreatedAt: now,
141
+ UpdatedAt: now,
142
+ },
143
+ ConditionExpression: 'attribute_not_exists(Pkey)',
144
+ });
145
+ return { success: true, product };
146
+ }
147
+ catch (error) {
148
+ return {
149
+ success: false,
150
+ error: error instanceof Error ? error.message : 'Create failed',
151
+ };
152
+ }
153
+ }
154
+ // ==================== UPDATE ====================
155
+ async update(productId, input, updatedBy, stripeSyncResult) {
156
+ const existing = await this.getById(productId);
157
+ if (!existing) {
158
+ return { success: false, error: `Product ${productId} not found` };
159
+ }
160
+ const now = Date.now();
161
+ // Merge prices if provided
162
+ let prices = existing.prices;
163
+ if (input.prices) {
164
+ prices = { ...existing.prices };
165
+ for (const [currency, priceInput] of Object.entries(input.prices)) {
166
+ const currencyKey = currency;
167
+ if (!priceInput)
168
+ continue;
169
+ let gstAmount;
170
+ if (currencyKey === src_1.CurrencyEnum.AUD && priceInput.includesGst) {
171
+ gstAmount = Number((priceInput.amount * (GST_RATE / (1 + GST_RATE))).toFixed(2));
172
+ }
173
+ prices[currencyKey] = {
174
+ ...prices[currencyKey],
175
+ amount: priceInput.amount,
176
+ stripePriceId: stripeSyncResult?.stripePrices[currencyKey] ?? prices[currencyKey]?.stripePriceId,
177
+ includesGst: priceInput.includesGst,
178
+ gstAmount,
179
+ };
180
+ }
181
+ }
182
+ const updatedProduct = {
183
+ ...existing,
184
+ ...input,
185
+ prices,
186
+ stripeProductId: stripeSyncResult?.stripeProductId ?? existing.stripeProductId,
187
+ updatedAt: now,
188
+ updatedBy,
189
+ // Handle null discount explicitly - convert to undefined
190
+ discount: input.discount === null ? undefined : (input.discount ?? existing.discount),
191
+ };
192
+ try {
193
+ await this.db.update({
194
+ TableName: this.tableName,
195
+ Key: {
196
+ Pkey: this.getPkey(),
197
+ Skey: this.getSkey(productId),
198
+ },
199
+ UpdateExpression: 'SET Details = :details, UpdatedAt = :updatedAt',
200
+ ExpressionAttributeValues: {
201
+ ':details': updatedProduct,
202
+ ':updatedAt': now,
203
+ },
204
+ });
205
+ return { success: true, product: updatedProduct };
206
+ }
207
+ catch (error) {
208
+ return {
209
+ success: false,
210
+ error: error instanceof Error ? error.message : 'Update failed',
211
+ };
212
+ }
213
+ }
214
+ // ==================== DELETE ====================
215
+ async delete(productId) {
216
+ const existing = await this.getById(productId);
217
+ if (!existing) {
218
+ return { success: false, error: `Product ${productId} not found` };
219
+ }
220
+ try {
221
+ await this.db.delete({
222
+ TableName: this.tableName,
223
+ Key: {
224
+ Pkey: this.getPkey(),
225
+ Skey: this.getSkey(productId),
226
+ },
227
+ });
228
+ return { success: true };
229
+ }
230
+ catch (error) {
231
+ return {
232
+ success: false,
233
+ error: error instanceof Error ? error.message : 'Delete failed',
234
+ };
235
+ }
236
+ }
237
+ // ==================== HELPERS ====================
238
+ async getProductForCurrency(productId, currency) {
239
+ const product = await this.getById(productId);
240
+ if (!product)
241
+ return null;
242
+ const priceConfig = product.prices[currency];
243
+ if (!priceConfig?.stripePriceId)
244
+ return null;
245
+ return {
246
+ product,
247
+ stripePriceId: priceConfig.stripePriceId,
248
+ amountCents: Math.round(priceConfig.amount * 100),
249
+ };
250
+ }
251
+ }
252
+ exports.ProductService = ProductService;
253
+ //# sourceMappingURL=product.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"product.service.js","sourceRoot":"","sources":["../../../src/billing/services/product.service.ts"],"names":[],"mappings":";;;AACA,oEAAwE;AAUxE,MAAM,QAAQ,GAAG,GAAG,CAAC,CAAC,wBAAwB;AAE9C,MAAa,cAAc;IACjB,EAAE,CAAkB;IACpB,SAAS,CAAS;IAClB,oBAAoB,CAAuB;IAEnD,YAAY,EAAmB,EAAE,SAAiB,EAAE,oBAA0C;QAC5F,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,oBAAoB,GAAG,oBAAoB,CAAC;IACnD,CAAC;IAED,iDAAiD;IAEzC,OAAO;QACb,OAAO,QAAQ,CAAC;IAClB,CAAC;IAEO,OAAO,CAAC,SAAiB;QAC/B,OAAO,WAAW,SAAS,EAAE,CAAC;IAChC,CAAC;IAED,iDAAiD;IAEjD,KAAK,CAAC,MAAM;QACV,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC;YACjC,SAAS,EAAE,IAAI,CAAC,SAAS;YACzB,sBAAsB,EAAE,iDAAiD;YACzE,yBAAyB,EAAE;gBACzB,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE;gBACvB,aAAa,EAAE,UAAU;aAC1B;SACF,CAAC,CAAC;QAEH,MAAM,QAAQ,GAAG,CAAC,MAAM,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,OAAkB,CAAC,CAAC;QAC5E,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,GAAG,C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