feat(medusa): Support batch remove resources on discount condition (#2444)
**what** - Add support to remove resources by batch on discount conditions - Add support on medusa-js and medusa-react **Tests** - Add integration tests to validate that the resources have been deleted and the length is the one expected - Add unit tests on medusa react FIXES CORE-609
This commit is contained in:
committed by
GitHub
parent
765a2cccda
commit
48411157b1
@@ -3,8 +3,8 @@ import { Request, Response } from "express"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { DiscountService } from "../../../../services"
|
||||
import {
|
||||
DiscountConditionInput,
|
||||
DiscountConditionMapTypeToProperty,
|
||||
UpsertDiscountConditionInput,
|
||||
} from "../../../../types/discount"
|
||||
import { IsArray } from "class-validator"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
@@ -18,7 +18,7 @@ import { FindParams } from "../../../../types/common"
|
||||
* parameters:
|
||||
* - (path) discount_id=* {string} The ID of the Product.
|
||||
* - (path) condition_id=* {string} The ID of the condition on which to add the item.
|
||||
* - (query) expand {string} (Comma separated) Which fields should be expanded in each discount of the result.
|
||||
* - (query) expand {string} (Comma separated) Which relations should be expanded in each discount of the result.
|
||||
* - (query) fields {string} (Comma separated) Which fields should be included in each discount of the result.
|
||||
* requestBody:
|
||||
* content:
|
||||
@@ -42,7 +42,6 @@ import { FindParams } from "../../../../types/common"
|
||||
* label: JS Client
|
||||
* source: |
|
||||
* import Medusa from "@medusajs/medusa-js"
|
||||
* import { DiscountConditionOperator } from "@medusajs/medusa"
|
||||
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
|
||||
* // must be previously logged in or use api token
|
||||
* medusa.admin.discounts.addConditionResourceBatch(discount_id, condition_id, {
|
||||
@@ -101,7 +100,7 @@ export default async (req: Request, res: Response) => {
|
||||
select: ["id", "type", "discount_rule_id"],
|
||||
})
|
||||
|
||||
const updateObj: UpsertDiscountConditionInput = {
|
||||
const updateObj: DiscountConditionInput = {
|
||||
id: condition_id,
|
||||
rule_id: condition.discount_rule_id,
|
||||
[DiscountConditionMapTypeToProperty[condition.type]]:
|
||||
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
import { Request, Response } from "express"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { DiscountConditionService, DiscountService } from "../../../../services"
|
||||
import {
|
||||
DiscountConditionInput,
|
||||
DiscountConditionMapTypeToProperty,
|
||||
} from "../../../../types/discount"
|
||||
import { IsArray } from "class-validator"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
|
||||
/**
|
||||
* @oas [delete] /discounts/{discount_id}/conditions/{condition_id}/batch
|
||||
* operationId: "DeleteDiscountsDiscountConditionsConditionBatch"
|
||||
* summary: "Delete a batch of resources from a discount condition"
|
||||
* description: "Delete a batch of resources from a discount condition."
|
||||
* x-authenticated: true
|
||||
* parameters:
|
||||
* - (path) discount_id=* {string} The ID of the Product.
|
||||
* - (path) condition_id=* {string} The ID of the condition on which to add the item.
|
||||
* - (query) expand {string} (Comma separated) Which relations should be expanded in each discount of the result.
|
||||
* - (query) fields {string} (Comma separated) Which fields should be included in each discount of the result.
|
||||
* requestBody:
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* required:
|
||||
* - resources
|
||||
* properties:
|
||||
* resources:
|
||||
* description: The resources to be deleted from the discount condition
|
||||
* type: array
|
||||
* items:
|
||||
* required:
|
||||
* - id
|
||||
* properties:
|
||||
* id:
|
||||
* description: The id of the item
|
||||
* type: string
|
||||
* x-codeSamples:
|
||||
* - lang: JavaScript
|
||||
* label: JS Client
|
||||
* source: |
|
||||
* import Medusa from "@medusajs/medusa-js"
|
||||
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
|
||||
* // must be previously logged in or use api token
|
||||
* medusa.admin.discounts.deleteConditionResourceBatch(discount_id, condition_id, {
|
||||
* resources: [{ id: item_id }]
|
||||
* })
|
||||
* .then(({ discount }) => {
|
||||
* console.log(discount.id);
|
||||
* });
|
||||
* - lang: Shell
|
||||
* label: cURL
|
||||
* source: |
|
||||
* curl --location --request DELETE 'https://medusa-url.com/admin/discounts/{id}/conditions/{condition_id}/batch' \
|
||||
* --header 'Authorization: Bearer {api_token}' \
|
||||
* --header 'Content-Type: application/json' \
|
||||
* --data-raw '{
|
||||
* "resources": [{ "id": "item_id" }]
|
||||
* }'
|
||||
* security:
|
||||
* - api_token: []
|
||||
* - cookie_auth: []
|
||||
* tags:
|
||||
* - Discount Condition
|
||||
* responses:
|
||||
* 200:
|
||||
* description: OK
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* properties:
|
||||
* discount:
|
||||
* $ref: "#/components/schemas/discount"
|
||||
* "400":
|
||||
* $ref: "#/components/responses/400_error"
|
||||
* "401":
|
||||
* $ref: "#/components/responses/unauthorized"
|
||||
* "404":
|
||||
* $ref: "#/components/responses/not_found_error"
|
||||
* "409":
|
||||
* $ref: "#/components/responses/invalid_state_error"
|
||||
* "422":
|
||||
* $ref: "#/components/responses/invalid_request_error"
|
||||
* "500":
|
||||
* $ref: "#/components/responses/500_error"
|
||||
*/
|
||||
export default async (req: Request, res: Response) => {
|
||||
const { discount_id, condition_id } = req.params
|
||||
|
||||
const conditionService: DiscountConditionService = req.scope.resolve(
|
||||
"discountConditionService"
|
||||
)
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
|
||||
const condition = await conditionService.retrieve(condition_id, {
|
||||
select: ["id", "type", "discount_rule_id"],
|
||||
})
|
||||
|
||||
const validatedBody =
|
||||
req.validatedBody as AdminDeleteDiscountsDiscountConditionsConditionBatchReq
|
||||
const data = {
|
||||
id: condition_id,
|
||||
rule_id: condition.discount_rule_id,
|
||||
[DiscountConditionMapTypeToProperty[condition.type]]:
|
||||
validatedBody.resources,
|
||||
} as Omit<DiscountConditionInput, "id"> & { id: string }
|
||||
|
||||
await manager.transaction(async (transactionManager) => {
|
||||
await conditionService
|
||||
.withTransaction(transactionManager)
|
||||
.removeResources(data)
|
||||
})
|
||||
|
||||
const discountService: DiscountService = req.scope.resolve("discountService")
|
||||
const discount = await discountService.retrieve(
|
||||
discount_id,
|
||||
req.retrieveConfig
|
||||
)
|
||||
|
||||
res.status(200).json({ discount })
|
||||
}
|
||||
|
||||
export class AdminDeleteDiscountsDiscountConditionsConditionBatchParams extends FindParams {}
|
||||
|
||||
export class AdminDeleteDiscountsDiscountConditionsConditionBatchReq {
|
||||
@IsArray()
|
||||
resources: { id: string }[]
|
||||
}
|
||||
@@ -34,6 +34,10 @@ import { AdminGetDiscountsDiscountConditionsConditionParams } from "./get-condit
|
||||
import { AdminDeleteDiscountsDiscountConditionsConditionParams } from "./delete-condition"
|
||||
import { AdminGetDiscountsDiscountCodeParams } from "./get-discount-by-code"
|
||||
import { AdminGetDiscountParams } from "./get-discount"
|
||||
import {
|
||||
AdminDeleteDiscountsDiscountConditionsConditionBatchParams,
|
||||
AdminDeleteDiscountsDiscountConditionsConditionBatchReq,
|
||||
} from "./delete-resources-from-condition-batch"
|
||||
|
||||
const route = Router()
|
||||
|
||||
@@ -172,6 +176,16 @@ export default (app) => {
|
||||
transformBody(AdminPostDiscountsDiscountConditionsConditionBatchReq),
|
||||
middlewares.wrap(require("./add-resources-to-condition-batch").default)
|
||||
)
|
||||
conditionRouter.delete(
|
||||
"/batch",
|
||||
transformQuery(AdminDeleteDiscountsDiscountConditionsConditionBatchParams, {
|
||||
defaultFields: defaultAdminDiscountsFields,
|
||||
defaultRelations: defaultAdminDiscountsRelations,
|
||||
isList: false,
|
||||
}),
|
||||
transformBody(AdminDeleteDiscountsDiscountConditionsConditionBatchReq),
|
||||
middlewares.wrap(require("./delete-resources-from-condition-batch").default)
|
||||
)
|
||||
|
||||
return app
|
||||
}
|
||||
@@ -235,3 +249,4 @@ export * from "./remove-region"
|
||||
export * from "./update-condition"
|
||||
export * from "./update-discount"
|
||||
export * from "./add-resources-to-condition-batch"
|
||||
export * from "./delete-resources-from-condition-batch"
|
||||
|
||||
Reference in New Issue
Block a user