fix: Creating shipping options with requirements (#428)
This commit is contained in:
1 parent
d8e531890d
commit
ae0ab03fac
4 files changed
+300
-131
No files matched your search
@@ -272,11 +272,7 @@ describe("ShippingOptionService", () => {
|
||||
amount: 10,
|
||||
})
|
||||
|
||||
expect(shippingOptionRequirementRepository.create).toBeCalledTimes(1)
|
||||
expect(shippingOptionRequirementRepository.create).toBeCalledWith({
|
||||
type: "max_subtotal",
|
||||
amount: 10,
|
||||
})
|
||||
expect(shippingOptionRequirementRepository.create).toBeCalledTimes(0)
|
||||
|
||||
expect(shippingOptionRepository.save).toBeCalledTimes(1)
|
||||
expect(shippingOptionRepository.save).toBeCalledWith({
|
||||
|
||||
@@ -96,6 +96,13 @@ class ShippingOptionService extends BaseService {
|
||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, "ID does not exist")
|
||||
}
|
||||
|
||||
// If no option id is provided, we are currently in the process of creating
|
||||
// a new shipping option. Therefore, simply return the requirement, such
|
||||
// that the cascading will take care of the creation of the requirement.
|
||||
if (!optionId) {
|
||||
return requirement
|
||||
}
|
||||
|
||||
let req
|
||||
if (existingReq) {
|
||||
req = await reqRepo.save({
|
||||
@@ -359,11 +366,33 @@ class ShippingOptionService extends BaseService {
|
||||
}
|
||||
|
||||
if ("requirements" in data) {
|
||||
option.requirements = await Promise.all(
|
||||
data.requirements.map(r => {
|
||||
return this.validateRequirement_(r, option.id)
|
||||
})
|
||||
)
|
||||
const acc = []
|
||||
for (const r of data.requirements) {
|
||||
const validated = await this.validateRequirement_(r)
|
||||
|
||||
if (acc.find(raw => raw.type === validated.type)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
"Only one requirement of each type is allowed"
|
||||
)
|
||||
}
|
||||
|
||||
if (
|
||||
acc.find(
|
||||
raw =>
|
||||
(raw.type === "max_subtotal" &&
|
||||
validated.amount > raw.amount) ||
|
||||
(raw.type === "min_subtotal" && validated.amount < raw.amount)
|
||||
)
|
||||
) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
"Max. subtotal must be greater than Min. subtotal"
|
||||
)
|
||||
}
|
||||
|
||||
acc.push(validated)
|
||||
}
|
||||
}
|
||||
|
||||
const result = await optionRepo.save(option)
|
||||
|
||||
Reference in new issue
Block a user