feat(types): promotion delete / update / retrieve / add/remove-rules (#5988)
This commit is contained in:
@@ -1,44 +1,86 @@
|
||||
import {
|
||||
ApplicationMethodAllocationValues,
|
||||
ApplicationMethodTargetTypeValues,
|
||||
ApplicationMethodTypeValues,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ApplicationMethodAllocation,
|
||||
ApplicationMethodTargetType,
|
||||
ApplicationMethodType,
|
||||
MedusaError,
|
||||
isDefined,
|
||||
} from "@medusajs/utils"
|
||||
import { CreateApplicationMethodDTO } from "../../types"
|
||||
|
||||
const allowedTargetTypes: string[] = [
|
||||
export const allowedAllocationTargetTypes: string[] = [
|
||||
ApplicationMethodTargetType.SHIPPING,
|
||||
ApplicationMethodTargetType.ITEM,
|
||||
]
|
||||
|
||||
const allowedAllocationTypes: string[] = [
|
||||
export const allowedAllocationTypes: string[] = [
|
||||
ApplicationMethodAllocation.ACROSS,
|
||||
ApplicationMethodAllocation.EACH,
|
||||
]
|
||||
|
||||
const allowedAllocationForQuantity: string[] = [
|
||||
export const allowedAllocationForQuantity: string[] = [
|
||||
ApplicationMethodAllocation.EACH,
|
||||
]
|
||||
|
||||
export function validateApplicationMethodAttributes(
|
||||
data: CreateApplicationMethodDTO
|
||||
) {
|
||||
export function validateApplicationMethodAttributes(data: {
|
||||
type: ApplicationMethodTypeValues
|
||||
target_type: ApplicationMethodTargetTypeValues
|
||||
allocation?: ApplicationMethodAllocationValues
|
||||
max_quantity?: number | null
|
||||
}) {
|
||||
const allTargetTypes: string[] = Object.values(ApplicationMethodTargetType)
|
||||
|
||||
if (!allTargetTypes.includes(data.target_type)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`application_method.target_type should be one of ${allTargetTypes.join(
|
||||
", "
|
||||
)}`
|
||||
)
|
||||
}
|
||||
|
||||
const allTypes: string[] = Object.values(ApplicationMethodType)
|
||||
|
||||
if (!allTypes.includes(data.type)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`application_method.type should be one of ${allTypes.join(", ")}`
|
||||
)
|
||||
}
|
||||
|
||||
if (
|
||||
allowedTargetTypes.includes(data.target_type) &&
|
||||
allowedAllocationTargetTypes.includes(data.target_type) &&
|
||||
!allowedAllocationTypes.includes(data.allocation || "")
|
||||
) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`application_method.allocation should be either '${allowedAllocationTypes.join(
|
||||
" OR "
|
||||
)}' when application_method.target_type is either '${allowedTargetTypes.join(
|
||||
)}' when application_method.target_type is either '${allowedAllocationTargetTypes.join(
|
||||
" OR "
|
||||
)}'`
|
||||
)
|
||||
}
|
||||
|
||||
const allAllocationTypes: string[] = Object.values(
|
||||
ApplicationMethodAllocation
|
||||
)
|
||||
|
||||
if (data.allocation && !allAllocationTypes.includes(data.allocation)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`application_method.allocation should be one of ${allAllocationTypes.join(
|
||||
", "
|
||||
)}`
|
||||
)
|
||||
}
|
||||
|
||||
if (
|
||||
allowedAllocationForQuantity.includes(data.allocation || "") &&
|
||||
data.allocation &&
|
||||
allowedAllocationForQuantity.includes(data.allocation) &&
|
||||
!isDefined(data.max_quantity)
|
||||
) {
|
||||
throw new MedusaError(
|
||||
|
||||
Reference in New Issue
Block a user