Skip to content

Commit

Permalink
softDeliveryCalculation for address check
Browse files Browse the repository at this point in the history
  • Loading branch information
Xziy committed Aug 25, 2024
1 parent 4964d2b commit feaa4d8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
29 changes: 20 additions & 9 deletions models/Order.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,21 +683,23 @@ let Model = {
order.paymentMethodTitle = (await PaymentMethod.findOne({ id: paymentMethodId })).title;
order.isPaymentPromise = await PaymentMethod.isPaymentPromise(paymentMethodId);
}
let softDeliveryCalculation = true;
/** if pickup, then you do not need to check the address*/
if (isSelfService) {
order.selfService = true;
emitter.emit("core:order-is-self-service", order, customer, isSelfService, address);
}
else {
order.selfService = false;
softDeliveryCalculation = await Settings.get("SOFT_DELIVERY_CALCULATION");
if (!address.city)
address.city = await Settings.get("CITY");
if (address) {
checkAddress(address);
checkAddress(address, softDeliveryCalculation);
order.address = { ...address };
}
else {
if (!isSelfService && order.address === null && !(await Settings.get("SOFT_DELIVERY_CALCULATION"))) {
if (!isSelfService && order.address === null && !softDeliveryCalculation) {
throw {
code: 5,
error: "address is required",
Expand Down Expand Up @@ -1510,24 +1512,33 @@ async function checkCustomerInfo(customer) {
};
}
}
function checkAddress(address) {
function checkAddress(address, softDeliveryCalculation = false) {
let error = [];
if (!address.street && !address.streetId && !address.buildingName) {
throw {
error.push({
code: 5,
error: "one of (street, streetId or buildingName) is required",
};
});
}
if (!address.home) {
throw {
error.push({
code: 6,
error: "address.home is required",
};
});
}
if (!address.city) {
throw {
error.push({
code: 7,
error: "address.city is required",
};
});
}
if (error.length) {
if (softDeliveryCalculation) {
return error;
}
else {
throw error[0];
}
}
}
async function checkPaymentMethod(paymentMethodId) {
Expand Down
30 changes: 21 additions & 9 deletions models/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,18 +856,21 @@ let Model = {
order.isPaymentPromise = await PaymentMethod.isPaymentPromise(paymentMethodId);
}

let softDeliveryCalculation: boolean = true;

/** if pickup, then you do not need to check the address*/
if (isSelfService) {
order.selfService = true;
emitter.emit("core:order-is-self-service", order, customer, isSelfService, address);
} else {
order.selfService = false;
softDeliveryCalculation = await Settings.get("SOFT_DELIVERY_CALCULATION");
if (!address.city) address.city = await Settings.get("CITY");
if (address) {
checkAddress(address);
checkAddress(address, softDeliveryCalculation);
order.address = { ...address };
} else {
if (!isSelfService && order.address === null && !(await Settings.get("SOFT_DELIVERY_CALCULATION"))) {
if (!isSelfService && order.address === null && !softDeliveryCalculation) {
throw {
code: 5,
error: "address is required",
Expand Down Expand Up @@ -1808,26 +1811,35 @@ async function checkCustomerInfo(customer: Customer) {
}
}

function checkAddress(address: Address) {
function checkAddress(address: Address, softDeliveryCalculation: boolean = false) {
let error = [];
if (!address.street && !address.streetId && ! address.buildingName) {
throw {
error.push({
code: 5,
error: "one of (street, streetId or buildingName) is required",
};
});
}

if (!address.home) {
throw {
error.push({
code: 6,
error: "address.home is required",
};
});
}

if (!address.city) {
throw {
error.push({
code: 7,
error: "address.city is required",
};
});
}

if(error.length) {
if(softDeliveryCalculation) {
return error
} else {
throw error[0]
}
}
}

Expand Down

0 comments on commit feaa4d8

Please sign in to comment.