@cloudcommerce/app-melhor-envio 0.3.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.
- package/.turbo/turbo-build.log +4 -0
- package/CHANGELOG.md +1 -0
- package/LICENSE.md +230 -0
- package/README.md +1 -0
- package/events.js +1 -0
- package/lib/functions-lib/database.d.ts +18 -0
- package/lib/functions-lib/database.js +115 -0
- package/lib/functions-lib/database.js.map +1 -0
- package/lib/functions-lib/events-to-melhor-envio.d.ts +7 -0
- package/lib/functions-lib/events-to-melhor-envio.js +112 -0
- package/lib/functions-lib/events-to-melhor-envio.js.map +1 -0
- package/lib/functions-lib/new-label.d.ts +42 -0
- package/lib/functions-lib/new-label.js +185 -0
- package/lib/functions-lib/new-label.js.map +1 -0
- package/lib/functions-lib/order-is-valid.d.ts +5 -0
- package/lib/functions-lib/order-is-valid.js +40 -0
- package/lib/functions-lib/order-is-valid.js.map +1 -0
- package/lib/functions-lib/tracking-codes.d.ts +2 -0
- package/lib/functions-lib/tracking-codes.js +164 -0
- package/lib/functions-lib/tracking-codes.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -0
- package/lib/melhor-envio-events.d.ts +6 -0
- package/lib/melhor-envio-events.js +17 -0
- package/lib/melhor-envio-events.js.map +1 -0
- package/lib/melhor-envio.d.ts +2 -0
- package/lib/melhor-envio.js +6 -0
- package/lib/melhor-envio.js.map +1 -0
- package/lib-mjs/calculate-melhor-envio.mjs +341 -0
- package/lib-mjs/functions/client-melhor-envio.mjs +14 -0
- package/lib-mjs/functions/error-handling.mjs +62 -0
- package/lib-mjs/functions/new-shipment.mjs +119 -0
- package/package.json +36 -0
- package/src/functions-lib/database.ts +140 -0
- package/src/functions-lib/events-to-melhor-envio.ts +137 -0
- package/src/functions-lib/new-label.ts +214 -0
- package/src/functions-lib/order-is-valid.ts +51 -0
- package/src/functions-lib/tracking-codes.ts +191 -0
- package/src/index.ts +1 -0
- package/src/melhor-envio-events.ts +24 -0
- package/src/melhor-envio.ts +7 -0
- package/tsconfig.json +6 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import type { Orders } from '@cloudcommerce/types';
|
|
2
|
+
import logger from 'firebase-functions/logger';
|
|
3
|
+
import api from '@cloudcommerce/api';
|
|
4
|
+
import config from '@cloudcommerce/firebase/lib/config';
|
|
5
|
+
import meClient from '../../lib-mjs/functions/client-melhor-envio.mjs';
|
|
6
|
+
import errorHandling from '../../lib-mjs/functions/error-handling.mjs';
|
|
7
|
+
import db, { Lable } from './database';
|
|
8
|
+
|
|
9
|
+
const getConfig = async () => {
|
|
10
|
+
try {
|
|
11
|
+
const app = (await api.get(
|
|
12
|
+
`applications?app_id=${config.get().apps.melhorEnvio}&fields=hidden_data`,
|
|
13
|
+
)).data.result;
|
|
14
|
+
|
|
15
|
+
return app[0].hidden_data;
|
|
16
|
+
} catch (err) {
|
|
17
|
+
logger.error('Error =>', err);
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const handleTrackingCodes = async () => {
|
|
23
|
+
const appConfig = await getConfig();
|
|
24
|
+
const accessToken: string | undefined = appConfig?.access_token;
|
|
25
|
+
const isSandbox = appConfig?.sandbox;
|
|
26
|
+
|
|
27
|
+
// eslint-disable-next-line no-async-promise-executor
|
|
28
|
+
const trackingCodes = new Promise(async (resolve, reject) => {
|
|
29
|
+
logger.log('>> Inciando rastreio de etiquetas');
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
const labels: Lable[] = await db.getAllLabels();
|
|
33
|
+
// .then(labels => {
|
|
34
|
+
const checkStatus = async (queue = 0) => {
|
|
35
|
+
const label = labels[queue];
|
|
36
|
+
if (!label) {
|
|
37
|
+
return resolve(null);
|
|
38
|
+
}
|
|
39
|
+
const next = () => {
|
|
40
|
+
queue += 1;
|
|
41
|
+
return checkStatus(queue);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// evita buscar configuração do app para o mesmo storeId
|
|
45
|
+
let order: Orders;
|
|
46
|
+
try {
|
|
47
|
+
order = (await api.get(`orders/${label.resourceId}`)).data;
|
|
48
|
+
} catch (err: any) {
|
|
49
|
+
errorHandling(err);
|
|
50
|
+
if (err.response && err.response.status === 404) {
|
|
51
|
+
await db.deleteLabel(label.id).catch(logger.error);
|
|
52
|
+
}
|
|
53
|
+
return next();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
const { data } = await meClient(accessToken, isSandbox)
|
|
58
|
+
.post('/shipment/tracking', { orders: [label.labelId] });
|
|
59
|
+
|
|
60
|
+
const orderLabel = order.hidden_metafields?.find((metafield) => {
|
|
61
|
+
return metafield.field === 'melhor_envio_label_id' && data[metafield.value];
|
|
62
|
+
});
|
|
63
|
+
if (!orderLabel || !orderLabel.value) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const tracking = data[orderLabel.value as unknown as string];
|
|
68
|
+
const shippingLine = order.shipping_lines && order.shipping_lines
|
|
69
|
+
.find(({ app }) => app && app.service_code && app.service_code.startsWith('ME'));
|
|
70
|
+
|
|
71
|
+
if (!shippingLine) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let shippingLineCurrentStatus = '';
|
|
76
|
+
if (shippingLine.status) {
|
|
77
|
+
shippingLineCurrentStatus = shippingLine.status.current;
|
|
78
|
+
} else {
|
|
79
|
+
shippingLineCurrentStatus = order.fulfillment_status
|
|
80
|
+
? order.fulfillment_status.current : '';
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let updateTo: string | undefined;
|
|
84
|
+
|
|
85
|
+
switch (tracking.status) {
|
|
86
|
+
case 'posted':
|
|
87
|
+
if (shippingLineCurrentStatus !== 'shipped' && shippingLineCurrentStatus !== 'delivered') {
|
|
88
|
+
updateTo = 'shipped';
|
|
89
|
+
}
|
|
90
|
+
break;
|
|
91
|
+
case 'delivered':
|
|
92
|
+
if (shippingLineCurrentStatus !== 'delivered' && tracking.tracking) {
|
|
93
|
+
updateTo = 'delivered';
|
|
94
|
+
}
|
|
95
|
+
break;
|
|
96
|
+
case 'undelivered':
|
|
97
|
+
if (shippingLineCurrentStatus !== 'returned') {
|
|
98
|
+
updateTo = 'returned';
|
|
99
|
+
}
|
|
100
|
+
break;
|
|
101
|
+
default:
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (tracking.tracking && (!shippingLine.tracking_codes
|
|
106
|
+
|| !shippingLine.tracking_codes.length)) {
|
|
107
|
+
try {
|
|
108
|
+
await api.patch(
|
|
109
|
+
`orders/${label.resourceId}/shipping_lines/${shippingLine._id}`,
|
|
110
|
+
{
|
|
111
|
+
tracking_codes: [{
|
|
112
|
+
code: tracking.tracking,
|
|
113
|
+
link: `https://www.melhorrastreio.com.br/rastreio/${tracking.tracking}`,
|
|
114
|
+
}],
|
|
115
|
+
},
|
|
116
|
+
);
|
|
117
|
+
logger.log(`Tracking code para ${order._id} / Pro.: ${tracking.protocol}`);
|
|
118
|
+
} catch (err: any) {
|
|
119
|
+
logger.error(
|
|
120
|
+
`> Error updating shipping_lines on order #${label.resourceId} => `,
|
|
121
|
+
err,
|
|
122
|
+
);
|
|
123
|
+
next();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (updateTo) {
|
|
128
|
+
const body = {
|
|
129
|
+
shipping_line_id: shippingLine._id,
|
|
130
|
+
date_time: new Date().toISOString(),
|
|
131
|
+
status: updateTo,
|
|
132
|
+
notification_code: tracking.id,
|
|
133
|
+
} as any;
|
|
134
|
+
|
|
135
|
+
await api.post(`orders/${label.resourceId}/fulfillments`, body);
|
|
136
|
+
logger.log(`Status do pedido ${order._id} alterado com sucesso para ${updateTo}`);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (tracking.status) {
|
|
140
|
+
switch (tracking.status) {
|
|
141
|
+
case 'delivered':
|
|
142
|
+
case 'undelivered':
|
|
143
|
+
case 'canceled':
|
|
144
|
+
await db.deleteLabel(label.id);
|
|
145
|
+
break;
|
|
146
|
+
default:
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
await db.updateLabel(tracking.status, label.labelId);
|
|
151
|
+
}
|
|
152
|
+
} catch (err: any) {
|
|
153
|
+
if (err.isAxiosError) {
|
|
154
|
+
const { response } = err;
|
|
155
|
+
if (response && response.status < 500) {
|
|
156
|
+
const payload = {
|
|
157
|
+
labelId: label.labelId,
|
|
158
|
+
status: response.status,
|
|
159
|
+
data: response.data,
|
|
160
|
+
config: response.config,
|
|
161
|
+
};
|
|
162
|
+
logger.error('Tracking_codes_err', JSON.stringify(payload, undefined, 4));
|
|
163
|
+
}
|
|
164
|
+
} else {
|
|
165
|
+
err.libCode = 'Tracking_codes_err';
|
|
166
|
+
logger.error(err);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return next();
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
await checkStatus();
|
|
173
|
+
} catch (err) {
|
|
174
|
+
reject(err);
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
if (appConfig && accessToken && appConfig.disable_tracking) {
|
|
179
|
+
await trackingCodes.catch((err) => {
|
|
180
|
+
logger.error('> Error => ', err);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
if (new Date().getHours() === 23) {
|
|
184
|
+
await db.clearLabels().catch((err) => {
|
|
185
|
+
logger.error('> Error removing old Lables => ', err);
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
export default handleTrackingCodes;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './melhor-envio';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/* eslint-disable import/prefer-default-export */
|
|
2
|
+
import '@cloudcommerce/firebase/lib/init';
|
|
3
|
+
import * as functions from 'firebase-functions/v1';
|
|
4
|
+
import config from '@cloudcommerce/firebase/lib/config';
|
|
5
|
+
import {
|
|
6
|
+
createAppEventsFunction,
|
|
7
|
+
ApiEventHandler,
|
|
8
|
+
} from '@cloudcommerce/firebase/lib/helpers/pubsub';
|
|
9
|
+
import handleApiEvent from './functions-lib/events-to-melhor-envio';
|
|
10
|
+
import handleTrackingCodes from './functions-lib/tracking-codes';
|
|
11
|
+
|
|
12
|
+
export const melhorenvio = {
|
|
13
|
+
cronTrackingCodes: functions.region(config.get().httpsFunctionOptions.region).pubsub
|
|
14
|
+
.schedule('*/30 * * * *')
|
|
15
|
+
.onRun(() => {
|
|
16
|
+
return handleTrackingCodes;
|
|
17
|
+
}),
|
|
18
|
+
|
|
19
|
+
onStoreEvent: createAppEventsFunction(
|
|
20
|
+
'melhorEnvio',
|
|
21
|
+
handleApiEvent as ApiEventHandler,
|
|
22
|
+
),
|
|
23
|
+
|
|
24
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/* eslint-disable import/prefer-default-export */
|
|
2
|
+
import type { AppModuleBody } from '@cloudcommerce/types';
|
|
3
|
+
import handleCalculateShipping from '../lib-mjs/calculate-melhor-envio.mjs';
|
|
4
|
+
|
|
5
|
+
export const calculateShipping = async (modBody: AppModuleBody) => {
|
|
6
|
+
return handleCalculateShipping(modBody);
|
|
7
|
+
};
|