feat: Add Discount Admin endpoint to JS client (#919)
This commit is contained in:
committed by
GitHub
parent
25fe224a10
commit
2ca1a8762d
@@ -36,7 +36,11 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
files: ["**/api/**/*.js", "**/api/**/*.ts"],
|
files: [
|
||||||
|
"**/api/**/*.js",
|
||||||
|
"**/api/**/*.ts",
|
||||||
|
"**/medusa-js/**/resources/**/*.ts",
|
||||||
|
],
|
||||||
rules: {
|
rules: {
|
||||||
"valid-jsdoc": ["off"],
|
"valid-jsdoc": ["off"],
|
||||||
},
|
},
|
||||||
|
|||||||
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 BaseResource from "../base"
|
||||||
import AdminAuthResource from "./auth"
|
import AdminAuthResource from "./auth"
|
||||||
import AdminCustomersResource from "./customers"
|
import AdminCustomersResource from "./customers"
|
||||||
|
import AdminDiscountsResource from "./discounts"
|
||||||
|
|
||||||
class Admin extends BaseResource {
|
class Admin extends BaseResource {
|
||||||
public auth = new AdminAuthResource(this.client)
|
public auth = new AdminAuthResource(this.client)
|
||||||
public customers = new AdminCustomersResource(this.client)
|
public customers = new AdminCustomersResource(this.client)
|
||||||
|
public discounts = new AdminDiscountsResource(this.client)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Admin
|
export default Admin
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ export default (container, config) => {
|
|||||||
|
|
||||||
export * from "./routes/admin/auth"
|
export * from "./routes/admin/auth"
|
||||||
export * from "./routes/admin/customers"
|
export * from "./routes/admin/customers"
|
||||||
|
export * from "./routes/admin/discounts"
|
||||||
export * from "./routes/admin/notifications"
|
export * from "./routes/admin/notifications"
|
||||||
export * from "./routes/admin/store"
|
export * from "./routes/admin/store"
|
||||||
export * from "./routes/admin/variants"
|
export * from "./routes/admin/variants"
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { IdMap } from "medusa-test-utils"
|
|||||||
import { request } from "../../../../../helpers/test-request"
|
import { request } from "../../../../../helpers/test-request"
|
||||||
import { DiscountServiceMock } from "../../../../../services/__mocks__/discount"
|
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", () => {
|
describe("successful addition", () => {
|
||||||
let subject
|
let subject
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ const defaultRelations = [
|
|||||||
"rule.valid_for",
|
"rule.valid_for",
|
||||||
]
|
]
|
||||||
|
|
||||||
describe("DELETE /admin/discounts/:discount_id/products/:variant_id", () => {
|
describe("DELETE /admin/discounts/:discount_id/products/:product_id", () => {
|
||||||
describe("successful addition", () => {
|
describe("successful addition", () => {
|
||||||
let subject
|
let subject
|
||||||
|
|
||||||
|
|||||||
@@ -2,14 +2,14 @@ import { defaultAdminDiscountsFields, defaultAdminDiscountsRelations } from "."
|
|||||||
import { Discount } from "../../../.."
|
import { Discount } from "../../../.."
|
||||||
import DiscountService from "../../../../services/discount"
|
import DiscountService from "../../../../services/discount"
|
||||||
/**
|
/**
|
||||||
* @oas [post] /discounts/{id}/products/{variant_id}
|
* @oas [post] /discounts/{id}/products/{product_id}
|
||||||
* operationId: "PostDiscountsDiscountProductsProduct"
|
* operationId: "PostDiscountsDiscountProductsProduct"
|
||||||
* summary: "Adds Product availability"
|
* summary: "Adds Product availability"
|
||||||
* description: "Adds a Product to the list of Products that a Discount can be used for."
|
* description: "Adds a Product to the list of Products that a Discount can be used for."
|
||||||
* x-authenticated: true
|
* x-authenticated: true
|
||||||
* parameters:
|
* parameters:
|
||||||
* - (path) id=* {string} The id of the Discount.
|
* - (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:
|
* tags:
|
||||||
* - Discount
|
* - Discount
|
||||||
* responses:
|
* responses:
|
||||||
@@ -23,10 +23,10 @@ import DiscountService from "../../../../services/discount"
|
|||||||
* $ref: "#/components/schemas/discount"
|
* $ref: "#/components/schemas/discount"
|
||||||
*/
|
*/
|
||||||
export default async (req, res) => {
|
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")
|
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, {
|
const discount: Discount = await discountService.retrieve(discount_id, {
|
||||||
select: defaultAdminDiscountsFields,
|
select: defaultAdminDiscountsFields,
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { Router } from "express"
|
import { Router } from "express"
|
||||||
import { Discount } from "../../../.."
|
|
||||||
import middlewares from "../../../middlewares"
|
|
||||||
import "reflect-metadata"
|
import "reflect-metadata"
|
||||||
|
import { Discount } from "../../../.."
|
||||||
import { DeleteResponse, PaginatedResponse } from "../../../../types/common"
|
import { DeleteResponse, PaginatedResponse } from "../../../../types/common"
|
||||||
|
import middlewares from "../../../middlewares"
|
||||||
|
|
||||||
const route = Router()
|
const route = Router()
|
||||||
|
|
||||||
@@ -41,11 +41,11 @@ export default (app) => {
|
|||||||
|
|
||||||
// Discount valid variants management
|
// Discount valid variants management
|
||||||
route.post(
|
route.post(
|
||||||
"/:discount_id/products/:variant_id",
|
"/:discount_id/products/:product_id",
|
||||||
middlewares.wrap(require("./add-valid-product").default)
|
middlewares.wrap(require("./add-valid-product").default)
|
||||||
)
|
)
|
||||||
route.delete(
|
route.delete(
|
||||||
"/:discount_id/products/:variant_id",
|
"/:discount_id/products/:product_id",
|
||||||
middlewares.wrap(require("./remove-valid-product").default)
|
middlewares.wrap(require("./remove-valid-product").default)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import DiscountService from "../../../../services/discount"
|
|
||||||
import { defaultAdminDiscountsFields, defaultAdminDiscountsRelations } from "."
|
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"
|
* operationId: "DeleteDiscountsDiscountProductsProduct"
|
||||||
* summary: "Remove Product availability"
|
* summary: "Remove Product availability"
|
||||||
* description: "Removes a Product from the list of Products that a Discount can be used for."
|
* 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"
|
* $ref: "#/components/schemas/discount"
|
||||||
*/
|
*/
|
||||||
export default async (req, res) => {
|
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")
|
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, {
|
const discount = await discountService.retrieve(discount_id, {
|
||||||
select: defaultAdminDiscountsFields,
|
select: defaultAdminDiscountsFields,
|
||||||
|
|||||||
Reference in New Issue
Block a user