feat(medusa): Support deleting prices from a price list by product or variant (#1555)

This commit is contained in:
Adrien de Peretti
2022-05-30 09:41:57 +02:00
committed by GitHub
parent ad9cfedf04
commit fa031fd28b
16 changed files with 602 additions and 78 deletions
@@ -0,0 +1,48 @@
import PriceListService from "../../../../services/price-list"
/**
* @oas [delete] /price-lists/{id}/products/{product_id}/prices
* operationId: "DeletePriceListsPriceListProductsProductPrices"
* summary: "Delete all the prices related to a specific product in a price list"
* description: "Delete all the prices related to a specific product in a price list"
* x-authenticated: true
* parameters:
* - (path) id=* {string} The id of the Price List that the Money Amounts that will be deleted belongs to.
* - (path) product_id=* {string} The id of the product from which the money amount will be deleted.
* tags:
* - Price List
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* ids:
* type: number
* description: The price ids that have been deleted.
* count:
* type: number
* description: The number of prices that have been deleted.
* object:
* type: string
* description: The type of the object that was deleted.
* deleted:
* type: boolean
*/
export default async (req, res) => {
const { id, product_id } = req.params
const priceListService: PriceListService =
req.scope.resolve("priceListService")
const [deletedPriceIds] = await priceListService.deleteProductPrices(id, [
product_id,
])
return res.json({
ids: deletedPriceIds,
object: "money-amount",
deleted: true,
})
}
@@ -0,0 +1,48 @@
import PriceListService from "../../../../services/price-list"
/**
* @oas [delete] /price-lists/{id}/variants/{variant_id}/prices
* operationId: "DeletePriceListsPriceListVariantsVariantPrices"
* summary: "Delete all the prices related to a specific variant in a price list"
* description: "Delete all the prices related to a specific variant in a price list"
* x-authenticated: true
* parameters:
* - (path) id=* {string} The id of the Price List that the Money Amounts that will be deleted belongs to.
* - (path) variant_id=* {string} The id of the variant from which the money amount will be deleted.
* tags:
* - Price List
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* ids:
* type: number
* description: The price ids that have been deleted.
* count:
* type: number
* description: The number of prices that have been deleted.
* object:
* type: string
* description: The type of the object that was deleted.
* deleted:
* type: boolean
*/
export default async (req, res) => {
const { id, variant_id } = req.params
const priceListService: PriceListService =
req.scope.resolve("priceListService")
const [deletedPriceIds] = await priceListService.deleteVariantPrices(id, [
variant_id,
])
return res.json({
ids: deletedPriceIds,
object: "money-amount",
deleted: true,
})
}
@@ -22,6 +22,15 @@ export default (app) => {
middlewares.wrap(require("./list-price-list-products").default)
)
route.delete(
"/:id/products/:product_id/prices",
middlewares.wrap(require("./delete-product-prices").default)
)
route.delete(
"/:id/variants/:variant_id/prices",
middlewares.wrap(require("./delete-variant-prices").default)
)
route.post("/", middlewares.wrap(require("./create-price-list").default))
route.post("/:id", middlewares.wrap(require("./update-price-list").default))
@@ -68,6 +77,9 @@ export type AdminPriceListDeleteBatchRes = {
object: string
}
export type AdminPriceListDeleteProductPricesRes = AdminPriceListDeleteBatchRes
export type AdminPriceListDeleteVariantPricesRes = AdminPriceListDeleteBatchRes
export type AdminPriceListDeleteRes = DeleteResponse
export type AdminPriceListsListRes = PaginatedResponse & {
@@ -8,7 +8,7 @@ import {
IsString,
ValidateNested,
} from "class-validator"
import { Product } from "../../../../models/product"
import { Product } from "../../../../models"
import { DateComparisonOperator } from "../../../../types/common"
import { validator } from "../../../../utils/validator"
import { FilterableProductProps } from "../../../../types/product"
@@ -18,7 +18,6 @@ import {
defaultAdminProductFields,
defaultAdminProductRelations,
} from "../products"
import listAndCount from "../../../../controllers/products/admin-list-products"
import { MedusaError } from "medusa-core-utils"
import { getListConfig } from "../../../../utils/get-query-config"
import PriceListService from "../../../../services/price-list"
@@ -49,7 +49,7 @@ export default async (req, res) => {
expandFields = validated.expand.split(",")
}
const listConfig: FindConfig<PriceList> = {
const listConfig: FindConfig<FilterablePriceListProps> = {
relations: expandFields,
skip: validated.offset,
take: validated.limit,
@@ -65,7 +65,7 @@ export default async (req, res) => {
}
}
const filterableFields = omit(validated, [
const filterableFields: FilterablePriceListProps = omit(validated, [
"limit",
"offset",
"expand",