fix: Standardize remaining batch methods and update FE calls (#7122)

This commit is contained in:
Stevche Radevski
2024-04-23 11:12:38 +02:00
committed by GitHub
parent ea14a3bd68
commit 9e3556686b
49 changed files with 851 additions and 611 deletions

View File

@@ -61,13 +61,13 @@ medusaIntegrationTestRunner({
})
})
describe("POST /admin/promotions/:id/rules/batch/add", () => {
describe("POST /admin/promotions/:id/rules/batch", () => {
it("should throw error when required params are missing", async () => {
const { response } = await api
.post(
`/admin/promotions/${standardPromotion.id}/rules/batch/add`,
`/admin/promotions/${standardPromotion.id}/rules/batch`,
{
rules: [
create: [
{
operator: "eq",
values: ["new value"],
@@ -89,9 +89,9 @@ medusaIntegrationTestRunner({
it("should throw error when promotion does not exist", async () => {
const { response } = await api
.post(
`/admin/promotions/does-not-exist/rules/batch/add`,
`/admin/promotions/does-not-exist/rules/batch`,
{
rules: [
create: [
{
attribute: "new_attr",
operator: "eq",
@@ -112,9 +112,9 @@ medusaIntegrationTestRunner({
it("should add rules to a promotion successfully", async () => {
const response = await api.post(
`/admin/promotions/${standardPromotion.id}/rules/batch/add`,
`/admin/promotions/${standardPromotion.id}/rules/batch`,
{
rules: [
create: [
{
operator: "eq",
attribute: "new_attr",
@@ -126,7 +126,14 @@ medusaIntegrationTestRunner({
)
expect(response.status).toEqual(200)
expect(response.data.promotion).toEqual(
const promotion = (
await api.get(
`/admin/promotions/${standardPromotion.id}?fields=*rules`,
adminHeaders
)
).data.promotion
expect(promotion).toEqual(
expect.objectContaining({
id: standardPromotion.id,
rules: expect.arrayContaining([
@@ -146,13 +153,13 @@ medusaIntegrationTestRunner({
})
})
describe("POST /admin/promotions/:id/target-rules/batch/add", () => {
describe("POST /admin/promotions/:id/target-rules/batch", () => {
it("should throw error when required params are missing", async () => {
const { response } = await api
.post(
`/admin/promotions/${standardPromotion.id}/target-rules/batch/add`,
`/admin/promotions/${standardPromotion.id}/target-rules/batch`,
{
rules: [
create: [
{
operator: "eq",
values: ["new value"],
@@ -174,9 +181,9 @@ medusaIntegrationTestRunner({
it("should throw error when promotion does not exist", async () => {
const { response } = await api
.post(
`/admin/promotions/does-not-exist/target-rules/batch/add`,
`/admin/promotions/does-not-exist/target-rules/batch`,
{
rules: [
create: [
{
attribute: "new_attr",
operator: "eq",
@@ -195,11 +202,11 @@ medusaIntegrationTestRunner({
})
})
it("should add target rules to a promotion successfully", async () => {
it.only("should add target rules to a promotion successfully", async () => {
const response = await api.post(
`/admin/promotions/${standardPromotion.id}/target-rules/batch/add`,
`/admin/promotions/${standardPromotion.id}/target-rules/batch`,
{
rules: [
create: [
{
operator: "eq",
attribute: "new_attr",
@@ -211,7 +218,14 @@ medusaIntegrationTestRunner({
)
expect(response.status).toEqual(200)
expect(response.data.promotion).toEqual(
const promotion = (
await api.get(
`/admin/promotions/${standardPromotion.id}`,
adminHeaders
)
).data.promotion
expect(promotion).toEqual(
expect.objectContaining({
id: standardPromotion.id,
application_method: expect.objectContaining({
@@ -233,13 +247,13 @@ medusaIntegrationTestRunner({
})
})
describe("POST /admin/promotions/:id/buy-rules/batch/add", () => {
describe("POST /admin/promotions/:id/buy-rules/batch", () => {
it("should throw error when required params are missing", async () => {
const { response } = await api
.post(
`/admin/promotions/${standardPromotion.id}/buy-rules/batch/add`,
`/admin/promotions/${standardPromotion.id}/buy-rules/batch`,
{
rules: [
create: [
{
operator: "eq",
values: ["new value"],
@@ -261,9 +275,9 @@ medusaIntegrationTestRunner({
it("should throw error when promotion does not exist", async () => {
const { response } = await api
.post(
`/admin/promotions/does-not-exist/buy-rules/batch/add`,
`/admin/promotions/does-not-exist/buy-rules/batch`,
{
rules: [
create: [
{
attribute: "new_attr",
operator: "eq",
@@ -285,9 +299,9 @@ medusaIntegrationTestRunner({
it("should throw an error when trying to add buy rules to a standard promotion", async () => {
const { response } = await api
.post(
`/admin/promotions/${standardPromotion.id}/buy-rules/batch/add`,
`/admin/promotions/${standardPromotion.id}/buy-rules/batch`,
{
rules: [
create: [
{
operator: "eq",
attribute: "new_attr",
@@ -324,9 +338,9 @@ medusaIntegrationTestRunner({
})
const response = await api.post(
`/admin/promotions/${buyGetPromotion.id}/buy-rules/batch/add`,
`/admin/promotions/${buyGetPromotion.id}/buy-rules/batch`,
{
rules: [
create: [
{
operator: "eq",
attribute: "new_attr",
@@ -338,7 +352,14 @@ medusaIntegrationTestRunner({
)
expect(response.status).toEqual(200)
expect(response.data.promotion).toEqual(
const promotion = (
await api.get(
`/admin/promotions/${standardPromotion.id}`,
adminHeaders
)
).data.promotion
expect(promotion).toEqual(
expect.objectContaining({
id: buyGetPromotion.id,
application_method: expect.objectContaining({
@@ -360,11 +381,11 @@ medusaIntegrationTestRunner({
})
})
describe("POST /admin/promotions/:id/rules/batch/remove", () => {
describe("POST /admin/promotions/:id/rules/batch", () => {
it("should throw error when required params are missing", async () => {
const { response } = await api
.post(
`/admin/promotions/${standardPromotion.id}/rules/batch/remove`,
`/admin/promotions/${standardPromotion.id}/rules/batch`,
{},
adminHeaders
)
@@ -381,8 +402,8 @@ medusaIntegrationTestRunner({
it("should throw error when promotion does not exist", async () => {
const { response } = await api
.post(
`/admin/promotions/does-not-exist/rules/batch/remove`,
{ rule_ids: ["test-rule-id"] },
`/admin/promotions/does-not-exist/rules/batch`,
{ delete: ["test-rule-id"] },
adminHeaders
)
.catch((e) => e)
@@ -396,12 +417,12 @@ medusaIntegrationTestRunner({
it("should remove rules from a promotion successfully", async () => {
const response = await api.post(
`/admin/promotions/${standardPromotion.id}/rules/batch/remove`,
{ rule_ids: [standardPromotion.rules[0].id] },
`/admin/promotions/${standardPromotion.id}/rules/batch`,
{ delete: [standardPromotion.rules[0].id] },
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data).toEqual({
expect(response.data.deleted).toEqual({
ids: [standardPromotion.rules[0].id],
object: "promotion-rule",
deleted: true,
@@ -416,11 +437,11 @@ medusaIntegrationTestRunner({
})
})
describe("POST /admin/promotions/:id/target-rules/batch/remove", () => {
describe("POST /admin/promotions/:id/target-rules/batch", () => {
it("should throw error when required params are missing", async () => {
const { response } = await api
.post(
`/admin/promotions/${standardPromotion.id}/target-rules/batch/remove`,
`/admin/promotions/${standardPromotion.id}/target-rules/batch`,
{},
adminHeaders
)
@@ -437,8 +458,8 @@ medusaIntegrationTestRunner({
it("should throw error when promotion does not exist", async () => {
const { response } = await api
.post(
`/admin/promotions/does-not-exist/target-rules/batch/remove`,
{ rule_ids: ["test-rule-id"] },
`/admin/promotions/does-not-exist/target-rules/batch`,
{ delete: ["test-rule-id"] },
adminHeaders
)
.catch((e) => e)
@@ -453,13 +474,13 @@ medusaIntegrationTestRunner({
it("should remove target rules from a promotion successfully", async () => {
const ruleId = standardPromotion.application_method.target_rules[0].id
const response = await api.post(
`/admin/promotions/${standardPromotion.id}/target-rules/batch/remove`,
{ rule_ids: [ruleId] },
`/admin/promotions/${standardPromotion.id}/target-rules/batch`,
{ delete: [ruleId] },
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data).toEqual({
expect(response.data.deleted).toEqual({
ids: [ruleId],
object: "promotion-rule",
deleted: true,
@@ -474,11 +495,11 @@ medusaIntegrationTestRunner({
})
})
describe("POST /admin/promotions/:id/buy-rules/batch/remove", () => {
describe("POST /admin/promotions/:id/buy-rules/batch", () => {
it("should throw error when required params are missing", async () => {
const { response } = await api
.post(
`/admin/promotions/${standardPromotion.id}/buy-rules/batch/remove`,
`/admin/promotions/${standardPromotion.id}/buy-rules/batch`,
{},
adminHeaders
)
@@ -495,8 +516,8 @@ medusaIntegrationTestRunner({
it("should throw error when promotion does not exist", async () => {
const { response } = await api
.post(
`/admin/promotions/does-not-exist/buy-rules/batch/remove`,
{ rule_ids: ["test-rule-id"] },
`/admin/promotions/does-not-exist/buy-rules/batch`,
{ delete: ["test-rule-id"] },
adminHeaders
)
.catch((e) => e)
@@ -527,13 +548,13 @@ medusaIntegrationTestRunner({
const ruleId = buyGetPromotion!.application_method!.buy_rules![0].id
const response = await api.post(
`/admin/promotions/${buyGetPromotion.id}/buy-rules/batch/remove`,
{ rule_ids: [ruleId] },
`/admin/promotions/${buyGetPromotion.id}/buy-rules/batch`,
{ delete: [ruleId] },
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data).toEqual({
expect(response.data.deleted).toEqual({
ids: [ruleId],
object: "promotion-rule",
deleted: true,
@@ -547,13 +568,13 @@ medusaIntegrationTestRunner({
})
})
describe("POST /admin/promotions/:id/rules/batch/update", () => {
describe("POST /admin/promotions/:id/rules/batch", () => {
it("should throw error when required params are missing", async () => {
const { response } = await api
.post(
`/admin/promotions/${standardPromotion.id}/rules/batch/update`,
`/admin/promotions/${standardPromotion.id}/rules/batch`,
{
rules: [
update: [
{
attribute: "test",
operator: "eq",
@@ -575,9 +596,9 @@ medusaIntegrationTestRunner({
it("should throw error when promotion does not exist", async () => {
const { response } = await api
.post(
`/admin/promotions/does-not-exist/rules/batch/update`,
`/admin/promotions/does-not-exist/rules/batch`,
{
rules: [
update: [
{
id: standardPromotion.rules[0].id,
attribute: "new_attr",
@@ -600,9 +621,9 @@ medusaIntegrationTestRunner({
it("should throw error when promotion rule id does not exist", async () => {
const { response } = await api
.post(
`/admin/promotions/${standardPromotion.id}/rules/batch/update`,
`/admin/promotions/${standardPromotion.id}/rules/batch`,
{
rules: [
update: [
{
id: "does-not-exist",
attribute: "new_attr",
@@ -624,9 +645,9 @@ medusaIntegrationTestRunner({
it("should add rules to a promotion successfully", async () => {
const response = await api.post(
`/admin/promotions/${standardPromotion.id}/rules/batch/update`,
`/admin/promotions/${standardPromotion.id}/rules/batch`,
{
rules: [
update: [
{
id: standardPromotion.rules[0].id,
operator: "eq",
@@ -639,7 +660,14 @@ medusaIntegrationTestRunner({
)
expect(response.status).toEqual(200)
expect(response.data.promotion).toEqual(
const promotion = (
await api.get(
`/admin/promotions/${standardPromotion.id}?fields=*rules`,
adminHeaders
)
).data.promotion
expect(promotion).toEqual(
expect.objectContaining({
id: standardPromotion.id,
rules: expect.arrayContaining([

View File

@@ -63,13 +63,13 @@ medusaIntegrationTestRunner({
})
})
describe("POST /admin/shipping-options/:id/rules/batch/add", () => {
describe("POST /admin/shipping-options/:id/rules/batch", () => {
it("should throw error when required params are missing", async () => {
const { response } = await api
.post(
`/admin/shipping-options/${shippingOption.id}/rules/batch/add`,
`/admin/shipping-options/${shippingOption.id}/rules/batch`,
{
rules: [{ operator: RuleOperator.EQ, value: "new_value" }],
create: [{ operator: RuleOperator.EQ, value: "new_value" }],
},
adminHeaders
)
@@ -86,9 +86,9 @@ medusaIntegrationTestRunner({
it.only("should throw error when shipping option does not exist", async () => {
const { response } = await api
.post(
`/admin/shipping-options/does-not-exist/rules/batch/add`,
`/admin/shipping-options/does-not-exist/rules/batch`,
{
rules: [
create: [
{ attribute: "new_attr", operator: "eq", value: "new value" },
],
},
@@ -106,9 +106,9 @@ medusaIntegrationTestRunner({
it("should add rules to a shipping option successfully", async () => {
const response = await api.post(
`/admin/shipping-options/${shippingOption.id}/rules/batch/add`,
`/admin/shipping-options/${shippingOption.id}/rules/batch`,
{
rules: [
create: [
{ operator: "eq", attribute: "new_attr", value: "new value" },
],
},
@@ -116,7 +116,14 @@ medusaIntegrationTestRunner({
)
expect(response.status).toEqual(200)
expect(response.data.shipping_option).toEqual(
const updatedShippingOption = (
await api.get(
`/admin/shipping-options/${shippingOption.id}`,
adminHeaders
)
).data.shipping_option
expect(updatedShippingOption).toEqual(
expect.objectContaining({
id: shippingOption.id,
rules: expect.arrayContaining([
@@ -140,11 +147,11 @@ medusaIntegrationTestRunner({
})
})
describe("POST /admin/shipping-options/:id/rules/batch/remove", () => {
describe("POST /admin/shipping-options/:id/rules/batch", () => {
it("should throw error when required params are missing", async () => {
const { response } = await api
.post(
`/admin/shipping-options/${shippingOption.id}/rules/batch/remove`,
`/admin/shipping-options/${shippingOption.id}/rules/batch`,
{},
adminHeaders
)
@@ -161,8 +168,8 @@ medusaIntegrationTestRunner({
it("should throw error when shipping option does not exist", async () => {
const { response } = await api
.post(
`/admin/shipping-options/does-not-exist/rules/batch/remove`,
{ rule_ids: ["test"] },
`/admin/shipping-options/does-not-exist/rules/batch`,
{ delete: ["test"] },
adminHeaders
)
.catch((e) => e)
@@ -176,15 +183,22 @@ medusaIntegrationTestRunner({
it("should add rules to a shipping option successfully", async () => {
const response = await api.post(
`/admin/shipping-options/${shippingOption.id}/rules/batch/remove`,
`/admin/shipping-options/${shippingOption.id}/rules/batch`,
{
rule_ids: [shippingOption.rules[0].id],
delete: [shippingOption.rules[0].id],
},
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.shipping_option).toEqual(
const updatedShippingOption = (
await api.get(
`/admin/shipping-options/${shippingOption.id}`,
adminHeaders
)
).data.shipping_option
expect(updatedShippingOption).toEqual(
expect.objectContaining({
id: shippingOption.id,
rules: [],

View File

@@ -32,8 +32,10 @@ const batchRemoveSalesChannelsFromApiKey = async (
payload: { sales_channel_ids: string[] }
) => {
return postRequest<AdminApiKeyResponse>(
`/admin/api-keys/${id}/sales-channels/batch/remove`,
payload
`/admin/api-keys/${id}/sales-channels`,
{
remove: payload.sales_channel_ids,
}
)
}
@@ -42,8 +44,10 @@ const batchAddSalesChannelsFromApiKey = async (
payload: { sales_channel_ids: string[] }
) => {
return postRequest<AdminApiKeyResponse>(
`/admin/api-keys/${id}/sales-channels/batch/add`,
payload
`/admin/api-keys/${id}/sales-channels`,
{
add: payload.sales_channel_ids,
}
)
}

View File

@@ -53,8 +53,10 @@ async function batchAddCustomers(
payload: { customer_ids: { id: string }[] }
) {
return postRequest<AdminCustomerGroupResponse>(
`/admin/customer-groups/${id}/customers/batch/add`,
payload
`/admin/customer-groups/${id}/customers`,
{
add: payload.customer_ids,
}
)
}
@@ -63,8 +65,10 @@ async function batchRemoveCustomers(
payload: { customer_ids: { id: string }[] }
) {
return postRequest<AdminCustomerGroupResponse>(
`/admin/customer-groups/${id}/customers/batch/remove`,
payload
`/admin/customer-groups/${id}/customers`,
{
remove: payload.customer_ids,
}
)
}

View File

@@ -110,8 +110,11 @@ async function batchPostLocationLevels(
payload: InventoryItemLocationBatch
) {
return postRequest<AdminInventoryLevelResponse>(
`/admin/inventory-items/${inventoryItemId}/location-levels/batch/combi`,
payload
`/admin/inventory-items/${inventoryItemId}/location-levels/batch`,
{
create: payload.creates,
delete: payload.deletes,
}
)
}

View File

@@ -32,20 +32,18 @@ async function deletePriceList(id: string) {
}
async function addPriceListPrices(id: string, payload: AddPriceListPricesReq) {
return postRequest<PriceListRes>(
`/admin/price-lists/${id}/prices/batch/add`,
payload
)
return postRequest<PriceListRes>(`/admin/price-lists/${id}/prices/batch`, {
create: payload.prices,
})
}
async function removePriceListPrices(
id: string,
payload: DeletePriceListPricesReq
) {
return postRequest<PriceListRes>(
`/admin/price-lists/${id}/prices/batch/remove`,
payload
)
return postRequest<PriceListRes>(`/admin/price-lists/${id}/prices/batch`, {
delete: payload.ids,
})
}
export const priceLists = {

View File

@@ -46,8 +46,10 @@ async function addPromotionRules(
payload: BatchAddPromotionRulesReq
) {
return postRequest<PromotionRes>(
`/admin/promotions/${id}/${ruleType}/batch/add`,
payload
`/admin/promotions/${id}/${ruleType}/batch`,
{
create: payload.rules,
}
)
}
@@ -57,8 +59,10 @@ async function updatePromotionRules(
payload: BatchUpdatePromotionRulesReq
) {
return postRequest<PromotionRes>(
`/admin/promotions/${id}/${ruleType}/batch/update`,
payload
`/admin/promotions/${id}/${ruleType}/batch`,
{
update: payload.rules,
}
)
}
@@ -68,8 +72,10 @@ async function removePromotionRules(
payload: BatchRemovePromotionRulesReq
) {
return postRequest<PromotionRes>(
`/admin/promotions/${id}/${ruleType}/batch/remove`,
payload
`/admin/promotions/${id}/${ruleType}/batch`,
{
delete: payload.rule_ids,
}
)
}

View File

@@ -48,8 +48,10 @@ async function batchRemoveProducts(
payload: RemoveProductsSalesChannelReq
) {
return postRequest<AdminSalesChannelResponse>(
`/admin/sales-channels/${id}/products/batch/remove`,
payload
`/admin/sales-channels/${id}/products`,
{
remove: payload.product_ids,
}
)
}
@@ -58,8 +60,10 @@ async function batchAddProducts(
payload: AddProductsSalesChannelReq
) {
return postRequest<AdminSalesChannelResponse>(
`/admin/sales-channels/${id}/products/batch/add`,
payload
`/admin/sales-channels/${id}/products`,
{
add: payload.product_ids,
}
)
}

View File

@@ -5,10 +5,9 @@ import {
} from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const addRulesToFulfillmentShippingOptionStepId =
"add-rules-to-fulfillment-shipping-option"
export const addRulesToFulfillmentShippingOptionStep = createStep(
addRulesToFulfillmentShippingOptionStepId,
export const createShippingOptionRulesStepId = "create-shipping-option-rules"
export const createShippingOptionRulesStep = createStep(
createShippingOptionRulesStepId,
async (
input: AddFulfillmentShippingOptionRulesWorkflowDTO,
{ container }

View File

@@ -6,10 +6,9 @@ import {
} from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const removeRulesFromFulfillmentShippingOptionStepId =
"remove-rules-from-fulfillment-shipping-option"
export const removeRulesFromFulfillmentShippingOptionStep = createStep(
removeRulesFromFulfillmentShippingOptionStepId,
export const deleteShippingOptionRulesStepId = "delete-shipping-option-rules"
export const deleteShippingOptionRulesStep = createStep(
deleteShippingOptionRulesStepId,
async (
input: RemoveFulfillmentShippingOptionRulesWorkflowDTO,
{ container }

View File

@@ -1,4 +1,4 @@
export * from "./add-rules-to-fulfillment-shipping-option"
export * from "./create-shipping-option-rules"
export * from "./add-shipping-options-prices"
export * from "./cancel-fulfillment"
export * from "./create-fulfillment"
@@ -8,7 +8,7 @@ export * from "./create-shipping-profiles"
export * from "./delete-fulfillment-sets"
export * from "./delete-service-zones"
export * from "./delete-shipping-options"
export * from "./remove-rules-from-fulfillment-shipping-option"
export * from "./delete-shipping-option-rules"
export * from "./set-shipping-options-prices"
export * from "./update-fulfillment"
export * from "./upsert-shipping-options"

View File

@@ -1,14 +0,0 @@
import { AddFulfillmentShippingOptionRulesWorkflowDTO } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { addRulesToFulfillmentShippingOptionStep } from "../steps"
export const addRulesToFulfillmentShippingOptionWorkflowId =
"add-rules-to-fulfillment-shipping-option-workflow"
export const addRulesToFulfillmentShippingOptionWorkflow = createWorkflow(
addRulesToFulfillmentShippingOptionWorkflowId,
(
input: WorkflowData<AddFulfillmentShippingOptionRulesWorkflowDTO>
): WorkflowData<void> => {
addRulesToFulfillmentShippingOptionStep(input)
}
)

View File

@@ -0,0 +1,51 @@
import {
BatchWorkflowInput,
BatchWorkflowOutput,
CreateShippingOptionRuleDTO,
ShippingOptionRuleDTO,
UpdateShippingOptionRuleDTO,
} from "@medusajs/types"
import {
WorkflowData,
createWorkflow,
parallelize,
transform,
} from "@medusajs/workflows-sdk"
import {
createShippingOptionRulesStep,
deleteShippingOptionRulesStep,
} from "../steps"
export const batchShippingOptionRulesWorkflowId = "batch-shipping-option-rules"
export const batchShippingOptionRulesWorkflow = createWorkflow(
batchShippingOptionRulesWorkflowId,
(
input: WorkflowData<
BatchWorkflowInput<
CreateShippingOptionRuleDTO,
UpdateShippingOptionRuleDTO
>
>
): WorkflowData<BatchWorkflowOutput<ShippingOptionRuleDTO>> => {
const createInput = transform({ input }, (data) => ({
data: data.input.create ?? [],
}))
const updateInput = transform({ input }, (data) => ({
data: data.input.update ?? [],
}))
const deleteInput = transform({ input }, (data) => ({
ids: data.input.delete ?? [],
}))
// TODO: Currently we don't support edits, add support for this.
// We just call the steps directly here since there are no independent workflows, switch to CRUD workflows if they get added.
const [created, deleted] = parallelize(
createShippingOptionRulesStep(createInput),
deleteShippingOptionRulesStep(deleteInput)
)
return transform({ created, deleted }, (data) => ({ ...data, updated: [] }))
}
)

View File

@@ -1,4 +1,4 @@
export * from "./add-rules-to-fulfillment-shipping-option"
export * from "./batch-shipping-option-rules"
export * from "./cancel-fulfillment"
export * from "./create-fulfillment"
export * from "./create-service-zones"
@@ -8,7 +8,6 @@ export * from "./create-shipping-profiles"
export * from "./delete-fulfillment-sets"
export * from "./delete-service-zones"
export * from "./delete-shipping-options"
export * from "./remove-rules-from-fulfillment-shipping-option"
export * from "./update-fulfillment"
export * from "./update-service-zones"
export * from "./update-shipping-options"

View File

@@ -1,14 +0,0 @@
import { RemoveFulfillmentShippingOptionRulesWorkflowDTO } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { removeRulesFromFulfillmentShippingOptionStep } from "../steps"
export const removeRulesFromFulfillmentShippingOptionWorkflowId =
"remove-rules-from-fulfillment-shipping-option-workflow"
export const removeRulesFromFulfillmentShippingOptionWorkflow = createWorkflow(
removeRulesFromFulfillmentShippingOptionWorkflowId,
(
input: WorkflowData<RemoveFulfillmentShippingOptionRulesWorkflowDTO>
): WorkflowData<void> => {
removeRulesFromFulfillmentShippingOptionStep(input)
}
)

View File

@@ -18,19 +18,42 @@ export const batchProductVariantsStep = createStep(
>,
{ container }
) => {
const { transaction: createTransaction, result: created } =
await createProductVariantsWorkflow(container).run({
input: { product_variants: data.create ?? [] },
})
const { transaction: updateTransaction, result: updated } =
await updateProductVariantsWorkflow(container).run({
input: { product_variants: data.update ?? [] },
})
const { transaction: deleteTransaction } =
const {
transaction: createTransaction,
result: created,
errors: createErrors,
} = await createProductVariantsWorkflow(container).run({
input: { product_variants: data.create ?? [] },
throwOnError: false,
})
if (createErrors?.length) {
throw createErrors[0].error
}
const {
transaction: updateTransaction,
result: updated,
errors: updateErrors,
} = await updateProductVariantsWorkflow(container).run({
input: { product_variants: data.update ?? [] },
throwOnError: false,
})
if (updateErrors?.length) {
throw updateErrors[0].error
}
const { transaction: deleteTransaction, errors: deleteErrors } =
await deleteProductVariantsWorkflow(container).run({
input: { ids: data.delete ?? [] },
throwOnError: false,
})
if (deleteErrors?.length) {
throw deleteErrors[0].error
}
return new StepResponse(
{
created,

View File

@@ -18,20 +18,42 @@ export const batchProductsStep = createStep(
>,
{ container }
) => {
const { transaction: createTransaction, result: created } =
await createProductsWorkflow(container).run({
input: { products: data.create ?? [] },
})
const { transaction: updateTransaction, result: updated } =
await updateProductsWorkflow(container).run({
input: { products: data.update ?? [] },
})
const { transaction: deleteTransaction } = await deleteProductsWorkflow(
container
).run({
input: { ids: data.delete ?? [] },
const {
transaction: createTransaction,
result: created,
errors: createErrors,
} = await createProductsWorkflow(container).run({
input: { products: data.create ?? [] },
throwOnError: false,
})
if (createErrors?.length) {
throw createErrors[0].error
}
const {
transaction: updateTransaction,
result: updated,
errors: updateErrors,
} = await updateProductsWorkflow(container).run({
input: { products: data.update ?? [] },
throwOnError: false,
})
if (updateErrors?.length) {
throw updateErrors[0].error
}
const { transaction: deleteTransaction, errors: deleteErrors } =
await deleteProductsWorkflow(container).run({
input: { ids: data.delete ?? [] },
throwOnError: false,
})
if (deleteErrors?.length) {
throw deleteErrors[0].error
}
return new StepResponse(
{
created,

View File

@@ -0,0 +1,33 @@
import { AddPromotionRulesWorkflowDTO } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { createPromotionRulesWorkflow } from "../workflows/create-promotion-rules"
export const createPromotionRulesWorkflowStepId =
"create-promotion-rules-workflow"
export const createPromotionRulesWorkflowStep = createStep(
createPromotionRulesWorkflowStepId,
async (data: AddPromotionRulesWorkflowDTO, { container }) => {
const {
transaction,
result: created,
errors,
} = await createPromotionRulesWorkflow(container).run({
input: data,
throwOnError: false,
})
if (errors?.length) {
throw errors[0].error
}
return new StepResponse(created, transaction)
},
async (transaction, { container }) => {
if (!transaction) {
return
}
await createPromotionRulesWorkflow(container).cancel({ transaction })
}
)

View File

@@ -0,0 +1,38 @@
import { RemovePromotionRulesWorkflowDTO } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { deletePromotionRulesWorkflow } from "../workflows/delete-promotion-rules"
export const deletePromotionRulesWorkflowStepId =
"delete-promotion-rules-workflow"
export const deletePromotionRulesWorkflowStep = createStep(
deletePromotionRulesWorkflowStepId,
async (data: RemovePromotionRulesWorkflowDTO, { container }) => {
const { transaction, errors } = await deletePromotionRulesWorkflow(
container
).run({
input: data,
throwOnError: false,
})
if (errors?.length) {
throw errors[0].error
}
return new StepResponse(
{
ids: data.data.rule_ids ?? [],
object: "promotion-rule",
deleted: true,
},
transaction
)
},
async (transaction, { container }) => {
if (!transaction) {
return
}
await deletePromotionRulesWorkflow(container).cancel({ transaction })
}
)

View File

@@ -0,0 +1,33 @@
import { UpdatePromotionRulesWorkflowDTO } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { updatePromotionRulesWorkflow } from "../workflows/update-promotion-rules"
export const updatePromotionRulesWorkflowStepId =
"update-promotion-rules-workflow"
export const updatePromotionRulesWorkflowStep = createStep(
updatePromotionRulesWorkflowStepId,
async (data: UpdatePromotionRulesWorkflowDTO, { container }) => {
const {
transaction,
result: updated,
errors,
} = await updatePromotionRulesWorkflow(container).run({
input: data,
throwOnError: false,
})
if (errors?.length) {
throw errors[0].error
}
return new StepResponse(updated, transaction)
},
async (transaction, { container }) => {
if (!transaction) {
return
}
await updatePromotionRulesWorkflow(container).cancel({ transaction })
}
)

View File

@@ -1,11 +0,0 @@
import { AddPromotionRulesWorkflowDTO } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { addRulesToPromotionsStep } from "../steps"
export const addRulesToPromotionsWorkflowId = "add-rules-to-promotions-workflow"
export const addRulesToPromotionsWorkflow = createWorkflow(
addRulesToPromotionsWorkflowId,
(input: WorkflowData<AddPromotionRulesWorkflowDTO>): WorkflowData<void> => {
addRulesToPromotionsStep(input)
}
)

View File

@@ -0,0 +1,52 @@
import {
BatchWorkflowInput,
BatchWorkflowOutput,
CreatePromotionRuleDTO,
PromotionRuleDTO,
UpdatePromotionRuleDTO,
} from "@medusajs/types"
import {
WorkflowData,
createWorkflow,
parallelize,
transform,
} from "@medusajs/workflows-sdk"
import { RuleType } from "@medusajs/utils"
import { createPromotionRulesWorkflowStep } from "../steps/create-promotion-rules-workflow"
import { updatePromotionRulesWorkflowStep } from "../steps/update-promotion-rules-workflow"
import { deletePromotionRulesWorkflowStep } from "../steps/delete-promotion-rules-workflow"
export const batchPromotionRulesWorkflowId = "batch-promotion-rules"
export const batchPromotionRulesWorkflow = createWorkflow(
batchPromotionRulesWorkflowId,
(
input: WorkflowData<
BatchWorkflowInput<CreatePromotionRuleDTO, UpdatePromotionRuleDTO> & {
id: string
rule_type: RuleType
}
>
): WorkflowData<BatchWorkflowOutput<PromotionRuleDTO>> => {
const createInput = transform({ input }, (data) => ({
rule_type: data.input.rule_type,
data: { id: data.input.id, rules: data.input.create ?? [] },
}))
const updateInput = transform({ input }, (data) => ({
data: data.input.update ?? [],
}))
const deleteInput = transform({ input }, (data) => ({
rule_type: data.input.rule_type,
data: { id: data.input.id, rule_ids: data.input.delete ?? [] },
}))
const [created, updated, deleted] = parallelize(
createPromotionRulesWorkflowStep(createInput),
updatePromotionRulesWorkflowStep(updateInput),
deletePromotionRulesWorkflowStep(deleteInput)
)
return transform({ created, updated, deleted }, (data) => data)
}
)

View File

@@ -0,0 +1,13 @@
import { AddPromotionRulesWorkflowDTO, PromotionRuleDTO } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { addRulesToPromotionsStep } from "../steps"
export const createPromotionRulesWorkflowId = "create-promotion-rules-workflow"
export const createPromotionRulesWorkflow = createWorkflow(
createPromotionRulesWorkflowId,
(
input: WorkflowData<AddPromotionRulesWorkflowDTO>
): WorkflowData<PromotionRuleDTO[]> => {
return addRulesToPromotionsStep(input)
}
)

View File

@@ -2,13 +2,12 @@ import { RemovePromotionRulesWorkflowDTO } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { removeRulesFromPromotionsStep } from "../steps"
export const removeRulesFromPromotionsWorkflowId =
"remove-rules-from-promotions-workflow"
export const removeRulesFromPromotionsWorkflow = createWorkflow(
removeRulesFromPromotionsWorkflowId,
export const deletePromotionRulesWorkflowId = "delete-promotion-rules-workflow"
export const deletePromotionRulesWorkflow = createWorkflow(
deletePromotionRulesWorkflowId,
(
input: WorkflowData<RemovePromotionRulesWorkflowDTO>
): WorkflowData<void> => {
removeRulesFromPromotionsStep(input)
return removeRulesFromPromotionsStep(input)
}
)

View File

@@ -1,9 +1,10 @@
export * from "./add-rules-to-promotions"
export * from "./batch-promotion-rules"
export * from "./create-campaigns"
export * from "./create-promotions"
export * from "./delete-campaigns"
export * from "./delete-promotions"
export * from "./remove-rules-from-promotions"
export * from "./update-campaigns"
export * from "./update-promotion-rules"
export * from "./delete-promotion-rules"
export * from "./create-promotion-rules"
export * from "./update-promotions"

View File

@@ -1,4 +1,7 @@
import { UpdatePromotionRulesWorkflowDTO } from "@medusajs/types"
import {
UpdatePromotionRulesWorkflowDTO,
PromotionRuleDTO,
} from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { updatePromotionRulesStep } from "../steps"
@@ -7,7 +10,7 @@ export const updatePromotionRulesWorkflow = createWorkflow(
updatePromotionRulesWorkflowId,
(
input: WorkflowData<UpdatePromotionRulesWorkflowDTO>
): WorkflowData<void> => {
updatePromotionRulesStep(input)
): WorkflowData<PromotionRuleDTO[]> => {
return updatePromotionRulesStep(input)
}
)

View File

@@ -8,6 +8,7 @@ import {
GeoZone,
ServiceZone,
ShippingOption,
ShippingOptionRule,
ShippingProfile,
} from "@models"
@@ -15,6 +16,7 @@ export const LinkableKeys: Record<string, string> = {
fulfillment_id: Fulfillment.name,
fulfillment_set_id: FulfillmentSet.name,
shipping_option_id: ShippingOption.name,
shipping_option_rule_id: ShippingOptionRule.name,
}
const entityLinkableKeysMap: MapToConfig = {}
@@ -81,5 +83,12 @@ export const joinerConfig: ModuleJoinerConfig = {
methodSuffix: "GeoZones",
},
},
{
name: ["shipping_option_rule", "shipping_option_rules"],
args: {
entity: ShippingOptionRule.name,
methodSuffix: "ShippingOptionRules",
},
},
],
} as ModuleJoinerConfig

View File

@@ -1,35 +0,0 @@
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
import { RuleType } from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../../types/routing"
import { refetchPromotion } from "../../../../helpers"
import { AdminCreateBatchRulesType } from "../../../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateBatchRulesType>,
res: MedusaResponse
) => {
const id = req.params.id
const workflow = addRulesToPromotionsWorkflow(req.scope)
const { errors } = await workflow.run({
input: {
rule_type: RuleType.BUY_RULES,
data: { id, ...req.validatedBody },
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const promotion = await refetchPromotion(
id,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json({ promotion })
}

View File

@@ -1,34 +0,0 @@
import { removeRulesFromPromotionsWorkflow } from "@medusajs/core-flows"
import { RuleType } from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../../types/routing"
import { AdminRemoveBatchRulesType } from "../../../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminRemoveBatchRulesType>,
res: MedusaResponse
) => {
const id = req.params.id
const workflow = removeRulesFromPromotionsWorkflow(req.scope)
const validatedBody = req.validatedBody
const { errors } = await workflow.run({
input: {
rule_type: RuleType.BUY_RULES,
data: { id, ...validatedBody },
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
ids: validatedBody.rule_ids,
object: "promotion-rule",
deleted: true,
})
}

View File

@@ -0,0 +1,46 @@
import { batchPromotionRulesWorkflow } from "@medusajs/core-flows"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../types/routing"
import { BatchMethodRequest } from "@medusajs/types"
import {
AdminCreatePromotionRuleType,
AdminUpdatePromotionRuleType,
} from "../../../validators"
import { RuleType } from "@medusajs/utils"
import { refetchBatchRules } from "../../../helpers"
export const POST = async (
req: AuthenticatedMedusaRequest<
BatchMethodRequest<
AdminCreatePromotionRuleType,
AdminUpdatePromotionRuleType
>
>,
res: MedusaResponse
) => {
const id = req.params.id
const { result, errors } = await batchPromotionRulesWorkflow(req.scope).run({
input: {
id,
rule_type: RuleType.BUY_RULES,
create: req.validatedBody.create,
update: req.validatedBody.update,
delete: req.validatedBody.delete,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const batchResults = await refetchBatchRules(
result,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json(batchResults)
}

View File

@@ -1,36 +0,0 @@
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
import { RuleType } from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../../types/routing"
import { refetchPromotion } from "../../../../helpers"
import { AdminCreateBatchRulesType } from "../../../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateBatchRulesType>,
res: MedusaResponse
) => {
const id = req.params.id
const workflow = addRulesToPromotionsWorkflow(req.scope)
const { errors } = await workflow.run({
input: {
rule_type: RuleType.RULES,
data: { id, ...req.validatedBody },
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const promotion = await refetchPromotion(
id,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json({ promotion })
}

View File

@@ -1,35 +0,0 @@
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../../types/routing"
import { removeRulesFromPromotionsWorkflow } from "@medusajs/core-flows"
import { RuleType } from "@medusajs/utils"
import { AdminRemoveBatchRulesType } from "../../../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminRemoveBatchRulesType>,
res: MedusaResponse
) => {
const id = req.params.id
const workflow = removeRulesFromPromotionsWorkflow(req.scope)
const validatedBody = req.validatedBody
const { errors } = await workflow.run({
input: {
rule_type: RuleType.RULES,
data: { id, ...validatedBody },
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
ids: validatedBody.rule_ids,
object: "promotion-rule",
deleted: true,
})
}

View File

@@ -0,0 +1,46 @@
import { batchPromotionRulesWorkflow } from "@medusajs/core-flows"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../types/routing"
import { BatchMethodRequest } from "@medusajs/types"
import {
AdminCreatePromotionRuleType,
AdminUpdatePromotionRuleType,
} from "../../../validators"
import { RuleType } from "@medusajs/utils"
import { refetchBatchRules } from "../../../helpers"
export const POST = async (
req: AuthenticatedMedusaRequest<
BatchMethodRequest<
AdminCreatePromotionRuleType,
AdminUpdatePromotionRuleType
>
>,
res: MedusaResponse
) => {
const id = req.params.id
const { result, errors } = await batchPromotionRulesWorkflow(req.scope).run({
input: {
id,
rule_type: RuleType.RULES,
create: req.validatedBody.create,
update: req.validatedBody.update,
delete: req.validatedBody.delete,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const batchResults = await refetchBatchRules(
result,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json(batchResults)
}

View File

@@ -1,40 +0,0 @@
import { updatePromotionRulesWorkflow } from "@medusajs/core-flows"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../../types/routing"
import { AdminUpdateBatchRulesType } from "../../../../validators"
import { refetchPromotion } from "../../../../helpers"
import { MedusaError } from "@medusajs/utils"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateBatchRulesType>,
res: MedusaResponse
) => {
const id = req.params.id
const workflow = updatePromotionRulesWorkflow(req.scope)
const { errors } = await workflow.run({
input: { data: req.validatedBody.rules },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const promotion = await refetchPromotion(
id,
req.scope,
req.remoteQueryConfig.fields
)
if (!promotion) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`Promotion with id: ${id} was not found`
)
}
res.status(200).json({ promotion })
}

View File

@@ -1,35 +0,0 @@
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
import { RuleType } from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../../types/routing"
import { refetchPromotion } from "../../../../helpers"
import { AdminCreateBatchRulesType } from "../../../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateBatchRulesType>,
res: MedusaResponse
) => {
const id = req.params.id
const workflow = addRulesToPromotionsWorkflow(req.scope)
const { errors } = await workflow.run({
input: {
rule_type: RuleType.TARGET_RULES,
data: { id, ...req.validatedBody },
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const promotion = await refetchPromotion(
id,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json({ promotion })
}

View File

@@ -1,34 +0,0 @@
import { removeRulesFromPromotionsWorkflow } from "@medusajs/core-flows"
import { RuleType } from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../../types/routing"
import { AdminRemoveBatchRulesType } from "../../../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminRemoveBatchRulesType>,
res: MedusaResponse
) => {
const id = req.params.id
const workflow = removeRulesFromPromotionsWorkflow(req.scope)
const validatedBody = req.validatedBody
const { errors } = await workflow.run({
input: {
rule_type: RuleType.TARGET_RULES,
data: { id, ...validatedBody },
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({
ids: validatedBody.rule_ids,
object: "promotion-rule",
deleted: true,
})
}

View File

@@ -0,0 +1,46 @@
import { batchPromotionRulesWorkflow } from "@medusajs/core-flows"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../types/routing"
import { BatchMethodRequest } from "@medusajs/types"
import {
AdminCreatePromotionRuleType,
AdminUpdatePromotionRuleType,
} from "../../../validators"
import { RuleType } from "@medusajs/utils"
import { refetchBatchRules } from "../../../helpers"
export const POST = async (
req: AuthenticatedMedusaRequest<
BatchMethodRequest<
AdminCreatePromotionRuleType,
AdminUpdatePromotionRuleType
>
>,
res: MedusaResponse
) => {
const id = req.params.id
const { result, errors } = await batchPromotionRulesWorkflow(req.scope).run({
input: {
id,
rule_type: RuleType.TARGET_RULES,
create: req.validatedBody.create,
update: req.validatedBody.update,
delete: req.validatedBody.delete,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const batchResults = await refetchBatchRules(
result,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json(batchResults)
}

View File

@@ -1,5 +1,7 @@
import { MedusaContainer } from "@medusajs/types"
import { BatchMethodResponse } from "@medusajs/types"
import { PromotionRuleDTO, MedusaContainer } from "@medusajs/types"
import {
promiseAll,
ContainerRegistrationKeys,
remoteQueryObjectFromString,
} from "@medusajs/utils"
@@ -21,3 +23,44 @@ export const refetchPromotion = async (
const promotions = await remoteQuery(queryObject)
return promotions[0]
}
export const refetchBatchRules = async (
batchResult: BatchMethodResponse<PromotionRuleDTO>,
scope: MedusaContainer,
fields: string[]
) => {
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
let created = Promise.resolve<PromotionRuleDTO[]>([])
let updated = Promise.resolve<PromotionRuleDTO[]>([])
if (batchResult.created.length) {
const createdQuery = remoteQueryObjectFromString({
entryPoint: "promotion_rule",
variables: {
filters: { id: batchResult.created.map((p) => p.id) },
},
fields: fields,
})
created = remoteQuery(createdQuery)
}
if (batchResult.updated.length) {
const updatedQuery = remoteQueryObjectFromString({
entryPoint: "promotion_rule",
variables: {
filters: { id: batchResult.updated.map((p) => p.id) },
},
fields: fields,
})
updated = remoteQuery(updatedQuery)
}
const [createdRes, updatedRes] = await promiseAll([created, updated])
return {
created: createdRes,
updated: updatedRes,
deleted: batchResult.deleted,
}
}

View File

@@ -3,17 +3,18 @@ import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { authenticate } from "../../../utils/authenticate-middleware"
import { validateAndTransformQuery } from "../../utils/validate-query"
import {
AdminCreateBatchRules,
AdminCreatePromotion,
AdminCreatePromotionRule,
AdminGetPromotionParams,
AdminGetPromotionRuleParams,
AdminGetPromotionRuleTypeParams,
AdminGetPromotionsParams,
AdminGetPromotionsRuleValueParams,
AdminRemoveBatchRules,
AdminUpdateBatchRules,
AdminUpdatePromotion,
AdminUpdatePromotionRule,
} from "./validators"
import { validateAndTransformBody } from "../../utils/validate-body"
import { createBatchBody } from "../../utils/validators"
export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [
{
@@ -74,78 +75,40 @@ export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [
},
{
method: ["POST"],
matcher: "/admin/promotions/:id/rules/batch/add",
matcher: "/admin/promotions/:id/rules/batch",
middlewares: [
validateAndTransformBody(AdminCreateBatchRules),
validateAndTransformBody(
createBatchBody(AdminCreatePromotionRule, AdminUpdatePromotionRule)
),
validateAndTransformQuery(
AdminGetPromotionParams,
QueryConfig.retrieveTransformQueryConfig
AdminGetPromotionRuleParams,
QueryConfig.retrieveRuleTransformQueryConfig
),
],
},
{
method: ["POST"],
matcher: "/admin/promotions/:id/target-rules/batch/add",
matcher: "/admin/promotions/:id/target-rules/batch",
middlewares: [
validateAndTransformBody(AdminCreateBatchRules),
validateAndTransformBody(
createBatchBody(AdminCreatePromotionRule, AdminUpdatePromotionRule)
),
validateAndTransformQuery(
AdminGetPromotionParams,
QueryConfig.retrieveTransformQueryConfig
AdminGetPromotionRuleParams,
QueryConfig.retrieveRuleTransformQueryConfig
),
],
},
{
method: ["POST"],
matcher: "/admin/promotions/:id/buy-rules/batch/add",
matcher: "/admin/promotions/:id/buy-rules/batch",
middlewares: [
validateAndTransformBody(AdminCreateBatchRules),
validateAndTransformQuery(
AdminGetPromotionParams,
QueryConfig.retrieveTransformQueryConfig
validateAndTransformBody(
createBatchBody(AdminCreatePromotionRule, AdminUpdatePromotionRule)
),
],
},
{
method: ["POST"],
matcher: "/admin/promotions/:id/rules/batch/update",
middlewares: [
validateAndTransformBody(AdminUpdateBatchRules),
validateAndTransformQuery(
AdminGetPromotionParams,
QueryConfig.retrieveTransformQueryConfig
),
],
},
{
method: ["POST"],
matcher: "/admin/promotions/:id/rules/batch/remove",
middlewares: [
validateAndTransformBody(AdminRemoveBatchRules),
validateAndTransformQuery(
AdminGetPromotionParams,
QueryConfig.retrieveTransformQueryConfig
),
],
},
{
method: ["POST"],
matcher: "/admin/promotions/:id/target-rules/batch/remove",
middlewares: [
validateAndTransformBody(AdminRemoveBatchRules),
validateAndTransformQuery(
AdminGetPromotionParams,
QueryConfig.retrieveTransformQueryConfig
),
],
},
{
method: ["POST"],
matcher: "/admin/promotions/:id/buy-rules/batch/remove",
middlewares: [
validateAndTransformBody(AdminRemoveBatchRules),
validateAndTransformQuery(
AdminGetPromotionParams,
QueryConfig.retrieveTransformQueryConfig
AdminGetPromotionRuleParams,
QueryConfig.retrieveRuleTransformQueryConfig
),
],
},

View File

@@ -19,6 +19,14 @@ export const defaultAdminPromotionFields = [
"rules.values.value",
]
export const defaultAdminPromotionRuleFields = [
"id",
"description",
"attribute",
"operator",
"values.value",
]
export const retrieveTransformQueryConfig = {
defaults: defaultAdminPromotionFields,
isList: false,
@@ -29,6 +37,16 @@ export const listTransformQueryConfig = {
isList: true,
}
export const retrieveRuleTransformQueryConfig = {
defaults: defaultAdminPromotionRuleFields,
isList: false,
}
export const listRuleTransformQueryConfig = {
...retrieveRuleTransformQueryConfig,
isList: true,
}
export const listRuleValueTransformQueryConfig = {
defaults: [],
allowed: [],

View File

@@ -36,6 +36,11 @@ export const AdminGetPromotionsParams = createFindParams({
})
)
export type AdminGetPromotionRuleParamsType = z.infer<
typeof AdminGetPromotionRuleParams
>
export const AdminGetPromotionRuleParams = createSelectParams()
export type AdminGetPromotionRuleTypeParamsType = z.infer<
typeof AdminGetPromotionRuleTypeParams
>
@@ -185,24 +190,3 @@ export const AdminUpdatePromotion = z
message:
"Buyget promotions require at least one buy rule and quantities to be defined",
})
export type AdminCreateBatchRulesType = z.infer<typeof AdminCreateBatchRules>
export const AdminCreateBatchRules = z
.object({
rules: z.array(AdminCreatePromotionRule).min(1),
})
.strict()
export type AdminUpdateBatchRulesType = z.infer<typeof AdminUpdateBatchRules>
export const AdminUpdateBatchRules = z
.object({
rules: z.array(AdminUpdatePromotionRule).min(1),
})
.strict()
export type AdminRemoveBatchRulesType = z.infer<typeof AdminRemoveBatchRules>
export const AdminRemoveBatchRules = z
.object({
rule_ids: z.array(z.string()).min(1),
})
.strict()

View File

@@ -1,37 +0,0 @@
import { addRulesToFulfillmentShippingOptionWorkflow } from "@medusajs/core-flows"
import { AdminShippingOptionRetrieveResponse } from "@medusajs/types"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../../types/routing"
import { AdminShippingOptionRulesBatchAddType } from "../../../../validators"
import { refetchShippingOption } from "../../../../helpers"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminShippingOptionRulesBatchAddType>,
res: MedusaResponse<AdminShippingOptionRetrieveResponse>
) => {
const id = req.params.id
const workflow = addRulesToFulfillmentShippingOptionWorkflow(req.scope)
const { errors } = await workflow.run({
input: {
data: req.validatedBody.rules.map((rule) => ({
...rule,
shipping_option_id: id,
})),
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const shippingOption = await refetchShippingOption(
req.params.id,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json({ shipping_option: shippingOption })
}

View File

@@ -1,31 +0,0 @@
import { removeRulesFromFulfillmentShippingOptionWorkflow } from "@medusajs/core-flows"
import { AdminShippingOptionRetrieveResponse } from "@medusajs/types"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../../types/routing"
import { AdminShippingOptionRulesBatchRemoveType } from "../../../../validators"
import { refetchShippingOption } from "../../../../helpers"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminShippingOptionRulesBatchRemoveType>,
res: MedusaResponse<AdminShippingOptionRetrieveResponse>
) => {
const workflow = removeRulesFromFulfillmentShippingOptionWorkflow(req.scope)
const { errors } = await workflow.run({
input: { ids: req.validatedBody.rule_ids },
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const shippingOption = await refetchShippingOption(
req.params.id,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json({ shipping_option: shippingOption })
}

View File

@@ -0,0 +1,48 @@
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../types/routing"
import { BatchMethodRequest } from "@medusajs/types"
import {
AdminCreateShippingOptionRuleType,
AdminUpdateShippingOptionRuleType,
} from "../../../validators"
import { refetchBatchRules } from "../../../helpers"
import { batchShippingOptionRulesWorkflow } from "@medusajs/core-flows"
export const POST = async (
req: AuthenticatedMedusaRequest<
BatchMethodRequest<
AdminCreateShippingOptionRuleType,
AdminUpdateShippingOptionRuleType
>
>,
res: MedusaResponse
) => {
const id = req.params.id
const { result, errors } = await batchShippingOptionRulesWorkflow(
req.scope
).run({
input: {
create: req.validatedBody.create?.map((c) => ({
...c,
shipping_option_id: id,
})),
update: req.validatedBody.update,
delete: req.validatedBody.delete,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const batchResults = await refetchBatchRules(
result,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json(batchResults)
}

View File

@@ -1,6 +1,11 @@
import { MedusaContainer } from "@medusajs/types"
import {
BatchMethodResponse,
MedusaContainer,
ShippingOptionRuleDTO,
} from "@medusajs/types"
import {
ContainerRegistrationKeys,
promiseAll,
remoteQueryObjectFromString,
} from "@medusajs/utils"
@@ -21,3 +26,44 @@ export const refetchShippingOption = async (
const shippingOptions = await remoteQuery(queryObject)
return shippingOptions[0]
}
export const refetchBatchRules = async (
batchResult: BatchMethodResponse<ShippingOptionRuleDTO>,
scope: MedusaContainer,
fields: string[]
) => {
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
let created = Promise.resolve<ShippingOptionRuleDTO[]>([])
let updated = Promise.resolve<ShippingOptionRuleDTO[]>([])
if (batchResult.created.length) {
const createdQuery = remoteQueryObjectFromString({
entryPoint: "shipping_option_rule",
variables: {
filters: { id: batchResult.created.map((p) => p.id) },
},
fields: fields,
})
created = remoteQuery(createdQuery)
}
if (batchResult.updated.length) {
const updatedQuery = remoteQueryObjectFromString({
entryPoint: "shipping_option_rule",
variables: {
filters: { id: batchResult.updated.map((p) => p.id) },
},
fields: fields,
})
updated = remoteQuery(updatedQuery)
}
const [createdRes, updatedRes] = await promiseAll([created, updated])
return {
created: createdRes,
updated: updatedRes,
deleted: batchResult.deleted,
}
}

View File

@@ -2,18 +2,21 @@ import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { authenticate } from "../../../utils/authenticate-middleware"
import {
AdminCreateShippingOption,
AdminCreateShippingOptionRule,
AdminGetShippingOptionParams,
AdminGetShippingOptionRuleParams,
AdminGetShippingOptionsParams,
AdminShippingOptionRulesBatchAdd,
AdminShippingOptionRulesBatchRemove,
AdminUpdateShippingOption,
AdminUpdateShippingOptionRule,
} from "./validators"
import {
listTransformQueryConfig,
retrieveRuleTransformQueryConfig,
retrieveTransformQueryConfig,
} from "./query-config"
import { validateAndTransformBody } from "../../utils/validate-body"
import { validateAndTransformQuery } from "../../utils/validate-query"
import { createBatchBody } from "../../utils/validators"
export const adminShippingOptionRoutesMiddlewares: MiddlewareRoute[] = [
{
@@ -58,23 +61,17 @@ export const adminShippingOptionRoutesMiddlewares: MiddlewareRoute[] = [
},
{
method: ["POST"],
matcher: "/admin/shipping-options/:id/rules/batch/add",
matcher: "/admin/shipping-options/:id/rules/batch",
middlewares: [
validateAndTransformBody(AdminShippingOptionRulesBatchAdd),
validateAndTransformQuery(
AdminGetShippingOptionParams,
retrieveTransformQueryConfig
validateAndTransformBody(
createBatchBody(
AdminCreateShippingOptionRule,
AdminUpdateShippingOptionRule
)
),
],
},
{
method: ["POST"],
matcher: "/admin/shipping-options/:id/rules/batch/remove",
middlewares: [
validateAndTransformBody(AdminShippingOptionRulesBatchRemove),
validateAndTransformQuery(
AdminGetShippingOptionParams,
retrieveTransformQueryConfig
AdminGetShippingOptionRuleParams,
retrieveRuleTransformQueryConfig
),
],
},

View File

@@ -23,3 +23,21 @@ export const listTransformQueryConfig = {
...retrieveTransformQueryConfig,
isList: true,
}
export const defaultAdminShippingOptionRuleFields = [
"id",
"description",
"attribute",
"operator",
"values.value",
]
export const retrieveRuleTransformQueryConfig = {
defaults: defaultAdminShippingOptionRuleFields,
isList: false,
}
export const listRuleTransformQueryConfig = {
...retrieveRuleTransformQueryConfig,
isList: true,
}

View File

@@ -21,6 +21,10 @@ export const AdminGetShippingOptionsParams = createFindParams({
/**
* SHIPPING OPTIONS RULES
*/
export type AdminGetShippingOptionRuleParamsType = z.infer<
typeof AdminGetShippingOptionRuleParams
>
export const AdminGetShippingOptionRuleParams = createSelectParams()
export type AdminCreateShippingOptionRuleType = z.infer<
typeof AdminCreateShippingOptionRule
@@ -33,21 +37,15 @@ export const AdminCreateShippingOptionRule = z
})
.strict()
export type AdminShippingOptionRulesBatchAddType = z.infer<
typeof AdminShippingOptionRulesBatchAdd
export type AdminUpdateShippingOptionRuleType = z.infer<
typeof AdminUpdateShippingOptionRule
>
export const AdminShippingOptionRulesBatchAdd = z
export const AdminUpdateShippingOptionRule = z
.object({
rules: AdminCreateShippingOptionRule.array(),
})
.strict()
export type AdminShippingOptionRulesBatchRemoveType = z.infer<
typeof AdminShippingOptionRulesBatchRemove
>
export const AdminShippingOptionRulesBatchRemove = z
.object({
rule_ids: z.array(z.string()),
id: z.string(),
operator: z.nativeEnum(RuleOperator),
attribute: z.string(),
value: z.string().or(z.array(z.string())),
})
.strict()

View File

@@ -1,11 +1,12 @@
import { Modules } from "@medusajs/modules-sdk"
import { ModuleJoinerConfig } from "@medusajs/types"
import { generateLinkableKeysMap } from "@medusajs/utils"
import { Campaign, Promotion } from "@models"
import { Campaign, Promotion, PromotionRule } from "@models"
export const LinkableKeys = {
promotion_id: Promotion.name,
campaign_id: Campaign.name,
promotion_rule_id: PromotionRule.name,
}
export const entityNameToLinkableKeysMap = generateLinkableKeysMap(LinkableKeys)
@@ -28,5 +29,12 @@ export const joinerConfig: ModuleJoinerConfig = {
methodSuffix: "Campaigns",
},
},
{
name: ["promotion_rule", "promotion_rules"],
args: {
entity: PromotionRule.name,
methodSuffix: "PromotionRules",
},
},
],
}