Skip to content

Commit

Permalink
feat(Simulations): Add simulation run events resources
Browse files Browse the repository at this point in the history
  • Loading branch information
danbillson committed Oct 7, 2024
1 parent 56102d7 commit 832cc89
Show file tree
Hide file tree
Showing 29 changed files with 363 additions and 46 deletions.
39 changes: 39 additions & 0 deletions src/__tests__/mocks/resources/simulation-run-events.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* ! Autogenerated code !
* Do not make changes to this file.
* Changes may be overwritten as part of auto-generation.
*/

import { ISimulationRunEventResponse } from '../../../types';
import { Response, ResponsePaginated } from '../../../internal';

export const SimulationRunEventMock: ISimulationRunEventResponse = {
id: 'ntfsimevt_123',
status: 'success',
event_type: 'subscription.created',
payload: {},
request: null,
response: null,
created_at: '2024-09-18T12:24:47.960617Z',
updated_at: '2024-09-18T12:24:48.309530Z',
};

export const SimulationRunEventMockResponse: Response<ISimulationRunEventResponse> = {
data: SimulationRunEventMock,
meta: {
request_id: '',
},
};

export const ListSimulationRunEventMockResponse: ResponsePaginated<ISimulationRunEventResponse> = {
data: [SimulationRunEventMock],
meta: {
request_id: '',
pagination: {
estimated_total: 10,
has_more: true,
next: '/simulations/ntfsim_123/runs/ntfsimrun_123/events?after=1',
per_page: 10,
},
},
};
93 changes: 93 additions & 0 deletions src/__tests__/resources/simulation-run-events.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* ! Autogenerated code !
* Do not make changes to this file.
* Changes may be overwritten as part of auto-generation.
*/

import { SimulationRunEventsResource, type ListSimulationRunEventsQueryParameters } from '../../resources';
import { getPaddleTestClient } from '../helpers/test-client';
import {
SimulationRunEventMockResponse,
SimulationRunEventMock,
ListSimulationRunEventMockResponse,
} from '../mocks/resources/simulation-run-events.mock';
import { QueryParameters } from '../../internal/base';

const simulationId = 'ntfsim_123';
const simulationRunId = 'ntfsimrun_123';
const simulationRunEventId = 'ntfsimevent_123';

describe('SimulationRunEventsResource', () => {
test('should return a list of simulationRunEvents', async () => {
const paddleInstance = getPaddleTestClient();
paddleInstance.get = jest.fn().mockResolvedValue(ListSimulationRunEventMockResponse);

const simulationRunEventsResource = new SimulationRunEventsResource(paddleInstance);
const simulationRunEventCollection = simulationRunEventsResource.list(simulationId, simulationRunId);

let simulationRunEvents = await simulationRunEventCollection.next();
expect(paddleInstance.get).toBeCalledWith(`/simulations/${simulationId}/runs/${simulationRunId}/events?`);
expect(simulationRunEvents.length).toBe(1);

simulationRunEvents = await simulationRunEventCollection.next();
expect(paddleInstance.get).toBeCalledWith(`/simulations/${simulationId}/runs/${simulationRunId}/events?after=1`);
expect(simulationRunEvents.length).toBe(1);
});

test('should accept query params and return a list of simulationRunEvents', async () => {
const paddleInstance = getPaddleTestClient();
paddleInstance.get = jest.fn().mockResolvedValue(ListSimulationRunEventMockResponse);
const simulationRunEventsResource = new SimulationRunEventsResource(paddleInstance);
const queryParams: ListSimulationRunEventsQueryParameters = {
after: '2',
id: ['1234'],
};

const simulationRunEventCollection = simulationRunEventsResource.list(simulationId, simulationRunId, queryParams);
let simulationRunEvents = await simulationRunEventCollection.next();

expect(paddleInstance.get).toBeCalledWith(
`/simulations/${simulationId}/runs/${simulationRunId}/events?after=2&id=1234`,
);
expect(simulationRunEvents.length).toBe(1);
});

test('should return a single simulationRunEvent by ID', async () => {
const simulationRunEventId = SimulationRunEventMock.id;
const paddleInstance = getPaddleTestClient();
paddleInstance.get = jest.fn().mockResolvedValue(SimulationRunEventMockResponse);

const simulationRunEventsResource = new SimulationRunEventsResource(paddleInstance);
const simulationRunEvent = await simulationRunEventsResource.get(
simulationId,
simulationRunId,
simulationRunEventId,
);

expect(paddleInstance.get).toBeCalledWith(
`/simulations/${simulationId}/runs/${simulationRunId}/events/${simulationRunEventId}`,
);
expect(simulationRunEvent).toBeDefined();
expect(simulationRunEvent.id).toBe(simulationRunEventId);
});

test('should replay an existing simulationRunEvent', async () => {
const simulationRunEventId = SimulationRunEventMock.id;

const paddleInstance = getPaddleTestClient();
paddleInstance.post = jest.fn().mockResolvedValue(SimulationRunEventMockResponse);

const simulationRunEventsResource = new SimulationRunEventsResource(paddleInstance);
const replayedSimulationRunEvent = await simulationRunEventsResource.replay(
simulationId,
simulationRunId,
simulationRunEventId,
);

expect(paddleInstance.post).toBeCalledWith(
`/simulations/${simulationId}/runs/${simulationRunId}/events/${simulationRunEventId}/replay`,
undefined,
);
expect(replayedSimulationRunEvent).toBeDefined();
});
});
1 change: 1 addition & 0 deletions src/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ export * from './report';
export * from './simulation-types';
export * from './simulation';
export * from './simulation-run';
export * from './simulation-run-event';

export type CustomData = object;
2 changes: 2 additions & 0 deletions src/entities/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ export * from './transaction-checkout';
export * from './total-adjustments';
export * from './payout-totals-adjustment';
export * from './import-meta';
export * from './simulation-event-request';
export * from './simulation-event-response';
15 changes: 15 additions & 0 deletions src/entities/shared/simulation-event-request.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 { type ISimulationEventRequest } from '../../types';

export class SimulationEventRequest {
public readonly body: string;

constructor(simulationEventRequestResponse: ISimulationEventRequest) {
this.body = simulationEventRequestResponse.body;
}
}
17 changes: 17 additions & 0 deletions src/entities/shared/simulation-event-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* ! Autogenerated code !
* Do not make changes to this file.
* Changes may be overwritten as part of auto-generation.
*/

import { type ISimulationEventResponse } from '../../types';

export class SimulationEventResponse {
public readonly body: string;
public readonly statusCode: number;

constructor(simulationEventResponse: ISimulationEventResponse) {
this.body = simulationEventResponse.body;
this.statusCode = simulationEventResponse.status_code;
}
}
8 changes: 8 additions & 0 deletions src/entities/simulation-run-event/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-run-event';
export * from './simulation-run-event-collection';
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 { SimulationRunEvent } from '../../entities';
import { type ISimulationRunEventResponse } from '../../types';
import { Collection } from '../../internal/base';

export class SimulationRunEventCollection extends Collection<ISimulationRunEventResponse, SimulationRunEvent> {
override fromJson(data: ISimulationRunEventResponse): SimulationRunEvent {
return new SimulationRunEvent(data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,32 @@
* Changes may be overwritten as part of auto-generation.
*/

import type { SimulationRunEventsStatus, SimulationScenarioType } from '../../enums';
import type { SimulationRunEventStatus, SimulationScenarioType } from '../../enums';
import type { IEventName } from '../../notifications';
import { type ISimulationRunEvent } from '../../types';
import type { ISimulationRunEventResponse } from '../../types';
import { SimulationEventRequest, SimulationEventResponse } from '../shared';

export class SimulationRunEvent {
public readonly id: string;
public readonly status: SimulationRunEventsStatus;
public readonly status: SimulationRunEventStatus;
public readonly eventType: IEventName | SimulationScenarioType;
public readonly payload: any;
public readonly request: {
body: string;
} | null;

public readonly response: {
body: string;
status_code: number;
} | null;

public readonly request: SimulationEventRequest | null;
public readonly response: SimulationEventResponse | null;
public readonly createdAt: string;
public readonly updatedAt: string;

constructor(simulationRunEventResponse: ISimulationRunEvent) {
constructor(simulationRunEventResponse: ISimulationRunEventResponse) {
this.id = simulationRunEventResponse.id;
this.status = simulationRunEventResponse.status;
this.eventType = simulationRunEventResponse.event_type;
this.payload = simulationRunEventResponse.payload;
this.request = simulationRunEventResponse.request ?? null;
this.response = simulationRunEventResponse.response ?? null;
this.request = simulationRunEventResponse.request
? new SimulationEventRequest(simulationRunEventResponse.request)
: null;
this.response = simulationRunEventResponse.response
? new SimulationEventResponse(simulationRunEventResponse.response)
: null;
this.createdAt = simulationRunEventResponse.created_at;
this.updatedAt = simulationRunEventResponse.updated_at;
}
Expand Down
1 change: 0 additions & 1 deletion src/entities/simulation-run/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@

export * from './simulation-run';
export * from './simulation-run-collection';
export * from './simulation-run-event';
2 changes: 1 addition & 1 deletion src/entities/simulation-run/simulation-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import type { SimulationRunStatus, SimulationScenarioType } from '../../enums';
import type { IEventName } from '../../notifications';
import type { ISimulationRunResponse } from '../../types';
import { SimulationRunEvent } from './simulation-run-event';
import { SimulationRunEvent } from '..';

export class SimulationRun {
public readonly id: string;
Expand Down
1 change: 1 addition & 0 deletions src/enums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export * from './report';
export * from './simulation-type';
export * from './simulation';
export * from './simulation-run';
export * from './simulation-run-event';
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 type SimulationRunEventsStatus = 'pending' | 'success' | 'failed' | 'aborted';
export * from './simulation-run-event-status';
7 changes: 7 additions & 0 deletions src/enums/simulation-run-event/simulation-run-event-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 SimulationRunEventStatus = 'pending' | 'success' | 'failed' | 'aborted';
1 change: 0 additions & 1 deletion src/enums/simulation-run/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
* Changes may be overwritten as part of auto-generation.
*/

export * from './simulation-run-events-status';
export * from './simulation-run-status';
3 changes: 3 additions & 0 deletions src/paddle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
SimulationTypesResource,
SimulationsResource,
SimulationRunsResource,
SimulationRunEventsResource,
SubscriptionsResource,
TransactionsResource,
} from './resources';
Expand Down Expand Up @@ -48,6 +49,7 @@ export class Paddle {
public simulationTypes: SimulationTypesResource;
public simulations: SimulationsResource;
public simulationRuns: SimulationRunsResource;
public simulationRunEvents: SimulationRunEventsResource;

constructor(apiKey: string, options?: PaddleOptions) {
this.client = new Client(
Expand All @@ -74,5 +76,6 @@ export class Paddle {
this.simulationTypes = new SimulationTypesResource(this.client);
this.simulations = new SimulationsResource(this.client);
this.simulationRuns = new SimulationRunsResource(this.client);
this.simulationRunEvents = new SimulationRunEventsResource(this.client);
}
}
1 change: 1 addition & 0 deletions src/resources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from './reports';
export * from './simulation-types';
export * from './simulations';
export * from './simulation-runs';
export * from './simulation-run-events';
76 changes: 76 additions & 0 deletions src/resources/simulation-run-events/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* ! Autogenerated code !
* Do not make changes to this file.
* Changes may be overwritten as part of auto-generation.
*/

import { SimulationRunEvent, SimulationRunEventCollection } from '../../entities';
import { type ISimulationRunEventResponse } from '../../types';
import { type ErrorResponse, type Response } from '../../internal';
import { BaseResource, PathParameters, QueryParameters } from '../../internal/base';
import type { ListSimulationRunEventsQueryParameters } from './operations';

export * from './operations';

const SimulationRunEventPaths = {
list: '/simulations/{simulation_id}/runs/{simulation_run_id}/events',
get: '/simulations/{simulation_id}/runs/{simulation_run_id}/events/{simulation_event_id}',
replay: '/simulations/{simulation_id}/runs/{simulation_run_id}/events/{simulation_event_id}/replay',
} as const;

export class SimulationRunEventsResource extends BaseResource {
public list(
simulationId: string,
simulationRunId: string,
queryParams?: ListSimulationRunEventsQueryParameters,
): SimulationRunEventCollection {
const queryParameters = new QueryParameters(queryParams);
const urlWithPathParams = new PathParameters(SimulationRunEventPaths.list, {
simulation_id: simulationId,
simulation_run_id: simulationRunId,
}).deriveUrl();

return new SimulationRunEventCollection(this.client, urlWithPathParams + queryParameters.toQueryString());
}

public async get(
simulationId: string,
simulationRunId: string,
simulationEventId: string,
): Promise<SimulationRunEvent> {
const urlWithPathParams = new PathParameters(SimulationRunEventPaths.get, {
simulation_id: simulationId,
simulation_run_id: simulationRunId,
simulation_event_id: simulationEventId,
}).deriveUrl();

const response = await this.client.get<undefined, Response<ISimulationRunEventResponse> | ErrorResponse>(
urlWithPathParams,
);

const data = this.handleResponse<ISimulationRunEventResponse>(response);

return new SimulationRunEvent(data);
}

public async replay(
simulationId: string,
simulationRunId: string,
simulationEventId: string,
): Promise<SimulationRunEvent> {
const urlWithPathParams = new PathParameters(SimulationRunEventPaths.replay, {
simulation_id: simulationId,
simulation_run_id: simulationRunId,
simulation_event_id: simulationEventId,
}).deriveUrl();

const response = await this.client.post<undefined, Response<ISimulationRunEventResponse> | ErrorResponse>(
urlWithPathParams,
undefined,
);

const data = this.handleResponse<ISimulationRunEventResponse>(response);

return new SimulationRunEvent(data);
}
}
Loading

0 comments on commit 832cc89

Please sign in to comment.