diff --git a/package.json b/package.json index b8fbd36a..9873e659 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@internxt/sdk", "author": "Internxt ", - "version": "1.9.6", + "version": "1.9.7", "description": "An sdk for interacting with Internxt's services", "repository": { "type": "git", diff --git a/src/drive/payments/index.ts b/src/drive/payments/index.ts index 8535ae8c..ee410261 100644 --- a/src/drive/payments/index.ts +++ b/src/drive/payments/index.ts @@ -3,6 +3,7 @@ import { headersWithToken } from '../../shared/headers'; import { HttpClient } from '../../shared/http/client'; import AppError from '../../shared/types/errors'; import { + AvailableProducts, CreateCheckoutSessionPayload, CreatedSubscriptionData, CreatePaymentSessionPayload, @@ -219,6 +220,14 @@ export class Payments { return this.client.patch('/billing', { ...payload }, this.headers()); } + /** + * Gets the available products from the user + * @returns an object containing available products + */ + public checkUserAvailableProducts(): Promise { + return this.client.get('/products', this.headers()); + } + /** * Returns the needed headers for the module requests * @private diff --git a/src/drive/payments/types.ts b/src/drive/payments/types.ts index f780d54c..b46b331f 100644 --- a/src/drive/payments/types.ts +++ b/src/drive/payments/types.ts @@ -204,3 +204,9 @@ export type CreatedSubscriptionData = { subscriptionId?: string; paymentIntentId?: string; }; + +export type AvailableProducts = { + featuresPerService: { + antivirus: boolean; + }; +}; diff --git a/test/drive/payments/index.test.ts b/test/drive/payments/index.test.ts index c5c4b003..927f5ad5 100644 --- a/test/drive/payments/index.test.ts +++ b/test/drive/payments/index.test.ts @@ -2,13 +2,12 @@ import sinon from 'sinon'; import { ApiSecurity, AppDetails } from '../../../src/shared'; import { headersWithToken } from '../../../src/shared/headers'; import { Payments } from '../../../src/drive'; -import { CreatePaymentSessionPayload, StripeSessionMode } from '../../../src/drive/payments/types'; +import { CreatePaymentSessionPayload, StripeSessionMode, UserType } from '../../../src/drive/payments/types'; import { HttpClient } from '../../../src/shared/http/client'; const httpClient = HttpClient.create(''); describe('payments service', () => { - beforeEach(() => { sinon.stub(HttpClient, 'create').returns(httpClient); }); @@ -18,11 +17,10 @@ describe('payments service', () => { }); describe('get products', () => { - it('should call with right params & return data', async () => { // Arrange const callStub = sinon.stub(httpClient, 'get').resolves({ - content: 'true' + content: 'true', }); const { client, headers } = clientAndHeadersWithToken(); @@ -30,23 +28,93 @@ describe('payments service', () => { const body = await client.getProducts(); // Assert - expect(callStub.firstCall.args).toEqual([ - '/v3/stripe/products', - headers - ]); + expect(callStub.firstCall.args).toEqual(['/v3/stripe/products', headers]); expect(body).toEqual({ - content: 'true' + content: 'true', }); }); + }); + + describe('getInvoices', () => { + afterEach(() => { + sinon.restore(); + }); + + it('should call with right params & return data', async () => { + const callStub = sinon.stub(httpClient, 'get').resolves([ + { id: 'invoice_123', amount: 1000 }, + { id: 'invoice_456', amount: 2000 }, + ]); + + const { client, headers } = clientAndHeadersWithToken(); + + const params = { + subscriptionId: 'sub_789', + startingAfter: 'invoice_123', + userType: UserType.Individual, + limit: 10, + }; + + const invoices = await client.getInvoices(params); + + const expectedQuery = new URLSearchParams({ + subscription: 'sub_789', + starting_after: 'invoice_123', + userType: UserType.Individual, + limit: '10', + }).toString(); + + expect(callStub.firstCall.args).toEqual([`/invoices?${expectedQuery}`, headers]); + + expect(invoices).toEqual([ + { id: 'invoice_123', amount: 1000 }, + { id: 'invoice_456', amount: 2000 }, + ]); + }); + + it('should handle missing optional parameters correctly', async () => { + const callStub = sinon.stub(httpClient, 'get').resolves([]); + + const { client, headers } = clientAndHeadersWithToken(); + + const params = {}; + + const expectedQuery = new URLSearchParams({ + userType: UserType.Individual, + }).toString(); + + const invoices = await client.getInvoices(params as any); + + expect(callStub.firstCall.args).toEqual([`/invoices?${expectedQuery}`, headers]); + expect(invoices).toEqual([]); + }); + }); + + describe('check if product is available', () => { + it('should call with right params & return data', async () => { + const callStub = sinon.stub(httpClient, 'get').resolves({ + featuresPerService: { + antivirus: false, + }, + }); + const { client, headers } = clientAndHeadersWithToken(); + + const body = await client.checkUserAvailableProducts(); + expect(callStub.firstCall.args).toEqual(['/products', headers]); + expect(body).toEqual({ + featuresPerService: { + antivirus: false, + }, + }); + }); }); describe('create payment session', () => { - it('should call with right params & return data', async () => { // Arrange const callStub = sinon.stub(httpClient, 'post').resolves({ - id: 'ident' + id: 'ident', }); const { client, headers } = clientAndHeadersWithToken(); const payload: CreatePaymentSessionPayload = { @@ -55,7 +123,7 @@ describe('payments service', () => { mode: StripeSessionMode.Payment, priceId: '', successUrl: '', - test: false + test: false, }; // Act @@ -72,26 +140,23 @@ describe('payments service', () => { successUrl: payload.successUrl, canceledUrl: payload.canceledUrl, }, - headers + headers, ]); expect(body).toEqual({ - id: 'ident' + id: 'ident', }); }); - }); - }); - function clientAndHeadersWithToken( apiUrl = '', clientName = 'c-name', clientVersion = '0.1', - token = 'token' + token = 'token', ): { - client: Payments, - headers: object + client: Payments; + headers: object; } { const appDetails: AppDetails = { clientName: clientName,