From 2ca1a8762da5bc30a246e2e77521071ed91e6c12 Mon Sep 17 00:00:00 2001 From: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Tue, 14 Dec 2021 09:47:15 +0100 Subject: [PATCH] feat: Add Discount Admin endpoint to JS client (#919) --- .eslintrc.js | 6 +- .../src/resources/admin/discounts.ts | 138 ++++++++++++++++++ .../medusa-js/src/resources/admin/index.ts | 2 + packages/medusa/src/api/index.js | 1 + .../discounts/__tests__/add-valid-product.js | 2 +- .../__tests__/remove-valid-product.js | 2 +- .../admin/discounts/add-valid-product.ts | 8 +- .../src/api/routes/admin/discounts/index.ts | 8 +- .../admin/discounts/remove-valid-product.ts | 8 +- 9 files changed, 160 insertions(+), 15 deletions(-) create mode 100644 packages/medusa-js/src/resources/admin/discounts.ts diff --git a/.eslintrc.js b/.eslintrc.js index 038cc6b184..9d33c15205 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -36,7 +36,11 @@ module.exports = { }, }, { - files: ["**/api/**/*.js", "**/api/**/*.ts"], + files: [ + "**/api/**/*.js", + "**/api/**/*.ts", + "**/medusa-js/**/resources/**/*.ts", + ], rules: { "valid-jsdoc": ["off"], }, diff --git a/packages/medusa-js/src/resources/admin/discounts.ts b/packages/medusa-js/src/resources/admin/discounts.ts new file mode 100644 index 0000000000..2338d42011 --- /dev/null +++ b/packages/medusa-js/src/resources/admin/discounts.ts @@ -0,0 +1,138 @@ +import { + AdminDiscountsDeleteRes, + AdminDiscountsListRes, + AdminDiscountsRes, + AdminGetDiscountsParams, + AdminPostDiscountsDiscountDynamicCodesReq, + AdminPostDiscountsDiscountReq, + AdminPostDiscountsReq, +} from "@medusajs/medusa" +import { ResponsePromise } from "../../typings" +import BaseResource from "../base" + +class AdminDiscountsResource extends BaseResource { + /** + * @description Adds region to discount + */ + addRegion(id: string, regionId: string): ResponsePromise { + const path = `/admin/discounts/${id}/regions/${regionId}` + return this.client.request("POST", path, {}) + } + + /** + * @description Add valid product + */ + addValidProduct( + id: string, + productId: string + ): ResponsePromise { + const path = `/admin/discounts/${id}/products/${productId}` + return this.client.request("POST", path, {}) + } + + /** + * @description Creates discounts + */ + create(payload: AdminPostDiscountsReq): ResponsePromise { + const path = `/admin/discounts` + return this.client.request("POST", path, payload) + } + + /** + * @description Updates discount + */ + update( + id: string, + payload: AdminPostDiscountsDiscountReq + ): ResponsePromise { + const path = `/admin/discounts/${id}` + return this.client.request("POST", path, payload) + } + + /** + * @description Creates a dynamic discount code + */ + createDynamicCode( + id: string, + payload: AdminPostDiscountsDiscountDynamicCodesReq + ): ResponsePromise { + const path = `/admin/discounts/${id}/dynamic-codes` + return this.client.request("POST", path, payload) + } + + /** + * @description Deletes a discount + */ + delete(id: string): ResponsePromise { + const path = `/admin/discounts/${id}` + return this.client.request("DELETE", path) + } + + /** + * @description Deletes a dynamic discount + */ + deleteDynamicCode( + id: string, + code: string + ): ResponsePromise { + const path = `/admin/discounts/${id}/dynamic-codes/${code}` + return this.client.request("DELETE", path) + } + + /** + * @description Retrieves a discount + */ + retrieve(id: string): ResponsePromise { + const path = `/admin/discounts/${id}` + return this.client.request("GET", path) + } + + /** + * @description Retrieves a discount by code + */ + retrieveByCode(code: string): ResponsePromise { + const path = `/admin/discounts/code/${code}` + return this.client.request("GET", path) + } + + /** + * @description Lists discounts + */ + list(query: AdminGetDiscountsParams): ResponsePromise { + let path = `/admin/discounts` + + if (query) { + const queryString = Object.entries(query).map(([key, value]) => { + return `${key}=${value}` + }) + + path = `/admin/discounts?${queryString.join("&")}` + } + + return this.client.request("GET", path) + } + + /** + * @description Removes a region from a discount + */ + removeRegion( + id: string, + regionId: string + ): ResponsePromise { + const path = `/admin/discounts/${id}/regions/${regionId}` + return this.client.request("DELETE", path) + } + + /** + * @description Removes a valid product from a discount + */ + removeValidProduct( + id: string, + productId: string + ): ResponsePromise { + const path = `/admin/discounts/${id}/products/${productId}` + return this.client.request("DELETE", path) + } +} + +export default AdminDiscountsResource diff --git a/packages/medusa-js/src/resources/admin/index.ts b/packages/medusa-js/src/resources/admin/index.ts index 2ef6a2295f..7a990ad5cc 100644 --- a/packages/medusa-js/src/resources/admin/index.ts +++ b/packages/medusa-js/src/resources/admin/index.ts @@ -1,10 +1,12 @@ import BaseResource from "../base" import AdminAuthResource from "./auth" import AdminCustomersResource from "./customers" +import AdminDiscountsResource from "./discounts" class Admin extends BaseResource { public auth = new AdminAuthResource(this.client) public customers = new AdminCustomersResource(this.client) + public discounts = new AdminDiscountsResource(this.client) } export default Admin diff --git a/packages/medusa/src/api/index.js b/packages/medusa/src/api/index.js index 0fd04877de..43372a0fc8 100644 --- a/packages/medusa/src/api/index.js +++ b/packages/medusa/src/api/index.js @@ -17,6 +17,7 @@ export default (container, config) => { export * from "./routes/admin/auth" export * from "./routes/admin/customers" +export * from "./routes/admin/discounts" export * from "./routes/admin/notifications" export * from "./routes/admin/store" export * from "./routes/admin/variants" diff --git a/packages/medusa/src/api/routes/admin/discounts/__tests__/add-valid-product.js b/packages/medusa/src/api/routes/admin/discounts/__tests__/add-valid-product.js index 0f37872e4c..6377ae7020 100644 --- a/packages/medusa/src/api/routes/admin/discounts/__tests__/add-valid-product.js +++ b/packages/medusa/src/api/routes/admin/discounts/__tests__/add-valid-product.js @@ -2,7 +2,7 @@ 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("POST /admin/discounts/:discount_id/products/:product_id", () => { describe("successful addition", () => { let subject diff --git a/packages/medusa/src/api/routes/admin/discounts/__tests__/remove-valid-product.js b/packages/medusa/src/api/routes/admin/discounts/__tests__/remove-valid-product.js index 4762083e9a..a64d6e14d7 100644 --- a/packages/medusa/src/api/routes/admin/discounts/__tests__/remove-valid-product.js +++ b/packages/medusa/src/api/routes/admin/discounts/__tests__/remove-valid-product.js @@ -27,7 +27,7 @@ const defaultRelations = [ "rule.valid_for", ] -describe("DELETE /admin/discounts/:discount_id/products/:variant_id", () => { +describe("DELETE /admin/discounts/:discount_id/products/:product_id", () => { describe("successful addition", () => { let subject diff --git a/packages/medusa/src/api/routes/admin/discounts/add-valid-product.ts b/packages/medusa/src/api/routes/admin/discounts/add-valid-product.ts index d3c5d8f930..4a5afd0717 100644 --- a/packages/medusa/src/api/routes/admin/discounts/add-valid-product.ts +++ b/packages/medusa/src/api/routes/admin/discounts/add-valid-product.ts @@ -2,14 +2,14 @@ import { defaultAdminDiscountsFields, defaultAdminDiscountsRelations } from "." import { Discount } from "../../../.." import DiscountService from "../../../../services/discount" /** - * @oas [post] /discounts/{id}/products/{variant_id} + * @oas [post] /discounts/{id}/products/{product_id} * operationId: "PostDiscountsDiscountProductsProduct" * summary: "Adds Product availability" * description: "Adds a Product to the list of Products that a Discount can be used for." * x-authenticated: true * parameters: * - (path) id=* {string} The id of the Discount. - * - (path) variant_id=* {string} The id of the Product. + * - (path) product_id=* {string} The id of the Product. * tags: * - Discount * responses: @@ -23,10 +23,10 @@ import DiscountService from "../../../../services/discount" * $ref: "#/components/schemas/discount" */ export default async (req, res) => { - const { discount_id, variant_id } = req.params + const { discount_id, product_id } = req.params const discountService: DiscountService = req.scope.resolve("discountService") - await discountService.addValidProduct(discount_id, variant_id) + await discountService.addValidProduct(discount_id, product_id) const discount: Discount = await discountService.retrieve(discount_id, { select: defaultAdminDiscountsFields, diff --git a/packages/medusa/src/api/routes/admin/discounts/index.ts b/packages/medusa/src/api/routes/admin/discounts/index.ts index d79bd753f1..20c6c23790 100644 --- a/packages/medusa/src/api/routes/admin/discounts/index.ts +++ b/packages/medusa/src/api/routes/admin/discounts/index.ts @@ -1,8 +1,8 @@ import { Router } from "express" -import { Discount } from "../../../.." -import middlewares from "../../../middlewares" import "reflect-metadata" +import { Discount } from "../../../.." import { DeleteResponse, PaginatedResponse } from "../../../../types/common" +import middlewares from "../../../middlewares" const route = Router() @@ -41,11 +41,11 @@ export default (app) => { // Discount valid variants management route.post( - "/:discount_id/products/:variant_id", + "/:discount_id/products/:product_id", middlewares.wrap(require("./add-valid-product").default) ) route.delete( - "/:discount_id/products/:variant_id", + "/:discount_id/products/:product_id", middlewares.wrap(require("./remove-valid-product").default) ) diff --git a/packages/medusa/src/api/routes/admin/discounts/remove-valid-product.ts b/packages/medusa/src/api/routes/admin/discounts/remove-valid-product.ts index 8cc583fddc..83ab7d582b 100644 --- a/packages/medusa/src/api/routes/admin/discounts/remove-valid-product.ts +++ b/packages/medusa/src/api/routes/admin/discounts/remove-valid-product.ts @@ -1,7 +1,7 @@ -import DiscountService from "../../../../services/discount" import { defaultAdminDiscountsFields, defaultAdminDiscountsRelations } from "." +import DiscountService from "../../../../services/discount" /** - * @oas [post] /discounts/{id}/products/{product_id} + * @oas [delete] /discounts/{id}/products/{product_id} * operationId: "DeleteDiscountsDiscountProductsProduct" * summary: "Remove Product availability" * description: "Removes a Product from the list of Products that a Discount can be used for." @@ -22,10 +22,10 @@ import { defaultAdminDiscountsFields, defaultAdminDiscountsRelations } from "." * $ref: "#/components/schemas/discount" */ export default async (req, res) => { - const { discount_id, variant_id } = req.params + const { discount_id, product_id } = req.params const discountService: DiscountService = req.scope.resolve("discountService") - await discountService.removeValidProduct(discount_id, variant_id) + await discountService.removeValidProduct(discount_id, product_id) const discount = await discountService.retrieve(discount_id, { select: defaultAdminDiscountsFields,