import { WorkTime } from "@webresto/worktime"; import ORM from "../interfaces/ORM"; import { ORMModel } from "../interfaces/ORMModel"; import { OptionalAll, RequiredField } from "../interfaces/toolsTS"; import hashCode from "../libs/hashCode"; import { Adapter } from "../adapters"; import { IconfigDiscount } from "../interfaces/ConfigDiscount"; import { someInArray } from "../libs/stringsInArray"; import PromotionCode from "../models/PromotionCode"; import { v4 as uuid } from "uuid"; // import Decimal from "decimal.js"; // sails.on("lifted", function () { // setInterval(async function () { // checkMaintenance(); // }, CHECK_INTERVAL); // }); let promotionRAM: Promotion[] = [] sails.on("lifted", async ()=>{ let promotions = await Promotion.find({ enable:true, createdByUser: true }) for(let i=0; i, | "id" | "configDiscount" | "isJoint" | "name" | "isPublic" | "description" | "concept" | "badge" | "isDeleted" | "createdByUser" | "externalId" >, ORM {} export interface PromotionRecord extends RequiredField< OptionalAll, | "id" | "configDiscount" | "isJoint" | "name" | "isPublic" | "description" | "concept" | "badge" | "isDeleted" | "createdByUser" | "externalId" >, ORM {} let Model = { async afterUpdate(record: PromotionRecord, cb: (err?: string) => void) { if (record.createdByUser) { // call recreation of discountHandler Adapter.getPromotionAdapter().recreateConfiguredPromotionHandler(record); } promotionRAM = await Promotion.find({enable: true, isDeleted: false}) cb(); }, async afterCreate(record: PromotionRecord, cb: (err?: string) => void) { if (record.createdByUser) { // call recreation of discountHandler Adapter.getPromotionAdapter().recreateConfiguredPromotionHandler(record); } promotionRAM = await Promotion.find({enable: true, isDeleted: false}) cb(); }, async afterDestroy(record: PromotionRecord, cb: (err?: string) => void) { // delete promotion in adapter Adapter.getPromotionAdapter().deletePromotion(record.id) promotionRAM = await Promotion.find({enable: true, isDeleted: false}) cb(); }, beforeUpdate(init: Promotion, cb: (err?: string) => void) { cb(); }, async beforeCreate(init: Promotion, cb: (err?: string) => void) { if (!init.id) { init.id = uuid(); } const PROMOTION_ENABLE_BY_DEFAULT = await Settings.get("PROMOTION_ENABLE_BY_DEFAULT"); // On creation, all promocodes are disabled. init.enable = (PROMOTION_ENABLE_BY_DEFAULT !== undefined) ? Boolean(PROMOTION_ENABLE_BY_DEFAULT) : process.env.NODE_ENV !== "production"; cb(); }, async createOrUpdate(values: Promotion): Promise { let sortOrder = values.sortOrder let isDeleted = values.isDeleted let enable = values.enable let worktime = values.worktime // Deleting user space variables try { delete(values.sortOrder); delete(values.isDeleted); delete(values.enable); delete(values.worktime); let hash = hashCode(JSON.stringify(values)); const promotion = await Promotion.findOne({ id: values.id }); if (!promotion) return Promotion.create({ hash, ...values, sortOrder, isDeleted, enable, worktime }).fetch(); if (hash === promotion.hash) { return promotion; } else { return (await Promotion.update({ id: values.id }, { hash, ...values }).fetch())[0]; } } catch (error) { sails.log.error(error); throw new Error(error) } }, getAllByConcept(concept: string[]): PromotionRecord[] { if(typeof concept === "string") concept = [concept]; if(!concept) concept = ["origin"]; const promotionAdapter = Adapter.getPromotionAdapter() if (!concept.length) throw "concept is required"; let activePromotionIds = promotionAdapter.getActivePromotionsIds() if(concept[0] === ""){ let filteredRAM = promotionRAM.filter(promotion => (promotion.concept[0] === undefined || promotion.concept[0] === "") && someInArray(promotion.id,activePromotionIds)) return filteredRAM; } let filteredRAM = promotionRAM.filter(promotion => someInArray(promotion.concept,concept) || (promotion.concept[0] === undefined || promotion.concept[0] === "") && someInArray(promotion.id,activePromotionIds)) if (!filteredRAM) throw "Promotion with concept: " + concept + " not found"; return filteredRAM; }, }; module.exports = { primaryKey: "id", attributes: attributes, ...Model, }; declare global { const Promotion: typeof Model & ORMModel; } // function between(from: number, to: number, a: number): boolean { // return (!from && !to) || (!from && to >= a) || (!to && from < a) || (from < a && to >= a); // } // async function checkMaintenance(){ // const maintenance = await Maintenance.getActiveMaintenance(); // if (maintenance) { // emitter.emit("core:maintenance-enabled", maintenance); // } else { // emitter.emit("core:maintenance-disabled"); // } // }