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:
Adrien de Peretti
2022-10-17 09:03:38 +00:00
committed by GitHub
parent 765a2cccda
commit 48411157b1
12 changed files with 432 additions and 12 deletions
@@ -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]]:
@@ -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"
@@ -133,7 +133,7 @@ export class DiscountConditionRepository extends Repository<DiscountCondition> {
async removeConditionResources(
id: string,
type: DiscountConditionType,
resourceIds: string[]
resourceIds: (string | { id: string })[]
): Promise<DeleteResult | void> {
const { conditionTable, joinTableForeignKey } =
this.getJoinTableResourceIdentifiers(type)
@@ -142,10 +142,13 @@ export class DiscountConditionRepository extends Repository<DiscountCondition> {
return Promise.resolve()
}
const idsToDelete = resourceIds.map((rId): string => {
return isString(rId) ? rId : rId.id
})
return await this.createQueryBuilder()
.delete()
.from(conditionTable)
.where({ condition_id: id, [joinTableForeignKey]: In(resourceIds) })
.where({ condition_id: id, [joinTableForeignKey]: In(idsToDelete) })
.execute()
}
@@ -12,7 +12,7 @@ import {
} from "../models"
import { DiscountConditionRepository } from "../repositories/discount-condition"
import { FindConfig } from "../types/common"
import { UpsertDiscountConditionInput } from "../types/discount"
import { DiscountConditionInput } from "../types/discount"
import { TransactionBaseService } from "../interfaces"
import { buildQuery, PostgresError } from "../utils"
@@ -69,7 +69,7 @@ class DiscountConditionService extends TransactionBaseService {
return condition
}
protected static resolveConditionType_(data: UpsertDiscountConditionInput):
protected static resolveConditionType_(data: DiscountConditionInput):
| {
type: DiscountConditionType
resource_ids: (string | { id: string })[]
@@ -107,7 +107,7 @@ class DiscountConditionService extends TransactionBaseService {
}
async upsertCondition(
data: UpsertDiscountConditionInput,
data: DiscountConditionInput,
overrideExisting: boolean = true
): Promise<
(
@@ -178,6 +178,38 @@ class DiscountConditionService extends TransactionBaseService {
)
}
async removeResources(
data: Omit<DiscountConditionInput, "id"> & { id: string }
): Promise<void> {
return await this.atomicPhase_(async (manager: EntityManager) => {
const resolvedConditionType =
DiscountConditionService.resolveConditionType_(data)
if (!resolvedConditionType) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Missing one of products, collections, tags, types or customer groups in data`
)
}
const discountConditionRepo: DiscountConditionRepository =
manager.getCustomRepository(this.discountConditionRepository_)
const resolvedCondition = await this.retrieve(data.id)
if (data.operator && data.operator !== resolvedCondition.operator) {
resolvedCondition.operator = data.operator
await discountConditionRepo.save(resolvedCondition)
}
await discountConditionRepo.removeConditionResources(
data.id,
resolvedConditionType.type,
resolvedConditionType.resource_ids
)
})
}
async delete(discountConditionId: string): Promise<DiscountCondition | void> {
return await this.atomicPhase_(async (manager: EntityManager) => {
const conditionRepo = manager.getCustomRepository(
+3 -3
View File
@@ -117,7 +117,7 @@ export const DiscountConditionMapTypeToProperty = {
[DiscountConditionType.CUSTOMER_GROUPS]: "customer_groups",
}
export type UpsertDiscountConditionInput = {
export type DiscountConditionInput = {
rule_id?: string
id?: string
operator?: DiscountConditionOperator
@@ -133,7 +133,7 @@ export type CreateDiscountRuleInput = {
type: DiscountRuleType
value: number
allocation: AllocationType
conditions?: UpsertDiscountConditionInput[]
conditions?: DiscountConditionInput[]
}
export type CreateDiscountInput = {
@@ -154,7 +154,7 @@ export type UpdateDiscountRuleInput = {
description?: string
value?: number
allocation?: AllocationType
conditions?: UpsertDiscountConditionInput[]
conditions?: DiscountConditionInput[]
}
export type UpdateDiscountInput = {