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

feat(types): add util to transform get response to an update request #6289

Merged
merged 5 commits into from
Feb 28, 2024

Conversation

riqwan
Copy link
Contributor

@riqwan riqwan commented Feb 1, 2024

what:

  • add util to transform get response to an update request

RESOLVES CORE-1687

Given an update data object, we can infer the fields and selects of a data object using a util we already have - getSelectsAndRelationsFromObjectArray. Using the fields and selects, we can call either the module retrieve or list method to get a snapshot of the data down to its exact attributes. We can pass this data into the revert step.

In the revert step, we just need to convert the snapshot received from retrieve or list to a shape that the update methods will accept. The util that is introduced in this PR aims to do exactly that, so that the revert step looks as simple as:

const { snapshotData, selects, relations } = revertInput

await promotionModule.updateCampaigns(
  convertItemResponseToUpdateRequest(snapshotData, selects, relations)
)

entity before update:

Campaign: {
  id: "campaign-test-1",
  name: "test campaign",
  budget: {
    total: 2000
  },
  promotions: [{ id: "promotion-1" }],
  rules: [
    { id: "rule-1", operator: "gt", value: "10" }
  ]
}

This is how the util will transform the data for different types of attributes in the object:

simple attributes:

invoke:
{
  id: "campaign-test-1",
  name: "change name",
}

compensate:
{
  id: "test-1",
  name: "test campaign"
}

one to one relationship:

invoke:
{
  id: "campaign-test-1",
  budget: { total: 4000 }
}

compensate:
{
  id: "campaign-test-1",
  budget: { total: 2000 }
}

one to many / many to many relationship:

invoke:
{
  id: "campaign-test-1",
  promotions: [{ id: "promotion-2" }]
  rules: [
    { id: "rule-1", operator: "gt", value: "20" },
    { operator: "gt", value: "20" }
  ]
}

compensate:
{
  id: "campaign-test-1",
  promotions: [{ id: "promotion-1" }]
  rules: [{ id: "rule-1", operator: "gt", value: "20" }]
}

all together:

invoke:
{
  id: "campaign-test-1",
  name: "change name",
  promotions: [{ id: "promotion-2" }],
  budget: { total: 4000 },
  rules: [
    { id: "rule-1", operator: "gt", value: "20" },
    { operator: "gt", value: "20" }
  ]
}

compensate:
{
  id: "test-1",
  name: "test campaign",
  promotions: [{ id: "promotion-1" }],
  budget: { total: 2000 },
  rules: [{ id: "rule-1", operator: "gt", value: "20" }],
}

Copy link

changeset-bot bot commented Feb 1, 2024

🦋 Changeset detected

Latest commit: 40be54d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@medusajs/utils Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Feb 1, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
medusa-dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 28, 2024 10:58am
3 Ignored Deployments
Name Status Preview Comments Updated (UTC)
api-reference ⬜️ Ignored (Inspect) Feb 28, 2024 10:58am
docs-ui ⬜️ Ignored (Inspect) Visit Preview Feb 28, 2024 10:58am
medusa-docs ⬜️ Ignored (Inspect) Visit Preview Feb 28, 2024 10:58am

@srindom
Copy link
Collaborator

srindom commented Feb 5, 2024

@riqwan - can you add more context to the functionality here. It's hard to decode as is; a few examples in the PR description would be very helpful

@olivermrbl
Copy link
Contributor

olivermrbl commented Feb 26, 2024

@riqwan, just bumping this one. Is this PR still relevant? If so, can I get you to add some examples of what this introduces?

@riqwan
Copy link
Contributor Author

riqwan commented Feb 26, 2024

Added a description, let me know if that helps 🤞🏻

Copy link
Collaborator

@srindom srindom left a comment

Choose a reason for hiding this comment

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

LGTM

@kodiakhq kodiakhq bot merged commit a6d7070 into develop Feb 28, 2024
17 checks passed
@kodiakhq kodiakhq bot deleted the feat/update-utils branch February 28, 2024 11:25
@github-actions github-actions bot mentioned this pull request Feb 28, 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.

3 participants