Skip to content

Commit

Permalink
feat(Simulations): Add simulation resource
Browse files Browse the repository at this point in the history
  • Loading branch information
danbillson committed Oct 3, 2024
1 parent adcca98 commit 61c686c
Show file tree
Hide file tree
Showing 21 changed files with 388 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/__tests__/mocks/resources/simulations.mock.ts
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,
},
},
};
109 changes: 109 additions & 0 deletions src/__tests__/resources/simulations.test.ts
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();
});
});
1 change: 1 addition & 0 deletions src/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ export * from './notification-settings';
export * from './notifications';
export * from './report';
export * from './simulation-types';
export * from './simulation';

export type CustomData = object;
8 changes: 8 additions & 0 deletions src/entities/simulation/index.ts
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';
15 changes: 15 additions & 0 deletions src/entities/simulation/simulation-collection.ts
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);
}
}
33 changes: 33 additions & 0 deletions src/entities/simulation/simulation.ts
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;
}
}
1 change: 1 addition & 0 deletions src/enums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * from './notification-settings';
export * from './notification';
export * from './report';
export * from './simulation-type';
export * from './simulation';
2 changes: 1 addition & 1 deletion src/enums/simulation-type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
* Changes may be overwritten as part of auto-generation.
*/

export * from './simulation-kind'
export * from './simulation-kind';
8 changes: 8 additions & 0 deletions src/enums/simulation/index.ts
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';
12 changes: 12 additions & 0 deletions src/enums/simulation/simulation-scenario-type.ts
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';
7 changes: 7 additions & 0 deletions src/enums/simulation/simulation-status.ts
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';
3 changes: 3 additions & 0 deletions src/paddle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ProductsResource,
ReportsResource,
SimulationTypesResource,
SimulationsResource,
SubscriptionsResource,
TransactionsResource,
} from './resources';
Expand Down Expand Up @@ -44,6 +45,7 @@ export class Paddle {
public notifications: NotificationsResource;
public reports: ReportsResource;
public simulationTypes: SimulationTypesResource;
public simulations: SimulationsResource;

constructor(apiKey: string, options?: PaddleOptions) {
this.client = new Client(
Expand All @@ -68,5 +70,6 @@ export class Paddle {
this.notifications = new NotificationsResource(this.client);
this.reports = new ReportsResource(this.client);
this.simulationTypes = new SimulationTypesResource(this.client);
this.simulations = new SimulationsResource(this.client);
}
}
1 change: 1 addition & 0 deletions src/resources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from './notification-settings';
export * from './notifications';
export * from './reports';
export * from './simulation-types';
export * from './simulations';
65 changes: 65 additions & 0 deletions src/resources/simulations/index.ts
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);
}
}
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;
}
9 changes: 9 additions & 0 deletions src/resources/simulations/operations/index.ts
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';
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[];
}
Loading

0 comments on commit 61c686c

Please sign in to comment.