import { Initializer, api, config, log } from "actionhero"; import * as fetch from "node-fetch"; import { RetailInvoiceConverter } from "../converters/RetailInvoiceConverter"; import { RentalReservationConverter } from "../converters/RentalReservationConverter"; import * as xml2json from 'xml2json'; import { promises as fs } from 'fs'; import { join } from "path"; export class exportOrders extends Initializer { constructor() { super(); this.name = "exportOrders"; this.loadPriority = 1002; this.startPriority = 1002; this.stopPriority = 1002; } async initialize() { api.exportOrders = { retailInvoiceConverter : new RetailInvoiceConverter(), rentalReservationConverter : new RentalReservationConverter(), }; api.exportOrders.getOrderData = async (orderId, store) => { const { apiCode } = config.shopifyMiddleware; const orderUrl = `https://${store}/admin/api/${apiCode}/orders/${orderId}.json`; const auth = await api.shopifyShops.getShop(store); const response = await fetch(orderUrl, { method: 'GET', headers: { "X-Shopify-Access-Token": auth.token, 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' } }); const responseData = await response.json(); return responseData; } api.exportOrders.getProductData = async (productId, store, fields) => { const { apiCode } = config.shopifyMiddleware; const orderUrl = `https://${store}/admin/api/2020-04/products/${productId}.json` + (fields ? `?fields=${fields}` : ""); const auth = await api.shopifyShops.getShop(store); const response = await fetch(orderUrl, { method: 'GET', headers: { "X-Shopify-Access-Token": auth.token, 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' } }); const responseData = await response.json(); return responseData; } api.exportOrders.getProductMetaFields = async (productId, store) => { const { apiCode } = config.shopifyMiddleware; const orderUrl = `https://${store}/admin/api/2020-04/products/${productId}/metafields.json`; const auth = await api.shopifyShops.getShop(store); const response = await fetch(orderUrl, { method: 'GET', headers: { "X-Shopify-Access-Token": auth.token, 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' } }); const responseData = await response.json(); return responseData; } api.exportOrders.markOrder = async (order, status, store) => { const tags = order.tags ? order.tags.split(",") : []; const filteredTag = tags.filter((tag) =>{ return tag.indexOf("EZRent") == -1; }); filteredTag.push("EZRent:" + status); const { apiCode } = config.shopifyMiddleware; const orderUrl = `https://${store}/admin/api/${apiCode}/orders/${order.id}.json`; const auth = await api.shopifyShops.getShop(store); const data = { "order": { "id": order.id, "tags": filteredTag.join(",") } } const response = await fetch(orderUrl, { method: 'PUT', body: JSON.stringify(data), headers: { "X-Shopify-Access-Token": auth.token, 'Content-Type': 'application/json;charset=UTF-8' } }); const responseData = await response.json(); return responseData; } api.exportOrders.getOrders = async ( store, params ) => { const { apiCode } = config.shopifyMiddleware; const ordersUrl = `https://${store}/admin/api/${apiCode}/orders.json?` + api.utils.parameterize(params); const auth = await api.shopifyShops.getShop(store); const response = await fetch(ordersUrl, { method: 'GET', headers: { "X-Shopify-Access-Token": auth.token, 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' } }); const responseData = await response.json(); return responseData.orders; } api.exportOrders.convertRetailInvoice = (order) => { const data = api.exportOrders.retailInvoiceConverter.shopify2Wintersteiger([order]); return data; } api.exportOrders.convertRentalReservation = (order) => { const data = api.exportOrders.rentalReservationConverter.shopify2Wintersteiger([order]); return data; } api.exportOrders.saveXMLFile = async (fileName, data, shop) => { const xmlString = xml2json.toXml(data); const { exportPath } = config.shopifyMiddleware; const dir = join(exportPath, shop.replace(".myshopify.com", "")); const dirExists = await api.utils.fs_IsFolder(dir); if (!dirExists){ await fs.mkdir(dir); } const filePath = join(dir, fileName); try { await fs.writeFile(filePath, xmlString); log(`${filePath} written to disk`); } catch (error){ log(error); } } } }