From 045b67c8b4f3a969fd3b19b017ba1dfb4d9b29ba Mon Sep 17 00:00:00 2001 From: Matheus Reis <35343551+matheusgnreis@users.noreply.github.com> Date: Fri, 12 Jul 2024 02:10:18 -0300 Subject: [PATCH] chore: update export order --- functions/lib/ux-group/export-order.js | 0 functions/lib/ux-group/send-waiting-orders.js | 107 ++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 functions/lib/ux-group/export-order.js create mode 100644 functions/lib/ux-group/send-waiting-orders.js diff --git a/functions/lib/ux-group/export-order.js b/functions/lib/ux-group/export-order.js new file mode 100644 index 0000000..e69de29 diff --git a/functions/lib/ux-group/send-waiting-orders.js b/functions/lib/ux-group/send-waiting-orders.js new file mode 100644 index 0000000..34c318f --- /dev/null +++ b/functions/lib/ux-group/send-waiting-orders.js @@ -0,0 +1,107 @@ +const { firestore } = require('firebase-admin') +const { setup } = require('@ecomplus/application-sdk') +const logger = require('firebase-functions/logger') +const getAppData = require('../store-api/get-app-data') +const exportOrder = require('./export-order') + +const listStoreIds = () => { + const storeIds = [] + const date = new Date() + date.setHours(date.getHours() - 24) + return firestore() + .collection('ecomplus_app_auth') + .where('updated_at', '>', firestore.Timestamp.fromDate(date)) + .get().then(querySnapshot => { + querySnapshot.forEach(documentSnapshot => { + const storeId = documentSnapshot.get('store_id') + if (storeIds.indexOf(storeId) === -1) { + storeIds.push(storeId) + } + }) + return storeIds + }) +} + +const fetchWaitingOrders = async ({ appSdk, storeId }) => { + const auth = await appSdk.getAuth(storeId) + return new Promise((resolve, reject) => { + getAppData({ appSdk, storeId, auth }) + .then(async (appData) => { + resolve() + if (storeId === 1024) { + appData.__order_settings = { + data: { + sender: { + fullName: 'Tia Sonia', + email: 'ecommerce@tiasonia.com.br', + document: '08385685000739', + ie: '206748880112', + address: { + postalCode: '06422120', + street: 'Avenida Gupê', + number: '10767', + neighborhood: 'Jardim Belval', + addressLine2: 'Galpões 15, 24 e 25', + city: 'Barueri', + state: 'SP', + country: 'BR' + } + }, + channel: 'ecommerce', + store: 'Tia Sônia' + } + } + } + const mandaeToken = appData.mandae_token + const mandaeOrderSettings = appData.__order_settings + if (mandaeToken && mandaeOrderSettings?.data) { + const d = new Date() + d.setDate(d.getDate() - 14) + const endpoint = '/orders.json' + + '?fields=_id,number,amount,fulfillment_status,shipping_lines' + + ',shipping_method_label,buyers' + + ',items.sku,items.name,items.final_price,items.price,items.quantity' + + '&shipping_lines.app.carrier=Ux' + + '&shipping_lines.tracking_codes.tag!=Ux' + + '&financial_status.current=paid' + + '&fulfillment_status.current=ready_for_shipping' + + `&updated_at>=${d.toISOString()}` + + '&sort=number' + + '&limit=200' + try { + const { response } = await appSdk.apiRequest(storeId, endpoint, 'GET') + const orders = response.data.result + for (let i = 0; i < orders.length; i++) { + const order = orders[i] + await exportOrder( + { appSdk, storeId, auth }, + { order, mandaeToken, mandaeOrderSettings } + ) + } + } catch (_err) { + if (_err.response) { + const err = new Error(`Failed exporting order for #${storeId}`) + logger.error(err, { + request: _err.config, + response: _err.response.data + }) + } else { + logger.error(_err) + } + } + } + }) + .catch(reject) + }) +} + +module.exports = context => setup(null, true, firestore()) + .then(appSdk => { + return listStoreIds().then(storeIds => { + const runAllStores = fn => storeIds + .sort(() => Math.random() - Math.random()) + .map(storeId => fn({ appSdk, storeId })) + return Promise.all(runAllStores(fetchWaitingOrders)) + }) + }) + .catch(logger.error)