Skip to content

Commit

Permalink
add soft delivery calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Xziy committed Aug 24, 2024
1 parent 481a461 commit d81beb3
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 17 deletions.
8 changes: 7 additions & 1 deletion adapters/delivery/DeliveryAdapter.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import Order from "../../models/Order";
/**
* **Soft delivery calculation**
* This is done so that some deliveries can agree on the cost of delivery themselves.
* If it is `allowed = true` and the `cost = null`,
* then this is only possible if the required delivery calculation flag is absent
*/
export interface Delivery {
deliveryTimeMinutes: number;
allowed: boolean;
cost: number;
cost: number | null;
item: string | undefined;
message: string;
}
Expand Down
8 changes: 7 additions & 1 deletion adapters/delivery/DeliveryAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import Order from "../../models/Order";


/**
* **Soft delivery calculation**
* This is done so that some deliveries can agree on the cost of delivery themselves.
* If it is `allowed = true` and the `cost = null`,
* then this is only possible if the required delivery calculation flag is absent
*/
export interface Delivery {
deliveryTimeMinutes: number
allowed: boolean
cost: number
cost: number | null
item: string | undefined
message: string
}
Expand Down
4 changes: 4 additions & 0 deletions interfaces/globalTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ declare global {
SYNC_BONUSTRANSACTION_AFTER_TIME: number;
DISABLE_USER_BONUS_PROGRAM_ON_FAIL: boolean;
ONLY_EXTERNAL_BONUS_SPEND_CHECK: boolean;
/**
* Allows you to make shipping calculations optional. Shipping calculations will occur. But it won't throw an error
*/
SOFT_DELIVERY_CALCULATION: boolean;
RESTOCORE_URL: string;
IMAGES_URL: string;
PROJECT_NAME: string;
Expand Down
5 changes: 5 additions & 0 deletions interfaces/globalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ declare global {
DISABLE_USER_BONUS_PROGRAM_ON_FAIL: boolean
ONLY_EXTERNAL_BONUS_SPEND_CHECK: boolean

/**
* Allows you to make shipping calculations optional. Shipping calculations will occur. But it won't throw an error
*/
SOFT_DELIVERY_CALCULATION: boolean

// from base modules
RESTOCORE_URL: string
IMAGES_URL: string
Expand Down
28 changes: 20 additions & 8 deletions models/Order.js
Original file line number Diff line number Diff line change
Expand Up @@ -1320,14 +1320,26 @@ let Model = {
delivery = await deliveryAdapter.calculate(order);
}
catch (error) {
sails.log.error("deliveryAdapter.calculate error:", error);
delivery = {
allowed: false,
cost: 0,
item: undefined,
message: error.replace(/[^\w\s]/gi, ''),
deliveryTimeMinutes: Infinity
};
if (await Settings.get("SOFT_DELIVERY_CALCULATION")) {
sails.log.debug("deliveryAdapter.calculate error:", error);
delivery = {
allowed: true,
cost: null,
item: undefined,
message: "Shipping cost has not been calculated",
deliveryTimeMinutes: undefined
};
}
else {
sails.log.error("deliveryAdapter.calculate error:", error);
delivery = {
allowed: false,
cost: 0,
item: undefined,
message: error.replace(/[^\w\s]/gi, ''),
deliveryTimeMinutes: undefined
};
}
}
order.delivery = delivery;
}
Expand Down
25 changes: 18 additions & 7 deletions models/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1585,13 +1585,24 @@ let Model = {
try {
delivery = await deliveryAdapter.calculate(order);
} catch (error) {
sails.log.error("deliveryAdapter.calculate error:", error)
delivery = {
allowed: false,
cost: 0,
item: undefined,
message: error.replace(/[^\w\s]/gi, ''),
deliveryTimeMinutes: Infinity
if(await Settings.get("SOFT_DELIVERY_CALCULATION")) {
sails.log.debug("deliveryAdapter.calculate error:", error)
delivery = {
allowed: true,
cost: null,
item: undefined,
message: "Shipping cost has not been calculated",
deliveryTimeMinutes: undefined
}
} else {
sails.log.error("deliveryAdapter.calculate error:", error)
delivery = {
allowed: false,
cost: 0,
item: undefined,
message: error.replace(/[^\w\s]/gi, ''),
deliveryTimeMinutes: undefined
}
}
}
order.delivery = delivery
Expand Down
8 changes: 8 additions & 0 deletions settings/soft_delivery_calculation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"key": "SOFT_DELIVERY_CALCULATION",
"type": "boolean",
"name": "Soft delivery calculation",
"description": "Allows you to make shipping calculations optional. Shipping calculations will occur. But it won't throw an error",
"defaultValue": true,
"isRequired": false
}

0 comments on commit d81beb3

Please sign in to comment.