Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Webhook Simulation resources #49

Merged
merged 11 commits into from
Oct 14, 2024
Merged

Add Webhook Simulation resources #49

merged 11 commits into from
Oct 14, 2024

Conversation

danbillson
Copy link
Contributor

@danbillson danbillson commented Oct 2, 2024

Simulation resources

Simulation Types

Relevant docs: https://developer.paddle.com/api-reference/simulation-types/overview

Retrieve a list of available simulation types

await paddle.simulationTypes.list();

Simulations

Relevant docs: https://developer.paddle.com/api-reference/simulations/overview

Retrieve a list of simulations

const collection = paddle.simulations.list();
const list = await collection.next();

Create a new simulation

const newSim = await paddle.simulations.create({
  notificationSettingId: "ntfset_01j964zn80sgz3vnnfxbakj0np",
  type: "subscription.created",
  name: "Test Simulation",
});

Fetch a single simulation

const sim = await paddle.simulations.get("ntfsim_01j990fgdbjyfqw28f19kvcgf2");

Update a simulation

const updatedSim = await paddle.simulations.update(
  "ntfsim_01j990fgdbjyfqw28f19kvcgf2",
  {
    name: "Updated Test Simulation",
  }
);

Simulation Runs

Relevant docs: https://developer.paddle.com/api-reference/simulation-runs/overview

Retrieve a list of simulation runs

const collection = paddle.simulationRuns.list();
const list = await collection.next();

Create a new simulation run

const simRun = await paddle.simulationRuns.create("ntfsim_01j990fgdbjyfqw28f19kvcgf2");

Fetch a single simulation run

const sim = await paddle.simulationRuns.get(
  "ntfsim_01j990fgdbjyfqw28f19kvcgf2", 
  "ntfsimrun_01j9c4x8kv6q6657rkbhc9a98f"
);

Simulation Run Event

https://developer.paddle.com/api-reference/simulation-events/overview

Retrieve a list of simulation runs

const collection = paddle.simulationRunEvents.list();
const list = await collection.next();

Fetch a single simulation run event

const simRunEvent = await paddle.simulationRunEvents.get(
  "ntfsim_01j990fgdbjyfqw28f19kvcgf2", 
  "ntfsimrun_01j9c4x8kv6q6657rkbhc9a98f",
  "ntfsimevt_01j9c4x8mqp3wydvqk1g2t6263
);

Replay a simulation run event

const simRunEvent = await paddle.simulationRunEvents.replay(
  "ntfsim_01j990fgdbjyfqw28f19kvcgf2", 
  "ntfsimrun_01j9c4x8kv6q6657rkbhc9a98f",
  "ntfsimevt_01j9c4x8mqp3wydvqk1g2t6263
);

@danbillson danbillson marked this pull request as ready for review October 8, 2024 08:07
@danbillson danbillson requested a review from a team as a code owner October 8, 2024 08:07
samcolby
samcolby previously approved these changes Oct 8, 2024
* feat(Simulations): narrow down simulation payload types

* refactor(Simulations): rename event map

* fix(Simulations): Include scenarios in union

* refactor(Simulations): rename DiscriminatedEventResponse
export type DiscriminatedSimulationEventResponse<Base> = {
[K in keyof SimulationEventPayloadMap]: Base & {
type: K;
payload?: K extends IEventName ? Partial<SimulationEventPayloadMap[K]['data']> | null : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to map directly to the notification entity? e.g. something like this:

Suggested change
payload?: K extends IEventName ? Partial<SimulationEventPayloadMap[K]['data']> | null : null;
payload?: K extends IEventName ? Partial<SimulationEventPayloadMap[K]> | null : null;

e.g. AddressNotification

interface SimulationEventPayloadMap {
  'address.created': AddressNotification;
  'address.updated': AddressNotification;
  'address.imported': AddressNotification;
.......

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had this conversation with Sam as well, essentially the event often changes the type i.e.
public override readonly data: Omit<SubscriptionNotification, 'transactionId'>
so it uses that instead of the raw Notification

this.notificationSettingId = simulationResponse.notification_setting_id;
this.name = simulationResponse.name;
this.type = simulationResponse.type;
this.payload = simulationResponse.payload ?? null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also create a notification entity here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Notification follows this pattern

this.payload = Webhooks.fromJson(notificationResponse.payload);

could apply that here as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the notification payload is a little different though and only cares about the data

Copy link
Collaborator

@vifer vifer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@danbillson danbillson merged commit 662553d into v1.x Oct 14, 2024
3 checks passed
@danbillson danbillson deleted the simulations branch October 14, 2024 10:57
@danbillson danbillson mentioned this pull request Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants