feat: Add Discount Admin endpoint to JS client (#919)
This commit is contained in:
committed by
GitHub
parent
25fe224a10
commit
2ca1a8762d
138
packages/medusa-js/src/resources/admin/discounts.ts
Normal file
138
packages/medusa-js/src/resources/admin/discounts.ts
Normal file
@@ -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<AdminDiscountsRes> {
|
||||
const path = `/admin/discounts/${id}/regions/${regionId}`
|
||||
return this.client.request("POST", path, {})
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Add valid product
|
||||
*/
|
||||
addValidProduct(
|
||||
id: string,
|
||||
productId: string
|
||||
): ResponsePromise<AdminDiscountsRes> {
|
||||
const path = `/admin/discounts/${id}/products/${productId}`
|
||||
return this.client.request("POST", path, {})
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Creates discounts
|
||||
*/
|
||||
create(payload: AdminPostDiscountsReq): ResponsePromise<AdminDiscountsRes> {
|
||||
const path = `/admin/discounts`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Updates discount
|
||||
*/
|
||||
update(
|
||||
id: string,
|
||||
payload: AdminPostDiscountsDiscountReq
|
||||
): ResponsePromise<AdminDiscountsRes> {
|
||||
const path = `/admin/discounts/${id}`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Creates a dynamic discount code
|
||||
*/
|
||||
createDynamicCode(
|
||||
id: string,
|
||||
payload: AdminPostDiscountsDiscountDynamicCodesReq
|
||||
): ResponsePromise<AdminDiscountsRes> {
|
||||
const path = `/admin/discounts/${id}/dynamic-codes`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Deletes a discount
|
||||
*/
|
||||
delete(id: string): ResponsePromise<AdminDiscountsDeleteRes> {
|
||||
const path = `/admin/discounts/${id}`
|
||||
return this.client.request("DELETE", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Deletes a dynamic discount
|
||||
*/
|
||||
deleteDynamicCode(
|
||||
id: string,
|
||||
code: string
|
||||
): ResponsePromise<AdminDiscountsRes> {
|
||||
const path = `/admin/discounts/${id}/dynamic-codes/${code}`
|
||||
return this.client.request("DELETE", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Retrieves a discount
|
||||
*/
|
||||
retrieve(id: string): ResponsePromise<AdminDiscountsRes> {
|
||||
const path = `/admin/discounts/${id}`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Retrieves a discount by code
|
||||
*/
|
||||
retrieveByCode(code: string): ResponsePromise<AdminDiscountsRes> {
|
||||
const path = `/admin/discounts/code/${code}`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Lists discounts
|
||||
*/
|
||||
list(query: AdminGetDiscountsParams): ResponsePromise<AdminDiscountsListRes> {
|
||||
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<AdminDiscountsRes> {
|
||||
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<AdminDiscountsRes> {
|
||||
const path = `/admin/discounts/${id}/products/${productId}`
|
||||
return this.client.request("DELETE", path)
|
||||
}
|
||||
}
|
||||
|
||||
export default AdminDiscountsResource
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
)
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user