fix: add orphan reward line cleanup and implement optional chaining for program_id access in POS loyalty logic

This commit is contained in:
Suherdy Yacob 2026-05-29 15:19:59 +07:00
parent e8e5e8d70a
commit dfd52fe1cc
2 changed files with 11 additions and 6 deletions

View File

@ -361,6 +361,11 @@ patch(PosOrder.prototype, {
const otherRewards = [];
const paymentRewards = [];
for (const line of otherRewardLines) {
// Skip (and delete) orphaned reward lines where program_id is missing
if (!line.reward_id || !line.reward_id.program_id) {
line.delete();
continue;
}
const claimedReward = {
reward: line.reward_id,
coupon_id: line.coupon_id?.id,
@ -373,8 +378,8 @@ patch(PosOrder.prototype, {
reward_identifier_code: line.reward_identifier_code,
};
if (
claimedReward.reward.program_id.program_type === "gift_card" ||
claimedReward.reward.program_id.program_type === "ewallet"
claimedReward.reward.program_id?.program_type === "gift_card" ||
claimedReward.reward.program_id?.program_type === "ewallet"
) {
paymentRewards.push(claimedReward);
} else if (claimedReward.reward.reward_type === "product") {
@ -418,7 +423,7 @@ patch(PosOrder.prototype, {
continue;
}
if (
claimedReward.reward.program_id.program_type === "coupons" &&
claimedReward.reward.program_id?.program_type === "coupons" &&
this.lines.find(
(rewardline) => rewardline.reward_id?.id === claimedReward.reward.id
)
@ -428,7 +433,7 @@ patch(PosOrder.prototype, {
if (
claimedReward.reward.reward_product_ids?.length === 1 &&
allRewardsMerged.filter(
(reward) => reward.reward.program_id.id === claimedReward.reward.program_id.id
(reward) => reward.reward.program_id?.id === claimedReward.reward.program_id?.id
).length === 1
) {
delete claimedReward.args["quantity"];

View File

@ -93,8 +93,8 @@ patch(ControlButtons.prototype, {
}
if (
(reward.reward_type == "product" && reward.program_id.applies_on !== "both") ||
(reward.program_id.applies_on == "both" && potentialQty)
(reward.reward_type == "product" && reward.program_id?.applies_on !== "both") ||
(reward.program_id?.applies_on == "both" && potentialQty)
) {
const product = args["product"] || reward.reward_product_ids[0];
await this.pos.addLineToCurrentOrder(