-
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(Simulations): Add simulation resource
- Loading branch information
1 parent
adcca98
commit 61c686c
Showing
21 changed files
with
388 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
import { ISimulationResponse } from '../../../types'; | ||
import { Response, ResponsePaginated } from '../../../internal'; | ||
|
||
export const SimulationMock: ISimulationResponse = { | ||
id: 'ntfsim_01ghbkd0frb9k95cnhwd1bxpvk', | ||
status: 'active', | ||
notification_setting_id: 'ntfset_01gt21c5pdx9q1e4mh1xrsjjn6', | ||
name: 'Julie', | ||
type: 'address.created', | ||
payload: null, | ||
last_run_at: '2024-10-12T07:20:50.52Z', | ||
created_at: '2024-10-12T07:20:50.52Z', | ||
updated_at: '2024-10-13T07:20:50.52Z', | ||
}; | ||
|
||
export const SimulationMockResponse: Response<ISimulationResponse> = { | ||
data: SimulationMock, | ||
meta: { | ||
request_id: '', | ||
}, | ||
}; | ||
|
||
export const ListSimulationMockResponse: ResponsePaginated<ISimulationResponse> = { | ||
data: [SimulationMock], | ||
meta: { | ||
request_id: '', | ||
pagination: { | ||
estimated_total: 10, | ||
has_more: true, | ||
next: '/simulations?after=1', | ||
per_page: 10, | ||
}, | ||
}, | ||
}; |
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,109 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
import { | ||
CreateSimulation, | ||
UpdateSimulation, | ||
SimulationsResource, | ||
ListSimulationQueryParameters, | ||
} from '../../resources'; | ||
import { getPaddleTestClient } from '../helpers/test-client'; | ||
import { | ||
SimulationMockResponse, | ||
SimulationMock, | ||
ListSimulationMockResponse, | ||
} from '../mocks/resources/simulations.mock'; | ||
|
||
describe('SimulationsResource', () => { | ||
test('should return a list of simulations', async () => { | ||
const paddleInstance = getPaddleTestClient(); | ||
paddleInstance.get = jest.fn().mockResolvedValue(ListSimulationMockResponse); | ||
|
||
const simulationsResource = new SimulationsResource(paddleInstance); | ||
const simulationCollection = simulationsResource.list(); | ||
|
||
let simulations = await simulationCollection.next(); | ||
expect(paddleInstance.get).toBeCalledWith('/simulations?'); | ||
expect(simulations.length).toBe(1); | ||
|
||
simulations = await simulationCollection.next(); | ||
expect(paddleInstance.get).toBeCalledWith('/simulations?after=1'); | ||
expect(simulations.length).toBe(1); | ||
}); | ||
|
||
test('should accept query params and return a list of simulations', async () => { | ||
const paddleInstance = getPaddleTestClient(); | ||
paddleInstance.get = jest.fn().mockResolvedValue(ListSimulationMockResponse); | ||
const simulationsResource = new SimulationsResource(paddleInstance); | ||
const queryParams: ListSimulationQueryParameters = { | ||
after: '2', | ||
id: ['1234'], | ||
}; | ||
|
||
const simulationCollection = simulationsResource.list(queryParams); | ||
let simulations = await simulationCollection.next(); | ||
|
||
expect(paddleInstance.get).toBeCalledWith('/simulations?after=2&id=1234'); | ||
expect(simulations.length).toBe(1); | ||
}); | ||
|
||
test('should return a single simulation by ID', async () => { | ||
const simulationId = SimulationMock.id; | ||
const paddleInstance = getPaddleTestClient(); | ||
paddleInstance.get = jest.fn().mockResolvedValue(SimulationMockResponse); | ||
|
||
const simulationsResource = new SimulationsResource(paddleInstance); | ||
const simulation = await simulationsResource.get(simulationId); | ||
|
||
expect(paddleInstance.get).toBeCalledWith(`/simulations/${simulationId}`); | ||
expect(simulation).toBeDefined(); | ||
expect(simulation.id).toBe(simulationId); | ||
}); | ||
|
||
test('should accepts query params and return a single simulation by ID', async () => { | ||
const simulationId = SimulationMock.id; | ||
const paddleInstance = getPaddleTestClient(); | ||
|
||
paddleInstance.get = jest.fn().mockResolvedValue(SimulationMockResponse); | ||
const simulationsResource = new SimulationsResource(paddleInstance); | ||
|
||
const simulation = await simulationsResource.get(simulationId); | ||
|
||
expect(simulation).toBeDefined(); | ||
expect(paddleInstance.get).toBeCalledWith(`/simulations/${simulationId}`); | ||
expect(simulation.id).toBe(simulationId); | ||
}); | ||
|
||
test('should create a new simulation', async () => { | ||
const newSimulation: CreateSimulation = SimulationMock; | ||
const paddleInstance = getPaddleTestClient(); | ||
|
||
paddleInstance.post = jest.fn().mockResolvedValue(SimulationMockResponse); | ||
const simulationsResource = new SimulationsResource(paddleInstance); | ||
const createdSimulation = await simulationsResource.create(newSimulation); | ||
|
||
expect(paddleInstance.post).toBeCalledWith(`/simulations`, newSimulation); | ||
expect(createdSimulation).toBeDefined(); | ||
expect(createdSimulation.id).toBeDefined(); | ||
expect(createdSimulation.name).toBe(newSimulation.name); | ||
}); | ||
|
||
test('should update an existing simulation', async () => { | ||
const simulationId = SimulationMock.id; | ||
const simulationToBeUpdated: UpdateSimulation = { | ||
name: 'Updated Simulation', | ||
}; | ||
|
||
const paddleInstance = getPaddleTestClient(); | ||
paddleInstance.patch = jest.fn().mockResolvedValue(SimulationMockResponse); | ||
|
||
const simulationsResource = new SimulationsResource(paddleInstance); | ||
const updatedSimulation = await simulationsResource.update(simulationId, simulationToBeUpdated); | ||
|
||
expect(paddleInstance.patch).toBeCalledWith(`/simulations/${simulationId}`, simulationToBeUpdated); | ||
expect(updatedSimulation).toBeDefined(); | ||
}); | ||
}); |
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,8 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
export * from './simulation'; | ||
export * from './simulation-collection'; |
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 { Simulation } from '../../entities'; | ||
import { type ISimulationResponse } from '../../types'; | ||
import { Collection } from '../../internal/base'; | ||
|
||
export class SimulationCollection extends Collection<ISimulationResponse, Simulation> { | ||
override fromJson(data: ISimulationResponse): Simulation { | ||
return new Simulation(data); | ||
} | ||
} |
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,33 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
import { type ISimulationResponse } from '../../types'; | ||
import type { SimulationScenarioType, SimulationStatus } from '../../enums'; | ||
import type { IEventName } from '../../notifications'; | ||
|
||
export class Simulation { | ||
public readonly id: string; | ||
public readonly status: SimulationStatus; | ||
public readonly notificationSettingId: string; | ||
public readonly name: string; | ||
public readonly type: IEventName | SimulationScenarioType; | ||
public readonly payload: any; | ||
public readonly lastRunAt: string | null; | ||
public readonly createdAt: string; | ||
public readonly updatedAt: string; | ||
|
||
constructor(simulationResponse: ISimulationResponse) { | ||
this.id = simulationResponse.id; | ||
this.status = simulationResponse.status; | ||
this.notificationSettingId = simulationResponse.notification_setting_id; | ||
this.name = simulationResponse.name; | ||
this.type = simulationResponse.type; | ||
this.payload = simulationResponse.payload ?? null; | ||
this.lastRunAt = simulationResponse.last_run_at ?? null; | ||
this.createdAt = simulationResponse.created_at; | ||
this.updatedAt = simulationResponse.updated_at; | ||
} | ||
} |
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,8 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
export * from './simulation-status'; | ||
export * from './simulation-scenario-type'; |
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,12 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
export type SimulationScenarioType = | ||
| 'subscription_creation' | ||
| 'subscription_renewal' | ||
| 'subscription_pause' | ||
| 'subscription_resume' | ||
| 'subscription_cancellation'; |
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 type SimulationStatus = 'active' | 'archived'; |
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,65 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
import { Simulation, SimulationCollection } from '../../entities'; | ||
import { type ErrorResponse, type Response } from '../../internal'; | ||
import { BaseResource, PathParameters, QueryParameters } from '../../internal/base'; | ||
import { type ISimulationResponse } from '../../types'; | ||
import { type CreateSimulation, type ListSimulationQueryParameters, type UpdateSimulation } from './operations'; | ||
|
||
export * from './operations'; | ||
|
||
const SimulationPaths = { | ||
list: '/simulations', | ||
create: '/simulations', | ||
get: '/simulations/{simulation_id}', | ||
update: '/simulations/{simulation_id}', | ||
} as const; | ||
|
||
export class SimulationsResource extends BaseResource { | ||
public list(queryParams?: ListSimulationQueryParameters): SimulationCollection { | ||
const queryParameters = new QueryParameters(queryParams); | ||
return new SimulationCollection(this.client, SimulationPaths.list + queryParameters.toQueryString()); | ||
} | ||
|
||
public async create(createSimulationParameters: CreateSimulation): Promise<Simulation> { | ||
const response = await this.client.post<CreateSimulation, Response<ISimulationResponse> | ErrorResponse>( | ||
SimulationPaths.create, | ||
createSimulationParameters, | ||
); | ||
|
||
const data = this.handleResponse<ISimulationResponse>(response); | ||
|
||
return new Simulation(data); | ||
} | ||
|
||
public async get(simulationId: string): Promise<Simulation> { | ||
const urlWithPathParams = new PathParameters(SimulationPaths.get, { | ||
simulation_id: simulationId, | ||
}).deriveUrl(); | ||
|
||
const response = await this.client.get<undefined, Response<ISimulationResponse> | ErrorResponse>(urlWithPathParams); | ||
|
||
const data = this.handleResponse<ISimulationResponse>(response); | ||
|
||
return new Simulation(data); | ||
} | ||
|
||
public async update(simulationId: string, updateSimulation: UpdateSimulation): Promise<Simulation> { | ||
const urlWithPathParams = new PathParameters(SimulationPaths.update, { | ||
simulation_id: simulationId, | ||
}).deriveUrl(); | ||
|
||
const response = await this.client.patch<UpdateSimulation, Response<ISimulationResponse> | ErrorResponse>( | ||
urlWithPathParams, | ||
updateSimulation, | ||
); | ||
|
||
const data = this.handleResponse<ISimulationResponse>(response); | ||
|
||
return new Simulation(data); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/resources/simulations/operations/create-simulation-request-body.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,15 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
import type { SimulationScenarioType } from '../../../enums'; | ||
import type { IEventName } from '../../../notifications'; | ||
|
||
export interface CreateSimulation { | ||
notificationSettingId: string; | ||
type: IEventName | SimulationScenarioType; | ||
name: string; | ||
payload?: any; | ||
} |
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 * from './create-simulation-request-body'; | ||
export * from './list-simulation-query-parameters'; | ||
export * from './update-simulation-request-body'; |
16 changes: 16 additions & 0 deletions
16
src/resources/simulations/operations/list-simulation-query-parameters.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,16 @@ | ||
/** | ||
* ! Autogenerated code ! | ||
* Do not make changes to this file. | ||
* Changes may be overwritten as part of auto-generation. | ||
*/ | ||
|
||
import { type SimulationStatus } from '../../../enums'; | ||
|
||
export interface ListSimulationQueryParameters { | ||
after?: string; | ||
notificationSettingId?: string[]; | ||
orderBy?: string; | ||
perPage?: number; | ||
id?: string[]; | ||
status?: SimulationStatus[]; | ||
} |
Oops, something went wrong.