-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add customer portal session resource * chore: remove unused mock Co-authored-by: davidgrayston-paddle <[email protected]> * test: add url assertion --------- Co-authored-by: davidgrayston-paddle <[email protected]>
- Loading branch information
1 parent
af27a2f
commit a3a192d
Showing
21 changed files
with
273 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/__tests__/mocks/resources/customer-portal-sessions.mock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
import { ICustomerPortalSessionResponse } from '../../../types/index.js'; | ||
import { Response, ResponsePaginated } from '../../../internal/index.js'; | ||
|
||
export const CustomerPortalSessionMock: ICustomerPortalSessionResponse = { | ||
id: 'cpls_01h4ge9r64c22exjsx0fy8b48b', | ||
customer_id: 'ctm_123', | ||
urls: { | ||
general: { | ||
overview: | ||
'https://customer-portal.paddle.com/cpl_01j7zbyqs3vah3aafp4jf62qaw?action=overview&token=pga_eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJjdG1fMDFncm5uNHp0YTVhMW1mMDJqanplN3kyeXMiLCJuYW1lIjoiSm9obiBEb2UiLCJpYXQiOjE3Mjc2NzkyMzh9._oO12IejzdKmyKTwb7BLjmiILkx4_cSyGjXraOBUI_g', | ||
}, | ||
subscriptions: [ | ||
{ | ||
id: 'sub_123', | ||
cancel_subscription: | ||
'https://customer-portal.paddle.com/cpl_01j7zbyqs3vah3aafp4jf62qaw?action=cancel_subscription&subscription_id=sub_01h04vsc0qhwtsbsxh3422wjs4&token=pga_eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJjdG1fMDFncm5uNHp0YTVhMW1mMDJqanplN3kyeXMiLCJuYW1lIjoiSm9obiBEb2UiLCJpYXQiOjE3Mjc2NzkyMzh9._oO12IejzdKmyKTwb7BLjmiILkx4_cSyGjXraOBUI_g', | ||
update_subscription_payment_method: | ||
'https://customer-portal.paddle.com/cpl_01j7zbyqs3vah3aafp4jf62qaw?action=update_subscription_payment_method&subscription_id=sub_01h04vsc0qhwtsbsxh3422wjs4&token=pga_eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJjdG1fMDFncm5uNHp0YTVhMW1mMDJqanplN3kyeXMiLCJuYW1lIjoiSm9obiBEb2UiLCJpYXQiOjE3Mjc2NzkyMzh9._oO12IejzdKmyKTwb7BLjmiILkx4_cSyGjXraOBUI_g', | ||
}, | ||
], | ||
}, | ||
created_at: '2024-10-25T06:53:58Z', | ||
}; | ||
|
||
export const CustomerPortalSessionMockResponse: Response<ICustomerPortalSessionResponse> = { | ||
data: CustomerPortalSessionMock, | ||
meta: { | ||
request_id: '', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
import { CustomerPortalSessionsResource } from '../../resources/index.js'; | ||
import { getPaddleTestClient } from '../helpers/test-client.js'; | ||
import { | ||
CustomerPortalSessionMockResponse, | ||
CustomerPortalSessionMock, | ||
} from '../mocks/resources/customer-portal-sessions.mock.js'; | ||
|
||
describe('CustomerPortalSessionsResource', () => { | ||
test('should create a new customer portal session', async () => { | ||
const customerId = CustomerPortalSessionMock.customer_id; | ||
const subscriptionIds = ['sub_123']; | ||
const paddleInstance = getPaddleTestClient(); | ||
paddleInstance.post = jest.fn().mockResolvedValue(CustomerPortalSessionMockResponse); | ||
|
||
const customerPortalSessionsResource = new CustomerPortalSessionsResource(paddleInstance); | ||
const createdCustomerPortalSession = await customerPortalSessionsResource.create(customerId, subscriptionIds); | ||
|
||
expect(paddleInstance.post).toBeCalledWith(`/customers/${customerId}/portal-sessions`, { | ||
subscriptionIds, | ||
}); | ||
expect(createdCustomerPortalSession).toBeDefined(); | ||
expect(createdCustomerPortalSession.id).toBeDefined(); | ||
expect(createdCustomerPortalSession.customerId).toBe(customerId); | ||
expect(createdCustomerPortalSession.urls.subscriptions[0].id).toBe(subscriptionIds[0]); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
src/entities/customer-portal-session/customer-portal-session.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
import { type ICustomerPortalSessionResponse } from '../../types/index.js'; | ||
import { Urls } from './urls.js'; | ||
|
||
export class CustomerPortalSession { | ||
public readonly id: string; | ||
public readonly customerId: string | null; | ||
public readonly urls: Urls; | ||
public readonly createdAt: string; | ||
|
||
constructor(customerPortalSessionResponse: ICustomerPortalSessionResponse) { | ||
this.id = customerPortalSessionResponse.id; | ||
this.customerId = customerPortalSessionResponse.customer_id; | ||
this.urls = new Urls(customerPortalSessionResponse.urls); | ||
this.createdAt = customerPortalSessionResponse.created_at; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/entities/customer-portal-session/customer-portal-subscription-url.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
import { type ICustomerPortalSubscriptionUrl } from '../../types/index.js'; | ||
|
||
export class CustomerPortalSubscriptionUrl { | ||
public readonly id: string; | ||
public readonly cancelSubscription: string; | ||
public readonly updateSubscriptionPaymentMethod: string; | ||
|
||
constructor(customerPortalSubscriptionUrlResponse: ICustomerPortalSubscriptionUrl) { | ||
this.id = customerPortalSubscriptionUrlResponse.id; | ||
this.cancelSubscription = customerPortalSubscriptionUrlResponse.cancel_subscription; | ||
this.updateSubscriptionPaymentMethod = customerPortalSubscriptionUrlResponse.update_subscription_payment_method; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
import { type IGeneralResponse } from '../../types/index.js'; | ||
|
||
export class General { | ||
public readonly overview: string; | ||
|
||
constructor(generalResponse: IGeneralResponse) { | ||
this.overview = generalResponse.overview; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
export * from './customer-portal-subscription-url.js'; | ||
export * from './customer-portal-session.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
import { type IUrlsResponse } from '../../types/index.js'; | ||
import { General } from './general.js'; | ||
import { CustomerPortalSubscriptionUrl } from './customer-portal-subscription-url.js'; | ||
|
||
export class Urls { | ||
public readonly general: General; | ||
public readonly subscriptions: CustomerPortalSubscriptionUrl[]; | ||
|
||
constructor(urlsResponse: IUrlsResponse) { | ||
this.general = new General(urlsResponse.general); | ||
this.subscriptions = urlsResponse.subscriptions?.map( | ||
(subscription) => new CustomerPortalSubscriptionUrl(subscription) ?? [], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
import { CustomerPortalSession } from '../../entities/index.js'; | ||
import { type ICustomerPortalSessionResponse } from '../../types/index.js'; | ||
import { type ErrorResponse, type Response } from '../../internal'; | ||
import { BaseResource, PathParameters } from '../../internal/base'; | ||
import { type CreateCustomerPortalSessionRequestBody } from './operations/index.js'; | ||
|
||
const CustomerPortalSessionPaths = { | ||
create: '/customers/{customer_id}/portal-sessions', | ||
} as const; | ||
|
||
export class CustomerPortalSessionsResource extends BaseResource { | ||
public async create(customerId: string, subscriptionIds: string[]): Promise<CustomerPortalSession> { | ||
const urlWithPathParams = new PathParameters(CustomerPortalSessionPaths.create, { | ||
customer_id: customerId, | ||
}).deriveUrl(); | ||
|
||
const response = await this.client.post< | ||
CreateCustomerPortalSessionRequestBody, | ||
Response<ICustomerPortalSessionResponse> | ErrorResponse | ||
>(urlWithPathParams, { | ||
subscriptionIds, | ||
}); | ||
|
||
const data = this.handleResponse<ICustomerPortalSessionResponse>(response); | ||
|
||
return new CustomerPortalSession(data); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...rces/customer-portal-sessions/operations/create-customer-portal-session-request-object.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
export interface CreateCustomerPortalSessionRequestBody { | ||
subscriptionIds: string[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
export * from './create-customer-portal-session-request-object.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/types/customer-portal-session/customer-portal-session-response.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
import { type IUrlsResponse } from './urls.js'; | ||
|
||
export interface ICustomerPortalSessionResponse { | ||
id: string; | ||
customer_id: string; | ||
urls: IUrlsResponse; | ||
created_at: string; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/types/customer-portal-session/customer-portal-subscription-url.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
export interface ICustomerPortalSubscriptionUrl { | ||
id: string; | ||
cancel_subscription: string; | ||
update_subscription_payment_method: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
export interface IGeneralResponse { | ||
overview: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
export * from './general.js'; | ||
export * from './customer-portal-subscription-url.js'; | ||
export * from './urls.js'; | ||
export * from './customer-portal-session-response.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
import { type IGeneralResponse } from './general.js'; | ||
import { type ICustomerPortalSubscriptionUrl } from './customer-portal-subscription-url.js'; | ||
|
||
export interface IUrlsResponse { | ||
general: IGeneralResponse; | ||
subscriptions: ICustomerPortalSubscriptionUrl[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters