cloudcommerce 0.2.2 → 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/CHANGELOG.md +45 -0
- package/ecomplus-stores/monocard/functions/ssr/package.json +0 -2
- package/ecomplus-stores/tia-sonia/functions/ssr/package.json +0 -2
- package/package.json +7 -7
- package/packages/api/package.json +1 -1
- package/packages/apps/correios/package.json +2 -2
- package/packages/apps/custom-payment/package.json +1 -1
- package/packages/apps/custom-shipping/package.json +1 -1
- package/packages/apps/datafrete/package.json +2 -2
- package/packages/apps/discounts/package.json +1 -1
- package/packages/apps/emails/package.json +1 -1
- package/packages/apps/fb-conversions/lib/fb-conversions-events.js +131 -111
- package/packages/apps/fb-conversions/lib/fb-conversions-events.js.map +1 -1
- package/packages/apps/fb-conversions/lib/functions-lib/create-fb-objects.js +69 -0
- package/packages/apps/fb-conversions/lib/functions-lib/create-fb-objects.js.map +1 -0
- package/packages/apps/fb-conversions/package.json +1 -1
- package/packages/apps/fb-conversions/src/fb-conversions-events.ts +173 -125
- package/packages/apps/fb-conversions/src/functions-lib/create-fb-objects.ts +104 -0
- package/packages/apps/frenet/package.json +2 -2
- package/packages/apps/galaxpay/package.json +2 -2
- package/packages/apps/google-analytics/package.json +2 -2
- package/packages/apps/infinitepay/package.json +2 -2
- package/packages/apps/jadlog/package.json +2 -2
- package/packages/apps/loyalty-points/package.json +1 -1
- package/packages/apps/melhor-envio/CHANGELOG.md +1 -0
- package/packages/apps/melhor-envio/README.md +1 -0
- package/packages/apps/melhor-envio/events.js +1 -0
- package/packages/apps/melhor-envio/lib/functions-lib/database.d.ts +18 -0
- package/packages/apps/melhor-envio/lib/functions-lib/database.js +115 -0
- package/packages/apps/melhor-envio/lib/functions-lib/database.js.map +1 -0
- package/packages/apps/melhor-envio/lib/functions-lib/events-to-melhor-envio.d.ts +7 -0
- package/packages/apps/melhor-envio/lib/functions-lib/events-to-melhor-envio.js +112 -0
- package/packages/apps/melhor-envio/lib/functions-lib/events-to-melhor-envio.js.map +1 -0
- package/packages/apps/melhor-envio/lib/functions-lib/new-label.d.ts +42 -0
- package/packages/apps/melhor-envio/lib/functions-lib/new-label.js +185 -0
- package/packages/apps/melhor-envio/lib/functions-lib/new-label.js.map +1 -0
- package/packages/apps/melhor-envio/lib/functions-lib/order-is-valid.d.ts +5 -0
- package/packages/apps/melhor-envio/lib/functions-lib/order-is-valid.js +40 -0
- package/packages/apps/melhor-envio/lib/functions-lib/order-is-valid.js.map +1 -0
- package/packages/apps/melhor-envio/lib/functions-lib/tracking-codes.d.ts +2 -0
- package/packages/apps/melhor-envio/lib/functions-lib/tracking-codes.js +164 -0
- package/packages/apps/melhor-envio/lib/functions-lib/tracking-codes.js.map +1 -0
- package/packages/apps/melhor-envio/lib/index.d.ts +1 -0
- package/packages/apps/melhor-envio/lib/index.js +2 -0
- package/packages/apps/melhor-envio/lib/index.js.map +1 -0
- package/packages/apps/melhor-envio/lib/melhor-envio-events.d.ts +6 -0
- package/packages/apps/melhor-envio/lib/melhor-envio-events.js +17 -0
- package/packages/apps/melhor-envio/lib/melhor-envio-events.js.map +1 -0
- package/packages/apps/melhor-envio/lib/melhor-envio.d.ts +2 -0
- package/packages/apps/melhor-envio/lib/melhor-envio.js +6 -0
- package/packages/apps/melhor-envio/lib/melhor-envio.js.map +1 -0
- package/packages/apps/melhor-envio/lib-mjs/calculate-melhor-envio.mjs +341 -0
- package/packages/apps/melhor-envio/lib-mjs/functions/client-melhor-envio.mjs +14 -0
- package/packages/apps/melhor-envio/lib-mjs/functions/error-handling.mjs +62 -0
- package/packages/apps/melhor-envio/lib-mjs/functions/new-shipment.mjs +119 -0
- package/packages/apps/melhor-envio/package.json +36 -0
- package/packages/apps/melhor-envio/src/functions-lib/database.ts +140 -0
- package/packages/apps/melhor-envio/src/functions-lib/events-to-melhor-envio.ts +137 -0
- package/packages/apps/melhor-envio/src/functions-lib/new-label.ts +214 -0
- package/packages/apps/melhor-envio/src/functions-lib/order-is-valid.ts +51 -0
- package/packages/apps/melhor-envio/src/functions-lib/tracking-codes.ts +191 -0
- package/packages/apps/melhor-envio/src/index.ts +1 -0
- package/packages/apps/melhor-envio/src/melhor-envio-events.ts +24 -0
- package/packages/apps/melhor-envio/src/melhor-envio.ts +7 -0
- package/packages/apps/melhor-envio/tsconfig.json +6 -0
- package/packages/apps/mercadopago/package.json +2 -2
- package/packages/apps/pagarme/package.json +2 -2
- package/packages/apps/paghiper/package.json +3 -3
- package/packages/apps/pix/package.json +2 -2
- package/packages/apps/tiny-erp/package.json +2 -2
- package/packages/cli/package.json +1 -1
- package/packages/config/package.json +1 -1
- package/packages/emails/package.json +3 -3
- package/packages/events/lib/firebase.js +2 -0
- package/packages/events/lib/firebase.js.map +1 -1
- package/packages/events/package.json +2 -1
- package/packages/events/src/firebase.ts +2 -0
- package/packages/firebase/lib/config.d.ts +4 -0
- package/packages/firebase/lib/config.js +8 -0
- package/packages/firebase/lib/config.js.map +1 -1
- package/packages/firebase/package.json +2 -2
- package/packages/firebase/src/config.ts +9 -0
- package/packages/i18n/lib/en_us/i19buyTogether.txt +1 -0
- package/packages/i18n/lib/en_us/i19buyTogetherWith.txt +1 -0
- package/packages/i18n/lib/en_us/i19report.txt +1 -0
- package/packages/i18n/lib/en_us/i19toggleMenu.txt +1 -0
- package/packages/i18n/lib/en_us/i19uponRequest.txt +1 -0
- package/packages/i18n/lib/en_us/i19usedPoints.txt +1 -0
- package/packages/i18n/lib/en_us.d.ts +6 -0
- package/packages/i18n/lib/en_us.js +6 -0
- package/packages/i18n/lib/en_us.js.map +1 -1
- package/packages/i18n/lib/pt_br/i19buyTogether.txt +1 -0
- package/packages/i18n/lib/pt_br/i19buyTogetherWith.txt +1 -0
- package/packages/i18n/lib/pt_br/i19report.txt +1 -0
- package/packages/i18n/lib/pt_br/i19toggleMenu.txt +1 -0
- package/packages/i18n/lib/pt_br/i19uponRequest.txt +1 -0
- package/packages/i18n/lib/pt_br/i19usedPoints.txt +1 -0
- package/packages/i18n/lib/pt_br.d.ts +6 -0
- package/packages/i18n/lib/pt_br.js +6 -0
- package/packages/i18n/lib/pt_br.js.map +1 -1
- package/packages/i18n/package.json +1 -1
- package/packages/i18n/src/en_us.ts +6 -0
- package/packages/i18n/src/pt_br.ts +6 -0
- package/packages/modules/lib/firebase/call-app-module.js +5 -0
- package/packages/modules/lib/firebase/call-app-module.js.map +1 -1
- package/packages/modules/package.json +3 -2
- package/packages/modules/src/firebase/call-app-module.ts +5 -0
- package/packages/passport/package.json +1 -1
- package/packages/ssr/package.json +6 -7
- package/packages/storefront/.eslintrc.cjs +1 -1
- package/packages/storefront/astro.config.mjs +8 -1
- package/packages/storefront/dist/client/_astro/PitchBar.209c6645.js +1 -0
- package/packages/storefront/dist/client/_astro/Prices.6fbcb5ac.js +1 -0
- package/packages/storefront/dist/client/_astro/Prices.vue_vue_type_script_setup_true_lang.44f23680.js +1 -0
- package/packages/storefront/dist/client/_astro/ProductCard.ee5eee91.js +1 -0
- package/packages/storefront/dist/client/_astro/ShopHeader.b801c785.js +1 -0
- package/packages/storefront/dist/client/_astro/_...slug_.32968ccf.css +1 -0
- package/packages/storefront/dist/client/_astro/client.5a46cc02.js +1 -0
- package/packages/storefront/dist/client/_astro/ecom-utils.92f137f6.js +1 -0
- package/packages/storefront/dist/client/_astro/hoisted.6edd7364.js +1 -0
- package/packages/storefront/dist/client/_astro/index.844a4059.js +1 -0
- package/packages/storefront/dist/client/{assets → _astro}/index.90df622b.css +0 -0
- package/packages/storefront/dist/client/_astro/modules-info.d9373e21.js +1 -0
- package/packages/storefront/dist/client/_astro/runtime-core.esm-bundler.f04cee62.js +1 -0
- package/packages/storefront/dist/client/_astro/runtime-dom.esm-bundler.00313542.js +1 -0
- package/packages/storefront/dist/client/_astro/server.60de185d.css +1 -0
- package/packages/storefront/dist/client/{chunks/session-utm.ceebe967.js → _astro/session-utm.72684b84.js} +0 -0
- package/packages/storefront/dist/client/_astro/use-component-variant.58788b6e.js +1 -0
- package/packages/storefront/dist/client/{chunks/workbox-window.prod.es5.10f2e5ac.js → _astro/workbox-window.prod.es5.295a6886.js} +0 -0
- package/packages/storefront/dist/client/fallback/index.html +79 -0
- package/packages/storefront/dist/client/sw.js +1 -1
- package/packages/storefront/dist/server/chunks/astro.89bd9221.mjs +3378 -0
- package/packages/storefront/dist/server/chunks/pages/all.23de4e5c.mjs +2453 -0
- package/packages/storefront/dist/server/chunks/prerender.f40361a3.mjs +2 -0
- package/packages/storefront/dist/server/entry.mjs +2016 -11995
- package/packages/storefront/package.json +10 -11
- package/packages/storefront/src/lib/assets/base.css +16 -11
- package/packages/storefront/src/lib/components/Carousel.vue +52 -82
- package/packages/storefront/src/lib/components/Drawer.vue +103 -0
- package/packages/storefront/src/lib/components/PitchBar.vue +16 -11
- package/packages/storefront/src/lib/components/Prices.vue +29 -29
- package/packages/storefront/src/lib/components/ShopHeader.vue +82 -0
- package/packages/storefront/src/lib/components/StickyHeader.vue +73 -32
- package/packages/storefront/src/lib/components/globals/Fade.vue +10 -14
- package/packages/storefront/src/lib/composables/use-component-variant.ts +6 -2
- package/packages/storefront/src/lib/composables/use-prices.ts +4 -2
- package/packages/storefront/src/lib/layouts/BaseBody.astro +1 -0
- package/packages/storefront/src/lib/layouts/PagesHeader.astro +28 -13
- package/packages/storefront/src/lib/ssr/Picture.astro +7 -2
- package/packages/storefront/tailwind.config.cjs +4 -0
- package/packages/storefront/uno.config.cjs +1 -1
- package/packages/types/package.json +1 -1
- package/packages/storefront/dist/client/PitchBar.5ae15bda.js +0 -1
- package/packages/storefront/dist/client/Prices.248d1aae.js +0 -1
- package/packages/storefront/dist/client/ProductCard.4848304c.js +0 -1
- package/packages/storefront/dist/client/assets/_...slug_.b701cbb1.css +0 -1
- package/packages/storefront/dist/client/assets/server.4d9646d8.css +0 -1
- package/packages/storefront/dist/client/chunks/Prices.vue_vue_type_script_setup_true_lang.2d01aaa2.js +0 -1
- package/packages/storefront/dist/client/chunks/ecom-utils.cd6787a7.js +0 -1
- package/packages/storefront/dist/client/chunks/modules-info.fcab5bba.js +0 -1
- package/packages/storefront/dist/client/chunks/runtime-dom.esm-bundler.923790dc.js +0 -1
- package/packages/storefront/dist/client/client.2356f675.js +0 -1
- package/packages/storefront/dist/client/hoisted.11b849a7.js +0 -1
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import logger from 'firebase-functions/logger';
|
|
2
|
+
import api from '@cloudcommerce/api';
|
|
3
|
+
import config from '@cloudcommerce/firebase/lib/config';
|
|
4
|
+
import meClient from '../../lib-mjs/functions/client-melhor-envio.mjs';
|
|
5
|
+
import errorHandling from '../../lib-mjs/functions/error-handling.mjs';
|
|
6
|
+
import db from './database.js';
|
|
7
|
+
|
|
8
|
+
const getConfig = async () => {
|
|
9
|
+
try {
|
|
10
|
+
const app = (await api.get(`applications?app_id=${config.get().apps.melhorEnvio}&fields=hidden_data`)).data.result;
|
|
11
|
+
return app[0].hidden_data;
|
|
12
|
+
} catch (err) {
|
|
13
|
+
logger.error('Error =>', err);
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const handleTrackingCodes = async () => {
|
|
18
|
+
const appConfig = await getConfig();
|
|
19
|
+
const accessToken = appConfig?.access_token;
|
|
20
|
+
const isSandbox = appConfig?.sandbox;
|
|
21
|
+
// eslint-disable-next-line no-async-promise-executor
|
|
22
|
+
const trackingCodes = new Promise(async (resolve, reject) => {
|
|
23
|
+
logger.log('>> Inciando rastreio de etiquetas');
|
|
24
|
+
try {
|
|
25
|
+
const labels = await db.getAllLabels();
|
|
26
|
+
// .then(labels => {
|
|
27
|
+
const checkStatus = async (queue = 0) => {
|
|
28
|
+
const label = labels[queue];
|
|
29
|
+
if (!label) {
|
|
30
|
+
return resolve(null);
|
|
31
|
+
}
|
|
32
|
+
const next = () => {
|
|
33
|
+
queue += 1;
|
|
34
|
+
return checkStatus(queue);
|
|
35
|
+
};
|
|
36
|
+
// evita buscar configuração do app para o mesmo storeId
|
|
37
|
+
let order;
|
|
38
|
+
try {
|
|
39
|
+
order = (await api.get(`orders/${label.resourceId}`)).data;
|
|
40
|
+
} catch (err) {
|
|
41
|
+
errorHandling(err);
|
|
42
|
+
if (err.response && err.response.status === 404) {
|
|
43
|
+
await db.deleteLabel(label.id).catch(logger.error);
|
|
44
|
+
}
|
|
45
|
+
return next();
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
const { data } = await meClient(accessToken, isSandbox)
|
|
49
|
+
.post('/shipment/tracking', { orders: [label.labelId] });
|
|
50
|
+
const orderLabel = order.hidden_metafields?.find((metafield) => {
|
|
51
|
+
return metafield.field === 'melhor_envio_label_id' && data[metafield.value];
|
|
52
|
+
});
|
|
53
|
+
if (!orderLabel || !orderLabel.value) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
const tracking = data[orderLabel.value];
|
|
57
|
+
const shippingLine = order.shipping_lines && order.shipping_lines
|
|
58
|
+
.find(({ app }) => app && app.service_code && app.service_code.startsWith('ME'));
|
|
59
|
+
if (!shippingLine) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
let shippingLineCurrentStatus = '';
|
|
63
|
+
if (shippingLine.status) {
|
|
64
|
+
shippingLineCurrentStatus = shippingLine.status.current;
|
|
65
|
+
} else {
|
|
66
|
+
shippingLineCurrentStatus = order.fulfillment_status
|
|
67
|
+
? order.fulfillment_status.current : '';
|
|
68
|
+
}
|
|
69
|
+
let updateTo;
|
|
70
|
+
switch (tracking.status) {
|
|
71
|
+
case 'posted':
|
|
72
|
+
if (shippingLineCurrentStatus !== 'shipped' && shippingLineCurrentStatus !== 'delivered') {
|
|
73
|
+
updateTo = 'shipped';
|
|
74
|
+
}
|
|
75
|
+
break;
|
|
76
|
+
case 'delivered':
|
|
77
|
+
if (shippingLineCurrentStatus !== 'delivered' && tracking.tracking) {
|
|
78
|
+
updateTo = 'delivered';
|
|
79
|
+
}
|
|
80
|
+
break;
|
|
81
|
+
case 'undelivered':
|
|
82
|
+
if (shippingLineCurrentStatus !== 'returned') {
|
|
83
|
+
updateTo = 'returned';
|
|
84
|
+
}
|
|
85
|
+
break;
|
|
86
|
+
default:
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
if (tracking.tracking && (!shippingLine.tracking_codes
|
|
90
|
+
|| !shippingLine.tracking_codes.length)) {
|
|
91
|
+
try {
|
|
92
|
+
await api.patch(`orders/${label.resourceId}/shipping_lines/${shippingLine._id}`, {
|
|
93
|
+
tracking_codes: [{
|
|
94
|
+
code: tracking.tracking,
|
|
95
|
+
link: `https://www.melhorrastreio.com.br/rastreio/${tracking.tracking}`,
|
|
96
|
+
}],
|
|
97
|
+
});
|
|
98
|
+
logger.log(`Tracking code para ${order._id} / Pro.: ${tracking.protocol}`);
|
|
99
|
+
} catch (err) {
|
|
100
|
+
logger.error(`> Error updating shipping_lines on order #${label.resourceId} => `, err);
|
|
101
|
+
next();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (updateTo) {
|
|
105
|
+
const body = {
|
|
106
|
+
shipping_line_id: shippingLine._id,
|
|
107
|
+
date_time: new Date().toISOString(),
|
|
108
|
+
status: updateTo,
|
|
109
|
+
notification_code: tracking.id,
|
|
110
|
+
};
|
|
111
|
+
await api.post(`orders/${label.resourceId}/fulfillments`, body);
|
|
112
|
+
logger.log(`Status do pedido ${order._id} alterado com sucesso para ${updateTo}`);
|
|
113
|
+
}
|
|
114
|
+
if (tracking.status) {
|
|
115
|
+
switch (tracking.status) {
|
|
116
|
+
case 'delivered':
|
|
117
|
+
case 'undelivered':
|
|
118
|
+
case 'canceled':
|
|
119
|
+
await db.deleteLabel(label.id);
|
|
120
|
+
break;
|
|
121
|
+
default:
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
} else {
|
|
125
|
+
await db.updateLabel(tracking.status, label.labelId);
|
|
126
|
+
}
|
|
127
|
+
} catch (err) {
|
|
128
|
+
if (err.isAxiosError) {
|
|
129
|
+
const { response } = err;
|
|
130
|
+
if (response && response.status < 500) {
|
|
131
|
+
const payload = {
|
|
132
|
+
labelId: label.labelId,
|
|
133
|
+
status: response.status,
|
|
134
|
+
data: response.data,
|
|
135
|
+
config: response.config,
|
|
136
|
+
};
|
|
137
|
+
logger.error('Tracking_codes_err', JSON.stringify(payload, undefined, 4));
|
|
138
|
+
}
|
|
139
|
+
} else {
|
|
140
|
+
err.libCode = 'Tracking_codes_err';
|
|
141
|
+
logger.error(err);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return next();
|
|
145
|
+
};
|
|
146
|
+
await checkStatus();
|
|
147
|
+
} catch (err) {
|
|
148
|
+
reject(err);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
if (appConfig && accessToken && appConfig.disable_tracking) {
|
|
152
|
+
await trackingCodes.catch((err) => {
|
|
153
|
+
logger.error('> Error => ', err);
|
|
154
|
+
});
|
|
155
|
+
if (new Date().getHours() === 23) {
|
|
156
|
+
await db.clearLabels().catch((err) => {
|
|
157
|
+
logger.error('> Error removing old Lables => ', err);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
export default handleTrackingCodes;
|
|
164
|
+
// # sourceMappingURL=tracking-codes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tracking-codes.js","sourceRoot":"","sources":["../../src/functions-lib/tracking-codes.ts"],"names":[],"mappings":"AACA,OAAO,MAAM,MAAM,2BAA2B,CAAC;AAC/C,OAAO,GAAG,MAAM,oBAAoB,CAAC;AACrC,OAAO,MAAM,MAAM,oCAAoC,CAAC;AACxD,OAAO,QAAQ,MAAM,iDAAiD,CAAC;AACvE,OAAO,aAAa,MAAM,4CAA4C,CAAC;AACvE,OAAO,EAAa,MAAM,YAAY,CAAC;AAEvC,MAAM,SAAS,GAAG,KAAK,IAAI,EAAE;IAC3B,IAAI;QACF,MAAM,GAAG,GAAG,CAAC,MAAM,GAAG,CAA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,CAAC,MAAM;6BACxB,CAAC;4BACF,MAAM,CAAC,KAAK,CAAC,oBAAoB,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC,CAAC;yBAC3E;qBACF;yBAAM;wBACL,GAAG,CAAC,OAAO,GAAG,oBAAoB,CAAC;wBACnC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;qBACnB;iBACF;gBACD,OAAO,IAAI,EAAE,CAAC;YAChB,CAAC,CAAC;YAEF,MAAM,WAAW,EAAE,CAAC;SACrB;QAAC,OAAO,GAAG,EAAE;YACZ,MAAM,CAAC,GAAG,CAAC,CAAC;SACb;IACH,CAAC,CAAC,CAAC;IAEH,IAAI,SAAS,IAAI,WAAW,IAAI,SAAS,CAAC,gBAAgB,EAAE;QAC1D,MAAM,aAAa,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YAChC,MAAM,CAAC,KAAK,CAAC,aAAa,EAAE,GAAG,CAAC,CAAC;QACnC,CAAC,CAAC,CAAC;QAEH,IAAI,IAAI,IAAI,EAAE,CAAC,QAAQ,EAAE,KAAK,EAAE,EAAE;YAChC,MAAM,EAAE,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;gBACnC,MAAM,CAAC,KAAK,CAAC,iCAAiC,EAAE,GAAG,CAAC,CAAC;YACvD,CAAC,CAAC,CAAC;SACJ;KACF;AACH,CAAC,CAAC;AAEF,eAAe,mBAAmB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './melhor-envio';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
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 { createAppEventsFunction } from '@cloudcommerce/firebase/lib/helpers/pubsub';
|
|
6
|
+
import handleApiEvent from './functions-lib/events-to-melhor-envio.js';
|
|
7
|
+
import handleTrackingCodes from './functions-lib/tracking-codes.js';
|
|
8
|
+
|
|
9
|
+
export const melhorenvio = {
|
|
10
|
+
cronTrackingCodes: functions.region(config.get().httpsFunctionOptions.region).pubsub
|
|
11
|
+
.schedule('*/30 * * * *')
|
|
12
|
+
.onRun(() => {
|
|
13
|
+
return handleTrackingCodes;
|
|
14
|
+
}),
|
|
15
|
+
onStoreEvent: createAppEventsFunction('melhorEnvio', handleApiEvent),
|
|
16
|
+
};
|
|
17
|
+
// # sourceMappingURL=melhor-envio-events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"melhor-envio-events.js","sourceRoot":"","sources":["../src/melhor-envio-events.ts"],"names":[],"mappings":"AAAA,iDAAiD;AACjD,OAAO,kCAAkC,CAAC;AAC1C,OAAO,KAAK,SAAS,MAAM,uBAAuB,CAAC;AACnD,OAAO,MAAM,MAAM,oCAAoC,CAAC;AACxD,OAAO,EACL,uBAAuB,GAExB,MAAM,4CAA4C,CAAC;AACpD,OAAO,cAAc,MAAM,wCAAwC,CAAC;AACpE,OAAO,mBAAmB,MAAM,gCAAgC,CAAC;AAEjE,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,iBAAiB,EAAE,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC,MAAM;SACjF,QAAQ,CAAC,cAAc,CAAC;SACxB,KAAK,CAAC,GAAG,EAAE;QACV,OAAO,mBAAmB,CAAC;IAC7B,CAAC,CAAC;IAEJ,YAAY,EAAE,uBAAuB,CACnC,aAAa,EACb,cAAiC,CAClC;CAEF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"melhor-envio.js","sourceRoot":"","sources":["../src/melhor-envio.ts"],"names":[],"mappings":"AAEA,OAAO,uBAAuB,MAAM,uCAAuC,CAAC;AAE5E,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,EAAE,OAAsB,EAAE,EAAE;IAChE,OAAO,uBAAuB,CAAC,OAAO,CAAC,CAAC;AAC1C,CAAC,CAAC"}
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import logger from 'firebase-functions/logger';
|
|
2
|
+
import api from '@cloudcommerce/api';
|
|
3
|
+
import { newShipment, matchService, sortServicesBy } from './functions/new-shipment.mjs';
|
|
4
|
+
import meClient from './functions/client-melhor-envio.mjs';
|
|
5
|
+
import errorHandling from './functions/error-handling.mjs';
|
|
6
|
+
|
|
7
|
+
export default async ({ params, application }) => {
|
|
8
|
+
const appData = {
|
|
9
|
+
...application.data,
|
|
10
|
+
...application.hidden_data,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
if (!appData.access_token) {
|
|
14
|
+
// no Melhor Envio access_token
|
|
15
|
+
return {
|
|
16
|
+
status: 409,
|
|
17
|
+
error: 'CALCULATE_SHIPPING_DISABLED',
|
|
18
|
+
message: 'Melhor Envio Token is unset on app hidden data',
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
if (
|
|
22
|
+
appData.skip_no_weight_item
|
|
23
|
+
&& params.items && params.items.find(({ weight }) => weight && !weight.value)
|
|
24
|
+
) {
|
|
25
|
+
return {
|
|
26
|
+
status: 409,
|
|
27
|
+
error: 'CALCULATE_SHIPPING_SKIPPED',
|
|
28
|
+
message: 'Melhor Envio configured to skip no weight items',
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
// start mounting response body
|
|
32
|
+
// https://apx-mods.e-com.plus/api/v1/calculate_shipping/response_schema.json?store_id=100
|
|
33
|
+
const response = {
|
|
34
|
+
shipping_services: [],
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
let shippingRules;
|
|
38
|
+
if (Array.isArray(appData.shipping_rules) && appData.shipping_rules.length) {
|
|
39
|
+
shippingRules = appData.shipping_rules;
|
|
40
|
+
} else {
|
|
41
|
+
shippingRules = [];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const destinationZip = params.to ? params.to.zip.replace(/\D/g, '') : '';
|
|
45
|
+
|
|
46
|
+
const checkZipCode = (rule) => {
|
|
47
|
+
// validate rule zip range
|
|
48
|
+
if (destinationZip && rule.zip_range) {
|
|
49
|
+
const { min, max } = rule.zip_range;
|
|
50
|
+
return Boolean((!min || destinationZip >= min) && (!max || destinationZip <= max));
|
|
51
|
+
}
|
|
52
|
+
return true;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// search for configured free shipping rule
|
|
56
|
+
if (Array.isArray(shippingRules)) {
|
|
57
|
+
for (let i = 0; i < shippingRules.length; i++) {
|
|
58
|
+
const rule = shippingRules[i];
|
|
59
|
+
if (rule.free_shipping && checkZipCode(rule)) {
|
|
60
|
+
if (!rule.min_amount) {
|
|
61
|
+
response.free_shipping_from_value = 0;
|
|
62
|
+
break;
|
|
63
|
+
} else if (!(response.free_shipping_from_value <= rule.min_amount)) {
|
|
64
|
+
response.free_shipping_from_value = rule.min_amount;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// params object follows calculate shipping request schema:
|
|
71
|
+
// https://apx-mods.e-com.plus/api/v1/calculate_shipping/schema.json?store_id=100
|
|
72
|
+
if (!params.to) {
|
|
73
|
+
// respond only with free shipping option
|
|
74
|
+
return response;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (params.items) {
|
|
78
|
+
const intZipCode = parseInt(params.to.zip.replace(/\D/g, ''), 10);
|
|
79
|
+
const token = appData.access_token;
|
|
80
|
+
const { sandbox } = appData;
|
|
81
|
+
|
|
82
|
+
if (!appData.merchant_address) {
|
|
83
|
+
// get merchant_address
|
|
84
|
+
const { data } = await meClient(token, sandbox).get('/');
|
|
85
|
+
const merchantAddress = data.address;
|
|
86
|
+
|
|
87
|
+
// update config.merchant_address
|
|
88
|
+
appData.merchant_address = merchantAddress;
|
|
89
|
+
// save merchant_address in hidden_data
|
|
90
|
+
|
|
91
|
+
const bodyUpdate = {
|
|
92
|
+
merchant_address: merchantAddress,
|
|
93
|
+
};
|
|
94
|
+
try {
|
|
95
|
+
await api.patch(`applications/${application._id}/hidden_data`, bodyUpdate);
|
|
96
|
+
} catch (err) {
|
|
97
|
+
logger.error('>>(App Melhor Envio): !<> Update merchant_address failed', err.message);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
let schema;
|
|
102
|
+
try {
|
|
103
|
+
schema = newShipment(appData, params);
|
|
104
|
+
} catch (e) {
|
|
105
|
+
logger.error('>>(App Melhor Envio): NEW_SHIPMENT_PARSE_ERR', e);
|
|
106
|
+
return {
|
|
107
|
+
status: 400,
|
|
108
|
+
error: 'CALCULATE_ERR',
|
|
109
|
+
message: 'Unexpected Error Try Later',
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// calculate the shipment
|
|
114
|
+
try {
|
|
115
|
+
const data = await meClient(token, sandbox).post('/shipment/calculate', schema);
|
|
116
|
+
|
|
117
|
+
let errorMsg;
|
|
118
|
+
data.forEach((service) => {
|
|
119
|
+
let isAvailable = true;
|
|
120
|
+
// check if service is not disabled
|
|
121
|
+
if (Array.isArray(appData.unavailable_for)) {
|
|
122
|
+
for (let i = 0; i < appData.unavailable_for.length; i++) {
|
|
123
|
+
if (appData.unavailable_for[i] && appData.unavailable_for[i].zip_range
|
|
124
|
+
&& appData.unavailable_for[i].service_name
|
|
125
|
+
) {
|
|
126
|
+
const unavailable = appData.unavailable_for[i];
|
|
127
|
+
if (intZipCode >= unavailable.zip_range.min
|
|
128
|
+
&& intZipCode <= unavailable.zip_range.max
|
|
129
|
+
&& matchService(unavailable, service.name)
|
|
130
|
+
) {
|
|
131
|
+
isAvailable = false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (!service.error && isAvailable) {
|
|
138
|
+
// mounte response body
|
|
139
|
+
const { to } = params;
|
|
140
|
+
const from = {
|
|
141
|
+
zip: appData.merchant_address.postal_code,
|
|
142
|
+
street: appData.merchant_address.address,
|
|
143
|
+
number: parseInt(appData.merchant_address.number, 10),
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const shippingLine = {
|
|
147
|
+
to,
|
|
148
|
+
from,
|
|
149
|
+
own_hand: service.additional_services.own_hand,
|
|
150
|
+
receipt: service.additional_services.receipt,
|
|
151
|
+
discount: 0,
|
|
152
|
+
total_price: parseFloat(service.price),
|
|
153
|
+
delivery_time: {
|
|
154
|
+
days: parseInt(service.delivery_time, 10),
|
|
155
|
+
working_days: true,
|
|
156
|
+
},
|
|
157
|
+
posting_deadline: {
|
|
158
|
+
days: 3,
|
|
159
|
+
...appData.posting_deadline,
|
|
160
|
+
},
|
|
161
|
+
custom_fields: [
|
|
162
|
+
{
|
|
163
|
+
field: 'by_melhor_envio',
|
|
164
|
+
value: 'true',
|
|
165
|
+
},
|
|
166
|
+
],
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const servicePkg = (service.packages && service.packages[0])
|
|
170
|
+
|| (service.volumes && service.volumes[0]);
|
|
171
|
+
|
|
172
|
+
if (servicePkg) {
|
|
173
|
+
shippingLine.package = {};
|
|
174
|
+
if (servicePkg.dimensions) {
|
|
175
|
+
shippingLine.package.dimensions = {
|
|
176
|
+
width: {
|
|
177
|
+
value: servicePkg.dimensions.width,
|
|
178
|
+
},
|
|
179
|
+
height: {
|
|
180
|
+
value: servicePkg.dimensions.height,
|
|
181
|
+
},
|
|
182
|
+
length: {
|
|
183
|
+
value: servicePkg.dimensions.length,
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
if (servicePkg.weight) {
|
|
188
|
+
shippingLine.package.weight = {
|
|
189
|
+
value: parseFloat(servicePkg.weight),
|
|
190
|
+
unit: 'kg',
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (appData.jadlog_agency) {
|
|
196
|
+
shippingLine.custom_fields.push({
|
|
197
|
+
field: 'jadlog_agency',
|
|
198
|
+
value: String(appData.jadlog_agency),
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// check for default configured additional/discount price
|
|
203
|
+
if (appData.additional_price) {
|
|
204
|
+
if (appData.additional_price > 0) {
|
|
205
|
+
shippingLine.other_additionals = [{
|
|
206
|
+
tag: 'additional_price',
|
|
207
|
+
label: 'Adicional padrão',
|
|
208
|
+
price: appData.additional_price,
|
|
209
|
+
}];
|
|
210
|
+
} else {
|
|
211
|
+
// negative additional price to apply discount
|
|
212
|
+
shippingLine.discount -= appData.additional_price;
|
|
213
|
+
}
|
|
214
|
+
// update total price
|
|
215
|
+
shippingLine.total_price += appData.additional_price;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// search for discount by shipping rule
|
|
219
|
+
if (Array.isArray(shippingRules)) {
|
|
220
|
+
for (let i = 0; i < shippingRules.length; i++) {
|
|
221
|
+
const rule = shippingRules[i];
|
|
222
|
+
if (rule && matchService(rule, service.name)
|
|
223
|
+
&& checkZipCode(rule) && !(rule.min_amount > params.subtotal)
|
|
224
|
+
) {
|
|
225
|
+
// valid shipping rule
|
|
226
|
+
if (rule.free_shipping) {
|
|
227
|
+
shippingLine.discount += shippingLine.total_price;
|
|
228
|
+
shippingLine.total_price = 0;
|
|
229
|
+
break;
|
|
230
|
+
} else if (rule.discount && rule.service_name) {
|
|
231
|
+
let discountValue = rule.discount.value;
|
|
232
|
+
if (rule.discount.percentage) {
|
|
233
|
+
discountValue *= (shippingLine.total_price / 100);
|
|
234
|
+
}
|
|
235
|
+
shippingLine.discount += discountValue;
|
|
236
|
+
shippingLine.total_price -= discountValue;
|
|
237
|
+
if (shippingLine.total_price < 0) {
|
|
238
|
+
shippingLine.total_price = 0;
|
|
239
|
+
}
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
let label = service.name;
|
|
247
|
+
if (appData.services && Array.isArray(appData.services) && appData.services.length) {
|
|
248
|
+
const serviceFound = appData.services.find((lookForService) => {
|
|
249
|
+
return lookForService && matchService(lookForService, label);
|
|
250
|
+
});
|
|
251
|
+
if (serviceFound && serviceFound.label) {
|
|
252
|
+
label = serviceFound.label;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
response.shipping_services.push({
|
|
257
|
+
label,
|
|
258
|
+
carrier: service.company.name,
|
|
259
|
+
service_name: service.name,
|
|
260
|
+
service_code: `ME ${service.id}`,
|
|
261
|
+
icon: service.company.picture,
|
|
262
|
+
shipping_line: shippingLine,
|
|
263
|
+
});
|
|
264
|
+
} else {
|
|
265
|
+
errorMsg += `${service.name}, ${service.error} \n`;
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
// sort services?
|
|
270
|
+
if (appData.sort_services && Array.isArray(response.shipping_services)
|
|
271
|
+
&& response.shipping_services.length) {
|
|
272
|
+
response.shipping_services.sort(sortServicesBy(appData.sort_services));
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return (!Array.isArray(response.shipping_services) || !response.shipping_services.length)
|
|
276
|
+
&& errorMsg ? {
|
|
277
|
+
status: 400,
|
|
278
|
+
error: 'CALCULATE_ERR_MSG',
|
|
279
|
+
message: errorMsg,
|
|
280
|
+
}
|
|
281
|
+
// success response with available shipping services
|
|
282
|
+
: response;
|
|
283
|
+
} catch (err) {
|
|
284
|
+
//
|
|
285
|
+
let message = 'Unexpected Error Try Later';
|
|
286
|
+
if (err.response && err.isAxiosError) {
|
|
287
|
+
const { data, status, config } = err.response;
|
|
288
|
+
let isAppError = true;
|
|
289
|
+
if (status >= 500) {
|
|
290
|
+
message = 'Melhor Envio offline no momento';
|
|
291
|
+
isAppError = false;
|
|
292
|
+
} else if (data) {
|
|
293
|
+
if (data.errors && typeof data.errors === 'object' && Object.keys(data.errors).length) {
|
|
294
|
+
const errorKeys = Object.keys(data.errors);
|
|
295
|
+
for (let i = 0; i < errorKeys.length; i++) {
|
|
296
|
+
const meError = data.errors[errorKeys[i]];
|
|
297
|
+
if (meError && meError.length) {
|
|
298
|
+
message = Array.isArray(meError) ? meError[0] : meError;
|
|
299
|
+
if (errorKeys[i].startsWith('to.')) {
|
|
300
|
+
// invalid merchant config on ME
|
|
301
|
+
// eg.: 'O campo to.postal code deve ter pelo menos 5 caracteres.'
|
|
302
|
+
isAppError = false;
|
|
303
|
+
break;
|
|
304
|
+
} else {
|
|
305
|
+
message += errorKeys[i];
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
} else if (data.error) {
|
|
310
|
+
message = `ME: ${data.error}`;
|
|
311
|
+
} else if (data.message) {
|
|
312
|
+
message = `ME: ${data.message}`;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (isAppError) {
|
|
317
|
+
// debug unexpected error
|
|
318
|
+
logger.error('>>(App Melhor Envio): CalculateShippingErr:', JSON.stringify({
|
|
319
|
+
status,
|
|
320
|
+
data,
|
|
321
|
+
config,
|
|
322
|
+
}, null, 4));
|
|
323
|
+
}
|
|
324
|
+
} else {
|
|
325
|
+
errorHandling(err);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return {
|
|
329
|
+
status: 409,
|
|
330
|
+
error: 'CALCULATE_ERR',
|
|
331
|
+
message,
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
} else {
|
|
335
|
+
return {
|
|
336
|
+
status: 400,
|
|
337
|
+
error: 'CALCULATE_EMPTY_CART',
|
|
338
|
+
message: 'Cannot calculate shipping without cart items',
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
|
|
3
|
+
export default (accessToken, isSandbox) => {
|
|
4
|
+
const headers = {
|
|
5
|
+
Accept: 'application/json',
|
|
6
|
+
'Content-Type': 'application/json',
|
|
7
|
+
Authorization: `Bearer ${accessToken}`,
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
return axios.create({
|
|
11
|
+
baseURL: `https://${isSandbox ? 'sandbox.' : ''}melhorenvio.com.br/api/v2/me`,
|
|
12
|
+
headers,
|
|
13
|
+
});
|
|
14
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import logger from 'firebase-functions/logger';
|
|
2
|
+
|
|
3
|
+
const ignoreError = (response) => {
|
|
4
|
+
// check response status code
|
|
5
|
+
// should ignore some error responses
|
|
6
|
+
const { status, data } = response;
|
|
7
|
+
if (status >= 400 && status < 500) {
|
|
8
|
+
switch (status) {
|
|
9
|
+
case 403:
|
|
10
|
+
// ignore resource limits errors
|
|
11
|
+
return true;
|
|
12
|
+
|
|
13
|
+
case 404:
|
|
14
|
+
if (data && data.error_code !== 20) {
|
|
15
|
+
// resource ID not found ?
|
|
16
|
+
// ignore
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
break;
|
|
20
|
+
default:
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
// must debug
|
|
25
|
+
return false;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export default (err) => {
|
|
29
|
+
// axios error object
|
|
30
|
+
// https://github.com/axios/axios#handling-errors
|
|
31
|
+
if (!err.appAuthRemoved && !err.appErrorLog) {
|
|
32
|
+
// error not treated by App SDK
|
|
33
|
+
if (err.response) {
|
|
34
|
+
if (ignoreError(err.response)) {
|
|
35
|
+
// ignore client error
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
err.responseJSON = JSON.stringify(err.response.data);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// debug unexpected response
|
|
42
|
+
logger.error('>>(App Melhor Envio) =>', err);
|
|
43
|
+
} else if (err.appErrorLog && !err.appErrorLogged) {
|
|
44
|
+
// cannot log to app hidden data
|
|
45
|
+
// debug app log error
|
|
46
|
+
const error = err.appErrorLog;
|
|
47
|
+
const { response, config } = error;
|
|
48
|
+
|
|
49
|
+
// handle error response
|
|
50
|
+
if (response) {
|
|
51
|
+
if (ignoreError(response)) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
// debug unexpected response
|
|
55
|
+
error.configJSON = {
|
|
56
|
+
originalRequest: JSON.stringify(err.config),
|
|
57
|
+
logRequest: JSON.stringify(config),
|
|
58
|
+
};
|
|
59
|
+
logger.error('>>(App Melhor Envio) => ', error);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|