@cloudcommerce/app-tiny-erp 0.0.59 → 0.0.60
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +32 -6
- package/lib/event-to-tiny.js +111 -0
- package/lib/event-to-tiny.js.map +1 -0
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -0
- package/lib/integration/after-tiny-queue.js +79 -0
- package/lib/integration/after-tiny-queue.js.map +1 -0
- package/lib/integration/export-order-to-tiny.js +81 -0
- package/lib/integration/export-order-to-tiny.js.map +1 -0
- package/lib/integration/export-product-to-tiny.js +58 -0
- package/lib/integration/export-product-to-tiny.js.map +1 -0
- package/lib/integration/helpers/format-tiny-date.js +7 -0
- package/lib/integration/helpers/format-tiny-date.js.map +1 -0
- package/lib/integration/import-order-from-tiny.js +94 -0
- package/lib/integration/import-order-from-tiny.js.map +1 -0
- package/lib/integration/import-product-from-tiny.js +169 -0
- package/lib/integration/import-product-from-tiny.js.map +1 -0
- package/lib/integration/parsers/order-from-tiny.js +46 -0
- package/lib/integration/parsers/order-from-tiny.js.map +1 -0
- package/lib/integration/parsers/order-to-tiny.js +193 -0
- package/lib/integration/parsers/order-to-tiny.js.map +1 -0
- package/lib/integration/parsers/product-from-tiny.js +199 -0
- package/lib/integration/parsers/product-from-tiny.js.map +1 -0
- package/lib/integration/parsers/product-to-tiny.js +129 -0
- package/lib/integration/parsers/product-to-tiny.js.map +1 -0
- package/lib/integration/parsers/status-from-tiny.js +34 -0
- package/lib/integration/parsers/status-from-tiny.js.map +1 -0
- package/lib/integration/parsers/status-to-tiny.js +39 -0
- package/lib/integration/parsers/status-to-tiny.js.map +1 -0
- package/lib/integration/post-tiny-erp.js +47 -0
- package/lib/integration/post-tiny-erp.js.map +1 -0
- package/lib/tiny-erp.js +17 -0
- package/lib/tiny-erp.js.map +1 -0
- package/lib/tiny-webhook.js +135 -0
- package/lib/tiny-webhook.js.map +1 -0
- package/package.json +13 -6
- package/src/event-to-tiny.ts +129 -0
- package/src/index.ts +1 -0
- package/src/integration/after-tiny-queue.ts +80 -0
- package/src/integration/export-order-to-tiny.ts +86 -0
- package/src/integration/export-product-to-tiny.ts +60 -0
- package/src/integration/helpers/format-tiny-date.ts +6 -0
- package/src/integration/import-order-from-tiny.ts +104 -0
- package/src/integration/import-product-from-tiny.ts +174 -0
- package/src/integration/parsers/order-from-tiny.ts +49 -0
- package/src/integration/parsers/order-to-tiny.ts +205 -0
- package/src/integration/parsers/product-from-tiny.ts +215 -0
- package/src/integration/parsers/product-to-tiny.ts +138 -0
- package/src/integration/parsers/status-from-tiny.ts +35 -0
- package/src/integration/parsers/status-to-tiny.ts +42 -0
- package/src/integration/post-tiny-erp.ts +52 -0
- package/src/tiny-erp.ts +23 -0
- package/src/tiny-webhook.ts +143 -0
- package/src/firebase.ts +0 -0
package/src/tiny-erp.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/* eslint-disable import/prefer-default-export */
|
|
2
|
+
|
|
3
|
+
import '@cloudcommerce/firebase/lib/init';
|
|
4
|
+
// eslint-disable-next-line import/no-unresolved
|
|
5
|
+
import { onRequest } from 'firebase-functions/v2/https';
|
|
6
|
+
import config from '@cloudcommerce/firebase/lib/config';
|
|
7
|
+
import {
|
|
8
|
+
createAppEventsFunction,
|
|
9
|
+
ApiEventHandler,
|
|
10
|
+
} from '@cloudcommerce/firebase/lib/helpers/pubsub';
|
|
11
|
+
import handleApiEvent from './event-to-tiny';
|
|
12
|
+
import handleTinyWebhook from './tiny-webhook';
|
|
13
|
+
|
|
14
|
+
const { httpsFunctionOptions } = config.get();
|
|
15
|
+
|
|
16
|
+
export const tinyErpOnApiEvent = createAppEventsFunction(
|
|
17
|
+
'tinyErp',
|
|
18
|
+
handleApiEvent as ApiEventHandler,
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
export const tinyErpWebhook = onRequest(httpsFunctionOptions, (req, res) => {
|
|
22
|
+
handleTinyWebhook(req, res);
|
|
23
|
+
});
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import type { Request, Response } from 'firebase-functions';
|
|
2
|
+
import { firestore } from 'firebase-admin';
|
|
3
|
+
import logger from 'firebase-functions/lib/logger';
|
|
4
|
+
import api from '@cloudcommerce/api';
|
|
5
|
+
import config from '@cloudcommerce/firebase/lib/config';
|
|
6
|
+
import updateAppData from '@cloudcommerce/firebase/lib/helpers/update-app-data';
|
|
7
|
+
import importProduct from './integration/import-product-from-tiny';
|
|
8
|
+
import importOrder from './integration/import-order-from-tiny';
|
|
9
|
+
import afterQueue from './integration/after-tiny-queue';
|
|
10
|
+
|
|
11
|
+
export default async (req: Request, res: Response) => {
|
|
12
|
+
const tinyToken = req.query.token;
|
|
13
|
+
if (typeof tinyToken === 'string' && tinyToken && req.body) {
|
|
14
|
+
const { dados, tipo } = req.body;
|
|
15
|
+
if (dados) {
|
|
16
|
+
/*
|
|
17
|
+
TODO: Check Tiny server IPs
|
|
18
|
+
const clientIp = req.get('x-forwarded-for') || req.connection.remoteAddress
|
|
19
|
+
*/
|
|
20
|
+
const { apps: { tinyErp: { appId } } } = config.get();
|
|
21
|
+
const applicationId = req.query._id;
|
|
22
|
+
const appEndpoint = applicationId && typeof applicationId === 'string'
|
|
23
|
+
? `applications/${applicationId}`
|
|
24
|
+
: `applications/app_id:${appId}`;
|
|
25
|
+
const application = (await api.get(appEndpoint as 'applications/id')).data;
|
|
26
|
+
const appData: Record<string, any> = {
|
|
27
|
+
...application.data,
|
|
28
|
+
...application.hidden_data,
|
|
29
|
+
};
|
|
30
|
+
if (appData.tiny_api_token !== tinyToken) {
|
|
31
|
+
return res.sendStatus(401);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (dados.idVendaTiny) {
|
|
35
|
+
let orderNumbers = appData.___importation?.order_numbers;
|
|
36
|
+
if (!Array.isArray(orderNumbers)) {
|
|
37
|
+
orderNumbers = [];
|
|
38
|
+
}
|
|
39
|
+
const orderNumber = `id:${dados.idVendaTiny}`;
|
|
40
|
+
if (!orderNumbers.includes(orderNumber)) {
|
|
41
|
+
logger.info(`> Tiny webhook: order ${orderNumber}`);
|
|
42
|
+
const saveToQueue = () => {
|
|
43
|
+
orderNumbers.push(orderNumber);
|
|
44
|
+
logger.info(`> Order numbers: ${JSON.stringify(orderNumbers)}`);
|
|
45
|
+
return updateAppData(application, {
|
|
46
|
+
___importation: {
|
|
47
|
+
...appData.___importation,
|
|
48
|
+
order_numbers: orderNumbers,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const queueEntry = {
|
|
54
|
+
nextId: orderNumber,
|
|
55
|
+
isNotQueued: true,
|
|
56
|
+
};
|
|
57
|
+
try {
|
|
58
|
+
const payload = await importOrder({}, queueEntry);
|
|
59
|
+
await afterQueue(queueEntry, appData, application, payload);
|
|
60
|
+
} catch (e) {
|
|
61
|
+
await saveToQueue();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (tipo === 'produto' || tipo === 'estoque') {
|
|
67
|
+
if ((dados.id || dados.idProduto) && (dados.codigo || dados.sku)) {
|
|
68
|
+
const nextId = String(dados.skuMapeamento || dados.sku || dados.codigo);
|
|
69
|
+
const tinyStockUpdate = {
|
|
70
|
+
ref: `${nextId}`,
|
|
71
|
+
tipo,
|
|
72
|
+
produto: {
|
|
73
|
+
id: dados.idProduto,
|
|
74
|
+
codigo: dados.sku,
|
|
75
|
+
...dados,
|
|
76
|
+
},
|
|
77
|
+
updatedAt: firestore.Timestamp.fromDate(new Date()),
|
|
78
|
+
};
|
|
79
|
+
logger.info(`> Tiny webhook: ${nextId} => ${tinyStockUpdate.produto.saldo}`);
|
|
80
|
+
const saveToQueue = () => {
|
|
81
|
+
let skus = appData.___importation && appData.___importation.skus;
|
|
82
|
+
if (!Array.isArray(skus)) {
|
|
83
|
+
skus = [];
|
|
84
|
+
}
|
|
85
|
+
if (!skus.includes(nextId)) {
|
|
86
|
+
return firestore().collection('tinyErpStockUpdates').add(tinyStockUpdate)
|
|
87
|
+
.then(() => {
|
|
88
|
+
skus.push(nextId);
|
|
89
|
+
logger.info(`> SKUs: ${JSON.stringify(skus)}`);
|
|
90
|
+
return updateAppData(application, {
|
|
91
|
+
___importation: {
|
|
92
|
+
...appData.___importation,
|
|
93
|
+
skus,
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
return Promise.resolve(null);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const queueEntry = {
|
|
102
|
+
nextId,
|
|
103
|
+
tinyStockUpdate,
|
|
104
|
+
isNotQueued: true,
|
|
105
|
+
};
|
|
106
|
+
try {
|
|
107
|
+
const payload = await importProduct({}, queueEntry, appData, false, true);
|
|
108
|
+
await afterQueue(queueEntry, appData, application, payload);
|
|
109
|
+
} catch (e) {
|
|
110
|
+
await saveToQueue();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (tipo === 'produto') {
|
|
116
|
+
const mapeamentos: any[] = [];
|
|
117
|
+
const parseTinyItem = (tinyItem) => {
|
|
118
|
+
if (tinyItem) {
|
|
119
|
+
const {
|
|
120
|
+
idMapeamento,
|
|
121
|
+
id,
|
|
122
|
+
codigo,
|
|
123
|
+
sku,
|
|
124
|
+
} = tinyItem;
|
|
125
|
+
mapeamentos.push({
|
|
126
|
+
idMapeamento: idMapeamento || id,
|
|
127
|
+
skuMapeamento: codigo || sku,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
parseTinyItem(dados);
|
|
132
|
+
if (Array.isArray(dados.variacoes)) {
|
|
133
|
+
dados.variacoes.forEach((variacao) => {
|
|
134
|
+
parseTinyItem(variacao.id ? variacao : variacao.variacao);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return res.status(200).send(mapeamentos);
|
|
138
|
+
}
|
|
139
|
+
return res.sendStatus(200);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return res.sendStatus(403);
|
|
143
|
+
};
|
package/src/firebase.ts
DELETED
|
File without changes
|