// initializers/myInitializer.ts import { Initializer, api } from "actionhero"; import { CronLog } from "../models/CronLog" import { OrderLog } from "../models/OrderLog" import * as crypto from "crypto"; export class exportLogs extends Initializer { constructor() { super(); this.name = "exportLogs"; this.loadPriority = 1001; this.startPriority = 1001; this.stopPriority = 1001; } async initialize() { api.exportLogs = api.exportLogs || {}; //order exportLogs ///////////// api.exportLogs.orders = {}; api.exportLogs.orders.createUpdateLog = async (logData) => { if(logData.id){ let log = await api.exportLogs.orders.getLog(logData.id, logData.shop); if(log){ log.status = logData.status; log.message = logData.message; return await log.save(); } } return await api.exportLogs.orders.createLog(logData); } api.exportLogs.orders.createLog = async (log) => { const response = await OrderLog.create(log); return response; } api.exportLogs.orders.getLog = async (id, shop) => { const orderLog = await OrderLog.findByPk(id); return orderLog.shop == shop && orderLog; } api.exportLogs.orders.list = async ( shop, where, limit, offset, sort ) => { const whereWithShop = { ...where, shop } console.log(whereWithShop); const order = sort || [ ['updatedAt', 'DESC'] ] return await OrderLog.findAndCountAll({where:whereWithShop, order, limit, offset}); } // cron exportLogs ///////////// api.exportLogs.cron = {}; api.exportLogs.cron.createUpdateLog = async (logData) => { if(logData.id){ let log = await api.exportLogs.orders.getLog(logData.id, logData.shop); if(log){ log.message = logData.message; return await log.save(); } } return await api.exportLogs.orders.createLog(logData); } api.exportLogs.cron.createLog = async (log) => { const response = await CronLog.create(log); return response; } api.exportLogs.cron.getLog = async (id, shop) => { const cronLog = await CronLog.findByPk(id); return cronLog.shop == shop && cronLog; } api.exportLogs.cron.list = async (shop, where, limit, offset, sort ) => { const whereWithShop = { ...where, shop } const order = sort || [ ['updatedAt', 'DESC'] ] return await CronLog.findAndCountAll({where:whereWithShop, order, limit, offset}); } } }