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

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" }],
}
```
This commit is contained in:
Riqwan Thamir
2024-02-28 16:55:50 +05:30
committed by GitHub
parent c5d35ec7f2
commit a6d7070dd6
6 changed files with 205 additions and 14 deletions

View File

@@ -1,6 +1,9 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPromotionModuleService, UpdateCampaignDTO } from "@medusajs/types"
import { getSelectsAndRelationsFromObjectArray } from "@medusajs/utils"
import {
convertItemResponseToUpdateRequest,
getSelectsAndRelationsFromObjectArray,
} from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const updateCampaignsStepId = "update-campaigns"
@@ -19,19 +22,27 @@ export const updateCampaignsStep = createStep(
const updatedCampaigns = await promotionModule.updateCampaigns(data)
return new StepResponse(updatedCampaigns, dataBeforeUpdate)
return new StepResponse(updatedCampaigns, {
dataBeforeUpdate,
selects,
relations,
})
},
async (dataBeforeUpdate, { container }) => {
if (!dataBeforeUpdate) {
async (revertInput, { container }) => {
if (!revertInput) {
return
}
const { dataBeforeUpdate, selects, relations } = revertInput
const promotionModule = container.resolve<IPromotionModuleService>(
ModuleRegistrationName.PROMOTION
)
// TODO: This still requires some sanitation of data and transformation of
// shapes for manytomany and oneToMany relations. Create a common util.
await promotionModule.updateCampaigns(dataBeforeUpdate)
await promotionModule.updateCampaigns(
dataBeforeUpdate.map((data) =>
convertItemResponseToUpdateRequest(data, selects, relations)
)
)
}
)

View File

@@ -1,6 +1,9 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPromotionModuleService, UpdatePromotionDTO } from "@medusajs/types"
import { getSelectsAndRelationsFromObjectArray } from "@medusajs/utils"
import {
convertItemResponseToUpdateRequest,
getSelectsAndRelationsFromObjectArray,
} from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const updatePromotionsStepId = "update-promotions"
@@ -19,19 +22,27 @@ export const updatePromotionsStep = createStep(
const updatedPromotions = await promotionModule.update(data)
return new StepResponse(updatedPromotions, dataBeforeUpdate)
return new StepResponse(updatedPromotions, {
dataBeforeUpdate,
selects,
relations,
})
},
async (dataBeforeUpdate, { container }) => {
if (!dataBeforeUpdate) {
async (revertInput, { container }) => {
if (!revertInput) {
return
}
const { dataBeforeUpdate, selects, relations } = revertInput
const promotionModule = container.resolve<IPromotionModuleService>(
ModuleRegistrationName.PROMOTION
)
// TODO: This still requires some sanitation of data and transformation of
// shapes for manytomany and oneToMany relations. Create a common util.
await promotionModule.update(dataBeforeUpdate)
await promotionModule.update(
dataBeforeUpdate.map((data) =>
convertItemResponseToUpdateRequest(data, selects, relations)
)
)
}
)