Skip to content

Commit

Permalink
add fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Xziy committed May 4, 2024
1 parent 5ef44b3 commit aaba593
Show file tree
Hide file tree
Showing 22 changed files with 84 additions and 50 deletions.
18 changes: 9 additions & 9 deletions adapters/promotion/default/configuredPromotion.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
const AbstractPromotion_1 = __importDefault(require("../AbstractPromotion"));
const findModelInstance_1 = __importDefault(require("../../../libs/findModelInstance"));
const decimal_js_1 = __importDefault(require("decimal.js"));
const stringsInArray_1 = require("../../../libs/stringsInArray");
const someInArray_1 = require("../../../libs/someInArray");
class ConfiguredPromotion extends AbstractPromotion_1.default {
constructor(promotion, config) {
super();
Expand All @@ -32,7 +32,7 @@ class ConfiguredPromotion extends AbstractPromotion_1.default {
}
condition(arg) {
if ((0, findModelInstance_1.default)(arg) === "Order" && (this.concept[0] === undefined || this.concept[0] === "")
? true : (0, stringsInArray_1.stringsInArray)(arg.concept, this.concept)) {
? true : (0, someInArray_1.someInArray)(arg.concept, this.concept)) {
let order = arg;
// TODO: if order.dishes type number[]
let orderDishes = order.dishes;
Expand All @@ -43,12 +43,12 @@ class ConfiguredPromotion extends AbstractPromotion_1.default {
return false;
}
if ((0, findModelInstance_1.default)(arg) === "Dish" && (this.concept[0] === undefined || this.concept[0] === "") ? true :
(0, stringsInArray_1.stringsInArray)(arg.concept, this.concept)) {
return (0, stringsInArray_1.stringsInArray)(arg.id, this.config.dishes);
(0, someInArray_1.someInArray)(arg.concept, this.concept)) {
return (0, someInArray_1.someInArray)(arg.id, this.config.dishes);
}
if ((0, findModelInstance_1.default)(arg) === "Group" && (this.concept[0] === undefined || this.concept[0] === "") ? true :
(0, stringsInArray_1.stringsInArray)(arg.concept, this.concept)) {
return (0, stringsInArray_1.stringsInArray)(arg.id, this.config.groups);
(0, someInArray_1.someInArray)(arg.concept, this.concept)) {
return (0, someInArray_1.someInArray)(arg.id, this.config.groups);
}
return false;
}
Expand Down Expand Up @@ -106,11 +106,11 @@ class ConfiguredPromotion extends AbstractPromotion_1.default {
continue;
}
if ((this.concept[0] === undefined || this.concept[0] === "") ?
false : !(0, stringsInArray_1.stringsInArray)(orderDish.dish.concept, this.concept)) {
false : !(0, someInArray_1.someInArray)(orderDish.dish.concept, this.concept)) {
continue;
}
let checkDishes = (0, stringsInArray_1.stringsInArray)(orderDish.dish.id, this.config.dishes);
let checkGroups = (0, stringsInArray_1.stringsInArray)(orderDish.dish.parentGroup, this.config.groups);
let checkDishes = (0, someInArray_1.someInArray)(orderDish.dish.id, this.config.dishes);
let checkGroups = (0, someInArray_1.someInArray)(orderDish.dish.parentGroup, this.config.groups);
if (!checkDishes || !checkGroups)
continue;
if (this.configDiscount.discountType === "flat") {
Expand Down
2 changes: 1 addition & 1 deletion adapters/rms/RMSAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class RMSAdapter {
const inactiveGroups = await Group.find({ isDeleted: true });
const inactiveGroupIds = inactiveGroups.map((group) => group.id);
// Delete all dishes in inactive groups or not in the updated list
await Dish.update({ where: { or: [{ parentGroup: { in: inactiveGroupIds } }, { rmsId: { "!=": allProductIds } }, { parentGroup: null }] } }, { isDeleted: true });
await Dish.update({ where: { or: [{ parentGroup: { in: inactiveGroupIds } }, { rmsId: { "!=": allProductIds } }, { parentGroup: null }] } }, { isDeleted: true }).fetch();
emitter.emit("rms-sync:after-sync-products");
}
return resolve();
Expand Down
2 changes: 1 addition & 1 deletion adapters/rms/RMSAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export default abstract class RMSAdapter {
const inactiveGroups = await Group.find({ isDeleted: true });
const inactiveGroupIds = inactiveGroups.map((group) => group.id);
// Delete all dishes in inactive groups or not in the updated list
await Dish.update({ where: { or: [{ parentGroup: { in: inactiveGroupIds } }, { rmsId: { "!=": allProductIds } }, { parentGroup: null }] } }, { isDeleted: true });
await Dish.update({ where: { or: [{ parentGroup: { in: inactiveGroupIds } }, { rmsId: { "!=": allProductIds } }, { parentGroup: null }] } }, { isDeleted: true }).fetch();
emitter.emit("rms-sync:after-sync-products");
}
return resolve();
Expand Down
7 changes: 7 additions & 0 deletions libs/AwaitEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ class AwaitEmitter {
res.push(new HandlerResponse(subscriber.id, null, null, true));
}
};
/**
*
* TODO:
* Add TTL for emmiter
*
* To know how many functions were performed before it
*/
const execHandler = async function () {
const now = new Date();
try {
Expand Down
8 changes: 7 additions & 1 deletion libs/AwaitEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,13 @@ export default class AwaitEmitter {
res.push(new HandlerResponse(subscriber.id, null, null, true));
}
};

/**
*
* TODO:
* Add TTL for emmiter
*
* To know how many functions were performed before it
*/
const execHandler = async function () {
const now = new Date();
try {
Expand Down
1 change: 1 addition & 0 deletions libs/stringsInArray.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export declare function stringsInArray(check: string[] | string, array: string[]): boolean;
export declare function someInArray(check: string[] | string, array: string[]): boolean;
24 changes: 22 additions & 2 deletions libs/stringsInArray.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.stringsInArray = void 0;
exports.someInArray = exports.stringsInArray = void 0;
function stringsInArray(check, array) {
// If check is an array of strings
if (Array.isArray(check)) {
// Check each string in the check array
for (const str of check) {
// If any of the strings is not present in the array array, return false
if (!array.includes(str)) {
return false;
}
}
// If all strings in the check array are present in the array array, return true
return true;
}
// If check is a string
else {
// Check if the check string is present in the array array
return array.includes(check);
}
}
exports.stringsInArray = stringsInArray;
function someInArray(check, array) {
if (typeof check === 'string') {
check = [check];
}
return array.some((e) => check.includes(e));
}
exports.stringsInArray = stringsInArray;
exports.someInArray = someInArray;
2 changes: 1 addition & 1 deletion models/OneTimePassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ let Model = {
},
async check(login, password) {
// Clean expired
await OneTimePassword.destroy({ expires: { "<": Date.now() } });
await OneTimePassword.destroy({ expires: { "<": Date.now() } }).fetch();
if (process.env.NODE_ENV !== "production" && process.env.DEFAULT_OTP === password) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion models/OneTimePassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ let Model = {

async check(login: string, password: string): Promise<boolean> {
// Clean expired
await OneTimePassword.destroy({expires: { "<": Date.now() }})
await OneTimePassword.destroy({expires: { "<": Date.now() }}).fetch()

if(process.env.NODE_ENV !== "production" && process.env.DEFAULT_OTP === password) {
return true
Expand Down
10 changes: 5 additions & 5 deletions models/Order.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ let Model = {
await OrderDish.update({ id: get.id }, { amount: get.amount }).fetch();
}
else {
await OrderDish.destroy({ id: get.id });
await OrderDish.destroy({ id: get.id }).fetch();
sails.log.info("destroy", get.id);
}
await Order.next(order.id, "CART");
Expand Down Expand Up @@ -687,7 +687,7 @@ let Model = {
// Custom emitters checks
const results = await emitter.emit("core:order-check", order, customer, isSelfService, address, paymentMethodId);
delete (order.dishes);
await Order.update({ id: order.id }, { ...order });
await Order.update({ id: order.id }, { ...order }).fetch();
////////////////////
// CHECKOUT COUNTING
try {
Expand Down Expand Up @@ -759,7 +759,7 @@ let Model = {
sails.log.silly("Order > check > after wait general emitter", order, results);
emitter.emit("core:order-after-check-counting", order);
delete (order.dishes);
await Order.update({ id: order.id }, { ...order });
await Order.update({ id: order.id }, { ...order }).fetch();
/** The check can pass without listeners, because the check itself is minimal
* has basic checks. And is self-sufficient, but
* is still set by default so all checks must be passed
Expand Down Expand Up @@ -1043,7 +1043,7 @@ let Model = {
if (!dish) {
sails.log.error("Dish with id " + orderDish.dish.id + " not found!");
emitter.emit("core:order-return-full-order-destroy-orderdish", dish, order);
await OrderDish.destroy({ id: orderDish.id });
await OrderDish.destroy({ id: orderDish.id }).fetch();
continue;
}
if (dish.balance === -1 ? false : dish.balance < orderDish.amount) {
Expand Down Expand Up @@ -1146,7 +1146,7 @@ let Model = {
}
catch (e) {
sails.log.error("Order > count > iterate orderDish error", e);
await OrderDish.destroy({ id: orderDish.id });
await OrderDish.destroy({ id: orderDish.id }).fetch();
uniqueDishes -= 1;
continue;
}
Expand Down
10 changes: 5 additions & 5 deletions models/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ let Model = {
if (get.amount > 0) {
await OrderDish.update({ id: get.id }, { amount: get.amount }).fetch();
} else {
await OrderDish.destroy({ id: get.id });
await OrderDish.destroy({ id: get.id }).fetch();
sails.log.info("destroy", get.id);
}

Expand Down Expand Up @@ -856,7 +856,7 @@ let Model = {
const results = await emitter.emit("core:order-check", order, customer, isSelfService, address, paymentMethodId);

delete (order.dishes);
await Order.update({ id: order.id }, { ...order });
await Order.update({ id: order.id }, { ...order }).fetch();

////////////////////
// CHECKOUT COUNTING
Expand Down Expand Up @@ -944,7 +944,7 @@ let Model = {
emitter.emit("core:order-after-check-counting", order);

delete (order.dishes);
await Order.update({ id: order.id }, { ...order });
await Order.update({ id: order.id }, { ...order }).fetch();


/** The check can pass without listeners, because the check itself is minimal
Expand Down Expand Up @@ -1273,7 +1273,7 @@ let Model = {
if (!dish) {
sails.log.error("Dish with id " + orderDish.dish.id + " not found!");
emitter.emit("core:order-return-full-order-destroy-orderdish", dish, order);
await OrderDish.destroy({ id: orderDish.id });
await OrderDish.destroy({ id: orderDish.id }).fetch();
continue;
}

Expand Down Expand Up @@ -1394,7 +1394,7 @@ let Model = {
totalWeight = totalWeight.plus(orderDish.totalWeight);
} catch (e) {
sails.log.error("Order > count > iterate orderDish error", e);
await OrderDish.destroy({ id: orderDish.id });
await OrderDish.destroy({ id: orderDish.id }).fetch();
uniqueDishes -= 1;
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion models/PaymentDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ let Model = {
};
emitter.emit("core:payment-document-before-create", payment);
try {
await PaymentDocument.create(payment);
await PaymentDocument.create(payment).fetch();
}
catch (e) {
sails.log.error("Error in paymentAdapter.createPayment :", e);
Expand Down
2 changes: 1 addition & 1 deletion models/PaymentDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ let Model = {

emitter.emit("core:payment-document-before-create", payment);
try {
await PaymentDocument.create(payment as PaymentDocument);
await PaymentDocument.create(payment as PaymentDocument).fetch();
} catch (e) {
sails.log.error("Error in paymentAdapter.createPayment :", e);
throw {
Expand Down
8 changes: 4 additions & 4 deletions models/Promotion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const hashCode_1 = __importDefault(require("../libs/hashCode"));
const adapters_1 = require("../adapters");
const stringsInArray_1 = require("../libs/stringsInArray");
const someInArray_1 = require("../libs/someInArray");
const uuid_1 = require("uuid");
// import Decimal from "decimal.js";
// sails.on("lifted", function () {
Expand Down Expand Up @@ -165,11 +165,11 @@ let Model = {
let activePromotionIds = promotionAdapter.getActivePromotionsIds();
if (concept[0] === "") {
let filteredRAM = promotionRAM.filter(promotion => (promotion.concept[0] === undefined || promotion.concept[0] === "")
&& (0, stringsInArray_1.stringsInArray)(promotion.id, activePromotionIds));
&& (0, someInArray_1.someInArray)(promotion.id, activePromotionIds));
return filteredRAM;
}
let filteredRAM = promotionRAM.filter(promotion => (0, stringsInArray_1.stringsInArray)(promotion.concept, concept) || (promotion.concept[0] === undefined || promotion.concept[0] === "")
&& (0, stringsInArray_1.stringsInArray)(promotion.id, activePromotionIds));
let filteredRAM = promotionRAM.filter(promotion => (0, someInArray_1.someInArray)(promotion.concept, concept) || (promotion.concept[0] === undefined || promotion.concept[0] === "")
&& (0, someInArray_1.someInArray)(promotion.id, activePromotionIds));
if (!filteredRAM)
throw "Promotion with concept: " + concept + " not found";
return filteredRAM;
Expand Down
4 changes: 2 additions & 2 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ let Model = {
throw `OTP checks failed`;
}
}
await UserDevice.update({ user: userId }, { isLoggedIn: false });
User.update({ id: userId }, { isDeleted: true });
await UserDevice.update({ user: userId }, { isLoggedIn: false }).fetch();
User.update({ id: userId }, { isDeleted: true }).fetch();
},
/**
* Returns phone string by user criteria
Expand Down
4 changes: 2 additions & 2 deletions models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ let Model = {
throw `OTP checks failed`
}
}
await UserDevice.update({ user: userId }, { isLoggedIn: false });
User.update({id: userId}, {isDeleted: true});
await UserDevice.update({ user: userId }, { isLoggedIn: false }).fetch();
User.update({id: userId}, {isDeleted: true}).fetch();
},

/**
Expand Down
2 changes: 1 addition & 1 deletion models/UserDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ let Model = {
},
/** Method set lastActivity for a device */
async setActivity(criteria, client = {}) {
await UserDevice.update(criteria, client);
await UserDevice.update(criteria, client).fetch();
},
async checkSession(sessionId, userId, client = {}) {
let ud = await UserDevice.findOne({ sessionId: sessionId });
Expand Down
2 changes: 1 addition & 1 deletion models/UserDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ let Model = {

/** Method set lastActivity for a device */
async setActivity(criteria: CriteriaQuery<UserDevice>, client: { lastIP?: string , userAgent?: string } = {}): Promise<void> {
await UserDevice.update(criteria, client);
await UserDevice.update(criteria, client).fetch();
},

async checkSession(sessionId: string, userId: string, client: { lastIP?: string , userAgent?: string } = {}): Promise<boolean> {
Expand Down
2 changes: 1 addition & 1 deletion models/UserOrderHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let Model = {
}
// Count orders
let count = await UserOrderHistory.find({ user: user.id });
await User.update({ id: user.id }, { orderCount: count.length });
await User.update({ id: user.id }, { orderCount: count.length }).fetch();
}
};
module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion models/UserOrderHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ let Model = {

// Count orders
let count = await UserOrderHistory.find({user: user.id})
await User.update({id: user.id}, {orderCount: count.length});
await User.update({id: user.id}, {orderCount: count.length}).fetch();
}
};

Expand Down
12 changes: 6 additions & 6 deletions test/generators/discount.generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
const faker_1 = __importDefault(require("faker"));
const findModelInstance_1 = __importDefault(require("../../libs/findModelInstance"));
const configuredPromotion_1 = __importDefault(require("../../adapters/promotion/default/configuredPromotion"));
const stringsInArray_1 = require("../../libs/stringsInArray");
const someInArray_1 = require("../../libs/someInArray");
const decimal_js_1 = __importDefault(require("decimal.js"));
var autoincrement = 0;
function discountGenerator(config = {
Expand Down Expand Up @@ -48,7 +48,7 @@ function discountGenerator(config = {
},
// productCategoryDiscounts: undefined,
condition: function (arg) {
if ((0, findModelInstance_1.default)(arg) === "Order" && (0, stringsInArray_1.stringsInArray)(arg.concept, this.concept)) {
if ((0, findModelInstance_1.default)(arg) === "Order" && (0, someInArray_1.someInArray)(arg.concept, this.concept)) {
let order = arg;
// TODO: if order.dishes type number[]
let orderDishes = order.dishes;
Expand All @@ -58,14 +58,14 @@ function discountGenerator(config = {
return true;
return false;
}
if ((0, findModelInstance_1.default)(arg) === "Dish" && (0, stringsInArray_1.stringsInArray)(arg.concept, this.concept)) {
return (0, stringsInArray_1.stringsInArray)(arg.id, this.configDiscount.dishes);
if ((0, findModelInstance_1.default)(arg) === "Dish" && (0, someInArray_1.someInArray)(arg.concept, this.concept)) {
return (0, someInArray_1.someInArray)(arg.id, this.configDiscount.dishes);
// if(this.config.dishes.includes(arg.id)){
// return true;
// }
}
if ((0, findModelInstance_1.default)(arg) === "Group" && (0, stringsInArray_1.stringsInArray)(arg.concept, this.concept)) {
return (0, stringsInArray_1.stringsInArray)(arg.id, this.configDiscount.groups);
if ((0, findModelInstance_1.default)(arg) === "Group" && (0, someInArray_1.someInArray)(arg.concept, this.concept)) {
return (0, someInArray_1.someInArray)(arg.id, this.configDiscount.groups);
// if(this.config.groups.includes(arg.id)){
// return true;
// }
Expand Down
Loading

0 comments on commit aaba593

Please sign in to comment.