fix: make shipping_option_id on requirements optional (#340)

* changed validator so that reqiurement shipping_option_id is now optional + added integration test that confirms that when an update contains a requirement without an ID it is created

* fix: formatting

* fix: un-bump babel-preset-medusa-package

* chore: update yarn.lock

* fix: implemented suggested changes, need to validate behaviour on clean branch so NOT ready for merging just yet

* fix: implemented suggested changes, need to validate behaviour on clean branch so NOT ready for merging just yet

* afix: made it impossible to set a min. subtotal requirement that is greater than max. subtotal

* fix: added explanation to error

* fix: Error when removing requirement on update

Co-authored-by: olivermrbl <oliver@mrbltech.com>
This commit is contained in:
Kasper Fabricius Kristensen
2021-09-09 09:03:00 +02:00
committed by GitHub
co-authored by olivermrbl
parent 56a1d99a07
commit 16b0fa377a
9 changed files with 2452 additions and 3711 deletions
File diff suppressed because it is too large Load Diff
@@ -56,7 +56,7 @@ export default async (req, res) => {
requirements: Validator.array()
.items(
Validator.object({
id: Validator.string().required(),
id: Validator.string().optional(),
type: Validator.string().required(),
amount: Validator.number()
.integer()
@@ -84,14 +84,11 @@ describe("ShippingOptionService", () => {
await optionService.update(IdMap.getId("option"), { requirements })
expect(shippingOptionRepository.save).toHaveBeenCalledTimes(1)
expect(shippingOptionRepository.save).toHaveBeenCalledWith({
requirements: [
{
shipping_option_id: IdMap.getId("option"),
type: "min_subtotal",
amount: 1,
},
],
expect(shippingOptionRequirementRepository.save).toHaveBeenCalledTimes(1)
expect(shippingOptionRequirementRepository.save).toHaveBeenCalledWith({
shipping_option_id: IdMap.getId("option"),
type: "min_subtotal",
amount: 1,
})
})
@@ -92,6 +92,10 @@ class ShippingOptionService extends BaseService {
where: { id: requirement.id },
})
if (!existingReq && requirement.id) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, "ID does not exist")
}
let req
if (existingReq) {
req = await reqRepo.save({
@@ -99,10 +103,12 @@ class ShippingOptionService extends BaseService {
...requirement,
})
} else {
req = await reqRepo.create({
const created = reqRepo.create({
shipping_option_id: optionId,
...requirement,
})
req = await reqRepo.save(created)
}
return req
@@ -447,9 +453,22 @@ class ShippingOptionService extends BaseService {
)
}
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)
}
option.requirements = acc
}
if ("price_type" in update) {
+704 -470
View File
File diff suppressed because it is too large Load Diff