cardus 0.0.4 → 0.0.6
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/.eslintignore +2 -0
- package/.eslintrc +14 -0
- package/.prettierrc +8 -0
- package/dist/index.js +65 -62
- package/dist/insertTempShipment/index.js +30 -21
- package/index.ts +263 -194
- package/insertTempShipment/index.ts +156 -138
- package/package.json +16 -6
- package/types/index.ts +162 -136
package/index.ts
CHANGED
|
@@ -1,220 +1,289 @@
|
|
|
1
|
-
import moment from
|
|
2
|
-
import { cookTempShipment } from
|
|
3
|
-
import {
|
|
1
|
+
import moment from 'moment';
|
|
2
|
+
import { cookTempShipment } from './insertTempShipment';
|
|
3
|
+
import {
|
|
4
|
+
ModifiedOrderExtended,
|
|
5
|
+
CrearBulto,
|
|
6
|
+
Group,
|
|
7
|
+
FindNextPickupDate,
|
|
8
|
+
ExecutionManager,
|
|
9
|
+
InsertParams,
|
|
10
|
+
CountriesService,
|
|
11
|
+
AddressesService,
|
|
12
|
+
IntegrationsService,
|
|
13
|
+
Type,
|
|
14
|
+
SellerAddress,
|
|
15
|
+
OriginalOrder,
|
|
16
|
+
ModifiedOrder
|
|
17
|
+
} from './types';
|
|
4
18
|
|
|
5
19
|
interface Services {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
20
|
+
integrationsService: IntegrationsService;
|
|
21
|
+
addressesService: AddressesService;
|
|
22
|
+
countriesService: CountriesService;
|
|
9
23
|
}
|
|
10
24
|
|
|
11
25
|
export class IntegrationManager {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
constructor(params: {
|
|
18
|
-
integrationsService: IntegrationsService,
|
|
19
|
-
addressesService: AddressesService,
|
|
20
|
-
countriesService: CountriesService,
|
|
21
|
-
type: Type,
|
|
22
|
-
executionManager: ExecutionManager,
|
|
23
|
-
findNextPickupDate: FindNextPickupDate,
|
|
24
|
-
}) {
|
|
25
|
-
this.services = {
|
|
26
|
-
integrationsService: params.integrationsService,
|
|
27
|
-
addressesService: params.addressesService,
|
|
28
|
-
countriesService: params.countriesService,
|
|
29
|
-
};
|
|
30
|
-
this.type = params.type;
|
|
31
|
-
this.executionManager = params.executionManager;
|
|
32
|
-
this.findNextPickupDate = params.findNextPickupDate;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async insertTempShipments({
|
|
36
|
-
payload: { idUsuario, agencyId, addressId, warehouseId, group },
|
|
37
|
-
fetchAllOrders,
|
|
38
|
-
crearBulto,
|
|
39
|
-
modificarOrdenOriginal,
|
|
40
|
-
}: InsertParams) {
|
|
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);
|
|
48
|
-
|
|
49
|
-
const allShopifyOrders = await fetchAllOrders();
|
|
50
|
-
|
|
51
|
-
const filteredOrders = await integrationsService.filterExternalDuplicatedOrders({
|
|
52
|
-
idUsuario,
|
|
53
|
-
orders: allShopifyOrders,
|
|
54
|
-
type: this.type,
|
|
55
|
-
});
|
|
26
|
+
services: Services;
|
|
27
|
+
type: Type;
|
|
28
|
+
executionManager: ExecutionManager;
|
|
29
|
+
findNextPickupDate: FindNextPickupDate;
|
|
56
30
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
});
|
|
31
|
+
constructor(params: {
|
|
32
|
+
integrationsService: IntegrationsService;
|
|
33
|
+
addressesService: AddressesService;
|
|
34
|
+
countriesService: CountriesService;
|
|
35
|
+
type: Type;
|
|
36
|
+
executionManager: ExecutionManager;
|
|
37
|
+
findNextPickupDate: FindNextPickupDate;
|
|
38
|
+
}) {
|
|
39
|
+
this.services = {
|
|
40
|
+
integrationsService: params.integrationsService,
|
|
41
|
+
addressesService: params.addressesService,
|
|
42
|
+
countriesService: params.countriesService
|
|
43
|
+
};
|
|
44
|
+
this.type = params.type;
|
|
45
|
+
this.executionManager = params.executionManager;
|
|
46
|
+
this.findNextPickupDate = params.findNextPickupDate;
|
|
47
|
+
}
|
|
91
48
|
|
|
92
|
-
|
|
93
|
-
}
|
|
49
|
+
async insertTempShipments({
|
|
50
|
+
payload: { idUsuario, agencyId, addressId, warehouseId, group },
|
|
51
|
+
fetchAllOrders,
|
|
52
|
+
crearBulto,
|
|
53
|
+
modificarOrdenOriginal
|
|
54
|
+
}: InsertParams) {
|
|
55
|
+
const estaAgrupado = Number(group?.grouped) === 1;
|
|
56
|
+
const { integrationsService, addressesService } = this.services;
|
|
57
|
+
// esto hay que pasarlo ya en los parametros
|
|
58
|
+
const sellerAddress =
|
|
59
|
+
warehouseId > 0
|
|
60
|
+
? await addressesService.getWarehouseAddress(idUsuario)
|
|
61
|
+
: await addressesService.getAddress(addressId, idUsuario);
|
|
94
62
|
|
|
95
|
-
|
|
96
|
-
{ order, agencyId, warehouseId, sellerAddress, idUsuario, group }:
|
|
97
|
-
{ order: ModifiedOrder, agencyId: number, warehouseId: number, sellerAddress: SellerAddress, idUsuario: number, group: Group }): Promise<ModifiedOrderExtended> => {
|
|
63
|
+
const allShopifyOrders = await fetchAllOrders();
|
|
98
64
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
});
|
|
65
|
+
const filteredOrders =
|
|
66
|
+
await integrationsService.filterExternalDuplicatedOrders({
|
|
67
|
+
idUsuario,
|
|
68
|
+
orders: allShopifyOrders,
|
|
69
|
+
type: this.type
|
|
70
|
+
});
|
|
106
71
|
|
|
107
|
-
|
|
72
|
+
const insertOrdersResult = await new this.executionManager({
|
|
73
|
+
arrayParams: allShopifyOrders,
|
|
74
|
+
executable: async (order: OriginalOrder) => {
|
|
75
|
+
const parsedOrder = modificarOrdenOriginal({ originalOrder: order });
|
|
76
|
+
const duplicatedTempShipment =
|
|
77
|
+
await integrationsService.checkDuplicateTempShipment(
|
|
78
|
+
parsedOrder.codigoEnvioExterno,
|
|
79
|
+
idUsuario
|
|
80
|
+
);
|
|
81
|
+
if (duplicatedTempShipment) return;
|
|
108
82
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
83
|
+
return this.insertOrder({
|
|
84
|
+
order: parsedOrder,
|
|
85
|
+
agencyId,
|
|
86
|
+
warehouseId,
|
|
87
|
+
sellerAddress,
|
|
88
|
+
idUsuario,
|
|
89
|
+
group
|
|
113
90
|
});
|
|
91
|
+
},
|
|
92
|
+
regularExecutionTime: 100, // ms
|
|
93
|
+
initialBatchQuantity: 1 // de uno en uno
|
|
94
|
+
}).upload();
|
|
114
95
|
|
|
115
|
-
|
|
96
|
+
const successfullShipments = insertOrdersResult
|
|
97
|
+
.filter(Boolean)
|
|
98
|
+
.filter((each) => each.status === 'fulfilled')
|
|
99
|
+
.map((each) => each.value);
|
|
116
100
|
|
|
117
|
-
|
|
101
|
+
await this.insertQuantityRelatedLines({
|
|
102
|
+
orders: successfullShipments,
|
|
103
|
+
idUsuario,
|
|
104
|
+
crearBulto,
|
|
105
|
+
estaAgrupado
|
|
106
|
+
});
|
|
118
107
|
|
|
119
|
-
|
|
120
|
-
|
|
108
|
+
return filteredOrders;
|
|
109
|
+
}
|
|
121
110
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
111
|
+
insertOrder = async ({
|
|
112
|
+
order,
|
|
113
|
+
agencyId,
|
|
114
|
+
warehouseId,
|
|
115
|
+
sellerAddress,
|
|
116
|
+
idUsuario,
|
|
117
|
+
group
|
|
118
|
+
}: {
|
|
119
|
+
order: ModifiedOrder;
|
|
120
|
+
agencyId: number;
|
|
121
|
+
warehouseId: number;
|
|
122
|
+
sellerAddress: SellerAddress;
|
|
123
|
+
idUsuario: number;
|
|
124
|
+
group: Group;
|
|
125
|
+
}): Promise<ModifiedOrderExtended> => {
|
|
126
|
+
const idEnvioTemporal = await this.insertTempShipment({
|
|
127
|
+
order,
|
|
128
|
+
agencyId,
|
|
129
|
+
warehouseId,
|
|
130
|
+
sellerAddress,
|
|
131
|
+
idUsuario
|
|
132
|
+
});
|
|
137
133
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
): Promise<number | false> => {
|
|
141
|
-
const idEnvioExterno = await this.services.integrationsService.insertExternalShipment({
|
|
142
|
-
id_envio_temporal_usuario: idEnvioTemporal,
|
|
143
|
-
codigo_envio_externo: order.codigoEnvioExterno,
|
|
144
|
-
id_usuario: idUsuario,
|
|
145
|
-
fecha_hora_creacion: moment().format("YYYY-MM-DD HH:mm:ss"),
|
|
146
|
-
servicio: this.type,
|
|
147
|
-
id_interno_shopify: order.idInternoShopify,
|
|
148
|
-
});
|
|
134
|
+
if (!idEnvioTemporal)
|
|
135
|
+
throw new Error('Temp shipments could not be created');
|
|
149
136
|
|
|
150
|
-
|
|
151
|
-
|
|
137
|
+
await this.insertExternalShipment({
|
|
138
|
+
idUsuario,
|
|
139
|
+
order,
|
|
140
|
+
idEnvioTemporal
|
|
141
|
+
});
|
|
152
142
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
const grouped = group.grouped === 1 || group.grouped === '1';
|
|
157
|
-
if (grouped)
|
|
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
|
-
});
|
|
167
|
-
};
|
|
143
|
+
const grouped = Number(group?.grouped === 1);
|
|
144
|
+
|
|
145
|
+
if (grouped) await this.insertTempShipmentLines({ idEnvioTemporal, group });
|
|
168
146
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
147
|
+
return Object.assign(order, { idEnvioTemporal });
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
insertTempShipment = async ({
|
|
151
|
+
order,
|
|
152
|
+
agencyId,
|
|
153
|
+
warehouseId,
|
|
154
|
+
sellerAddress,
|
|
155
|
+
idUsuario
|
|
156
|
+
}: {
|
|
157
|
+
order: ModifiedOrder;
|
|
158
|
+
agencyId: number;
|
|
159
|
+
warehouseId: number;
|
|
160
|
+
sellerAddress: SellerAddress;
|
|
161
|
+
idUsuario: number;
|
|
162
|
+
}) => {
|
|
163
|
+
const tempShipmentData = await cookTempShipment({
|
|
164
|
+
order,
|
|
165
|
+
type: this.type,
|
|
166
|
+
agencyId,
|
|
167
|
+
warehouseId,
|
|
168
|
+
sellerAddress,
|
|
169
|
+
idUsuario,
|
|
170
|
+
findNextPickupDate: this.findNextPickupDate,
|
|
171
|
+
countriesService: this.services.countriesService
|
|
172
|
+
});
|
|
173
|
+
return await this.services.integrationsService.insertTempShipment(
|
|
174
|
+
tempShipmentData
|
|
175
|
+
);
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
insertExternalShipment = async ({
|
|
179
|
+
idUsuario,
|
|
180
|
+
order,
|
|
181
|
+
idEnvioTemporal
|
|
182
|
+
}: {
|
|
183
|
+
idUsuario: number;
|
|
184
|
+
order: ModifiedOrder;
|
|
185
|
+
idEnvioTemporal: number;
|
|
186
|
+
}): Promise<number | false> => {
|
|
187
|
+
const idEnvioExterno =
|
|
188
|
+
await this.services.integrationsService.insertExternalShipment({
|
|
189
|
+
id_envio_temporal_usuario: idEnvioTemporal,
|
|
190
|
+
codigo_envio_externo: order.codigoEnvioExterno,
|
|
191
|
+
id_usuario: idUsuario,
|
|
192
|
+
fecha_hora_creacion: moment().format('YYYY-MM-DD HH:mm:ss'),
|
|
193
|
+
servicio: this.type,
|
|
194
|
+
id_interno_shopify: order.idInternoShopify
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
return idEnvioExterno;
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
insertTempShipmentLines = async ({
|
|
201
|
+
idEnvioTemporal,
|
|
202
|
+
group
|
|
203
|
+
}: {
|
|
204
|
+
idEnvioTemporal: number;
|
|
205
|
+
group: Group;
|
|
206
|
+
}) => {
|
|
207
|
+
const grouped = group.grouped === 1 || group.grouped === '1';
|
|
208
|
+
if (grouped)
|
|
209
|
+
await this.services.integrationsService.insertTempShipmentLines({
|
|
210
|
+
id_envio: idEnvioTemporal,
|
|
211
|
+
bulto: {
|
|
212
|
+
peso: group.weight,
|
|
213
|
+
alto: group.height,
|
|
214
|
+
ancho: group.width,
|
|
215
|
+
largo: group.length
|
|
172
216
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
217
|
+
});
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
async insertQuantityRelatedLines({
|
|
221
|
+
orders,
|
|
222
|
+
idUsuario,
|
|
223
|
+
crearBulto,
|
|
224
|
+
estaAgrupado
|
|
225
|
+
}: {
|
|
226
|
+
orders: ModifiedOrderExtended[];
|
|
227
|
+
idUsuario: number;
|
|
228
|
+
crearBulto: CrearBulto;
|
|
229
|
+
estaAgrupado: boolean;
|
|
230
|
+
}) {
|
|
231
|
+
const { integrationsService } = this.services;
|
|
232
|
+
const quantityRelatedFunctions: Array<Promise<undefined>> = [];
|
|
233
|
+
|
|
234
|
+
const obtenerBultosYCantidades = orders.map(
|
|
235
|
+
async ({ idEnvioTemporal, lineas }) => {
|
|
236
|
+
if (!idEnvioTemporal) return null;
|
|
237
|
+
return await Promise.all(
|
|
238
|
+
lineas.map(async (line) => {
|
|
239
|
+
const { bulto, cantidad = 0 } = await crearBulto({
|
|
240
|
+
lineItems: { idEnvioTemporal, line },
|
|
241
|
+
idUsuario
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
if (cantidad === 0) return null;
|
|
245
|
+
|
|
246
|
+
return { bulto, cantidad, idEnvioTemporal };
|
|
247
|
+
})
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
const bultosYCantidades = (await new Promise(async (res) => {
|
|
253
|
+
const bultosYCantidadesAnidados = await Promise.all(
|
|
254
|
+
obtenerBultosYCantidades
|
|
255
|
+
);
|
|
256
|
+
const bultosYCantidades = bultosYCantidadesAnidados
|
|
257
|
+
.flat()
|
|
258
|
+
.filter(Boolean);
|
|
259
|
+
res(bultosYCantidades);
|
|
260
|
+
})) as Array<any>;
|
|
261
|
+
|
|
262
|
+
bultosYCantidades.forEach(({ bulto, cantidad, idEnvioTemporal }) => {
|
|
263
|
+
try {
|
|
264
|
+
const arrayFake = Array.from({ length: Number(cantidad) });
|
|
265
|
+
arrayFake.forEach(() => {
|
|
266
|
+
if (!estaAgrupado) {
|
|
267
|
+
quantityRelatedFunctions.push(
|
|
268
|
+
integrationsService.insertTempShipmentLines({
|
|
269
|
+
id_envio: idEnvioTemporal,
|
|
270
|
+
bulto
|
|
271
|
+
})
|
|
190
272
|
);
|
|
191
|
-
|
|
273
|
+
}
|
|
192
274
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
bultosYCantidades.forEach(({ bulto, cantidad, idEnvioTemporal }) => {
|
|
200
|
-
try {
|
|
201
|
-
const arrayFake = Array.from({ length: Number(cantidad) });
|
|
202
|
-
arrayFake.forEach(() => {
|
|
203
|
-
if (!estaAgrupado) {
|
|
204
|
-
quantityRelatedFunctions.push(
|
|
205
|
-
integrationsService.insertTempShipmentLines({ id_envio: idEnvioTemporal, bulto }),
|
|
206
|
-
);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
quantityRelatedFunctions.push(
|
|
210
|
-
integrationsService.insertTempDetailsShipment({ id_envio: idEnvioTemporal, bulto }),
|
|
211
|
-
);
|
|
212
|
-
});
|
|
213
|
-
} catch (e) {
|
|
214
|
-
console.log(e);
|
|
215
|
-
}
|
|
275
|
+
quantityRelatedFunctions.push(
|
|
276
|
+
integrationsService.insertTempDetailsShipment({
|
|
277
|
+
id_envio: idEnvioTemporal,
|
|
278
|
+
bulto
|
|
279
|
+
})
|
|
280
|
+
);
|
|
216
281
|
});
|
|
282
|
+
} catch (e) {
|
|
283
|
+
console.log(e);
|
|
284
|
+
}
|
|
285
|
+
});
|
|
217
286
|
|
|
218
|
-
|
|
219
|
-
|
|
287
|
+
await Promise.allSettled(quantityRelatedFunctions);
|
|
288
|
+
}
|
|
220
289
|
}
|