Skip to content

Commit

Permalink
fix: Can not create price based or weight based shipping method #671
Browse files Browse the repository at this point in the history
  • Loading branch information
treoden committed Dec 10, 2024
1 parent 2dfad83 commit b6b0f16
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 27 deletions.
7 changes: 7 additions & 0 deletions packages/evershop/src/lib/util/throwIf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function throwIf(condition, message) {
if (condition) {
throw new Error(message);
}
}

module.exports = exports = throwIf;
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,31 @@ const {
// eslint-disable-next-line no-unused-vars
module.exports = async (request, response, deledate, next) => {
const { id } = request.params;
let { cost, calculate_api, condition_type, max, min } = request.body;
let {
cost,
calculate_api,
condition_type,
max,
min,
weight_based_cost,
price_based_cost
} = request.body;
const { method_id, is_enabled, calculation_type } = request.body;
// Make sure cost or calculate_api is provided
if (
(request.body.cost === undefined || request.body.cost === null) &&
(request.body.calculate_api === undefined ||
request.body.calculate_api === null)
) {
response.status(INVALID_PAYLOAD);
response.json({
error: {
status: INVALID_PAYLOAD,
message: 'Either cost or calculate_api must be provided'
}
});
return;
}

if (calculation_type === 'api') {
cost = weight_based_cost = price_based_cost = null;
} else if (calculation_type === 'price_based_rate') {
calculate_api = cost = weight_based_cost = null;
} else if (calculation_type === 'weight_based_rate') {
calculate_api = cost = price_based_cost = null;
} else {
calculate_api = weight_based_cost = price_based_cost = null;
}
if (condition_type === 'none') {
condition_type = null;
min = max = null;
}

if (calculation_type === 'api') {
cost = null;
} else {
calculate_api = null;
}

const connection = await getConnection();
await startTransaction(connection);

Expand Down Expand Up @@ -83,10 +79,6 @@ module.exports = async (request, response, deledate, next) => {
return;
}

if (calculate_api) {
cost = null;
}

const zoneMethod = await insert('shipping_zone_method')
.given({
zone_id: zone.shipping_zone_id,
Expand All @@ -95,6 +87,8 @@ module.exports = async (request, response, deledate, next) => {
is_enabled,
calculate_api,
condition_type,
price_based_cost,
weight_based_cost,
max,
min
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"calculation_type": {
"type": "string",
"enum": ["flat_rate", "api"]
"enum": ["flat_rate", "price_based_rate", "weight_based_rate", "api"]
},
"calculate_api": {
"type": "string"
Expand All @@ -30,6 +30,48 @@
"max": {
"type": ["string", "number"],
"pattern": "^\\d+(\\.\\d{1,2})?$"
},
"weight_based_cost": {
"type": "array",
"items": {
"type": "object",
"properties": {
"min_weight": {
"type": ["string", "number"],
"pattern": "^\\d+(\\.\\d{1,2})?$"
},
"cost": {
"type": ["string", "number"],
"pattern": "^\\d+(\\.\\d{1,2})?$"
}
},
"additionalProperties": false,
"required": [
"min_weight",
"cost"
]
}
},
"price_based_cost": {
"type": "array",
"items": {
"type": "object",
"properties": {
"min_price": {
"type": ["string", "number"],
"pattern": "^\\d+(\\.\\d{1,2})?$"
},
"cost": {
"type": ["string", "number"],
"pattern": "^\\d+(\\.\\d{1,2})?$"
}
},
"additionalProperties": false,
"required": [
"min_price",
"cost"
]
}
}
},
"additionalProperties": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { error } = require('@evershop/evershop/src/lib/log/logger');
const throwIf = require('@evershop/evershop/src/lib/util/throwIf');

// eslint-disable-next-line no-unused-vars
module.exports = async (request, response, deledate, next) => {
const {
cost,
calculate_api,
weight_based_cost,
price_based_cost,
calculation_type
} = request.body;

try {
if (calculation_type === 'api') {
throwIf(!calculate_api, 'API calculation type requires calculate_api');
} else if (calculation_type === 'price_based_rate') {
throwIf(
!price_based_cost || price_based_cost.length === 0,
'Require price based rates'
);
} else if (calculation_type === 'weight_based_rate') {
throwIf(
!weight_based_cost || weight_based_cost.length === 0,
'Require weight based rates'
);
} else {
throwIf(!cost, 'Flat rate calculation type requires cost');
}
return next();
} catch (e) {
error(e);
return next(e);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const validate = require('../addShippingZoneMethod/validateMethod');

module.exports = async (request, response, deledate, next) => validate(request, response, deledate, next);

0 comments on commit b6b0f16

Please sign in to comment.