fix: Standardize remaining batch methods and update FE calls (#7122)
This commit is contained in:
1 parent
ea14a3bd68
commit
9e3556686b
49 files changed
+851
-611
No files matched your search
@@ -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 () => {
|
it("should throw error when required params are missing", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/rules/batch/add`,
|
`/admin/promotions/${standardPromotion.id}/rules/batch`,
|
||||||
{
|
{
|
||||||
rules: [
|
create: [
|
||||||
{
|
{
|
||||||
operator: "eq",
|
operator: "eq",
|
||||||
values: ["new value"],
|
values: ["new value"],
|
||||||
@@ -89,9 +89,9 @@ medusaIntegrationTestRunner({
|
|||||||
it("should throw error when promotion does not exist", async () => {
|
it("should throw error when promotion does not exist", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/does-not-exist/rules/batch/add`,
|
`/admin/promotions/does-not-exist/rules/batch`,
|
||||||
{
|
{
|
||||||
rules: [
|
create: [
|
||||||
{
|
{
|
||||||
attribute: "new_attr",
|
attribute: "new_attr",
|
||||||
operator: "eq",
|
operator: "eq",
|
||||||
@@ -112,9 +112,9 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
it("should add rules to a promotion successfully", async () => {
|
it("should add rules to a promotion successfully", async () => {
|
||||||
const response = await api.post(
|
const response = await api.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/rules/batch/add`,
|
`/admin/promotions/${standardPromotion.id}/rules/batch`,
|
||||||
{
|
{
|
||||||
rules: [
|
create: [
|
||||||
{
|
{
|
||||||
operator: "eq",
|
operator: "eq",
|
||||||
attribute: "new_attr",
|
attribute: "new_attr",
|
||||||
@@ -126,7 +126,14 @@ medusaIntegrationTestRunner({
|
|||||||
)
|
)
|
||||||
|
|
||||||
expect(response.status).toEqual(200)
|
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({
|
expect.objectContaining({
|
||||||
id: standardPromotion.id,
|
id: standardPromotion.id,
|
||||||
rules: expect.arrayContaining([
|
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 () => {
|
it("should throw error when required params are missing", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/target-rules/batch/add`,
|
`/admin/promotions/${standardPromotion.id}/target-rules/batch`,
|
||||||
{
|
{
|
||||||
rules: [
|
create: [
|
||||||
{
|
{
|
||||||
operator: "eq",
|
operator: "eq",
|
||||||
values: ["new value"],
|
values: ["new value"],
|
||||||
@@ -174,9 +181,9 @@ medusaIntegrationTestRunner({
|
|||||||
it("should throw error when promotion does not exist", async () => {
|
it("should throw error when promotion does not exist", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/does-not-exist/target-rules/batch/add`,
|
`/admin/promotions/does-not-exist/target-rules/batch`,
|
||||||
{
|
{
|
||||||
rules: [
|
create: [
|
||||||
{
|
{
|
||||||
attribute: "new_attr",
|
attribute: "new_attr",
|
||||||
operator: "eq",
|
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(
|
const response = await api.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/target-rules/batch/add`,
|
`/admin/promotions/${standardPromotion.id}/target-rules/batch`,
|
||||||
{
|
{
|
||||||
rules: [
|
create: [
|
||||||
{
|
{
|
||||||
operator: "eq",
|
operator: "eq",
|
||||||
attribute: "new_attr",
|
attribute: "new_attr",
|
||||||
@@ -211,7 +218,14 @@ medusaIntegrationTestRunner({
|
|||||||
)
|
)
|
||||||
|
|
||||||
expect(response.status).toEqual(200)
|
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({
|
expect.objectContaining({
|
||||||
id: standardPromotion.id,
|
id: standardPromotion.id,
|
||||||
application_method: expect.objectContaining({
|
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 () => {
|
it("should throw error when required params are missing", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/buy-rules/batch/add`,
|
`/admin/promotions/${standardPromotion.id}/buy-rules/batch`,
|
||||||
{
|
{
|
||||||
rules: [
|
create: [
|
||||||
{
|
{
|
||||||
operator: "eq",
|
operator: "eq",
|
||||||
values: ["new value"],
|
values: ["new value"],
|
||||||
@@ -261,9 +275,9 @@ medusaIntegrationTestRunner({
|
|||||||
it("should throw error when promotion does not exist", async () => {
|
it("should throw error when promotion does not exist", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/does-not-exist/buy-rules/batch/add`,
|
`/admin/promotions/does-not-exist/buy-rules/batch`,
|
||||||
{
|
{
|
||||||
rules: [
|
create: [
|
||||||
{
|
{
|
||||||
attribute: "new_attr",
|
attribute: "new_attr",
|
||||||
operator: "eq",
|
operator: "eq",
|
||||||
@@ -285,9 +299,9 @@ medusaIntegrationTestRunner({
|
|||||||
it("should throw an error when trying to add buy rules to a standard promotion", async () => {
|
it("should throw an error when trying to add buy rules to a standard promotion", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/buy-rules/batch/add`,
|
`/admin/promotions/${standardPromotion.id}/buy-rules/batch`,
|
||||||
{
|
{
|
||||||
rules: [
|
create: [
|
||||||
{
|
{
|
||||||
operator: "eq",
|
operator: "eq",
|
||||||
attribute: "new_attr",
|
attribute: "new_attr",
|
||||||
@@ -324,9 +338,9 @@ medusaIntegrationTestRunner({
|
|||||||
})
|
})
|
||||||
|
|
||||||
const response = await api.post(
|
const response = await api.post(
|
||||||
`/admin/promotions/${buyGetPromotion.id}/buy-rules/batch/add`,
|
`/admin/promotions/${buyGetPromotion.id}/buy-rules/batch`,
|
||||||
{
|
{
|
||||||
rules: [
|
create: [
|
||||||
{
|
{
|
||||||
operator: "eq",
|
operator: "eq",
|
||||||
attribute: "new_attr",
|
attribute: "new_attr",
|
||||||
@@ -338,7 +352,14 @@ medusaIntegrationTestRunner({
|
|||||||
)
|
)
|
||||||
|
|
||||||
expect(response.status).toEqual(200)
|
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({
|
expect.objectContaining({
|
||||||
id: buyGetPromotion.id,
|
id: buyGetPromotion.id,
|
||||||
application_method: expect.objectContaining({
|
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 () => {
|
it("should throw error when required params are missing", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/rules/batch/remove`,
|
`/admin/promotions/${standardPromotion.id}/rules/batch`,
|
||||||
{},
|
{},
|
||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
@@ -381,8 +402,8 @@ medusaIntegrationTestRunner({
|
|||||||
it("should throw error when promotion does not exist", async () => {
|
it("should throw error when promotion does not exist", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/does-not-exist/rules/batch/remove`,
|
`/admin/promotions/does-not-exist/rules/batch`,
|
||||||
{ rule_ids: ["test-rule-id"] },
|
{ delete: ["test-rule-id"] },
|
||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
.catch((e) => e)
|
.catch((e) => e)
|
||||||
@@ -396,12 +417,12 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
it("should remove rules from a promotion successfully", async () => {
|
it("should remove rules from a promotion successfully", async () => {
|
||||||
const response = await api.post(
|
const response = await api.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/rules/batch/remove`,
|
`/admin/promotions/${standardPromotion.id}/rules/batch`,
|
||||||
{ rule_ids: [standardPromotion.rules[0].id] },
|
{ delete: [standardPromotion.rules[0].id] },
|
||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
expect(response.status).toEqual(200)
|
expect(response.status).toEqual(200)
|
||||||
expect(response.data).toEqual({
|
expect(response.data.deleted).toEqual({
|
||||||
ids: [standardPromotion.rules[0].id],
|
ids: [standardPromotion.rules[0].id],
|
||||||
object: "promotion-rule",
|
object: "promotion-rule",
|
||||||
deleted: true,
|
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 () => {
|
it("should throw error when required params are missing", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/target-rules/batch/remove`,
|
`/admin/promotions/${standardPromotion.id}/target-rules/batch`,
|
||||||
{},
|
{},
|
||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
@@ -437,8 +458,8 @@ medusaIntegrationTestRunner({
|
|||||||
it("should throw error when promotion does not exist", async () => {
|
it("should throw error when promotion does not exist", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/does-not-exist/target-rules/batch/remove`,
|
`/admin/promotions/does-not-exist/target-rules/batch`,
|
||||||
{ rule_ids: ["test-rule-id"] },
|
{ delete: ["test-rule-id"] },
|
||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
.catch((e) => e)
|
.catch((e) => e)
|
||||||
@@ -453,13 +474,13 @@ medusaIntegrationTestRunner({
|
|||||||
it("should remove target rules from a promotion successfully", async () => {
|
it("should remove target rules from a promotion successfully", async () => {
|
||||||
const ruleId = standardPromotion.application_method.target_rules[0].id
|
const ruleId = standardPromotion.application_method.target_rules[0].id
|
||||||
const response = await api.post(
|
const response = await api.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/target-rules/batch/remove`,
|
`/admin/promotions/${standardPromotion.id}/target-rules/batch`,
|
||||||
{ rule_ids: [ruleId] },
|
{ delete: [ruleId] },
|
||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(response.status).toEqual(200)
|
expect(response.status).toEqual(200)
|
||||||
expect(response.data).toEqual({
|
expect(response.data.deleted).toEqual({
|
||||||
ids: [ruleId],
|
ids: [ruleId],
|
||||||
object: "promotion-rule",
|
object: "promotion-rule",
|
||||||
deleted: true,
|
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 () => {
|
it("should throw error when required params are missing", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/buy-rules/batch/remove`,
|
`/admin/promotions/${standardPromotion.id}/buy-rules/batch`,
|
||||||
{},
|
{},
|
||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
@@ -495,8 +516,8 @@ medusaIntegrationTestRunner({
|
|||||||
it("should throw error when promotion does not exist", async () => {
|
it("should throw error when promotion does not exist", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/does-not-exist/buy-rules/batch/remove`,
|
`/admin/promotions/does-not-exist/buy-rules/batch`,
|
||||||
{ rule_ids: ["test-rule-id"] },
|
{ delete: ["test-rule-id"] },
|
||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
.catch((e) => e)
|
.catch((e) => e)
|
||||||
@@ -527,13 +548,13 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
const ruleId = buyGetPromotion!.application_method!.buy_rules![0].id
|
const ruleId = buyGetPromotion!.application_method!.buy_rules![0].id
|
||||||
const response = await api.post(
|
const response = await api.post(
|
||||||
`/admin/promotions/${buyGetPromotion.id}/buy-rules/batch/remove`,
|
`/admin/promotions/${buyGetPromotion.id}/buy-rules/batch`,
|
||||||
{ rule_ids: [ruleId] },
|
{ delete: [ruleId] },
|
||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(response.status).toEqual(200)
|
expect(response.status).toEqual(200)
|
||||||
expect(response.data).toEqual({
|
expect(response.data.deleted).toEqual({
|
||||||
ids: [ruleId],
|
ids: [ruleId],
|
||||||
object: "promotion-rule",
|
object: "promotion-rule",
|
||||||
deleted: true,
|
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 () => {
|
it("should throw error when required params are missing", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/rules/batch/update`,
|
`/admin/promotions/${standardPromotion.id}/rules/batch`,
|
||||||
{
|
{
|
||||||
rules: [
|
update: [
|
||||||
{
|
{
|
||||||
attribute: "test",
|
attribute: "test",
|
||||||
operator: "eq",
|
operator: "eq",
|
||||||
@@ -575,9 +596,9 @@ medusaIntegrationTestRunner({
|
|||||||
it("should throw error when promotion does not exist", async () => {
|
it("should throw error when promotion does not exist", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/does-not-exist/rules/batch/update`,
|
`/admin/promotions/does-not-exist/rules/batch`,
|
||||||
{
|
{
|
||||||
rules: [
|
update: [
|
||||||
{
|
{
|
||||||
id: standardPromotion.rules[0].id,
|
id: standardPromotion.rules[0].id,
|
||||||
attribute: "new_attr",
|
attribute: "new_attr",
|
||||||
@@ -600,9 +621,9 @@ medusaIntegrationTestRunner({
|
|||||||
it("should throw error when promotion rule id does not exist", async () => {
|
it("should throw error when promotion rule id does not exist", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/rules/batch/update`,
|
`/admin/promotions/${standardPromotion.id}/rules/batch`,
|
||||||
{
|
{
|
||||||
rules: [
|
update: [
|
||||||
{
|
{
|
||||||
id: "does-not-exist",
|
id: "does-not-exist",
|
||||||
attribute: "new_attr",
|
attribute: "new_attr",
|
||||||
@@ -624,9 +645,9 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
it("should add rules to a promotion successfully", async () => {
|
it("should add rules to a promotion successfully", async () => {
|
||||||
const response = await api.post(
|
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,
|
id: standardPromotion.rules[0].id,
|
||||||
operator: "eq",
|
operator: "eq",
|
||||||
@@ -639,7 +660,14 @@ medusaIntegrationTestRunner({
|
|||||||
)
|
)
|
||||||
|
|
||||||
expect(response.status).toEqual(200)
|
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({
|
expect.objectContaining({
|
||||||
id: standardPromotion.id,
|
id: standardPromotion.id,
|
||||||
rules: expect.arrayContaining([
|
rules: expect.arrayContaining([
|
||||||
|
|||||||
+29
-15
@@ -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 () => {
|
it("should throw error when required params are missing", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.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
|
adminHeaders
|
||||||
)
|
)
|
||||||
@@ -86,9 +86,9 @@ medusaIntegrationTestRunner({
|
|||||||
it.only("should throw error when shipping option does not exist", async () => {
|
it.only("should throw error when shipping option does not exist", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.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" },
|
{ attribute: "new_attr", operator: "eq", value: "new value" },
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@@ -106,9 +106,9 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
it("should add rules to a shipping option successfully", async () => {
|
it("should add rules to a shipping option successfully", async () => {
|
||||||
const response = await api.post(
|
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" },
|
{ operator: "eq", attribute: "new_attr", value: "new value" },
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@@ -116,7 +116,14 @@ medusaIntegrationTestRunner({
|
|||||||
)
|
)
|
||||||
|
|
||||||
expect(response.status).toEqual(200)
|
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({
|
expect.objectContaining({
|
||||||
id: shippingOption.id,
|
id: shippingOption.id,
|
||||||
rules: expect.arrayContaining([
|
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 () => {
|
it("should throw error when required params are missing", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/shipping-options/${shippingOption.id}/rules/batch/remove`,
|
`/admin/shipping-options/${shippingOption.id}/rules/batch`,
|
||||||
{},
|
{},
|
||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
@@ -161,8 +168,8 @@ medusaIntegrationTestRunner({
|
|||||||
it("should throw error when shipping option does not exist", async () => {
|
it("should throw error when shipping option does not exist", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/shipping-options/does-not-exist/rules/batch/remove`,
|
`/admin/shipping-options/does-not-exist/rules/batch`,
|
||||||
{ rule_ids: ["test"] },
|
{ delete: ["test"] },
|
||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
.catch((e) => e)
|
.catch((e) => e)
|
||||||
@@ -176,15 +183,22 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
it("should add rules to a shipping option successfully", async () => {
|
it("should add rules to a shipping option successfully", async () => {
|
||||||
const response = await api.post(
|
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
|
adminHeaders
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(response.status).toEqual(200)
|
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({
|
expect.objectContaining({
|
||||||
id: shippingOption.id,
|
id: shippingOption.id,
|
||||||
rules: [],
|
rules: [],
|
||||||
|
|||||||
@@ -32,8 +32,10 @@ const batchRemoveSalesChannelsFromApiKey = async (
|
|||||||
payload: { sales_channel_ids: string[] }
|
payload: { sales_channel_ids: string[] }
|
||||||
) => {
|
) => {
|
||||||
return postRequest<AdminApiKeyResponse>(
|
return postRequest<AdminApiKeyResponse>(
|
||||||
`/admin/api-keys/${id}/sales-channels/batch/remove`,
|
`/admin/api-keys/${id}/sales-channels`,
|
||||||
payload
|
{
|
||||||
|
remove: payload.sales_channel_ids,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,8 +44,10 @@ const batchAddSalesChannelsFromApiKey = async (
|
|||||||
payload: { sales_channel_ids: string[] }
|
payload: { sales_channel_ids: string[] }
|
||||||
) => {
|
) => {
|
||||||
return postRequest<AdminApiKeyResponse>(
|
return postRequest<AdminApiKeyResponse>(
|
||||||
`/admin/api-keys/${id}/sales-channels/batch/add`,
|
`/admin/api-keys/${id}/sales-channels`,
|
||||||
payload
|
{
|
||||||
|
add: payload.sales_channel_ids,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,8 +53,10 @@ async function batchAddCustomers(
|
|||||||
payload: { customer_ids: { id: string }[] }
|
payload: { customer_ids: { id: string }[] }
|
||||||
) {
|
) {
|
||||||
return postRequest<AdminCustomerGroupResponse>(
|
return postRequest<AdminCustomerGroupResponse>(
|
||||||
`/admin/customer-groups/${id}/customers/batch/add`,
|
`/admin/customer-groups/${id}/customers`,
|
||||||
payload
|
{
|
||||||
|
add: payload.customer_ids,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,8 +65,10 @@ async function batchRemoveCustomers(
|
|||||||
payload: { customer_ids: { id: string }[] }
|
payload: { customer_ids: { id: string }[] }
|
||||||
) {
|
) {
|
||||||
return postRequest<AdminCustomerGroupResponse>(
|
return postRequest<AdminCustomerGroupResponse>(
|
||||||
`/admin/customer-groups/${id}/customers/batch/remove`,
|
`/admin/customer-groups/${id}/customers`,
|
||||||
payload
|
{
|
||||||
|
remove: payload.customer_ids,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -110,8 +110,11 @@ async function batchPostLocationLevels(
|
|||||||
payload: InventoryItemLocationBatch
|
payload: InventoryItemLocationBatch
|
||||||
) {
|
) {
|
||||||
return postRequest<AdminInventoryLevelResponse>(
|
return postRequest<AdminInventoryLevelResponse>(
|
||||||
`/admin/inventory-items/${inventoryItemId}/location-levels/batch/combi`,
|
`/admin/inventory-items/${inventoryItemId}/location-levels/batch`,
|
||||||
payload
|
{
|
||||||
|
create: payload.creates,
|
||||||
|
delete: payload.deletes,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,20 +32,18 @@ async function deletePriceList(id: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function addPriceListPrices(id: string, payload: AddPriceListPricesReq) {
|
async function addPriceListPrices(id: string, payload: AddPriceListPricesReq) {
|
||||||
return postRequest<PriceListRes>(
|
return postRequest<PriceListRes>(`/admin/price-lists/${id}/prices/batch`, {
|
||||||
`/admin/price-lists/${id}/prices/batch/add`,
|
create: payload.prices,
|
||||||
payload
|
})
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function removePriceListPrices(
|
async function removePriceListPrices(
|
||||||
id: string,
|
id: string,
|
||||||
payload: DeletePriceListPricesReq
|
payload: DeletePriceListPricesReq
|
||||||
) {
|
) {
|
||||||
return postRequest<PriceListRes>(
|
return postRequest<PriceListRes>(`/admin/price-lists/${id}/prices/batch`, {
|
||||||
`/admin/price-lists/${id}/prices/batch/remove`,
|
delete: payload.ids,
|
||||||
payload
|
})
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const priceLists = {
|
export const priceLists = {
|
||||||
|
|||||||
@@ -46,8 +46,10 @@ async function addPromotionRules(
|
|||||||
payload: BatchAddPromotionRulesReq
|
payload: BatchAddPromotionRulesReq
|
||||||
) {
|
) {
|
||||||
return postRequest<PromotionRes>(
|
return postRequest<PromotionRes>(
|
||||||
`/admin/promotions/${id}/${ruleType}/batch/add`,
|
`/admin/promotions/${id}/${ruleType}/batch`,
|
||||||
payload
|
{
|
||||||
|
create: payload.rules,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,8 +59,10 @@ async function updatePromotionRules(
|
|||||||
payload: BatchUpdatePromotionRulesReq
|
payload: BatchUpdatePromotionRulesReq
|
||||||
) {
|
) {
|
||||||
return postRequest<PromotionRes>(
|
return postRequest<PromotionRes>(
|
||||||
`/admin/promotions/${id}/${ruleType}/batch/update`,
|
`/admin/promotions/${id}/${ruleType}/batch`,
|
||||||
payload
|
{
|
||||||
|
update: payload.rules,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,8 +72,10 @@ async function removePromotionRules(
|
|||||||
payload: BatchRemovePromotionRulesReq
|
payload: BatchRemovePromotionRulesReq
|
||||||
) {
|
) {
|
||||||
return postRequest<PromotionRes>(
|
return postRequest<PromotionRes>(
|
||||||
`/admin/promotions/${id}/${ruleType}/batch/remove`,
|
`/admin/promotions/${id}/${ruleType}/batch`,
|
||||||
payload
|
{
|
||||||
|
delete: payload.rule_ids,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,8 +48,10 @@ async function batchRemoveProducts(
|
|||||||
payload: RemoveProductsSalesChannelReq
|
payload: RemoveProductsSalesChannelReq
|
||||||
) {
|
) {
|
||||||
return postRequest<AdminSalesChannelResponse>(
|
return postRequest<AdminSalesChannelResponse>(
|
||||||
`/admin/sales-channels/${id}/products/batch/remove`,
|
`/admin/sales-channels/${id}/products`,
|
||||||
payload
|
{
|
||||||
|
remove: payload.product_ids,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,8 +60,10 @@ async function batchAddProducts(
|
|||||||
payload: AddProductsSalesChannelReq
|
payload: AddProductsSalesChannelReq
|
||||||
) {
|
) {
|
||||||
return postRequest<AdminSalesChannelResponse>(
|
return postRequest<AdminSalesChannelResponse>(
|
||||||
`/admin/sales-channels/${id}/products/batch/add`,
|
`/admin/sales-channels/${id}/products`,
|
||||||
payload
|
{
|
||||||
|
add: payload.product_ids,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-4
@@ -5,10 +5,9 @@ import {
|
|||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||||
|
|
||||||
export const addRulesToFulfillmentShippingOptionStepId =
|
export const createShippingOptionRulesStepId = "create-shipping-option-rules"
|
||||||
"add-rules-to-fulfillment-shipping-option"
|
export const createShippingOptionRulesStep = createStep(
|
||||||
export const addRulesToFulfillmentShippingOptionStep = createStep(
|
createShippingOptionRulesStepId,
|
||||||
addRulesToFulfillmentShippingOptionStepId,
|
|
||||||
async (
|
async (
|
||||||
input: AddFulfillmentShippingOptionRulesWorkflowDTO,
|
input: AddFulfillmentShippingOptionRulesWorkflowDTO,
|
||||||
{ container }
|
{ container }
|
||||||
+3
-4
@@ -6,10 +6,9 @@ import {
|
|||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||||
|
|
||||||
export const removeRulesFromFulfillmentShippingOptionStepId =
|
export const deleteShippingOptionRulesStepId = "delete-shipping-option-rules"
|
||||||
"remove-rules-from-fulfillment-shipping-option"
|
export const deleteShippingOptionRulesStep = createStep(
|
||||||
export const removeRulesFromFulfillmentShippingOptionStep = createStep(
|
deleteShippingOptionRulesStepId,
|
||||||
removeRulesFromFulfillmentShippingOptionStepId,
|
|
||||||
async (
|
async (
|
||||||
input: RemoveFulfillmentShippingOptionRulesWorkflowDTO,
|
input: RemoveFulfillmentShippingOptionRulesWorkflowDTO,
|
||||||
{ container }
|
{ container }
|
||||||
@@ -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 "./add-shipping-options-prices"
|
||||||
export * from "./cancel-fulfillment"
|
export * from "./cancel-fulfillment"
|
||||||
export * from "./create-fulfillment"
|
export * from "./create-fulfillment"
|
||||||
@@ -8,7 +8,7 @@ export * from "./create-shipping-profiles"
|
|||||||
export * from "./delete-fulfillment-sets"
|
export * from "./delete-fulfillment-sets"
|
||||||
export * from "./delete-service-zones"
|
export * from "./delete-service-zones"
|
||||||
export * from "./delete-shipping-options"
|
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 "./set-shipping-options-prices"
|
||||||
export * from "./update-fulfillment"
|
export * from "./update-fulfillment"
|
||||||
export * from "./upsert-shipping-options"
|
export * from "./upsert-shipping-options"
|
||||||
|
|||||||
-14
@@ -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)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
@@ -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: [] }))
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
export * from "./add-rules-to-fulfillment-shipping-option"
|
export * from "./batch-shipping-option-rules"
|
||||||
export * from "./cancel-fulfillment"
|
export * from "./cancel-fulfillment"
|
||||||
export * from "./create-fulfillment"
|
export * from "./create-fulfillment"
|
||||||
export * from "./create-service-zones"
|
export * from "./create-service-zones"
|
||||||
@@ -8,7 +8,6 @@ export * from "./create-shipping-profiles"
|
|||||||
export * from "./delete-fulfillment-sets"
|
export * from "./delete-fulfillment-sets"
|
||||||
export * from "./delete-service-zones"
|
export * from "./delete-service-zones"
|
||||||
export * from "./delete-shipping-options"
|
export * from "./delete-shipping-options"
|
||||||
export * from "./remove-rules-from-fulfillment-shipping-option"
|
|
||||||
export * from "./update-fulfillment"
|
export * from "./update-fulfillment"
|
||||||
export * from "./update-service-zones"
|
export * from "./update-service-zones"
|
||||||
export * from "./update-shipping-options"
|
export * from "./update-shipping-options"
|
||||||
-14
@@ -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)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
@@ -18,19 +18,42 @@ export const batchProductVariantsStep = createStep(
|
|||||||
>,
|
>,
|
||||||
{ container }
|
{ container }
|
||||||
) => {
|
) => {
|
||||||
const { transaction: createTransaction, result: created } =
|
const {
|
||||||
await createProductVariantsWorkflow(container).run({
|
transaction: createTransaction,
|
||||||
input: { product_variants: data.create ?? [] },
|
result: created,
|
||||||
})
|
errors: createErrors,
|
||||||
const { transaction: updateTransaction, result: updated } =
|
} = await createProductVariantsWorkflow(container).run({
|
||||||
await updateProductVariantsWorkflow(container).run({
|
input: { product_variants: data.create ?? [] },
|
||||||
input: { product_variants: data.update ?? [] },
|
throwOnError: false,
|
||||||
})
|
})
|
||||||
const { transaction: deleteTransaction } =
|
|
||||||
|
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({
|
await deleteProductVariantsWorkflow(container).run({
|
||||||
input: { ids: data.delete ?? [] },
|
input: { ids: data.delete ?? [] },
|
||||||
|
throwOnError: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (deleteErrors?.length) {
|
||||||
|
throw deleteErrors[0].error
|
||||||
|
}
|
||||||
|
|
||||||
return new StepResponse(
|
return new StepResponse(
|
||||||
{
|
{
|
||||||
created,
|
created,
|
||||||
|
|||||||
@@ -18,20 +18,42 @@ export const batchProductsStep = createStep(
|
|||||||
>,
|
>,
|
||||||
{ container }
|
{ container }
|
||||||
) => {
|
) => {
|
||||||
const { transaction: createTransaction, result: created } =
|
const {
|
||||||
await createProductsWorkflow(container).run({
|
transaction: createTransaction,
|
||||||
input: { products: data.create ?? [] },
|
result: created,
|
||||||
})
|
errors: createErrors,
|
||||||
const { transaction: updateTransaction, result: updated } =
|
} = await createProductsWorkflow(container).run({
|
||||||
await updateProductsWorkflow(container).run({
|
input: { products: data.create ?? [] },
|
||||||
input: { products: data.update ?? [] },
|
throwOnError: false,
|
||||||
})
|
|
||||||
const { transaction: deleteTransaction } = await deleteProductsWorkflow(
|
|
||||||
container
|
|
||||||
).run({
|
|
||||||
input: { ids: data.delete ?? [] },
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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(
|
return new StepResponse(
|
||||||
{
|
{
|
||||||
created,
|
created,
|
||||||
|
|||||||
@@ -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 })
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -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 })
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -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 })
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -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)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
)
|
||||||
+4
-5
@@ -2,13 +2,12 @@ import { RemovePromotionRulesWorkflowDTO } from "@medusajs/types"
|
|||||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||||
import { removeRulesFromPromotionsStep } from "../steps"
|
import { removeRulesFromPromotionsStep } from "../steps"
|
||||||
|
|
||||||
export const removeRulesFromPromotionsWorkflowId =
|
export const deletePromotionRulesWorkflowId = "delete-promotion-rules-workflow"
|
||||||
"remove-rules-from-promotions-workflow"
|
export const deletePromotionRulesWorkflow = createWorkflow(
|
||||||
export const removeRulesFromPromotionsWorkflow = createWorkflow(
|
deletePromotionRulesWorkflowId,
|
||||||
removeRulesFromPromotionsWorkflowId,
|
|
||||||
(
|
(
|
||||||
input: WorkflowData<RemovePromotionRulesWorkflowDTO>
|
input: WorkflowData<RemovePromotionRulesWorkflowDTO>
|
||||||
): WorkflowData<void> => {
|
): WorkflowData<void> => {
|
||||||
removeRulesFromPromotionsStep(input)
|
return removeRulesFromPromotionsStep(input)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
export * from "./add-rules-to-promotions"
|
export * from "./batch-promotion-rules"
|
||||||
export * from "./create-campaigns"
|
export * from "./create-campaigns"
|
||||||
export * from "./create-promotions"
|
export * from "./create-promotions"
|
||||||
export * from "./delete-campaigns"
|
export * from "./delete-campaigns"
|
||||||
export * from "./delete-promotions"
|
export * from "./delete-promotions"
|
||||||
export * from "./remove-rules-from-promotions"
|
|
||||||
export * from "./update-campaigns"
|
export * from "./update-campaigns"
|
||||||
export * from "./update-promotion-rules"
|
export * from "./update-promotion-rules"
|
||||||
|
export * from "./delete-promotion-rules"
|
||||||
|
export * from "./create-promotion-rules"
|
||||||
export * from "./update-promotions"
|
export * from "./update-promotions"
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
import { UpdatePromotionRulesWorkflowDTO } from "@medusajs/types"
|
import {
|
||||||
|
UpdatePromotionRulesWorkflowDTO,
|
||||||
|
PromotionRuleDTO,
|
||||||
|
} from "@medusajs/types"
|
||||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||||
import { updatePromotionRulesStep } from "../steps"
|
import { updatePromotionRulesStep } from "../steps"
|
||||||
|
|
||||||
@@ -7,7 +10,7 @@ export const updatePromotionRulesWorkflow = createWorkflow(
|
|||||||
updatePromotionRulesWorkflowId,
|
updatePromotionRulesWorkflowId,
|
||||||
(
|
(
|
||||||
input: WorkflowData<UpdatePromotionRulesWorkflowDTO>
|
input: WorkflowData<UpdatePromotionRulesWorkflowDTO>
|
||||||
): WorkflowData<void> => {
|
): WorkflowData<PromotionRuleDTO[]> => {
|
||||||
updatePromotionRulesStep(input)
|
return updatePromotionRulesStep(input)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -8,6 +8,7 @@ import {
|
|||||||
GeoZone,
|
GeoZone,
|
||||||
ServiceZone,
|
ServiceZone,
|
||||||
ShippingOption,
|
ShippingOption,
|
||||||
|
ShippingOptionRule,
|
||||||
ShippingProfile,
|
ShippingProfile,
|
||||||
} from "@models"
|
} from "@models"
|
||||||
|
|
||||||
@@ -15,6 +16,7 @@ export const LinkableKeys: Record<string, string> = {
|
|||||||
fulfillment_id: Fulfillment.name,
|
fulfillment_id: Fulfillment.name,
|
||||||
fulfillment_set_id: FulfillmentSet.name,
|
fulfillment_set_id: FulfillmentSet.name,
|
||||||
shipping_option_id: ShippingOption.name,
|
shipping_option_id: ShippingOption.name,
|
||||||
|
shipping_option_rule_id: ShippingOptionRule.name,
|
||||||
}
|
}
|
||||||
|
|
||||||
const entityLinkableKeysMap: MapToConfig = {}
|
const entityLinkableKeysMap: MapToConfig = {}
|
||||||
@@ -81,5 +83,12 @@ export const joinerConfig: ModuleJoinerConfig = {
|
|||||||
methodSuffix: "GeoZones",
|
methodSuffix: "GeoZones",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: ["shipping_option_rule", "shipping_option_rules"],
|
||||||
|
args: {
|
||||||
|
entity: ShippingOptionRule.name,
|
||||||
|
methodSuffix: "ShippingOptionRules",
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
} as ModuleJoinerConfig
|
} as ModuleJoinerConfig
|
||||||
@@ -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 })
|
|
||||||
}
|
|
||||||
@@ -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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -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 })
|
|
||||||
}
|
|
||||||
@@ -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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -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 })
|
|
||||||
}
|
|
||||||
@@ -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 })
|
|
||||||
}
|
|
||||||
@@ -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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
import { MedusaContainer } from "@medusajs/types"
|
import { BatchMethodResponse } from "@medusajs/types"
|
||||||
|
import { PromotionRuleDTO, MedusaContainer } from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
|
promiseAll,
|
||||||
ContainerRegistrationKeys,
|
ContainerRegistrationKeys,
|
||||||
remoteQueryObjectFromString,
|
remoteQueryObjectFromString,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
@@ -21,3 +23,44 @@ export const refetchPromotion = async (
|
|||||||
const promotions = await remoteQuery(queryObject)
|
const promotions = await remoteQuery(queryObject)
|
||||||
return promotions[0]
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,17 +3,18 @@ import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
|||||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||||
import { validateAndTransformQuery } from "../../utils/validate-query"
|
import { validateAndTransformQuery } from "../../utils/validate-query"
|
||||||
import {
|
import {
|
||||||
AdminCreateBatchRules,
|
|
||||||
AdminCreatePromotion,
|
AdminCreatePromotion,
|
||||||
|
AdminCreatePromotionRule,
|
||||||
AdminGetPromotionParams,
|
AdminGetPromotionParams,
|
||||||
|
AdminGetPromotionRuleParams,
|
||||||
AdminGetPromotionRuleTypeParams,
|
AdminGetPromotionRuleTypeParams,
|
||||||
AdminGetPromotionsParams,
|
AdminGetPromotionsParams,
|
||||||
AdminGetPromotionsRuleValueParams,
|
AdminGetPromotionsRuleValueParams,
|
||||||
AdminRemoveBatchRules,
|
|
||||||
AdminUpdateBatchRules,
|
|
||||||
AdminUpdatePromotion,
|
AdminUpdatePromotion,
|
||||||
|
AdminUpdatePromotionRule,
|
||||||
} from "./validators"
|
} from "./validators"
|
||||||
import { validateAndTransformBody } from "../../utils/validate-body"
|
import { validateAndTransformBody } from "../../utils/validate-body"
|
||||||
|
import { createBatchBody } from "../../utils/validators"
|
||||||
|
|
||||||
export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [
|
export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||||
{
|
{
|
||||||
@@ -74,78 +75,40 @@ export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/promotions/:id/rules/batch/add",
|
matcher: "/admin/promotions/:id/rules/batch",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
validateAndTransformBody(AdminCreateBatchRules),
|
validateAndTransformBody(
|
||||||
|
createBatchBody(AdminCreatePromotionRule, AdminUpdatePromotionRule)
|
||||||
|
),
|
||||||
validateAndTransformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetPromotionParams,
|
AdminGetPromotionRuleParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveRuleTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/promotions/:id/target-rules/batch/add",
|
matcher: "/admin/promotions/:id/target-rules/batch",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
validateAndTransformBody(AdminCreateBatchRules),
|
validateAndTransformBody(
|
||||||
|
createBatchBody(AdminCreatePromotionRule, AdminUpdatePromotionRule)
|
||||||
|
),
|
||||||
validateAndTransformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetPromotionParams,
|
AdminGetPromotionRuleParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveRuleTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/promotions/:id/buy-rules/batch/add",
|
matcher: "/admin/promotions/:id/buy-rules/batch",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
validateAndTransformBody(AdminCreateBatchRules),
|
validateAndTransformBody(
|
||||||
validateAndTransformQuery(
|
createBatchBody(AdminCreatePromotionRule, AdminUpdatePromotionRule)
|
||||||
AdminGetPromotionParams,
|
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
|
||||||
),
|
),
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
method: ["POST"],
|
|
||||||
matcher: "/admin/promotions/:id/rules/batch/update",
|
|
||||||
middlewares: [
|
|
||||||
validateAndTransformBody(AdminUpdateBatchRules),
|
|
||||||
validateAndTransformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetPromotionParams,
|
AdminGetPromotionRuleParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveRuleTransformQueryConfig
|
||||||
),
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
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
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -19,6 +19,14 @@ export const defaultAdminPromotionFields = [
|
|||||||
"rules.values.value",
|
"rules.values.value",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
export const defaultAdminPromotionRuleFields = [
|
||||||
|
"id",
|
||||||
|
"description",
|
||||||
|
"attribute",
|
||||||
|
"operator",
|
||||||
|
"values.value",
|
||||||
|
]
|
||||||
|
|
||||||
export const retrieveTransformQueryConfig = {
|
export const retrieveTransformQueryConfig = {
|
||||||
defaults: defaultAdminPromotionFields,
|
defaults: defaultAdminPromotionFields,
|
||||||
isList: false,
|
isList: false,
|
||||||
@@ -29,6 +37,16 @@ export const listTransformQueryConfig = {
|
|||||||
isList: true,
|
isList: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const retrieveRuleTransformQueryConfig = {
|
||||||
|
defaults: defaultAdminPromotionRuleFields,
|
||||||
|
isList: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
export const listRuleTransformQueryConfig = {
|
||||||
|
...retrieveRuleTransformQueryConfig,
|
||||||
|
isList: true,
|
||||||
|
}
|
||||||
|
|
||||||
export const listRuleValueTransformQueryConfig = {
|
export const listRuleValueTransformQueryConfig = {
|
||||||
defaults: [],
|
defaults: [],
|
||||||
allowed: [],
|
allowed: [],
|
||||||
|
|||||||
@@ -36,6 +36,11 @@ export const AdminGetPromotionsParams = createFindParams({
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
export type AdminGetPromotionRuleParamsType = z.infer<
|
||||||
|
typeof AdminGetPromotionRuleParams
|
||||||
|
>
|
||||||
|
export const AdminGetPromotionRuleParams = createSelectParams()
|
||||||
|
|
||||||
export type AdminGetPromotionRuleTypeParamsType = z.infer<
|
export type AdminGetPromotionRuleTypeParamsType = z.infer<
|
||||||
typeof AdminGetPromotionRuleTypeParams
|
typeof AdminGetPromotionRuleTypeParams
|
||||||
>
|
>
|
||||||
@@ -185,24 +190,3 @@ export const AdminUpdatePromotion = z
|
|||||||
message:
|
message:
|
||||||
"Buyget promotions require at least one buy rule and quantities to be defined",
|
"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()
|
|
||||||
@@ -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 })
|
|
||||||
}
|
|
||||||
@@ -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 })
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -1,6 +1,11 @@
|
|||||||
import { MedusaContainer } from "@medusajs/types"
|
import {
|
||||||
|
BatchMethodResponse,
|
||||||
|
MedusaContainer,
|
||||||
|
ShippingOptionRuleDTO,
|
||||||
|
} from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
ContainerRegistrationKeys,
|
ContainerRegistrationKeys,
|
||||||
|
promiseAll,
|
||||||
remoteQueryObjectFromString,
|
remoteQueryObjectFromString,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
@@ -21,3 +26,44 @@ export const refetchShippingOption = async (
|
|||||||
const shippingOptions = await remoteQuery(queryObject)
|
const shippingOptions = await remoteQuery(queryObject)
|
||||||
return shippingOptions[0]
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,18 +2,21 @@ import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
|||||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||||
import {
|
import {
|
||||||
AdminCreateShippingOption,
|
AdminCreateShippingOption,
|
||||||
|
AdminCreateShippingOptionRule,
|
||||||
AdminGetShippingOptionParams,
|
AdminGetShippingOptionParams,
|
||||||
|
AdminGetShippingOptionRuleParams,
|
||||||
AdminGetShippingOptionsParams,
|
AdminGetShippingOptionsParams,
|
||||||
AdminShippingOptionRulesBatchAdd,
|
|
||||||
AdminShippingOptionRulesBatchRemove,
|
|
||||||
AdminUpdateShippingOption,
|
AdminUpdateShippingOption,
|
||||||
|
AdminUpdateShippingOptionRule,
|
||||||
} from "./validators"
|
} from "./validators"
|
||||||
import {
|
import {
|
||||||
listTransformQueryConfig,
|
listTransformQueryConfig,
|
||||||
|
retrieveRuleTransformQueryConfig,
|
||||||
retrieveTransformQueryConfig,
|
retrieveTransformQueryConfig,
|
||||||
} from "./query-config"
|
} from "./query-config"
|
||||||
import { validateAndTransformBody } from "../../utils/validate-body"
|
import { validateAndTransformBody } from "../../utils/validate-body"
|
||||||
import { validateAndTransformQuery } from "../../utils/validate-query"
|
import { validateAndTransformQuery } from "../../utils/validate-query"
|
||||||
|
import { createBatchBody } from "../../utils/validators"
|
||||||
|
|
||||||
export const adminShippingOptionRoutesMiddlewares: MiddlewareRoute[] = [
|
export const adminShippingOptionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||||
{
|
{
|
||||||
@@ -58,23 +61,17 @@ export const adminShippingOptionRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/shipping-options/:id/rules/batch/add",
|
matcher: "/admin/shipping-options/:id/rules/batch",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
validateAndTransformBody(AdminShippingOptionRulesBatchAdd),
|
validateAndTransformBody(
|
||||||
validateAndTransformQuery(
|
createBatchBody(
|
||||||
AdminGetShippingOptionParams,
|
AdminCreateShippingOptionRule,
|
||||||
retrieveTransformQueryConfig
|
AdminUpdateShippingOptionRule
|
||||||
|
)
|
||||||
),
|
),
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
method: ["POST"],
|
|
||||||
matcher: "/admin/shipping-options/:id/rules/batch/remove",
|
|
||||||
middlewares: [
|
|
||||||
validateAndTransformBody(AdminShippingOptionRulesBatchRemove),
|
|
||||||
validateAndTransformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetShippingOptionParams,
|
AdminGetShippingOptionRuleParams,
|
||||||
retrieveTransformQueryConfig
|
retrieveRuleTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -23,3 +23,21 @@ export const listTransformQueryConfig = {
|
|||||||
...retrieveTransformQueryConfig,
|
...retrieveTransformQueryConfig,
|
||||||
isList: true,
|
isList: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const defaultAdminShippingOptionRuleFields = [
|
||||||
|
"id",
|
||||||
|
"description",
|
||||||
|
"attribute",
|
||||||
|
"operator",
|
||||||
|
"values.value",
|
||||||
|
]
|
||||||
|
|
||||||
|
export const retrieveRuleTransformQueryConfig = {
|
||||||
|
defaults: defaultAdminShippingOptionRuleFields,
|
||||||
|
isList: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
export const listRuleTransformQueryConfig = {
|
||||||
|
...retrieveRuleTransformQueryConfig,
|
||||||
|
isList: true,
|
||||||
|
}
|
||||||
@@ -21,6 +21,10 @@ export const AdminGetShippingOptionsParams = createFindParams({
|
|||||||
/**
|
/**
|
||||||
* SHIPPING OPTIONS RULES
|
* SHIPPING OPTIONS RULES
|
||||||
*/
|
*/
|
||||||
|
export type AdminGetShippingOptionRuleParamsType = z.infer<
|
||||||
|
typeof AdminGetShippingOptionRuleParams
|
||||||
|
>
|
||||||
|
export const AdminGetShippingOptionRuleParams = createSelectParams()
|
||||||
|
|
||||||
export type AdminCreateShippingOptionRuleType = z.infer<
|
export type AdminCreateShippingOptionRuleType = z.infer<
|
||||||
typeof AdminCreateShippingOptionRule
|
typeof AdminCreateShippingOptionRule
|
||||||
@@ -33,21 +37,15 @@ export const AdminCreateShippingOptionRule = z
|
|||||||
})
|
})
|
||||||
.strict()
|
.strict()
|
||||||
|
|
||||||
export type AdminShippingOptionRulesBatchAddType = z.infer<
|
export type AdminUpdateShippingOptionRuleType = z.infer<
|
||||||
typeof AdminShippingOptionRulesBatchAdd
|
typeof AdminUpdateShippingOptionRule
|
||||||
>
|
>
|
||||||
export const AdminShippingOptionRulesBatchAdd = z
|
export const AdminUpdateShippingOptionRule = z
|
||||||
.object({
|
.object({
|
||||||
rules: AdminCreateShippingOptionRule.array(),
|
id: z.string(),
|
||||||
})
|
operator: z.nativeEnum(RuleOperator),
|
||||||
.strict()
|
attribute: z.string(),
|
||||||
|
value: z.string().or(z.array(z.string())),
|
||||||
export type AdminShippingOptionRulesBatchRemoveType = z.infer<
|
|
||||||
typeof AdminShippingOptionRulesBatchRemove
|
|
||||||
>
|
|
||||||
export const AdminShippingOptionRulesBatchRemove = z
|
|
||||||
.object({
|
|
||||||
rule_ids: z.array(z.string()),
|
|
||||||
})
|
})
|
||||||
.strict()
|
.strict()
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
import { Modules } from "@medusajs/modules-sdk"
|
import { Modules } from "@medusajs/modules-sdk"
|
||||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||||
import { generateLinkableKeysMap } from "@medusajs/utils"
|
import { generateLinkableKeysMap } from "@medusajs/utils"
|
||||||
import { Campaign, Promotion } from "@models"
|
import { Campaign, Promotion, PromotionRule } from "@models"
|
||||||
|
|
||||||
export const LinkableKeys = {
|
export const LinkableKeys = {
|
||||||
promotion_id: Promotion.name,
|
promotion_id: Promotion.name,
|
||||||
campaign_id: Campaign.name,
|
campaign_id: Campaign.name,
|
||||||
|
promotion_rule_id: PromotionRule.name,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const entityNameToLinkableKeysMap = generateLinkableKeysMap(LinkableKeys)
|
export const entityNameToLinkableKeysMap = generateLinkableKeysMap(LinkableKeys)
|
||||||
@@ -28,5 +29,12 @@ export const joinerConfig: ModuleJoinerConfig = {
|
|||||||
methodSuffix: "Campaigns",
|
methodSuffix: "Campaigns",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: ["promotion_rule", "promotion_rules"],
|
||||||
|
args: {
|
||||||
|
entity: PromotionRule.name,
|
||||||
|
methodSuffix: "PromotionRules",
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user