cardus 0.0.1 → 0.0.3

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/dist/index.js ADDED
@@ -0,0 +1,177 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.IntegrationManager = void 0;
16
+ const moment_1 = __importDefault(require("moment"));
17
+ const insertTempShipment_1 = require("./insertTempShipment");
18
+ class IntegrationManager {
19
+ constructor(params) {
20
+ this.insertOrder = (_a) => __awaiter(this, [_a], void 0, function* ({ order, agencyId, warehouseId, sellerAddress, idUsuario, group }) {
21
+ const idEnvioTemporal = yield this.insertTempShipment({
22
+ order,
23
+ agencyId,
24
+ warehouseId,
25
+ sellerAddress,
26
+ idUsuario,
27
+ });
28
+ if (!idEnvioTemporal)
29
+ throw new Error('Temp shipments could not be created');
30
+ yield this.insertExternalShipment({
31
+ idUsuario,
32
+ order,
33
+ idEnvioTemporal,
34
+ });
35
+ const grouped = Number((group === null || group === void 0 ? void 0 : group.grouped) === 1);
36
+ if (grouped)
37
+ yield this.insertTempShipmentLines({ idEnvioTemporal, group });
38
+ return Object.assign(order, { idEnvioTemporal });
39
+ });
40
+ this.insertTempShipment = (_b) => __awaiter(this, [_b], void 0, function* ({ order, agencyId, warehouseId, sellerAddress, idUsuario }) {
41
+ const tempShipmentData = yield (0, insertTempShipment_1.cookTempShipment)({
42
+ order,
43
+ type: this.type,
44
+ agencyId,
45
+ warehouseId,
46
+ sellerAddress,
47
+ idUsuario,
48
+ findNextPickupDate: this.findNextPickupDate,
49
+ countriesService: this.services.countriesService,
50
+ });
51
+ return yield this.services.integrationsService.insertTempShipment(tempShipmentData);
52
+ });
53
+ this.insertExternalShipment = (_c) => __awaiter(this, [_c], void 0, function* ({ idUsuario, order, idEnvioTemporal }) {
54
+ const idEnvioExterno = yield this.services.integrationsService.insertExternalShipment({
55
+ id_envio_temporal_usuario: idEnvioTemporal,
56
+ codigo_envio_externo: order.codigoEnvioExterno,
57
+ id_usuario: idUsuario,
58
+ fecha_hora_creacion: (0, moment_1.default)().format("YYYY-MM-DD HH:mm:ss"),
59
+ servicio: this.type,
60
+ id_interno_shopify: order.idInternoShopify,
61
+ });
62
+ return idEnvioExterno;
63
+ });
64
+ this.insertTempShipmentLines = (_d) => __awaiter(this, [_d], void 0, function* ({ idEnvioTemporal, group }) {
65
+ const grouped = group.grouped === 1 || group.grouped === '1';
66
+ if (grouped)
67
+ yield this.services.integrationsService.insertTempShipmentLines({
68
+ id_envio: idEnvioTemporal,
69
+ bulto: {
70
+ peso: group.weight,
71
+ alto: group.height,
72
+ ancho: group.width,
73
+ largo: group.length,
74
+ },
75
+ });
76
+ });
77
+ this.services = {
78
+ integrationsService: params.integrationsService,
79
+ addressesService: params.addressesService,
80
+ countriesService: params.countriesService,
81
+ };
82
+ this.type = params.type;
83
+ this.executionManager = params.executionManager;
84
+ this.findNextPickupDate = params.findNextPickupDate;
85
+ }
86
+ insertTempShipments(_a) {
87
+ return __awaiter(this, arguments, void 0, function* ({ payload: { idUsuario, agencyId, addressId, warehouseId, group }, fetchAllOrders, crearBulto, modificarOrdenOriginal, }) {
88
+ try {
89
+ const estaAgrupado = Number(group === null || group === void 0 ? void 0 : group.grouped) === 1;
90
+ const { integrationsService, addressesService } = this.services;
91
+ // esto hay que pasarlo ya en los parametros
92
+ const sellerAddress = warehouseId > 0
93
+ ? yield addressesService.getWarehouseAddress(idUsuario)
94
+ : yield addressesService.getAddress(addressId, idUsuario);
95
+ const allShopifyOrders = yield fetchAllOrders();
96
+ const filteredOrders = yield integrationsService.filterExternalDuplicatedOrders({
97
+ idUsuario,
98
+ orders: allShopifyOrders,
99
+ type: this.type,
100
+ });
101
+ const insertOrdersResult = yield new this.executionManager({
102
+ arrayParams: allShopifyOrders,
103
+ executable: (order) => __awaiter(this, void 0, void 0, function* () {
104
+ const parsedOrder = modificarOrdenOriginal({ originalOrder: order });
105
+ const duplicatedTempShipment = yield integrationsService.checkDuplicateTempShipment(parsedOrder.codigoEnvioExterno, idUsuario);
106
+ if (duplicatedTempShipment)
107
+ return;
108
+ return this.insertOrder({
109
+ order: parsedOrder,
110
+ agencyId,
111
+ warehouseId,
112
+ sellerAddress,
113
+ idUsuario,
114
+ group,
115
+ });
116
+ }),
117
+ regularExecutionTime: 100, // ms
118
+ initialBatchQuantity: 1, // de uno en uno
119
+ }).upload();
120
+ const successfullShipments = insertOrdersResult
121
+ .filter(Boolean)
122
+ .filter((each) => each.status === "fulfilled")
123
+ .map((each) => each.value);
124
+ yield this.insertQuantityRelatedLines({
125
+ orders: successfullShipments,
126
+ idUsuario,
127
+ crearBulto,
128
+ estaAgrupado,
129
+ });
130
+ return filteredOrders;
131
+ }
132
+ catch (e) {
133
+ console.log(e);
134
+ }
135
+ });
136
+ }
137
+ insertQuantityRelatedLines(_a) {
138
+ return __awaiter(this, arguments, void 0, function* ({ orders, idUsuario, crearBulto, estaAgrupado }) {
139
+ const { integrationsService } = this.services;
140
+ const quantityRelatedFunctions = [];
141
+ const obtenerBultosYCantidades = orders.map((_b) => __awaiter(this, [_b], void 0, function* ({ idEnvioTemporal, lineas }) {
142
+ if (!idEnvioTemporal)
143
+ return null;
144
+ return yield Promise.all(lineas.map((line) => __awaiter(this, void 0, void 0, function* () {
145
+ const { bulto, cantidad = 0 } = yield crearBulto({
146
+ lineItems: { idEnvioTemporal, line },
147
+ idUsuario,
148
+ });
149
+ if (cantidad === 0)
150
+ return null;
151
+ return { bulto, cantidad, idEnvioTemporal };
152
+ })));
153
+ }));
154
+ const bultosYCantidades = yield new Promise((res) => __awaiter(this, void 0, void 0, function* () {
155
+ const bultosYCantidadesAnidados = yield Promise.all(obtenerBultosYCantidades);
156
+ const bultosYCantidades = bultosYCantidadesAnidados.flat().filter(Boolean);
157
+ res(bultosYCantidades);
158
+ }));
159
+ bultosYCantidades.forEach(({ bulto, cantidad, idEnvioTemporal }) => {
160
+ try {
161
+ const arrayFake = Array.from({ length: Number(cantidad) });
162
+ arrayFake.forEach(() => {
163
+ if (!estaAgrupado) {
164
+ quantityRelatedFunctions.push(integrationsService.insertTempShipmentLines({ id_envio: idEnvioTemporal, bulto }));
165
+ }
166
+ quantityRelatedFunctions.push(integrationsService.insertTempDetailsShipment({ id_envio: idEnvioTemporal, bulto }));
167
+ });
168
+ }
169
+ catch (e) {
170
+ console.log(e);
171
+ }
172
+ });
173
+ yield Promise.allSettled(quantityRelatedFunctions);
174
+ });
175
+ }
176
+ }
177
+ exports.IntegrationManager = IntegrationManager;
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.cookTempShipment = void 0;
16
+ const moment_1 = __importDefault(require("moment"));
17
+ const cookTempShipment = (_a) => __awaiter(void 0, [_a], void 0, function* ({ order, type, agencyId, warehouseId, sellerAddress, idUsuario, findNextPickupDate, countriesService, }) {
18
+ let zonaLLegada = {};
19
+ if (order.codigoPaisLLegada)
20
+ zonaLLegada = yield countriesService.getCountryFromCountryCode(order.codigoPaisLLegada);
21
+ const setDatosVendedor = () => ({
22
+ id_usuario: idUsuario,
23
+ id_agencia: agencyId,
24
+ nom_ape_salida: sellerAddress.nombre,
25
+ direccion_salida: sellerAddress.direccion,
26
+ cod_pos_salida: sellerAddress.codigo_postal,
27
+ poblacion_salida: sellerAddress.poblacion,
28
+ provincia_salida: sellerAddress.provincia,
29
+ id_zona_salida: sellerAddress.id_zona,
30
+ pais_salida: sellerAddress.nombre_pais,
31
+ tlf_salida: sellerAddress.telefono,
32
+ observaciones_salida: sellerAddress.observaciones,
33
+ contacto_salida: sellerAddress.nombre,
34
+ mail_salida: sellerAddress.mail,
35
+ });
36
+ const setUbicacionComprador = (order) => __awaiter(void 0, void 0, void 0, function* () {
37
+ const compradorLocation = {
38
+ pais_llegada: null,
39
+ provincia_llegada: null,
40
+ id_zona_llegada: null,
41
+ };
42
+ if (order.codigoPaisLLegada) {
43
+ const _zonaLLegada = zonaLLegada;
44
+ compradorLocation.pais_llegada = (_zonaLLegada === null || _zonaLLegada === void 0 ? void 0 : _zonaLLegada.nombre_pais) || null;
45
+ if ((_zonaLLegada === null || _zonaLLegada === void 0 ? void 0 : _zonaLLegada.id_pais) && order.codigoPostalLLegada) {
46
+ const oTempShipmentProvincia = yield countriesService.getProvinceByCountryIdAndPostalCode(_zonaLLegada.id_pais, order.codigoPostalLLegada);
47
+ compradorLocation.provincia_llegada = (oTempShipmentProvincia === null || oTempShipmentProvincia === void 0 ? void 0 : oTempShipmentProvincia.nombre_provincia) || null;
48
+ compradorLocation.id_zona_llegada = yield countriesService.getZoneIdByCountryIdAndPostalCode(_zonaLLegada.id_pais, order.codigoPostalLLegada);
49
+ }
50
+ }
51
+ return compradorLocation;
52
+ });
53
+ const setDatosComprador = (order) => __awaiter(void 0, void 0, void 0, function* () {
54
+ return ({
55
+ nom_ape_llegada: order.primerApellidoLLegada
56
+ ? order.nombreLLegada + " " + order.primerApellidoLLegada
57
+ : order.nombreLLegada,
58
+ contacto_llegada: order.primerApellidoLLegada
59
+ ? order.nombreLLegada + " " + order.primerApellidoLLegada
60
+ : order.nombreLLegada,
61
+ direccion: order.direccionLLegada2
62
+ ? order.direccionLLegada1 + " " + order.direccionLLegada2
63
+ : order.direccionLLegada1,
64
+ cod_pos_llegada: order.codigoPostalLLegada,
65
+ poblacion_llegada: order.ciudadLLegada,
66
+ tlf_llegada: order.telefonoLLegada || "no informado",
67
+ mail_llegada: order.emailLLegada || "no informado",
68
+ });
69
+ });
70
+ const setReembolso = (order) => {
71
+ return { cantidad_contrareembolso: order.cantidadContrareembolso };
72
+ };
73
+ const setFechas = (order) => __awaiter(void 0, void 0, void 0, function* () {
74
+ const date = (0, moment_1.default)().format("YYYY-MM-DD");
75
+ const beginHour = "09:00";
76
+ const afterHour = "13:00";
77
+ if (order.codigoPaisLLegada) {
78
+ const _zonaLLegada = zonaLLegada;
79
+ const oDeliveryDate = yield findNextPickupDate({
80
+ cp_salida: sellerAddress.codigo_postal,
81
+ id_pais_origen: sellerAddress.id_pais,
82
+ id_pais_destino: _zonaLLegada.id_pais,
83
+ idAgencia: agencyId
84
+ });
85
+ if (oDeliveryDate) {
86
+ const deliveryDateFramgents = oDeliveryDate.fecha_recogida_horas.split(",");
87
+ return {
88
+ fecha_recogida: deliveryDateFramgents[0],
89
+ hora_desde: deliveryDateFramgents[1],
90
+ hora_hasta: deliveryDateFramgents[2],
91
+ };
92
+ }
93
+ }
94
+ return {
95
+ fecha_recogida: date,
96
+ hora_desde: beginHour,
97
+ hora_hasta: afterHour,
98
+ };
99
+ });
100
+ const setBultos = (order) => ({
101
+ num_bultos: warehouseId ? 0 : order.lineas.length,
102
+ tipo_envio: type,
103
+ codigo_envio_externo: order.codigoEnvioExterno,
104
+ id_almacen: warehouseId,
105
+ });
106
+ const setters = [setDatosVendedor, setDatosComprador, setUbicacionComprador, setReembolso, setFechas, setBultos];
107
+ const dataInsert = yield setters.reduce((acc, curr) => __awaiter(void 0, void 0, void 0, function* () {
108
+ const accumulated = yield acc;
109
+ const stepResult = yield curr(Object.assign(Object.assign({}, order), accumulated));
110
+ return Object.assign(Object.assign({}, accumulated), stepResult);
111
+ }), {});
112
+ return dataInsert;
113
+ });
114
+ exports.cookTempShipment = cookTempShipment;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/index.ts CHANGED
@@ -38,67 +38,63 @@ export class IntegrationManager {
38
38
  crearBulto,
39
39
  modificarOrdenOriginal,
40
40
  }: InsertParams) {
41
- try {
42
- const estaAgrupado = Number(group?.grouped) === 1;
43
- const { integrationsService, addressesService } = this.services;
44
- // esto hay que pasarlo ya en los parametros
45
- const sellerAddress =
46
- warehouseId > 0
47
- ? await addressesService.getWarehouseAddress(idUsuario)
48
- : await addressesService.getAddress(addressId, idUsuario);
49
-
50
- const allShopifyOrders = await fetchAllOrders();
51
-
52
- const filteredOrders = await integrationsService.filterExternalDuplicatedOrders({
53
- idUsuario,
54
- orders: allShopifyOrders,
55
- type: this.type,
56
- });
41
+ const estaAgrupado = Number(group?.grouped) === 1;
42
+ const { integrationsService, addressesService } = this.services;
43
+ // esto hay que pasarlo ya en los parametros
44
+ const sellerAddress =
45
+ warehouseId > 0
46
+ ? await addressesService.getWarehouseAddress(idUsuario)
47
+ : await addressesService.getAddress(addressId, idUsuario);
57
48
 
58
- const insertOrdersResult = await new this.executionManager({
59
- arrayParams: allShopifyOrders,
60
- executable: async (order: OriginalOrder) => {
61
- const parsedOrder = modificarOrdenOriginal({ originalOrder: order })
62
- const duplicatedTempShipment = await integrationsService.checkDuplicateTempShipment(
63
- parsedOrder.codigoEnvioExterno,
64
- idUsuario,
65
- );
66
- if (duplicatedTempShipment) return;
49
+ const allShopifyOrders = await fetchAllOrders();
67
50
 
68
- return this.insertOrder({
69
- order: parsedOrder,
70
- agencyId,
71
- warehouseId,
72
- sellerAddress,
73
- idUsuario,
74
- group,
75
- });
76
- },
77
- regularExecutionTime: 100, // ms
78
- initialBatchQuantity: 1, // de uno en uno
79
- }).upload();
80
-
81
- const successfullShipments = insertOrdersResult
82
- .filter(Boolean)
83
- .filter((each) => each.status === "fulfilled")
84
- .map((each) => each.value)
85
-
86
- await this.insertQuantityRelatedLines({
87
- orders: successfullShipments,
88
- idUsuario,
89
- crearBulto,
90
- estaAgrupado,
91
- });
51
+ const filteredOrders = await integrationsService.filterExternalDuplicatedOrders({
52
+ idUsuario,
53
+ orders: allShopifyOrders,
54
+ type: this.type,
55
+ });
92
56
 
93
- return filteredOrders;
94
- } catch (e) {
95
- console.log(e)
96
- }
57
+ const insertOrdersResult = await new this.executionManager({
58
+ arrayParams: allShopifyOrders,
59
+ executable: async (order: OriginalOrder) => {
60
+ const parsedOrder = modificarOrdenOriginal({ originalOrder: order })
61
+ const duplicatedTempShipment = await integrationsService.checkDuplicateTempShipment(
62
+ parsedOrder.codigoEnvioExterno,
63
+ idUsuario,
64
+ );
65
+ if (duplicatedTempShipment) return;
66
+
67
+ return this.insertOrder({
68
+ order: parsedOrder,
69
+ agencyId,
70
+ warehouseId,
71
+ sellerAddress,
72
+ idUsuario,
73
+ group,
74
+ });
75
+ },
76
+ regularExecutionTime: 100, // ms
77
+ initialBatchQuantity: 1, // de uno en uno
78
+ }).upload();
79
+
80
+ const successfullShipments = insertOrdersResult
81
+ .filter(Boolean)
82
+ .filter((each) => each.status === "fulfilled")
83
+ .map((each) => each.value)
84
+
85
+ await this.insertQuantityRelatedLines({
86
+ orders: successfullShipments,
87
+ idUsuario,
88
+ crearBulto,
89
+ estaAgrupado,
90
+ });
91
+
92
+ return filteredOrders;
97
93
  }
98
94
 
99
95
  insertOrder = async (
100
96
  { order, agencyId, warehouseId, sellerAddress, idUsuario, group }:
101
- { order: ModifiedOrder, agencyId: number, warehouseId: number, sellerAddress: SellerAddress, idUsuario: number, group: Group }): Promise<ModifiedOrderExtended> => {
97
+ { order: ModifiedOrder, agencyId: number, warehouseId: number, sellerAddress: SellerAddress, idUsuario: number, group: Group }): Promise<ModifiedOrderExtended> => {
102
98
 
103
99
  const idEnvioTemporal = await this.insertTempShipment({
104
100
  order,
@@ -139,7 +135,7 @@ export class IntegrationManager {
139
135
  return await this.services.integrationsService.insertTempShipment(tempShipmentData);
140
136
  };
141
137
 
142
- insertExternalShipment = async ({ idUsuario, order, idEnvioTemporal }:
138
+ insertExternalShipment = async ({ idUsuario, order, idEnvioTemporal }:
143
139
  { idUsuario: number, order: ModifiedOrder, idEnvioTemporal: number }
144
140
  ): Promise<number | false> => {
145
141
  const idEnvioExterno = await this.services.integrationsService.insertExternalShipment({
@@ -155,22 +151,22 @@ export class IntegrationManager {
155
151
  };
156
152
 
157
153
  insertTempShipmentLines = async ({ idEnvioTemporal, group }: {
158
- idEnvioTemporal: number, group: Group
154
+ idEnvioTemporal: number, group: Group
159
155
  }) => {
160
156
  const grouped = group.grouped === 1 || group.grouped === '1';
161
157
  if (grouped)
162
- await this.services.integrationsService.insertTempShipmentLines({
163
- id_envio: idEnvioTemporal,
164
- bulto: {
165
- peso: group.weight,
166
- alto: group.height,
167
- ancho: group.width,
168
- largo: group.length,
169
- },
170
- });
158
+ await this.services.integrationsService.insertTempShipmentLines({
159
+ id_envio: idEnvioTemporal,
160
+ bulto: {
161
+ peso: group.weight,
162
+ alto: group.height,
163
+ ancho: group.width,
164
+ largo: group.length,
165
+ },
166
+ });
171
167
  };
172
168
 
173
- async insertQuantityRelatedLines({ orders, idUsuario, crearBulto, estaAgrupado }:
169
+ async insertQuantityRelatedLines({ orders, idUsuario, crearBulto, estaAgrupado }:
174
170
  {
175
171
  orders: ModifiedOrderExtended[], idUsuario: number, crearBulto: CrearBulto, estaAgrupado: boolean
176
172
  }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "cardus",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "an integration manager",
5
- "main": "index.js",
5
+ "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "scripts": {
8
8
  "build": "rm -rf dist && tsc --build",