@cloudcommerce/app-tiny-erp 2.8.8 → 2.10.0

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.
Files changed (35) hide show
  1. package/lib/event-to-tiny.js.map +1 -1
  2. package/lib/integration/after-tiny-queue.js.map +1 -1
  3. package/lib/integration/export-order-to-tiny.js.map +1 -1
  4. package/lib/integration/export-product-to-tiny.js.map +1 -1
  5. package/lib/integration/import-order-from-tiny.js.map +1 -1
  6. package/lib/integration/import-product-from-tiny.js.map +1 -1
  7. package/lib/integration/parsers/order-from-tiny.js.map +1 -1
  8. package/lib/integration/parsers/order-to-tiny.js.map +1 -1
  9. package/lib/integration/parsers/product-from-tiny.js.map +1 -1
  10. package/lib/integration/parsers/product-to-tiny.js.map +1 -1
  11. package/lib/integration/parsers/status-from-tiny.js.map +1 -1
  12. package/lib/integration/parsers/status-to-tiny.js.map +1 -1
  13. package/lib/integration/post-tiny-erp.js.map +1 -1
  14. package/lib/tiny-webhook.js.map +1 -1
  15. package/package.json +12 -6
  16. package/.turbo/turbo-build.log +0 -5
  17. package/CHANGELOG.md +0 -1
  18. package/src/event-to-tiny.ts +0 -134
  19. package/src/index.ts +0 -1
  20. package/src/integration/after-tiny-queue.ts +0 -80
  21. package/src/integration/export-order-to-tiny.ts +0 -113
  22. package/src/integration/export-product-to-tiny.ts +0 -60
  23. package/src/integration/helpers/format-tiny-date.ts +0 -6
  24. package/src/integration/import-order-from-tiny.ts +0 -109
  25. package/src/integration/import-product-from-tiny.ts +0 -176
  26. package/src/integration/parsers/order-from-tiny.ts +0 -61
  27. package/src/integration/parsers/order-to-tiny.ts +0 -212
  28. package/src/integration/parsers/product-from-tiny.ts +0 -318
  29. package/src/integration/parsers/product-to-tiny.ts +0 -158
  30. package/src/integration/parsers/status-from-tiny.ts +0 -35
  31. package/src/integration/parsers/status-to-tiny.ts +0 -42
  32. package/src/integration/post-tiny-erp.ts +0 -52
  33. package/src/tiny-erp.ts +0 -26
  34. package/src/tiny-webhook.ts +0 -103
  35. package/tsconfig.json +0 -3
@@ -1,35 +0,0 @@
1
- import type { Orders } from '@cloudcommerce/types';
2
-
3
- export default (situacao: string) => {
4
- let financialStatus: Exclude<Orders['financial_status'], undefined>['current'] | undefined;
5
- let fulfillmentStatus: Exclude<Orders['fulfillment_status'], undefined>['current'] | undefined;
6
- switch (situacao) {
7
- case 'aprovado':
8
- financialStatus = 'paid';
9
- break;
10
- case 'preparando_envio':
11
- case 'preparando envio':
12
- fulfillmentStatus = 'in_separation';
13
- break;
14
- case 'faturado':
15
- case 'faturado (atendido)':
16
- case 'atendido':
17
- fulfillmentStatus = 'invoice_issued';
18
- break;
19
- case 'pronto_envio':
20
- case 'pronto para envio':
21
- fulfillmentStatus = 'ready_for_shipping';
22
- break;
23
- case 'enviado':
24
- fulfillmentStatus = 'shipped';
25
- break;
26
- case 'entregue':
27
- fulfillmentStatus = 'delivered';
28
- break;
29
- case 'cancelado':
30
- financialStatus = 'voided';
31
- break;
32
- default:
33
- }
34
- return { financialStatus, fulfillmentStatus };
35
- };
@@ -1,42 +0,0 @@
1
- import type { Orders } from '@cloudcommerce/types';
2
-
3
- export default (order: Orders) => {
4
- const financialStatus = order.financial_status && order.financial_status.current;
5
- switch (financialStatus) {
6
- case 'pending':
7
- case 'under_analysis':
8
- case 'unknown':
9
- case 'authorized':
10
- case 'partially_paid':
11
- return 'aberto';
12
- case 'voided':
13
- case 'refunded':
14
- case 'in_dispute':
15
- case 'unauthorized':
16
- return 'cancelado';
17
- default:
18
- }
19
-
20
- switch (order.fulfillment_status && order.fulfillment_status.current) {
21
- case 'in_production':
22
- case 'in_separation':
23
- return 'preparando_envio';
24
- case 'invoice_issued':
25
- return 'faturado';
26
- case 'ready_for_shipping':
27
- return 'pronto_envio';
28
- case 'shipped':
29
- case 'partially_shipped':
30
- return 'enviado';
31
- case 'delivered':
32
- return 'entregue';
33
- case 'returned':
34
- return 'cancelado';
35
- default:
36
- }
37
-
38
- if (financialStatus === 'paid') {
39
- return 'aprovado';
40
- }
41
- return 'aberto';
42
- };
@@ -1,52 +0,0 @@
1
- import axios, { AxiosRequestConfig } from 'axios';
2
-
3
- export default (
4
- url: string,
5
- body: Record<string, any>,
6
- token = process.env.TINYERP_TOKEN,
7
- options: Partial<AxiosRequestConfig> = {},
8
- ) => {
9
- // https://www.tiny.com.br/ajuda/api/api2
10
- let data = `token=${token}&formato=JSON`;
11
- if (body) {
12
- Object.keys(body).forEach((field) => {
13
- if (body[field]) {
14
- switch (typeof body[field]) {
15
- case 'object':
16
- data += `&${field}=${JSON.stringify(body[field])}`;
17
- break;
18
- case 'string':
19
- case 'number':
20
- data += `&${field}=${body[field]}`;
21
- break;
22
- default:
23
- }
24
- }
25
- });
26
- }
27
-
28
- return axios.post(url, data, {
29
- baseURL: 'https://api.tiny.com.br/api2/',
30
- timeout: 30000,
31
- ...options,
32
- })
33
- .then((response) => {
34
- const { retorno } = response.data;
35
- if (retorno.status === 'Erro') {
36
- const err: any = new Error('Tiny error response');
37
- const tinyErrorCode = parseInt(retorno.codigo_erro, 10);
38
- if (tinyErrorCode <= 2) {
39
- response.status = 401;
40
- } else if (tinyErrorCode === 6) {
41
- response.status = 503;
42
- } else if (tinyErrorCode === 20) {
43
- response.status = 404;
44
- }
45
- err.response = response;
46
- err.config = response.config;
47
- err.request = response.request;
48
- throw err;
49
- }
50
- return retorno;
51
- });
52
- };
package/src/tiny-erp.ts DELETED
@@ -1,26 +0,0 @@
1
- /* eslint-disable import/prefer-default-export */
2
-
3
- import '@cloudcommerce/firebase/lib/init';
4
- import functions from 'firebase-functions/v1';
5
- import config from '@cloudcommerce/firebase/lib/config';
6
- import {
7
- createAppEventsFunction,
8
- ApiEventHandler,
9
- } from '@cloudcommerce/firebase/lib/helpers/pubsub';
10
- import handleApiEvent from './event-to-tiny';
11
- import handleTinyWebhook from './tiny-webhook';
12
-
13
- const { httpsFunctionOptions } = config.get();
14
- const { region } = httpsFunctionOptions;
15
-
16
- export const tinyerp = {
17
- onStoreEvent: createAppEventsFunction(
18
- 'tinyErp',
19
- handleApiEvent as ApiEventHandler,
20
- ),
21
-
22
- webhook: functions
23
- .region(region)
24
- .runWith(httpsFunctionOptions)
25
- .https.onRequest(handleTinyWebhook),
26
- };
@@ -1,103 +0,0 @@
1
- import type { Request, Response } from 'firebase-functions';
2
- import type { Applications } from '@cloudcommerce/types';
3
- import logger from 'firebase-functions/logger';
4
- import api from '@cloudcommerce/api';
5
- import config from '@cloudcommerce/firebase/lib/config';
6
- import importProduct from './integration/import-product-from-tiny';
7
- import importOrder from './integration/import-order-from-tiny';
8
-
9
- let appData: Record<string, any> = {};
10
- let application: Applications;
11
-
12
- export default async (req: Request, res: Response) => {
13
- const tinyToken = req.query.token;
14
- if (typeof tinyToken === 'string' && tinyToken && req.body) {
15
- const { dados, tipo } = req.body;
16
- if (dados) {
17
- /*
18
- TODO: Check Tiny server IPs
19
- const clientIp = req.get('x-forwarded-for') || req.connection.remoteAddress
20
- */
21
- const { TINYERP_TOKEN } = process.env;
22
- if (!TINYERP_TOKEN || TINYERP_TOKEN !== tinyToken) {
23
- const { apps: { tinyErp: { appId } } } = config.get();
24
- const applicationId = req.query._id;
25
- const appEndpoint = applicationId && typeof applicationId === 'string'
26
- ? `applications/${applicationId}`
27
- : `applications/app_id:${appId}`;
28
- application = (await api.get(appEndpoint as `applications/${Applications['_id']}`)).data;
29
- appData = {
30
- ...application.data,
31
- ...application.hidden_data,
32
- };
33
- if (appData.tiny_api_token !== tinyToken) {
34
- res.sendStatus(401);
35
- return;
36
- }
37
- process.env.TINYERP_TOKEN = tinyToken;
38
- }
39
-
40
- if (dados.idVendaTiny) {
41
- const orderNumber = `id:${dados.idVendaTiny}`;
42
- const queueEntry = {
43
- nextId: orderNumber,
44
- isNotQueued: true,
45
- };
46
- await importOrder({}, queueEntry);
47
- } else if (
48
- (tipo === 'produto' || tipo === 'estoque')
49
- && (dados.id || dados.idProduto)
50
- && (dados.codigo || dados.sku)
51
- ) {
52
- const nextId = String(dados.skuMapeamento || dados.sku || dados.codigo);
53
- const tinyStockUpdate = {
54
- ref: `${nextId}`,
55
- tipo,
56
- produto: {
57
- id: dados.idProduto,
58
- codigo: dados.sku,
59
- ...dados,
60
- },
61
- };
62
- logger.info(`> Tiny webhook: ${nextId} => ${tinyStockUpdate.produto.saldo}`);
63
- const queueEntry = {
64
- nextId,
65
- tinyStockUpdate,
66
- isNotQueued: true,
67
- app: application,
68
- };
69
- await importProduct({}, queueEntry, appData, false, true);
70
- }
71
-
72
- if (tipo === 'produto') {
73
- const mapeamentos: any[] = [];
74
- const parseTinyItem = (tinyItem) => {
75
- if (tinyItem) {
76
- const {
77
- idMapeamento,
78
- id,
79
- codigo,
80
- sku,
81
- } = tinyItem;
82
- mapeamentos.push({
83
- idMapeamento: idMapeamento || id,
84
- skuMapeamento: codigo || sku,
85
- });
86
- }
87
- };
88
- parseTinyItem(dados);
89
- if (Array.isArray(dados.variacoes)) {
90
- dados.variacoes.forEach((variacao) => {
91
- parseTinyItem(variacao.id ? variacao : variacao.variacao);
92
- });
93
- }
94
- res.status(200).send(mapeamentos);
95
- return;
96
- }
97
- res.sendStatus(200);
98
- return;
99
- }
100
- logger.warn('< Invalid Tiny Webhook body', req.body);
101
- }
102
- res.sendStatus(403);
103
- };
package/tsconfig.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "extends": "../../../tsconfig.json"
3
- }