// initializers/myInitializer.ts import { Initializer, api } from "actionhero"; import { ShopSetting } from "../models/ShopSetting" import * as crypto from "crypto"; export class shopSettings extends Initializer { constructor() { super(); this.name = "shopSettings"; this.loadPriority = 1001; this.startPriority = 1001; this.stopPriority = 1001; } async initialize() { api.shopSettings = api.shopSettings || {}; api.shopSettings.list = async (shop) => { return await ShopSetting.findAll({where:{shop:shop}}); } api.shopSettings.get = async (shop, key) => { return await ShopSetting.findAll({ where: {shop:shop, key:key} }); } api.shopSettings.getValue = async (shop, key) => { const setting = await api.shopSettings.get(shop, key); if(setting.length){ return setting[0].value; } } api.shopSettings.getInstalledShops = async () => { return await ShopSetting.findAll({ where: {key:"installed", value:true} }); } api.shopSettings.set = async (shop, key, value) => { const setting = await api.shopSettings.get(shop, key); if(setting.length){ setting[0].value = value; await setting[0].save(); }else{ await ShopSetting.create({ shop, key, value }) } return true; } api.shopSettings.setDefaults = async (shop) => { await api.shopSettings.set(shop, "installed", true); await api.shopSettings.set(shop, "webhooks", true); await api.shopSettings.set(shop, "exportLocation", "not used"); await api.shopSettings.set(shop, "exportPatern", "{{type}}{{order_id}}_{{date_time}}"); await api.shopSettings.set(shop, "exportInterval", 1440); const password = Math.random().toString(36).slice(-8); await api.shopSettings.set(shop, "ftp_password", password); return true; } api.shopSettings.removeSettings = async (shop) => { return await ShopSetting.destroy({ where: { shop: shop } }) } } }