From 20735de62d066fb41971dce28343a6d3559ecbce Mon Sep 17 00:00:00 2001 From: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Sun, 12 Apr 2020 17:18:07 +0200 Subject: [PATCH] Adds admin discount routes / endpoints (#34) --- .../admin/discounts/__tests__/add-region.js | 42 +++++++++++ .../discounts/__tests__/add-valid-variant.js | 42 +++++++++++ .../discounts/__tests__/create-discount.js | 74 +++++++++++++++++++ .../discounts/__tests__/delete-discount.js | 42 +++++++++++ .../admin/discounts/__tests__/get-discount.js | 34 +++++++++ .../discounts/__tests__/list-discounts.js | 28 +++++++ .../discounts/__tests__/remove-region.js | 42 +++++++++++ .../__tests__/remove-valid-variant.js | 42 +++++++++++ .../discounts/__tests__/update-discount.js | 50 +++++++++++++ .../api/routes/admin/discounts/add-region.js | 15 ++++ .../admin/discounts/add-valid-variant.js | 15 ++++ .../routes/admin/discounts/create-discount.js | 39 ++++++++++ .../routes/admin/discounts/delete-discount.js | 15 ++++ .../routes/admin/discounts/get-discount.js | 11 +++ .../src/api/routes/admin/discounts/index.js | 46 ++++++++++++ .../routes/admin/discounts/list-discounts.js | 10 +++ .../routes/admin/discounts/remove-region.js | 14 ++++ .../admin/discounts/remove-valid-variant.js | 14 ++++ .../routes/admin/discounts/update-discount.js | 43 +++++++++++ packages/medusa/src/api/routes/admin/index.js | 2 + .../medusa/src/services/__mocks__/discount.js | 20 +++++ 21 files changed, 640 insertions(+) create mode 100644 packages/medusa/src/api/routes/admin/discounts/__tests__/add-region.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/__tests__/add-valid-variant.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/__tests__/create-discount.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/__tests__/delete-discount.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/__tests__/get-discount.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/__tests__/list-discounts.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/__tests__/remove-region.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/__tests__/remove-valid-variant.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/__tests__/update-discount.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/add-region.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/add-valid-variant.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/create-discount.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/delete-discount.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/get-discount.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/index.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/list-discounts.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/remove-region.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/remove-valid-variant.js create mode 100644 packages/medusa/src/api/routes/admin/discounts/update-discount.js diff --git a/packages/medusa/src/api/routes/admin/discounts/__tests__/add-region.js b/packages/medusa/src/api/routes/admin/discounts/__tests__/add-region.js new file mode 100644 index 0000000000..2a5c876568 --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/__tests__/add-region.js @@ -0,0 +1,42 @@ +import { IdMap } from "medusa-test-utils" +import { request } from "../../../../../helpers/test-request" +import { DiscountServiceMock } from "../../../../../services/__mocks__/discount" + +describe("POST /admin/discounts/:discount_id/regions/:region_id", () => { + describe("successful addition", () => { + let subject + + beforeAll(async () => { + subject = await request( + "POST", + `/admin/discounts/${IdMap.getId("total10")}/regions/${IdMap.getId( + "region-france" + )}`, + { + adminSession: { + jwt: { + userId: IdMap.getId("admin_user"), + }, + }, + } + ) + }) + + it("returns 200", () => { + expect(subject.status).toEqual(200) + }) + + it("calls service retrieve", () => { + expect(DiscountServiceMock.retrieve).toHaveBeenCalledTimes(1) + expect(DiscountServiceMock.retrieve).toHaveBeenCalledWith( + IdMap.getId("total10") + ) + + expect(DiscountServiceMock.addRegion).toHaveBeenCalledTimes(1) + expect(DiscountServiceMock.addRegion).toHaveBeenCalledWith( + IdMap.getId("total10"), + IdMap.getId("region-france") + ) + }) + }) +}) diff --git a/packages/medusa/src/api/routes/admin/discounts/__tests__/add-valid-variant.js b/packages/medusa/src/api/routes/admin/discounts/__tests__/add-valid-variant.js new file mode 100644 index 0000000000..d4c33d647f --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/__tests__/add-valid-variant.js @@ -0,0 +1,42 @@ +import { IdMap } from "medusa-test-utils" +import { request } from "../../../../../helpers/test-request" +import { DiscountServiceMock } from "../../../../../services/__mocks__/discount" + +describe("POST /admin/discounts/:discount_id/variants/:variant_id", () => { + describe("successful addition", () => { + let subject + + beforeAll(async () => { + subject = await request( + "POST", + `/admin/discounts/${IdMap.getId("total10")}/variants/${IdMap.getId( + "testVariant" + )}`, + { + adminSession: { + jwt: { + userId: IdMap.getId("admin_user"), + }, + }, + } + ) + }) + + it("returns 200", () => { + expect(subject.status).toEqual(200) + }) + + it("calls service retrieve", () => { + expect(DiscountServiceMock.retrieve).toHaveBeenCalledTimes(1) + expect(DiscountServiceMock.retrieve).toHaveBeenCalledWith( + IdMap.getId("total10") + ) + + expect(DiscountServiceMock.addValidVariant).toHaveBeenCalledTimes(1) + expect(DiscountServiceMock.addValidVariant).toHaveBeenCalledWith( + IdMap.getId("total10"), + IdMap.getId("testVariant") + ) + }) + }) +}) diff --git a/packages/medusa/src/api/routes/admin/discounts/__tests__/create-discount.js b/packages/medusa/src/api/routes/admin/discounts/__tests__/create-discount.js new file mode 100644 index 0000000000..5399cd9724 --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/__tests__/create-discount.js @@ -0,0 +1,74 @@ +import { IdMap } from "medusa-test-utils" +import { request } from "../../../../../helpers/test-request" +import { DiscountServiceMock } from "../../../../../services/__mocks__/discount" + +describe("POST /admin/discounts", () => { + describe("successful creation", () => { + let subject + + beforeAll(async () => { + subject = await request("POST", "/admin/discounts", { + payload: { + code: "TEST", + discount_rule: { + type: "fixed", + value: 10, + allocation: "total", + }, + }, + adminSession: { + jwt: { + userId: IdMap.getId("admin_user"), + }, + }, + }) + }) + + it("returns 200", () => { + expect(subject.status).toEqual(200) + }) + + it("calls service create", () => { + expect(DiscountServiceMock.create).toHaveBeenCalledTimes(1) + expect(DiscountServiceMock.create).toHaveBeenCalledWith({ + code: "TEST", + discount_rule: { + type: "fixed", + value: 10, + allocation: "total", + }, + }) + }) + }) + + describe("fails on invalid data", () => { + let subject + + beforeAll(async () => { + subject = await request("POST", "/admin/discounts", { + payload: { + code: "10%OFF", + discount_rule: { + value: 10, + allocation: "total", + }, + }, + adminSession: { + jwt: { + userId: IdMap.getId("admin_user"), + }, + }, + }) + }) + + it("returns 400", () => { + expect(subject.status).toEqual(400) + }) + + it("returns error", () => { + expect(subject.body.message[0].message).toEqual( + `"discount_rule.type" is required` + ) + }) + }) +}) diff --git a/packages/medusa/src/api/routes/admin/discounts/__tests__/delete-discount.js b/packages/medusa/src/api/routes/admin/discounts/__tests__/delete-discount.js new file mode 100644 index 0000000000..f2bacfafe1 --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/__tests__/delete-discount.js @@ -0,0 +1,42 @@ +import { IdMap } from "medusa-test-utils" +import { request } from "../../../../../helpers/test-request" +import { DiscountServiceMock } from "../../../../../services/__mocks__/discount" + +describe("DELETE /admin/discounts/discount_id", () => { + describe("successful deletion", () => { + let subject + + beforeAll(async () => { + subject = await request( + "DELETE", + `/admin/discounts/${IdMap.getId("total10")}`, + { + adminSession: { + jwt: { + userId: IdMap.getId("admin_user"), + }, + }, + } + ) + }) + + it("returns 200", () => { + expect(subject.status).toEqual(200) + }) + + it("calls service delete", () => { + expect(DiscountServiceMock.delete).toHaveBeenCalledTimes(1) + expect(DiscountServiceMock.delete).toHaveBeenCalledWith( + IdMap.getId("total10") + ) + }) + + it("returns correct delete object", () => { + expect(subject.body).toEqual({ + id: IdMap.getId("total10"), + object: "discount", + deleted: true, + }) + }) + }) +}) diff --git a/packages/medusa/src/api/routes/admin/discounts/__tests__/get-discount.js b/packages/medusa/src/api/routes/admin/discounts/__tests__/get-discount.js new file mode 100644 index 0000000000..304799289b --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/__tests__/get-discount.js @@ -0,0 +1,34 @@ +import { IdMap } from "medusa-test-utils" +import { request } from "../../../../../helpers/test-request" +import { DiscountServiceMock } from "../../../../../services/__mocks__/discount" + +describe("GET /admin/discounts/:discount_id", () => { + describe("successful retrieval", () => { + let subject + + beforeAll(async () => { + subject = await request( + "GET", + `/admin/discounts/${IdMap.getId("total10")}`, + { + adminSession: { + jwt: { + userId: IdMap.getId("admin_user"), + }, + }, + } + ) + }) + + it("returns 200", () => { + expect(subject.status).toEqual(200) + }) + + it("calls service retrieve", () => { + expect(DiscountServiceMock.retrieve).toHaveBeenCalledTimes(1) + expect(DiscountServiceMock.retrieve).toHaveBeenCalledWith( + IdMap.getId("total10") + ) + }) + }) +}) diff --git a/packages/medusa/src/api/routes/admin/discounts/__tests__/list-discounts.js b/packages/medusa/src/api/routes/admin/discounts/__tests__/list-discounts.js new file mode 100644 index 0000000000..5c4e0ccf15 --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/__tests__/list-discounts.js @@ -0,0 +1,28 @@ +import { IdMap } from "medusa-test-utils" +import { request } from "../../../../../helpers/test-request" +import { DiscountServiceMock } from "../../../../../services/__mocks__/discount" + +describe("GET /admin/discounts", () => { + describe("successful retrieval", () => { + let subject + + beforeAll(async () => { + subject = await request("GET", `/admin/discounts`, { + adminSession: { + jwt: { + userId: IdMap.getId("admin_user"), + }, + }, + }) + }) + + it("returns 200", () => { + expect(subject.status).toEqual(200) + }) + + it("calls service retrieve", () => { + expect(DiscountServiceMock.list).toHaveBeenCalledTimes(1) + expect(DiscountServiceMock.list).toHaveBeenCalledWith() + }) + }) +}) diff --git a/packages/medusa/src/api/routes/admin/discounts/__tests__/remove-region.js b/packages/medusa/src/api/routes/admin/discounts/__tests__/remove-region.js new file mode 100644 index 0000000000..66e862fc51 --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/__tests__/remove-region.js @@ -0,0 +1,42 @@ +import { IdMap } from "medusa-test-utils" +import { request } from "../../../../../helpers/test-request" +import { DiscountServiceMock } from "../../../../../services/__mocks__/discount" + +describe("DELETE /admin/discounts/:discount_id/regions/region_id", () => { + describe("successful removal", () => { + let subject + + beforeAll(async () => { + subject = await request( + "DELETE", + `/admin/discounts/${IdMap.getId("total10")}/regions/${IdMap.getId( + "region-france" + )}`, + { + adminSession: { + jwt: { + userId: IdMap.getId("admin_user"), + }, + }, + } + ) + }) + + it("returns 200", () => { + expect(subject.status).toEqual(200) + }) + + it("calls service retrieve", () => { + expect(DiscountServiceMock.retrieve).toHaveBeenCalledTimes(1) + expect(DiscountServiceMock.retrieve).toHaveBeenCalledWith( + IdMap.getId("total10") + ) + + expect(DiscountServiceMock.removeRegion).toHaveBeenCalledTimes(1) + expect(DiscountServiceMock.removeRegion).toHaveBeenCalledWith( + IdMap.getId("total10"), + IdMap.getId("region-france") + ) + }) + }) +}) diff --git a/packages/medusa/src/api/routes/admin/discounts/__tests__/remove-valid-variant.js b/packages/medusa/src/api/routes/admin/discounts/__tests__/remove-valid-variant.js new file mode 100644 index 0000000000..0882c76758 --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/__tests__/remove-valid-variant.js @@ -0,0 +1,42 @@ +import { IdMap } from "medusa-test-utils" +import { request } from "../../../../../helpers/test-request" +import { DiscountServiceMock } from "../../../../../services/__mocks__/discount" + +describe("DELETE /admin/discounts/:discount_id/variants/:variant_id", () => { + describe("successful addition", () => { + let subject + + beforeAll(async () => { + subject = await request( + "DELETE", + `/admin/discounts/${IdMap.getId("total10")}/variants/${IdMap.getId( + "testVariant" + )}`, + { + adminSession: { + jwt: { + userId: IdMap.getId("admin_user"), + }, + }, + } + ) + }) + + it("returns 200", () => { + expect(subject.status).toEqual(200) + }) + + it("calls service retrieve", () => { + expect(DiscountServiceMock.retrieve).toHaveBeenCalledTimes(1) + expect(DiscountServiceMock.retrieve).toHaveBeenCalledWith( + IdMap.getId("total10") + ) + + expect(DiscountServiceMock.removeValidVariant).toHaveBeenCalledTimes(1) + expect(DiscountServiceMock.removeValidVariant).toHaveBeenCalledWith( + IdMap.getId("total10"), + IdMap.getId("testVariant") + ) + }) + }) +}) diff --git a/packages/medusa/src/api/routes/admin/discounts/__tests__/update-discount.js b/packages/medusa/src/api/routes/admin/discounts/__tests__/update-discount.js new file mode 100644 index 0000000000..b45617a22e --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/__tests__/update-discount.js @@ -0,0 +1,50 @@ +import { IdMap } from "medusa-test-utils" +import { request } from "../../../../../helpers/test-request" +import { DiscountServiceMock } from "../../../../../services/__mocks__/discount" + +describe("POST /admin/discounts", () => { + describe("successful update", () => { + let subject + + beforeAll(async () => { + subject = await request( + "POST", + `/admin/discounts/${IdMap.getId("total10")}`, + { + payload: { + code: "10TOTALOFF", + discount_rule: { + type: "fixed", + value: 10, + allocation: "total", + }, + }, + adminSession: { + jwt: { + userId: IdMap.getId("admin_user"), + }, + }, + } + ) + }) + + it("returns 200", () => { + expect(subject.status).toEqual(200) + }) + + it("calls service method", () => { + expect(DiscountServiceMock.update).toHaveBeenCalledTimes(1) + expect(DiscountServiceMock.update).toHaveBeenCalledWith( + IdMap.getId("total10"), + { + code: "10TOTALOFF", + discount_rule: { + type: "fixed", + value: 10, + allocation: "total", + }, + } + ) + }) + }) +}) diff --git a/packages/medusa/src/api/routes/admin/discounts/add-region.js b/packages/medusa/src/api/routes/admin/discounts/add-region.js new file mode 100644 index 0000000000..c32001800b --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/add-region.js @@ -0,0 +1,15 @@ +import { MedusaError, Validator } from "medusa-core-utils" + +export default async (req, res) => { + const { discount_id, region_id } = req.params + try { + const discountService = req.scope.resolve("discountService") + + await discountService.addRegion(discount_id, region_id) + + const data = discountService.retrieve(discount_id) + res.status(200).json(data) + } catch (err) { + throw err + } +} diff --git a/packages/medusa/src/api/routes/admin/discounts/add-valid-variant.js b/packages/medusa/src/api/routes/admin/discounts/add-valid-variant.js new file mode 100644 index 0000000000..c924eb5b14 --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/add-valid-variant.js @@ -0,0 +1,15 @@ +import { MedusaError, Validator } from "medusa-core-utils" + +export default async (req, res) => { + const { discount_id, variant_id } = req.params + try { + const discountService = req.scope.resolve("discountService") + + await discountService.addValidVariant(discount_id, variant_id) + + const data = discountService.retrieve(discount_id) + res.status(200).json(data) + } catch (err) { + throw err + } +} diff --git a/packages/medusa/src/api/routes/admin/discounts/create-discount.js b/packages/medusa/src/api/routes/admin/discounts/create-discount.js new file mode 100644 index 0000000000..e6cc7044b7 --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/create-discount.js @@ -0,0 +1,39 @@ +import { MedusaError, Validator } from "medusa-core-utils" + +export default async (req, res) => { + const schema = Validator.object().keys({ + code: Validator.string().required(), + discount_rule: Validator.object() + .keys({ + description: Validator.string().optional(), + type: Validator.string().required(), + value: Validator.number().required(), + allocation: Validator.string().required(), + valid_for: Validator.array().items(Validator.string()), + usage_limit: Validator.number().optional(), + }) + .required(), + usage_count: Validator.number().optional(), + disabled: Validator.boolean().optional(), + starts_at: Validator.date().optional(), + ends_at: Validator.date().optional(), + regions: Validator.array() + .items(Validator.string()) + .optional(), + metadata: Validator.object().optional(), + }) + + const { value, error } = schema.validate(req.body) + if (error) { + throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) + } + + try { + const discountService = req.scope.resolve("discountService") + const data = await discountService.create(value) + + res.status(200).json(data) + } catch (err) { + throw err + } +} diff --git a/packages/medusa/src/api/routes/admin/discounts/delete-discount.js b/packages/medusa/src/api/routes/admin/discounts/delete-discount.js new file mode 100644 index 0000000000..63d4d487a4 --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/delete-discount.js @@ -0,0 +1,15 @@ +export default async (req, res) => { + const { discount_id } = req.params + try { + const discountService = req.scope.resolve("discountService") + await discountService.delete(discount_id) + + res.json({ + id: discount_id, + object: "discount", + deleted: true, + }) + } catch (err) { + throw err + } +} diff --git a/packages/medusa/src/api/routes/admin/discounts/get-discount.js b/packages/medusa/src/api/routes/admin/discounts/get-discount.js new file mode 100644 index 0000000000..c589e94db3 --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/get-discount.js @@ -0,0 +1,11 @@ +export default async (req, res) => { + const { discount_id } = req.params + try { + const discountService = req.scope.resolve("discountService") + const data = await discountService.retrieve(discount_id) + + res.status(200).json(data) + } catch (err) { + throw err + } +} diff --git a/packages/medusa/src/api/routes/admin/discounts/index.js b/packages/medusa/src/api/routes/admin/discounts/index.js new file mode 100644 index 0000000000..62ef5b8cb0 --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/index.js @@ -0,0 +1,46 @@ +import { Router } from "express" +import middlewares from "../../../middlewares" + +const route = Router() + +export default app => { + app.use("/discounts", route) + + route.get("/", middlewares.wrap(require("./list-discounts").default)) + route.post("/", middlewares.wrap(require("./create-discount").default)) + + route.get( + "/:discount_id", + middlewares.wrap(require("./get-discount").default) + ) + route.post( + "/:discount_id", + middlewares.wrap(require("./update-discount").default) + ) + route.delete( + "/:discount_id", + middlewares.wrap(require("./delete-discount").default) + ) + + // Discount valid variants management + route.post( + "/:discount_id/variants/:variant_id", + middlewares.wrap(require("./add-valid-variant").default) + ) + route.delete( + "/:discount_id/variants/:variant_id", + middlewares.wrap(require("./remove-valid-variant").default) + ) + + // Discount region management + route.post( + "/:discount_id/regions/:region_id", + middlewares.wrap(require("./add-region").default) + ) + route.delete( + "/:discount_id/regions/:region_id", + middlewares.wrap(require("./remove-region").default) + ) + + return app +} diff --git a/packages/medusa/src/api/routes/admin/discounts/list-discounts.js b/packages/medusa/src/api/routes/admin/discounts/list-discounts.js new file mode 100644 index 0000000000..a8801c3a61 --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/list-discounts.js @@ -0,0 +1,10 @@ +export default async (req, res) => { + try { + const discountService = req.scope.resolve("discountService") + const data = await discountService.list() + + res.status(200).json(data) + } catch (err) { + throw err + } +} diff --git a/packages/medusa/src/api/routes/admin/discounts/remove-region.js b/packages/medusa/src/api/routes/admin/discounts/remove-region.js new file mode 100644 index 0000000000..2c27fafaac --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/remove-region.js @@ -0,0 +1,14 @@ +export default async (req, res) => { + const { discount_id, region_id } = req.params + + try { + const discountService = req.scope.resolve("discountService") + + await discountService.removeRegion(discount_id, region_id) + + const data = discountService.retrieve(discount_id) + res.status(200).json(data) + } catch (err) { + throw err + } +} diff --git a/packages/medusa/src/api/routes/admin/discounts/remove-valid-variant.js b/packages/medusa/src/api/routes/admin/discounts/remove-valid-variant.js new file mode 100644 index 0000000000..4d67d26795 --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/remove-valid-variant.js @@ -0,0 +1,14 @@ +export default async (req, res) => { + const { discount_id, variant_id } = req.params + + try { + const discountService = req.scope.resolve("discountService") + + await discountService.removeValidVariant(discount_id, variant_id) + + const data = discountService.retrieve(discount_id) + res.status(200).json(data) + } catch (err) { + throw err + } +} diff --git a/packages/medusa/src/api/routes/admin/discounts/update-discount.js b/packages/medusa/src/api/routes/admin/discounts/update-discount.js new file mode 100644 index 0000000000..3a2075f448 --- /dev/null +++ b/packages/medusa/src/api/routes/admin/discounts/update-discount.js @@ -0,0 +1,43 @@ +import { MedusaError, Validator } from "medusa-core-utils" + +export default async (req, res) => { + const { discount_id } = req.params + const schema = Validator.object().keys({ + code: Validator.string().required(), + discount_rule: Validator.object() + .keys({ + description: Validator.string().optional(), + type: Validator.string().required(), + value: Validator.number().required(), + allocation: Validator.string().required(), + valid_for: Validator.array().items(Validator.string()), + usage_limit: Validator.number().optional(), + }) + .required(), + usage_count: Validator.number().optional(), + disabled: Validator.boolean().optional(), + starts_at: Validator.date().optional(), + ends_at: Validator.date().optional(), + regions: Validator.array() + .items(Validator.string()) + .optional(), + metadata: Validator.object().optional(), + }) + + const { value, error } = schema.validate(req.body) + if (error) { + throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) + } + + try { + const discountService = req.scope.resolve("discountService") + + await discountService.update(discount_id, value) + + const data = await discountService.retrieve(discount_id) + + res.status(200).json(data) + } catch (err) { + throw err + } +} diff --git a/packages/medusa/src/api/routes/admin/index.js b/packages/medusa/src/api/routes/admin/index.js index cb31d93ee7..479bd74a7a 100644 --- a/packages/medusa/src/api/routes/admin/index.js +++ b/packages/medusa/src/api/routes/admin/index.js @@ -6,6 +6,7 @@ import productVariantRoutes from "./product-variants" import regionRoutes from "./regions" import shippingOptionRoutes from "./shipping-options" import shippingProfileRoutes from "./shipping-profiles" +import discountRoutes from "./discounts" const route = Router() @@ -22,6 +23,7 @@ export default app => { regionRoutes(route) shippingOptionRoutes(route) shippingProfileRoutes(route) + discountRoutes(route) // productVariantRoutes(route) return app diff --git a/packages/medusa/src/services/__mocks__/discount.js b/packages/medusa/src/services/__mocks__/discount.js index 9b22d2a185..e287a99393 100644 --- a/packages/medusa/src/services/__mocks__/discount.js +++ b/packages/medusa/src/services/__mocks__/discount.js @@ -2,6 +2,9 @@ import { IdMap } from "medusa-test-utils" import { discounts } from "../../models/__mocks__/discount" export const DiscountServiceMock = { + create: jest.fn().mockImplementation(data => { + return Promise.resolve(data) + }), retrieveByCode: jest.fn().mockImplementation(data => { if (data === "10%OFF") { return Promise.resolve(discounts.total10Percent) @@ -26,6 +29,23 @@ export const DiscountServiceMock = { } return Promise.resolve(undefined) }), + update: jest.fn().mockImplementation(data => { + return Promise.resolve() + }), + delete: jest.fn().mockImplementation(data => { + return Promise.resolve({ + _id: IdMap.getId("total10"), + object: "discount", + deleted: true, + }) + }), + list: jest.fn().mockImplementation(data => { + return Promise.resolve([]) + }), + addRegion: jest.fn().mockReturnValue(Promise.resolve()), + removeRegion: jest.fn().mockReturnValue(Promise.resolve()), + addValidVariant: jest.fn().mockReturnValue(Promise.resolve()), + removeValidVariant: jest.fn().mockReturnValue(Promise.resolve()), } const mock = jest.fn().mockImplementation(() => {