Skip to content

Commit

Permalink
fix PAYMENT apply promocode
Browse files Browse the repository at this point in the history
  • Loading branch information
Xziy committed Feb 20, 2024
1 parent f2e7958 commit 51cb099
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"'${workspaceFolder}/test/{,!(fixture)/**}/*.test.ts'",
"--exit",
"--timeout=600000",
"--grep=Percentage"
"--grep=promocode"
],
"sourceMaps": true,
"internalConsoleOptions": "openOnSessionStart",
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Hide all files that end in .d.ts
"**/*.d.ts": true,
// Hide all files that end with .js if they have a .ts file
"**/*.js": {
"**/*.1js": {
"when": "$(basename).ts"
},
},
Expand Down
3 changes: 1 addition & 2 deletions adapters/promotion/AbstractPromotionAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ class AbstractPromotionAdapter {
// if Order.status ="PAYMENT" or "ORDER" can't clear promotions
if (order.state === "ORDER")
throw "order with orderId " + order.id + "in state ORDER";
if (order.state === "PAYMENT")
throw "order with orderId " + order.id + "in state PAYMENT";
//if (order.state === "PAYMENT") throw "order with orderId " + order.id + "in state PAYMENT";
// const orderDishes = await OrderDish.find({ order: order.id }).populate("dish");
await OrderDish.destroy({ order: order.id, addedBy: "promotion" }).fetch();
await OrderDish.update({ order: order.id }, { discountTotal: 0, discountType: null, discountAmount: 0, discountMessage: null }).fetch();
Expand Down
2 changes: 1 addition & 1 deletion adapters/promotion/AbstractPromotionAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default abstract class AbstractPromotionAdapter {
public async clearOfPromotion(order: Order): Promise<Order> {
// if Order.status ="PAYMENT" or "ORDER" can't clear promotions
if (order.state === "ORDER") throw "order with orderId " + order.id + "in state ORDER";
if (order.state === "PAYMENT") throw "order with orderId " + order.id + "in state PAYMENT";
//if (order.state === "PAYMENT") throw "order with orderId " + order.id + "in state PAYMENT";

// const orderDishes = await OrderDish.find({ order: order.id }).populate("dish");

Expand Down
7 changes: 5 additions & 2 deletions adapters/rms/RMSAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,18 @@ class RMSAdapter {
for (const group of currentRMSGroupsFlatTree) {
const productsToUpdate = await rmsAdapter.loadProductsByGroup(group);
// Get ids of all current products in group
const productIds = productsToUpdate.map((product) => product.rmsId);
allProductIds = allProductIds.concat(productIds);
const productIds = productsToUpdate.map((product) => product.id);
const productRMSIds = productsToUpdate.map((product) => product.rmsId);
allProductIds = allProductIds.concat(productRMSIds);
sails.log.silly("ADAPTER RMS > syncProducts sync Group dishes:", productsToUpdate.length);
for (let product of productsToUpdate) {
emitter.emit("rms-sync:before-each-product-item", product);
// Update or create product
product.concept = product.concept ?? "origin";
const productData = { ...product, isDeleted: false };
let createdProduct = await Dish.createOrUpdate(productData);
// Set isDeleted for absent products in ERP
await Dish.update({ id: { "!=": productIds }, parentGroup: group.id }, { isDeleted: true }).fetch();
const SKIP_LOAD_PRODUCT_IMAGES = (await Settings.get("SKIP_LOAD_PRODUCT_IMAGES")) ?? false;
// Load images
if (product.images && product.images.length && !SKIP_LOAD_PRODUCT_IMAGES) {
Expand Down
6 changes: 5 additions & 1 deletion models/Order.js
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,8 @@ let Model = {
async applyPromotionCode(criteria, promotionCodeString) {
let order = await Order.findOne(criteria);
let updateData = {};
if (!["CART", "CHECKOUT", "PAYMENT"].includes(order.state))
throw `Order with orderId ${order.id} - apply promocode on current state: (${order.state})`;
if (!promotionCodeString || promotionCodeString === null) {
updateData = {
promotionCode: null,
Expand Down Expand Up @@ -1338,7 +1340,9 @@ let Model = {
}
}
await Order.update({ id: order.id }, updateData).fetch();
return await Order.countCart(criteria);
await Order.next(order.id, "CART");
const basket = await Order.countCart({ id: order.id });
return basket;
}
};
// Waterline model export
Expand Down
8 changes: 6 additions & 2 deletions models/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,9 @@ let Model = {
let order = await Order.findOne(criteria);
let updateData: any = {};


if (!["CART", "CHECKOUT", "PAYMENT"].includes(order.state)) throw `Order with orderId ${order.id} - apply promocode on current state: (${order.state})`;

if (!promotionCodeString || promotionCodeString === null) {
updateData = {
promotionCode: null,
Expand Down Expand Up @@ -1607,9 +1610,10 @@ let Model = {
}

await Order.update({ id: order.id }, updateData).fetch();
return await Order.countCart(criteria);
await Order.next(order.id, "CART");
const basket = await Order.countCart({ id: order.id });
return basket
}

};

// Waterline model export
Expand Down
18 changes: 14 additions & 4 deletions test/integration/adapters/rms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,20 @@ describe("RMS adapter", function () {
await rmsAdapter.syncProducts();
let count = await Dish.count({ isDeleted: false });
(0, chai_1.expect)(count).to.equal(616);
let countGroups = await Group.count({ isDeleted: false });
let groups = await Group.find({ isDeleted: false });
const countGroups = groups.length;
(0, chai_1.expect)(countGroups).to.equal(88);
// Should delete unknown products
let unknownProduct = (0, dish_generator_1.default)({
parentGroup: groups[0].id,
price: 100,
name: "Unknown product"
});
product = await Dish.create(unknownProduct).fetch();
count = await Dish.count({ isDeleted: false });
(0, chai_1.expect)(count).to.equal(617);
await rmsAdapter.syncProducts();
count = await Dish.count({ isDeleted: false });
(0, chai_1.expect)(count).to.equal(616);
});
});
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
10 changes: 10 additions & 0 deletions test/integration/promotionCode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const adapters_1 = require("../../adapters");
const chai_1 = require("chai");
const customer_1 = require("../mocks/customer");
const dish_generator_1 = __importDefault(require("../generators/dish.generator"));
const ExternalTestPaymentSystem_1 = __importDefault(require("../unit/external_payments/ExternalTestPaymentSystem"));
describe("Promotion code integration test", function () {
this.timeout(60000);
let promotionAdapter;
Expand Down Expand Up @@ -60,9 +62,17 @@ describe("Promotion code integration test", function () {
(0, chai_1.expect)(result.discountTotal).to.equal(1.45);
(0, chai_1.expect)(result.total).to.equal(109.85);
(0, chai_1.expect)(result.basketTotal).to.equal(111.3);
// After go to payment promocode should work, till ORDER state
await ExternalTestPaymentSystem_1.default.getInstance();
const paymentMethod = (await PaymentMethod.find({}))[0];
await Order.check({ id: order.id }, customer_1.customer, false, customer_1.address, paymentMethod.id);
await Order.payment({ id: order.id });
result = await Order.findOne({ id: order.id });
console.log(result, 1);
// CLEAR PROMOCODE
await Order.applyPromotionCode({ id: order.id }, null);
result = await Order.findOne({ id: order.id });
console.log(result, 2);
(0, chai_1.expect)(result.discountTotal).to.equal(0);
(0, chai_1.expect)(result.total).to.equal(111.3);
// NOT VALID PROMOCODE
Expand Down
16 changes: 16 additions & 0 deletions test/integration/promotionCode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import discountGenerator from "../generators/discount.generator";
import ConfiguredPromotion from "../../adapters/promotion/default/configuredPromotion";
import Decimal from "decimal.js";
import { PromotionAdapter } from "../../adapters/promotion/default/promotionAdapter";
import TestPaymentSystem from "../unit/external_payments/ExternalTestPaymentSystem";



Expand Down Expand Up @@ -90,11 +91,26 @@ describe("Promotion code integration test", function () {
expect(result.total).to.equal(109.85);
expect(result.basketTotal).to.equal(111.3);


// After go to payment promocode should work, till ORDER state
await TestPaymentSystem.getInstance();
const paymentMethod = (await PaymentMethod.find({}))[0];
await Order.check({id: order.id}, customer, false, address, paymentMethod.id);
await Order.payment({id: order.id});
result = await Order.findOne({id: order.id})
console.log(result,1)


// CLEAR PROMOCODE
await Order.applyPromotionCode({id: order.id}, null);
result = await Order.findOne({id: order.id})
console.log(result,2)

expect(result.discountTotal).to.equal(0);
expect(result.total).to.equal(111.3);
expect(result.state).to.equal("CART");



// NOT VALID PROMOCODE
await Order.applyPromotionCode({id: order.id}, "WINTER2024NHATRANG");
Expand Down

0 comments on commit 51cb099

Please sign in to comment.