// define a single task in a file import { Task, api, task } from "actionhero"; import moment from 'moment' import { join, basename } from "path"; import { promises as fs } from 'fs'; import { GraphQLClient } from 'graphql-request'; export class ImportStockFile extends Task { constructor() { super(); this.name = "importStockFile"; this.description = "I export a specific retail invoice for a shopify order"; this.frequency = 0; this.queue = "importExport"; this.middleware = []; this.inputs = { file: { required: true}, shop: { required: true}, cronId: {required: false} }; } async run(params) { const {file, cronId, shop} = params; const dir = api.imports.getShopImportDir(shop); const path = join(dir, file); const processingPath = join(dir, "processing", file); const errorPath = join(dir, "error", file); const archivePath = join(dir, "archive", file); let log = await api.exportLogs.orders.createLog({ orderId: file + "_" + moment().valueOf(), cronId: cronId, type: "import:stock", shop: shop, status: "started" }); try{ await fs.rename(path, processingPath); await api.imports.processFile(processingPath, shop); await fs.rename(processingPath, archivePath); log.message = archivePath log.status = "success" api.exportLogs.orders.createUpdateLog(log); }catch(e){ api.log(e.stack, "error"); await fs.rename(processingPath, errorPath); await fs.appendFile(errorPath, e.stack); log.message = e.stack log.status = "error" api.exportLogs.orders.createUpdateLog(log); // // await task.enqueue( // "sendEmail", // { email: "ryan@43webstudio.com", // subject: `Stock ${path} errored during import on ${shop}.`, // message: e.stack}, // "default" // ); } } getDateString(){ return moment().format(); } }