|
1
|
import { |
|
2
|
CalculatedShippingOptionPrice, |
|
3
|
Context, |
|
4
|
DAL, |
|
5
|
FilterableFulfillmentSetProps, |
|
6
|
FindConfig, |
|
7
|
FulfillmentDTO, |
|
8
|
FulfillmentOption, |
|
9
|
FulfillmentTypes, |
|
10
|
IFulfillmentModuleService, |
|
11
|
InferEntityType, |
|
12
|
InternalModuleDeclaration, |
|
13
|
Logger, |
|
14
|
ModuleJoinerConfig, |
|
15
|
ModulesSdkTypes, |
|
16
|
ShippingOptionDTO, |
|
17
|
SoftDeleteReturn, |
|
18
|
UpdateFulfillmentSetDTO, |
|
19
|
UpdateServiceZoneDTO, |
|
20
|
ValidateFulfillmentDataContext, |
|
21
|
} from "@medusajs/framework/types" |
|
22
|
import { |
|
23
|
arrayDifference, |
|
24
|
deepCopy, |
|
25
|
deepEqualObj, |
|
26
|
EmitEvents, |
|
27
|
getSetDifference, |
|
28
|
InjectManager, |
|
29
|
InjectTransactionManager, |
|
30
|
isDefined, |
|
31
|
isPresent, |
|
32
|
isString, |
|
33
|
MedusaContext, |
|
34
|
MedusaError, |
|
35
|
ModulesSdkUtils, |
|
36
|
promiseAll, |
|
37
|
} from "@medusajs/framework/utils" |
|
38
|
import { |
|
39
|
Fulfillment, |
|
40
|
FulfillmentProvider, |
|
41
|
FulfillmentSet, |
|
42
|
GeoZone, |
|
43
|
ServiceZone, |
|
44
|
ShippingOption, |
|
45
|
ShippingOptionRule, |
|
46
|
ShippingOptionType, |
|
47
|
ShippingProfile, |
|
48
|
} from "@models" |
|
49
|
import { isContextValid, Rule, validateAndNormalizeRules } from "@utils" |
|
50
|
import { joinerConfig } from "../joiner-config" |
|
51
|
import { UpdateShippingOptionsInput } from "../types/service" |
|
52
|
import FulfillmentProviderService from "./fulfillment-provider" |
|
53
|
|
|
54
|
const generateMethodForModels = { |
|
55
|
FulfillmentSet, |
|
56
|
ServiceZone, |
|
57
|
ShippingOption, |
|
58
|
GeoZone, |
|
59
|
ShippingProfile, |
|
60
|
ShippingOptionRule, |
|
61
|
ShippingOptionType, |
|
62
|
FulfillmentProvider, |
|
63
|
// Not adding Fulfillment to not auto generate the methods under the hood and only provide the methods we want to expose |
|
64
|
} |
|
65
|
|
|
66
|
type InjectedDependencies = { |
|
67
|
baseRepository: DAL.RepositoryService |
|
68
|
fulfillmentAddressService: ModulesSdkTypes.IMedusaInternalService<any> |
|
69
|
fulfillmentSetService: ModulesSdkTypes.IMedusaInternalService<any> |
|
70
|
serviceZoneService: ModulesSdkTypes.IMedusaInternalService<any> |
|
71
|
geoZoneService: ModulesSdkTypes.IMedusaInternalService<any> |
|
72
|
shippingProfileService: ModulesSdkTypes.IMedusaInternalService<any> |
|
73
|
shippingOptionService: ModulesSdkTypes.IMedusaInternalService<any> |
|
74
|
shippingOptionRuleService: ModulesSdkTypes.IMedusaInternalService<any> |
|
75
|
shippingOptionTypeService: ModulesSdkTypes.IMedusaInternalService<any> |
|
76
|
fulfillmentProviderService: FulfillmentProviderService |
|
77
|
fulfillmentService: ModulesSdkTypes.IMedusaInternalService<any> |
|
78
|
logger?: Logger |
|
79
|
} |
|
80
|
|
|
81
|
export default class FulfillmentModuleService |
|
82
|
extends ModulesSdkUtils.MedusaService<{ |
|
83
|
FulfillmentSet: { dto: FulfillmentTypes.FulfillmentSetDTO } |
|
84
|
ServiceZone: { dto: FulfillmentTypes.ServiceZoneDTO } |
|
85
|
ShippingOption: { dto: FulfillmentTypes.ShippingOptionDTO } |
|
86
|
GeoZone: { dto: FulfillmentTypes.GeoZoneDTO } |
|
87
|
ShippingProfile: { dto: FulfillmentTypes.ShippingProfileDTO } |
|
88
|
ShippingOptionRule: { dto: FulfillmentTypes.ShippingOptionRuleDTO } |
|
89
|
ShippingOptionType: { dto: FulfillmentTypes.ShippingOptionTypeDTO } |
|
90
|
FulfillmentProvider: { dto: FulfillmentTypes.FulfillmentProviderDTO } |
|
91
|
}>(generateMethodForModels) |
|
92
|
implements IFulfillmentModuleService |
|
93
|
{ |
|
94
|
protected baseRepository_: DAL.RepositoryService |
|
95
|
protected readonly fulfillmentSetService_: ModulesSdkTypes.IMedusaInternalService< |
|
96
|
InferEntityType<typeof FulfillmentSet> |
|
97
|
> |
|
98
|
protected readonly serviceZoneService_: ModulesSdkTypes.IMedusaInternalService< |
|
99
|
InferEntityType<typeof ServiceZone> |
|
100
|
> |
|
101
|
protected readonly geoZoneService_: ModulesSdkTypes.IMedusaInternalService< |
|
102
|
InferEntityType<typeof GeoZone> |
|
103
|
> |
|
104
|
protected readonly shippingProfileService_: ModulesSdkTypes.IMedusaInternalService< |
|
105
|
InferEntityType<typeof ShippingProfile> |
|
106
|
> |
|
107
|
protected readonly shippingOptionService_: ModulesSdkTypes.IMedusaInternalService< |
|
108
|
InferEntityType<typeof ShippingOption> |
|
109
|
> |
|
110
|
protected readonly shippingOptionRuleService_: ModulesSdkTypes.IMedusaInternalService< |
|
111
|
InferEntityType<typeof ShippingOptionRule> |
|
112
|
> |
|
113
|
protected readonly shippingOptionTypeService_: ModulesSdkTypes.IMedusaInternalService< |
|
114
|
InferEntityType<typeof ShippingOptionType> |
|
115
|
> |
|
116
|
protected readonly fulfillmentProviderService_: FulfillmentProviderService |
|
117
|
protected readonly fulfillmentService_: ModulesSdkTypes.IMedusaInternalService< |
|
118
|
InferEntityType<typeof Fulfillment> |
|
119
|
> |
|
120
|
|
|
121
|
constructor( |
|
122
|
{ |
|
123
|
baseRepository, |
|
124
|
fulfillmentSetService, |
|
125
|
serviceZoneService, |
|
126
|
geoZoneService, |
|
127
|
shippingProfileService, |
|
128
|
shippingOptionService, |
|
129
|
shippingOptionRuleService, |
|
130
|
shippingOptionTypeService, |
|
131
|
fulfillmentProviderService, |
|
132
|
fulfillmentService, |
|
133
|
fulfillmentAddressService, |
|
134
|
}: InjectedDependencies, |
|
135
|
protected readonly moduleDeclaration: InternalModuleDeclaration |
|
136
|
) { |
|
137
|
// @ts-ignore |
|
138
|
super(...arguments) |
|
139
|
this.baseRepository_ = baseRepository |
|
140
|
this.fulfillmentSetService_ = fulfillmentSetService |
|
141
|
this.serviceZoneService_ = serviceZoneService |
|
142
|
this.geoZoneService_ = geoZoneService |
|
143
|
this.shippingProfileService_ = shippingProfileService |
|
144
|
this.shippingOptionService_ = shippingOptionService |
|
145
|
this.shippingOptionRuleService_ = shippingOptionRuleService |
|
146
|
this.shippingOptionTypeService_ = shippingOptionTypeService |
|
147
|
this.fulfillmentProviderService_ = fulfillmentProviderService |
|
148
|
this.fulfillmentService_ = fulfillmentService |
|
149
|
} |
|
150
|
|
|
151
|
__joinerConfig(): ModuleJoinerConfig { |
|
152
|
return joinerConfig |
|
153
|
} |
|
154
|
|
|
155
|
@InjectManager() |
|
156
|
// @ts-ignore |
|
157
|
async listShippingOptions( |
|
158
|
filters: FulfillmentTypes.FilterableShippingOptionForContextProps = {}, |
|
159
|
config: FindConfig<FulfillmentTypes.ShippingOptionDTO> = {}, |
|
160
|
@MedusaContext() sharedContext: Context = {} |
|
161
|
): Promise<FulfillmentTypes.ShippingOptionDTO[]> { |
|
162
|
// Eventually, we could call normalizeListShippingOptionsForContextParams to translate the address and make a and condition with the other filters |
|
163
|
// In that case we could remote the address check below |
|
164
|
if (filters?.context || filters?.address) { |
|
165
|
return await this.listShippingOptionsForContext( |
|
166
|
filters, |
|
167
|
config, |
|
168
|
sharedContext |
|
169
|
) |
|
170
|
} |
|
171
|
|
|
172
|
return await super.listShippingOptions(filters, config, sharedContext) |
|
173
|
} |
|
174
|
|
|
175
|
@InjectManager() |
|
176
|
async listShippingOptionsForContext( |
|
177
|
filters: FulfillmentTypes.FilterableShippingOptionForContextProps, |
|
178
|
config: FindConfig<ShippingOptionDTO> = {}, |
|
179
|
@MedusaContext() sharedContext: Context = {} |
|
180
|
): Promise<FulfillmentTypes.ShippingOptionDTO[]> { |
|
181
|
const { |
|
182
|
context, |
|
183
|
config: normalizedConfig, |
|
184
|
filters: normalizedFilters, |
|
185
|
} = FulfillmentModuleService.normalizeListShippingOptionsForContextParams( |
|
186
|
filters, |
|
187
|
config |
|
188
|
) |
|
189
|
|
|
190
|
let shippingOptions = await this.shippingOptionService_.list( |
|
191
|
normalizedFilters, |
|
192
|
normalizedConfig, |
|
193
|
sharedContext |
|
194
|
) |
|
195
|
|
|
196
|
if (context) { |
|
197
|
shippingOptions = shippingOptions.filter((shippingOption) => { |
|
198
|
if (!shippingOption.rules?.length) { |
|
199
|
return true |
|
200
|
} |
|
201
|
|
|
202
|
return isContextValid( |
|
203
|
context, |
|
204
|
shippingOption.rules.map((r) => r) as unknown as Rule[] |
|
205
|
) |
|
206
|
}) |
|
207
|
} |
|
208
|
|
|
209
|
return await this.baseRepository_.serialize< |
|
210
|
FulfillmentTypes.ShippingOptionDTO[] |
|
211
|
>(shippingOptions) |
|
212
|
} |
|
213
|
|
|
214
|
@InjectManager() |
|
215
|
async retrieveFulfillment( |
|
216
|
id: string, |
|
217
|
config: FindConfig<FulfillmentTypes.FulfillmentDTO> = {}, |
|
218
|
@MedusaContext() sharedContext: Context = {} |
|
219
|
): Promise<FulfillmentTypes.FulfillmentDTO> { |
|
220
|
const fulfillment = await this.fulfillmentService_.retrieve( |
|
221
|
id, |
|
222
|
config, |
|
223
|
sharedContext |
|
224
|
) |
|
225
|
|
|
226
|
return await this.baseRepository_.serialize<FulfillmentTypes.FulfillmentDTO>( |
|
227
|
fulfillment |
|
228
|
) |
|
229
|
} |
|
230
|
|
|
231
|
@InjectManager() |
|
232
|
async listFulfillments( |
|
233
|
filters: FulfillmentTypes.FilterableFulfillmentProps = {}, |
|
234
|
config: FindConfig<FulfillmentTypes.FulfillmentDTO> = {}, |
|
235
|
@MedusaContext() sharedContext: Context = {} |
|
236
|
): Promise<FulfillmentTypes.FulfillmentDTO[]> { |
|
237
|
const fulfillments = await this.fulfillmentService_.list( |
|
238
|
filters, |
|
239
|
config, |
|
240
|
sharedContext |
|
241
|
) |
|
242
|
|
|
243
|
return await this.baseRepository_.serialize< |
|
244
|
FulfillmentTypes.FulfillmentDTO[] |
|
245
|
>(fulfillments) |
|
246
|
} |
|
247
|
|
|
248
|
@InjectManager() |
|
249
|
async listAndCountFulfillments( |
|
250
|
filters?: FilterableFulfillmentSetProps, |
|
251
|
config?: FindConfig<FulfillmentDTO>, |
|
252
|
@MedusaContext() sharedContext: Context = {} |
|
253
|
): Promise<[FulfillmentDTO[], number]> { |
|
254
|
const [fulfillments, count] = await this.fulfillmentService_.listAndCount( |
|
255
|
filters, |
|
256
|
config, |
|
257
|
sharedContext |
|
258
|
) |
|
259
|
|
|
260
|
return [ |
|
261
|
await this.baseRepository_.serialize<FulfillmentTypes.FulfillmentDTO[]>( |
|
262
|
fulfillments |
|
263
|
), |
|
264
|
count, |
|
265
|
] |
|
266
|
} |
|
267
|
|
|
268
|
// @ts-expect-error |
|
269
|
createFulfillmentSets( |
|
270
|
data: FulfillmentTypes.CreateFulfillmentSetDTO[], |
|
271
|
sharedContext?: Context |
|
272
|
): Promise<FulfillmentTypes.FulfillmentSetDTO[]> |
|
273
|
// @ts-expect-error |
|
274
|
createFulfillmentSets( |
|
275
|
data: FulfillmentTypes.CreateFulfillmentSetDTO, |
|
276
|
sharedContext?: Context |
|
277
|
): Promise<FulfillmentTypes.FulfillmentSetDTO> |
|
278
|
|
|
279
|
@InjectManager() |
|
280
|
@EmitEvents() |
|
281
|
// @ts-expect-error |
|
282
|
async createFulfillmentSets( |
|
283
|
data: |
|
284
|
| FulfillmentTypes.CreateFulfillmentSetDTO |
|
285
|
| FulfillmentTypes.CreateFulfillmentSetDTO[], |
|
286
|
@MedusaContext() sharedContext: Context = {} |
|
287
|
): Promise< |
|
288
|
FulfillmentTypes.FulfillmentSetDTO | FulfillmentTypes.FulfillmentSetDTO[] |
|
289
|
> { |
|
290
|
const createdFulfillmentSets = await this.createFulfillmentSets_( |
|
291
|
data, |
|
292
|
sharedContext |
|
293
|
) |
|
294
|
|
|
295
|
const returnedFulfillmentSets = Array.isArray(data) |
|
296
|
? createdFulfillmentSets |
|
297
|
: createdFulfillmentSets[0] |
|
298
|
|
|
299
|
return await this.baseRepository_.serialize< |
|
300
|
FulfillmentTypes.FulfillmentSetDTO | FulfillmentTypes.FulfillmentSetDTO[] |
|
301
|
>(returnedFulfillmentSets) |
|
302
|
} |
|
303
|
|
|
304
|
@InjectTransactionManager() |
|
305
|
protected async createFulfillmentSets_( |
|
306
|
data: |
|
307
|
| FulfillmentTypes.CreateFulfillmentSetDTO |
|
308
|
| FulfillmentTypes.CreateFulfillmentSetDTO[], |
|
309
|
@MedusaContext() sharedContext: Context = {} |
|
310
|
): Promise<InferEntityType<typeof FulfillmentSet>[]> { |
|
311
|
const data_ = Array.isArray(data) ? data : [data] |
|
312
|
|
|
313
|
if (!data_.length) { |
|
314
|
return [] |
|
315
|
} |
|
316
|
|
|
317
|
for (const fulfillmentSet of data_) { |
|
318
|
if (fulfillmentSet.service_zones?.length) { |
|
319
|
for (const serviceZone of fulfillmentSet.service_zones) { |
|
320
|
if (serviceZone.geo_zones?.length) { |
|
321
|
FulfillmentModuleService.validateGeoZones(serviceZone.geo_zones) |
|
322
|
} |
|
323
|
} |
|
324
|
} |
|
325
|
} |
|
326
|
|
|
327
|
const createdFulfillmentSets = await this.fulfillmentSetService_.create( |
|
328
|
data_, |
|
329
|
sharedContext |
|
330
|
) |
|
331
|
|
|
332
|
return createdFulfillmentSets |
|
333
|
} |
|
334
|
|
|
335
|
// @ts-ignore |
|
336
|
createServiceZones( |
|
337
|
data: FulfillmentTypes.CreateServiceZoneDTO[], |
|
338
|
sharedContext?: Context |
|
339
|
): Promise<FulfillmentTypes.ServiceZoneDTO[]> |
|
340
|
// @ts-expect-error |
|
341
|
createServiceZones( |
|
342
|
data: FulfillmentTypes.CreateServiceZoneDTO, |
|
343
|
sharedContext?: Context |
|
344
|
): Promise<FulfillmentTypes.ServiceZoneDTO> |
|
345
|
|
|
346
|
@InjectManager() |
|
347
|
@EmitEvents() |
|
348
|
// @ts-expect-error |
|
349
|
async createServiceZones( |
|
350
|
data: |
|
351
|
| FulfillmentTypes.CreateServiceZoneDTO[] |
|
352
|
| FulfillmentTypes.CreateServiceZoneDTO, |
|
353
|
@MedusaContext() sharedContext: Context = {} |
|
354
|
): Promise< |
|
355
|
FulfillmentTypes.ServiceZoneDTO | FulfillmentTypes.ServiceZoneDTO[] |
|
356
|
> { |
|
357
|
const createdServiceZones = await this.createServiceZones_( |
|
358
|
data, |
|
359
|
sharedContext |
|
360
|
) |
|
361
|
|
|
362
|
return await this.baseRepository_.serialize< |
|
363
|
FulfillmentTypes.ServiceZoneDTO | FulfillmentTypes.ServiceZoneDTO[] |
|
364
|
>(Array.isArray(data) ? createdServiceZones : createdServiceZones[0]) |
|
365
|
} |
|
366
|
|
|
367
|
@InjectTransactionManager() |
|
368
|
protected async createServiceZones_( |
|
369
|
data: |
|
370
|
| FulfillmentTypes.CreateServiceZoneDTO[] |
|
371
|
| FulfillmentTypes.CreateServiceZoneDTO, |
|
372
|
@MedusaContext() sharedContext: Context = {} |
|
373
|
): Promise<InferEntityType<typeof ServiceZone>[]> { |
|
374
|
const data_ = Array.isArray(data) ? data : [data] |
|
375
|
|
|
376
|
if (!data_.length) { |
|
377
|
return [] |
|
378
|
} |
|
379
|
|
|
380
|
for (const serviceZone of data_) { |
|
381
|
if (serviceZone.geo_zones?.length) { |
|
382
|
if (serviceZone.geo_zones?.length) { |
|
383
|
FulfillmentModuleService.validateGeoZones(serviceZone.geo_zones) |
|
384
|
} |
|
385
|
} |
|
386
|
} |
|
387
|
|
|
388
|
const createdServiceZones = await this.serviceZoneService_.create( |
|
389
|
data_, |
|
390
|
sharedContext |
|
391
|
) |
|
392
|
|
|
393
|
return createdServiceZones |
|
394
|
} |
|
395
|
|
|
396
|
// @ts-ignore |
|
397
|
createShippingOptions( |
|
398
|
data: FulfillmentTypes.CreateShippingOptionDTO[], |
|
399
|
sharedContext?: Context |
|
400
|
): Promise<FulfillmentTypes.ShippingOptionDTO[]> |
|
401
|
// @ts-expect-error |
|
402
|
createShippingOptions( |
|
403
|
data: FulfillmentTypes.CreateShippingOptionDTO, |
|
404
|
sharedContext?: Context |
|
405
|
): Promise<FulfillmentTypes.ShippingOptionDTO> |
|
406
|
|
|
407
|
@InjectManager() |
|
408
|
@EmitEvents() |
|
409
|
// @ts-expect-error |
|
410
|
async createShippingOptions( |
|
411
|
data: |
|
412
|
| FulfillmentTypes.CreateShippingOptionDTO[] |
|
413
|
| FulfillmentTypes.CreateShippingOptionDTO, |
|
414
|
@MedusaContext() sharedContext: Context = {} |
|
415
|
): Promise< |
|
416
|
FulfillmentTypes.ShippingOptionDTO | FulfillmentTypes.ShippingOptionDTO[] |
|
417
|
> { |
|
418
|
const createdShippingOptions = await this.createShippingOptions_( |
|
419
|
data, |
|
420
|
sharedContext |
|
421
|
) |
|
422
|
|
|
423
|
return await this.baseRepository_.serialize< |
|
424
|
FulfillmentTypes.ShippingOptionDTO | FulfillmentTypes.ShippingOptionDTO[] |
|
425
|
>(Array.isArray(data) ? createdShippingOptions : createdShippingOptions[0]) |
|
426
|
} |
|
427
|
|
|
428
|
@InjectTransactionManager() |
|
429
|
async createShippingOptions_( |
|
430
|
data: |
|
431
|
| FulfillmentTypes.CreateShippingOptionDTO[] |
|
432
|
| FulfillmentTypes.CreateShippingOptionDTO, |
|
433
|
@MedusaContext() sharedContext: Context = {} |
|
434
|
): Promise<InferEntityType<typeof ShippingOption>[]> { |
|
435
|
const data_ = Array.isArray(data) ? data : [data] |
|
436
|
|
|
437
|
if (!data_.length) { |
|
438
|
return [] |
|
439
|
} |
|
440
|
|
|
441
|
const rules = data_.flatMap((d) => d.rules).filter(Boolean) |
|
442
|
if (rules.length) { |
|
443
|
validateAndNormalizeRules(rules as Record<string, unknown>[]) |
|
444
|
} |
|
445
|
|
|
446
|
const createdSO = await this.shippingOptionService_.create( |
|
447
|
data_, |
|
448
|
sharedContext |
|
449
|
) |
|
450
|
|
|
451
|
return createdSO |
|
452
|
} |
|
453
|
|
|
454
|
// @ts-ignore |
|
455
|
createShippingProfiles( |
|
456
|
data: FulfillmentTypes.CreateShippingProfileDTO[], |
|
457
|
sharedContext?: Context |
|
458
|
): Promise<FulfillmentTypes.ShippingProfileDTO[]> |
|
459
|
// @ts-expect-error |
|
460
|
createShippingProfiles( |
|
461
|
data: FulfillmentTypes.CreateShippingProfileDTO, |
|
462
|
sharedContext?: Context |
|
463
|
): Promise<FulfillmentTypes.ShippingProfileDTO> |
|
464
|
|
|
465
|
@InjectManager() |
|
466
|
@EmitEvents() |
|
467
|
// @ts-expect-error |
|
468
|
async createShippingProfiles( |
|
469
|
data: |
|
470
|
| FulfillmentTypes.CreateShippingProfileDTO[] |
|
471
|
| FulfillmentTypes.CreateShippingProfileDTO, |
|
472
|
@MedusaContext() sharedContext: Context = {} |
|
473
|
): Promise< |
|
474
|
FulfillmentTypes.ShippingProfileDTO | FulfillmentTypes.ShippingProfileDTO[] |
|
475
|
> { |
|
476
|
const createdShippingProfiles = await this.createShippingProfiles_( |
|
477
|
data, |
|
478
|
sharedContext |
|
479
|
) |
|
480
|
|
|
481
|
return await this.baseRepository_.serialize< |
|
482
|
| FulfillmentTypes.ShippingProfileDTO |
|
483
|
| FulfillmentTypes.ShippingProfileDTO[] |
|
484
|
>( |
|
485
|
Array.isArray(data) ? createdShippingProfiles : createdShippingProfiles[0] |
|
486
|
) |
|
487
|
} |
|
488
|
|
|
489
|
@InjectTransactionManager() |
|
490
|
async createShippingProfiles_( |
|
491
|
data: |
|
492
|
| FulfillmentTypes.CreateShippingProfileDTO[] |
|
493
|
| FulfillmentTypes.CreateShippingProfileDTO, |
|
494
|
@MedusaContext() sharedContext: Context = {} |
|
495
|
): Promise<InferEntityType<typeof ShippingProfile>[]> { |
|
496
|
const data_ = Array.isArray(data) ? data : [data] |
|
497
|
|
|
498
|
if (!data_.length) { |
|
499
|
return [] |
|
500
|
} |
|
501
|
|
|
502
|
return await this.shippingProfileService_.create(data_, sharedContext) |
|
503
|
} |
|
504
|
|
|
505
|
// @ts-expect-error |
|
506
|
createGeoZones( |
|
507
|
data: FulfillmentTypes.CreateGeoZoneDTO[], |
|
508
|
sharedContext?: Context |
|
509
|
): Promise<FulfillmentTypes.GeoZoneDTO[]> |
|
510
|
// @ts-expect-error |
|
511
|
createGeoZones( |
|
512
|
data: FulfillmentTypes.CreateGeoZoneDTO, |
|
513
|
sharedContext?: Context |
|
514
|
): Promise<FulfillmentTypes.GeoZoneDTO> |
|
515
|
|
|
516
|
@InjectManager() |
|
517
|
@EmitEvents() |
|
518
|
// @ts-expect-error |
|
519
|
async createGeoZones( |
|
520
|
data: |
|
521
|
| FulfillmentTypes.CreateGeoZoneDTO |
|
522
|
| FulfillmentTypes.CreateGeoZoneDTO[], |
|
523
|
@MedusaContext() sharedContext: Context = {} |
|
524
|
): Promise<FulfillmentTypes.GeoZoneDTO | FulfillmentTypes.GeoZoneDTO[]> { |
|
525
|
const data_ = Array.isArray(data) ? data : [data] |
|
526
|
|
|
527
|
FulfillmentModuleService.validateGeoZones(data_) |
|
528
|
|
|
529
|
const createdGeoZones = await this.geoZoneService_.create( |
|
530
|
data_, |
|
531
|
sharedContext |
|
532
|
) |
|
533
|
|
|
534
|
return await this.baseRepository_.serialize<FulfillmentTypes.GeoZoneDTO[]>( |
|
535
|
Array.isArray(data) ? createdGeoZones : createdGeoZones[0] |
|
536
|
) |
|
537
|
} |
|
538
|
|
|
539
|
// @ts-expect-error |
|
540
|
async createShippingOptionRules( |
|
541
|
data: FulfillmentTypes.CreateShippingOptionRuleDTO[], |
|
542
|
sharedContext?: Context |
|
543
|
): Promise<FulfillmentTypes.ShippingOptionRuleDTO[]> |
|
544
|
// @ts-expect-error |
|
545
|
async createShippingOptionRules( |
|
546
|
data: FulfillmentTypes.CreateShippingOptionRuleDTO, |
|
547
|
sharedContext?: Context |
|
548
|
): Promise<FulfillmentTypes.ShippingOptionRuleDTO> |
|
549
|
|
|
550
|
@InjectManager() |
|
551
|
@EmitEvents() |
|
552
|
// @ts-expect-error |
|
553
|
async createShippingOptionRules( |
|
554
|
data: |
|
555
|
| FulfillmentTypes.CreateShippingOptionRuleDTO[] |
|
556
|
| FulfillmentTypes.CreateShippingOptionRuleDTO, |
|
557
|
@MedusaContext() sharedContext: Context = {} |
|
558
|
): Promise< |
|
559
|
| FulfillmentTypes.ShippingOptionRuleDTO |
|
560
|
| FulfillmentTypes.ShippingOptionRuleDTO[] |
|
561
|
> { |
|
562
|
const createdShippingOptionRules = await this.createShippingOptionRules_( |
|
563
|
data, |
|
564
|
sharedContext |
|
565
|
) |
|
566
|
|
|
567
|
return await this.baseRepository_.serialize< |
|
568
|
| FulfillmentTypes.ShippingOptionRuleDTO |
|
569
|
| FulfillmentTypes.ShippingOptionRuleDTO[] |
|
570
|
>( |
|
571
|
Array.isArray(data) |
|
572
|
? createdShippingOptionRules |
|
573
|
: createdShippingOptionRules[0] |
|
574
|
) |
|
575
|
} |
|
576
|
|
|
577
|
@InjectTransactionManager() |
|
578
|
async createShippingOptionRules_( |
|
579
|
data: |
|
580
|
| FulfillmentTypes.CreateShippingOptionRuleDTO[] |
|
581
|
| FulfillmentTypes.CreateShippingOptionRuleDTO, |
|
582
|
@MedusaContext() sharedContext: Context = {} |
|
583
|
): Promise<InferEntityType<typeof ShippingOptionRule>[]> { |
|
584
|
const data_ = Array.isArray(data) ? data : [data] |
|
585
|
|
|
586
|
if (!data_.length) { |
|
587
|
return [] |
|
588
|
} |
|
589
|
|
|
590
|
validateAndNormalizeRules(data_ as unknown as Record<string, unknown>[]) |
|
591
|
|
|
592
|
const createdSORules = await this.shippingOptionRuleService_.create( |
|
593
|
data_, |
|
594
|
sharedContext |
|
595
|
) |
|
596
|
|
|
597
|
return createdSORules |
|
598
|
} |
|
599
|
|
|
600
|
@InjectManager() |
|
601
|
@EmitEvents() |
|
602
|
async createFulfillment( |
|
603
|
data: FulfillmentTypes.CreateFulfillmentDTO, |
|
604
|
@MedusaContext() sharedContext: Context = {} |
|
605
|
): Promise<FulfillmentTypes.FulfillmentDTO> { |
|
606
|
const { order, additional_data, ...fulfillmentDataToCreate } = data |
|
607
|
|
|
608
|
const fulfillment = await this.fulfillmentService_.create( |
|
609
|
fulfillmentDataToCreate, |
|
610
|
sharedContext |
|
611
|
) |
|
612
|
|
|
613
|
const { |
|
614
|
items, |
|
615
|
data: fulfillmentData, |
|
616
|
provider_id, |
|
617
|
...fulfillmentRest |
|
618
|
} = fulfillment |
|
619
|
|
|
620
|
try { |
|
621
|
const providerResult = |
|
622
|
await this.fulfillmentProviderService_.createFulfillment( |
|
623
|
provider_id!, // TODO: should we add a runtime check on provider_id being provided? |
|
624
|
fulfillmentData || {}, |
|
625
|
items.map((i) => i), |
|
626
|
order, |
|
627
|
fulfillmentRest as unknown as Partial<FulfillmentDTO>, |
|
628
|
additional_data |
|
629
|
) |
|
630
|
await this.fulfillmentService_.update( |
|
631
|
{ |
|
632
|
id: fulfillment.id, |
|
633
|
data: providerResult.data ?? {}, |
|
634
|
labels: providerResult.labels ?? [], |
|
635
|
}, |
|
636
|
sharedContext |
|
637
|
) |
|
638
|
} catch (error) { |
|
639
|
await this.fulfillmentService_.delete(fulfillment.id, sharedContext) |
|
640
|
throw error |
|
641
|
} |
|
642
|
|
|
643
|
return await this.baseRepository_.serialize<FulfillmentTypes.FulfillmentDTO>( |
|
644
|
fulfillment |
|
645
|
) |
|
646
|
} |
|
647
|
|
|
648
|
@InjectManager() |
|
649
|
@EmitEvents() |
|
650
|
async deleteFulfillment( |
|
651
|
id: string, |
|
652
|
@MedusaContext() sharedContext: Context = {} |
|
653
|
): Promise<void> { |
|
654
|
const fulfillment = await this.fulfillmentService_.retrieve( |
|
655
|
id, |
|
656
|
{}, |
|
657
|
sharedContext |
|
658
|
) |
|
659
|
|
|
660
|
if (!isPresent(fulfillment.canceled_at)) { |
|
661
|
throw new MedusaError( |
|
662
|
MedusaError.Types.INVALID_DATA, |
|
663
|
`Fulfillment with id ${fulfillment.id} needs to be canceled first before deleting` |
|
664
|
) |
|
665
|
} |
|
666
|
|
|
667
|
await this.fulfillmentService_.delete(id, sharedContext) |
|
668
|
} |
|
669
|
|
|
670
|
@InjectManager() |
|
671
|
@EmitEvents() |
|
672
|
async createReturnFulfillment( |
|
673
|
data: FulfillmentTypes.CreateFulfillmentDTO, |
|
674
|
@MedusaContext() sharedContext: Context = {} |
|
675
|
): Promise<FulfillmentTypes.FulfillmentDTO> { |
|
676
|
const { order, ...fulfillmentDataToCreate } = data |
|
677
|
|
|
678
|
const fulfillment = await this.fulfillmentService_.create( |
|
679
|
fulfillmentDataToCreate, |
|
680
|
sharedContext |
|
681
|
) |
|
682
|
|
|
683
|
const shippingOption = await this.shippingOptionService_.retrieve( |
|
684
|
fulfillment.shipping_option_id!, |
|
685
|
{ |
|
686
|
select: ["id", "name", "data", "metadata"], |
|
687
|
}, |
|
688
|
sharedContext |
|
689
|
) |
|
690
|
|
|
691
|
try { |
|
692
|
const providerResult = |
|
693
|
await this.fulfillmentProviderService_.createReturn( |
|
694
|
fulfillment.provider_id!, // TODO: should we add a runtime check on provider_id being provided?, |
|
695
|
{ |
|
696
|
...fulfillment, |
|
697
|
shipping_option: shippingOption, |
|
698
|
} as Record<any, any> |
|
699
|
) |
|
700
|
await this.fulfillmentService_.update( |
|
701
|
{ |
|
702
|
id: fulfillment.id, |
|
703
|
data: providerResult.data ?? {}, |
|
704
|
labels: providerResult.labels ?? [], |
|
705
|
}, |
|
706
|
sharedContext |
|
707
|
) |
|
708
|
} catch (error) { |
|
709
|
await this.fulfillmentService_.delete(fulfillment.id, sharedContext) |
|
710
|
throw error |
|
711
|
} |
|
712
|
|
|
713
|
return await this.baseRepository_.serialize<FulfillmentTypes.FulfillmentDTO>( |
|
714
|
fulfillment |
|
715
|
) |
|
716
|
} |
|
717
|
|
|
718
|
// @ts-expect-error |
|
719
|
updateFulfillmentSets( |
|
720
|
data: FulfillmentTypes.UpdateFulfillmentSetDTO[], |
|
721
|
sharedContext?: Context |
|
722
|
): Promise<FulfillmentTypes.FulfillmentSetDTO[]> |
|
723
|
// @ts-expect-error |
|
724
|
updateFulfillmentSets( |
|
725
|
data: FulfillmentTypes.UpdateFulfillmentSetDTO, |
|
726
|
sharedContext?: Context |
|
727
|
): Promise<FulfillmentTypes.FulfillmentSetDTO> |
|
728
|
|
|
729
|
@InjectManager() |
|
730
|
@EmitEvents() |
|
731
|
// @ts-expect-error |
|
732
|
async updateFulfillmentSets( |
|
733
|
data: UpdateFulfillmentSetDTO[] | UpdateFulfillmentSetDTO, |
|
734
|
@MedusaContext() sharedContext: Context = {} |
|
735
|
): Promise< |
|
736
|
FulfillmentTypes.FulfillmentSetDTO[] | FulfillmentTypes.FulfillmentSetDTO |
|
737
|
> { |
|
738
|
const updatedFulfillmentSets = await this.updateFulfillmentSets_( |
|
739
|
data, |
|
740
|
sharedContext |
|
741
|
) |
|
742
|
|
|
743
|
return await this.baseRepository_.serialize< |
|
744
|
FulfillmentTypes.FulfillmentSetDTO | FulfillmentTypes.FulfillmentSetDTO[] |
|
745
|
>(updatedFulfillmentSets) |
|
746
|
} |
|
747
|
|
|
748
|
@InjectTransactionManager() |
|
749
|
protected async updateFulfillmentSets_( |
|
750
|
data: UpdateFulfillmentSetDTO[] | UpdateFulfillmentSetDTO, |
|
751
|
@MedusaContext() sharedContext: Context = {} |
|
752
|
): Promise< |
|
753
|
| InferEntityType<typeof FulfillmentSet>[] |
|
754
|
| InferEntityType<typeof FulfillmentSet> |
|
755
|
> { |
|
756
|
const data_ = Array.isArray(data) ? data : [data] |
|
757
|
|
|
758
|
if (!data_.length) { |
|
759
|
return [] |
|
760
|
} |
|
761
|
|
|
762
|
const fulfillmentSetIds = data_.map((f) => f.id) |
|
763
|
if (!fulfillmentSetIds.length) { |
|
764
|
return [] |
|
765
|
} |
|
766
|
|
|
767
|
const fulfillmentSets = await this.fulfillmentSetService_.list( |
|
768
|
{ |
|
769
|
id: fulfillmentSetIds, |
|
770
|
}, |
|
771
|
{ |
|
772
|
relations: ["service_zones", "service_zones.geo_zones"], |
|
773
|
take: fulfillmentSetIds.length, |
|
774
|
}, |
|
775
|
sharedContext |
|
776
|
) |
|
777
|
|
|
778
|
const fulfillmentSetSet = new Set(fulfillmentSets.map((f) => f.id)) |
|
779
|
const expectedFulfillmentSetSet = new Set(data_.map((f) => f.id)) |
|
780
|
const missingFulfillmentSetIds = getSetDifference( |
|
781
|
expectedFulfillmentSetSet, |
|
782
|
fulfillmentSetSet |
|
783
|
) |
|
784
|
|
|
785
|
if (missingFulfillmentSetIds.size) { |
|
786
|
throw new MedusaError( |
|
787
|
MedusaError.Types.NOT_FOUND, |
|
788
|
`The following fulfillment sets does not exists: ${Array.from( |
|
789
|
missingFulfillmentSetIds |
|
790
|
).join(", ")}` |
|
791
|
) |
|
792
|
} |
|
793
|
|
|
794
|
const fulfillmentSetMap = new Map< |
|
795
|
string, |
|
796
|
InferEntityType<typeof FulfillmentSet> |
|
797
|
>(fulfillmentSets.map((f) => [f.id, f])) |
|
798
|
|
|
799
|
const serviceZoneIdsToDelete: string[] = [] |
|
800
|
const geoZoneIdsToDelete: string[] = [] |
|
801
|
const existingServiceZoneIds: string[] = [] |
|
802
|
const existingGeoZoneIds: string[] = [] |
|
803
|
|
|
804
|
data_.forEach((fulfillmentSet) => { |
|
805
|
if (fulfillmentSet.service_zones) { |
|
806
|
/** |
|
807
|
* Detect and delete service zones that are not in the updated |
|
808
|
*/ |
|
809
|
|
|
810
|
const existingFulfillmentSet = fulfillmentSetMap.get(fulfillmentSet.id)! |
|
811
|
const existingServiceZones = existingFulfillmentSet.service_zones |
|
812
|
const updatedServiceZones = fulfillmentSet.service_zones |
|
813
|
const toDeleteServiceZoneIds = getSetDifference( |
|
814
|
new Set(existingServiceZones.map((s) => s.id)), |
|
815
|
new Set( |
|
816
|
updatedServiceZones |
|
817
|
.map((s) => "id" in s && s.id) |
|
818
|
.filter((id): id is string => !!id) |
|
819
|
) |
|
820
|
) |
|
821
|
|
|
822
|
if (toDeleteServiceZoneIds.size) { |
|
823
|
serviceZoneIdsToDelete.push(...Array.from(toDeleteServiceZoneIds)) |
|
824
|
geoZoneIdsToDelete.push( |
|
825
|
...existingServiceZones |
|
826
|
.filter((s) => toDeleteServiceZoneIds.has(s.id)) |
|
827
|
.flatMap((s) => s.geo_zones.map((g) => g.id)) |
|
828
|
) |
|
829
|
} |
|
830
|
|
|
831
|
/** |
|
832
|
* Detect and re assign service zones to the fulfillment set that are still present |
|
833
|
*/ |
|
834
|
|
|
835
|
const serviceZonesMap = new Map( |
|
836
|
existingFulfillmentSet.service_zones.map((serviceZone) => [ |
|
837
|
serviceZone.id, |
|
838
|
serviceZone, |
|
839
|
]) |
|
840
|
) |
|
841
|
const serviceZonesSet = new Set( |
|
842
|
existingServiceZones |
|
843
|
.map((s) => "id" in s && s.id) |
|
844
|
.filter((id): id is string => !!id) |
|
845
|
) |
|
846
|
|
|
847
|
const expectedServiceZoneSet = new Set( |
|
848
|
fulfillmentSet.service_zones |
|
849
|
.map((s) => "id" in s && s.id) |
|
850
|
.filter((id): id is string => !!id) |
|
851
|
) |
|
852
|
|
|
853
|
const missingServiceZoneIds = getSetDifference( |
|
854
|
expectedServiceZoneSet, |
|
855
|
serviceZonesSet |
|
856
|
) |
|
857
|
|
|
858
|
if (missingServiceZoneIds.size) { |
|
859
|
throw new MedusaError( |
|
860
|
MedusaError.Types.NOT_FOUND, |
|
861
|
`The following service zones does not exists: ${Array.from( |
|
862
|
missingServiceZoneIds |
|
863
|
).join(", ")}` |
|
864
|
) |
|
865
|
} |
|
866
|
|
|
867
|
// re assign service zones to the fulfillment set |
|
868
|
if (fulfillmentSet.service_zones) { |
|
869
|
fulfillmentSet.service_zones = fulfillmentSet.service_zones.map( |
|
870
|
(serviceZone) => { |
|
871
|
if (!("id" in serviceZone)) { |
|
872
|
if (serviceZone.geo_zones?.length) { |
|
873
|
FulfillmentModuleService.validateGeoZones( |
|
874
|
serviceZone.geo_zones |
|
875
|
) |
|
876
|
} |
|
877
|
return serviceZone |
|
878
|
} |
|
879
|
|
|
880
|
const existingServiceZone = serviceZonesMap.get(serviceZone.id)! |
|
881
|
existingServiceZoneIds.push(existingServiceZone.id) |
|
882
|
|
|
883
|
if (existingServiceZone.geo_zones.length) { |
|
884
|
existingGeoZoneIds.push( |
|
885
|
...existingServiceZone.geo_zones.map((g) => g.id) |
|
886
|
) |
|
887
|
} |
|
888
|
|
|
889
|
return serviceZonesMap.get(serviceZone.id)! |
|
890
|
} |
|
891
|
) |
|
892
|
} |
|
893
|
} |
|
894
|
}) |
|
895
|
|
|
896
|
if (serviceZoneIdsToDelete.length) { |
|
897
|
await promiseAll([ |
|
898
|
this.geoZoneService_.delete( |
|
899
|
{ |
|
900
|
id: geoZoneIdsToDelete, |
|
901
|
}, |
|
902
|
sharedContext |
|
903
|
), |
|
904
|
this.serviceZoneService_.delete( |
|
905
|
{ |
|
906
|
id: serviceZoneIdsToDelete, |
|
907
|
}, |
|
908
|
sharedContext |
|
909
|
), |
|
910
|
]) |
|
911
|
} |
|
912
|
|
|
913
|
const updatedFulfillmentSets = await this.fulfillmentSetService_.update( |
|
914
|
data_, |
|
915
|
sharedContext |
|
916
|
) |
|
917
|
|
|
918
|
return Array.isArray(data) |
|
919
|
? updatedFulfillmentSets |
|
920
|
: updatedFulfillmentSets[0] |
|
921
|
} |
|
922
|
|
|
923
|
// @ts-ignore |
|
924
|
updateServiceZones( |
|
925
|
id: string, |
|
926
|
data: FulfillmentTypes.UpdateServiceZoneDTO, |
|
927
|
sharedContext?: Context |
|
928
|
): Promise<FulfillmentTypes.ServiceZoneDTO> |
|
929
|
// @ts-expect-error |
|
930
|
updateServiceZones( |
|
931
|
selector: FulfillmentTypes.FilterableServiceZoneProps, |
|
932
|
data: FulfillmentTypes.UpdateServiceZoneDTO, |
|
933
|
sharedContext?: Context |
|
934
|
): Promise<FulfillmentTypes.ServiceZoneDTO[]> |
|
935
|
|
|
936
|
@InjectManager() |
|
937
|
@EmitEvents() |
|
938
|
// @ts-expect-error |
|
939
|
async updateServiceZones( |
|
940
|
idOrSelector: string | FulfillmentTypes.FilterableServiceZoneProps, |
|
941
|
data: FulfillmentTypes.UpdateServiceZoneDTO, |
|
942
|
@MedusaContext() sharedContext: Context = {} |
|
943
|
): Promise< |
|
944
|
FulfillmentTypes.ServiceZoneDTO[] | FulfillmentTypes.ServiceZoneDTO |
|
945
|
> { |
|
946
|
const normalizedInput: UpdateServiceZoneDTO[] = [] |
|
947
|
|
|
948
|
if (isString(idOrSelector)) { |
|
949
|
normalizedInput.push({ id: idOrSelector, ...data }) |
|
950
|
} else { |
|
951
|
const serviceZones = await this.serviceZoneService_.list( |
|
952
|
{ ...idOrSelector }, |
|
953
|
{}, |
|
954
|
sharedContext |
|
955
|
) |
|
956
|
|
|
957
|
if (!serviceZones.length) { |
|
958
|
return [] |
|
959
|
} |
|
960
|
|
|
961
|
for (const serviceZone of serviceZones) { |
|
962
|
normalizedInput.push({ id: serviceZone.id, ...data }) |
|
963
|
} |
|
964
|
} |
|
965
|
|
|
966
|
const updatedServiceZones = await this.updateServiceZones_( |
|
967
|
normalizedInput, |
|
968
|
sharedContext |
|
969
|
) |
|
970
|
|
|
971
|
const toReturn = isString(idOrSelector) |
|
972
|
? updatedServiceZones[0] |
|
973
|
: updatedServiceZones |
|
974
|
|
|
975
|
return await this.baseRepository_.serialize< |
|
976
|
FulfillmentTypes.ServiceZoneDTO | FulfillmentTypes.ServiceZoneDTO[] |
|
977
|
>(toReturn) |
|
978
|
} |
|
979
|
|
|
980
|
@InjectTransactionManager() |
|
981
|
protected async updateServiceZones_( |
|
982
|
data: |
|
983
|
| FulfillmentTypes.UpdateServiceZoneDTO[] |
|
984
|
| FulfillmentTypes.UpdateServiceZoneDTO, |
|
985
|
@MedusaContext() sharedContext: Context = {} |
|
986
|
): Promise< |
|
987
|
InferEntityType<typeof ServiceZone> | InferEntityType<typeof ServiceZone>[] |
|
988
|
> { |
|
989
|
const data_ = Array.isArray(data) ? data : [data] |
|
990
|
|
|
991
|
if (!data_.length) { |
|
992
|
return [] |
|
993
|
} |
|
994
|
|
|
995
|
const serviceZoneIds = data_.map((s) => s.id) |
|
996
|
if (!serviceZoneIds.length) { |
|
997
|
return [] |
|
998
|
} |
|
999
|
|
|
1000
|
const serviceZones = await this.serviceZoneService_.list( |
|
1001
|
{ |
|
1002
|
id: serviceZoneIds, |
|
1003
|
}, |
|
1004
|
{ |
|
1005
|
relations: ["geo_zones"], |
|
1006
|
take: serviceZoneIds.length, |
|
1007
|
}, |
|
1008
|
sharedContext |
|
1009
|
) |
|
1010
|
|
|
1011
|
const serviceZoneSet = new Set(serviceZones.map((s) => s.id)) |
|
1012
|
const expectedServiceZoneSet = new Set(data_.map((s) => s.id)) |
|
1013
|
const missingServiceZoneIds = getSetDifference( |
|
1014
|
expectedServiceZoneSet, |
|
1015
|
serviceZoneSet |
|
1016
|
) |
|
1017
|
|
|
1018
|
if (missingServiceZoneIds.size) { |
|
1019
|
throw new MedusaError( |
|
1020
|
MedusaError.Types.NOT_FOUND, |
|
1021
|
`The following service zones does not exists: ${Array.from( |
|
1022
|
missingServiceZoneIds |
|
1023
|
).join(", ")}` |
|
1024
|
) |
|
1025
|
} |
|
1026
|
|
|
1027
|
const serviceZoneMap = new Map<string, InferEntityType<typeof ServiceZone>>( |
|
1028
|
serviceZones.map((s) => [s.id, s]) |
|
1029
|
) |
|
1030
|
|
|
1031
|
const geoZoneIdsToDelete: string[] = [] |
|
1032
|
const existingGeoZoneIds: string[] = [] |
|
1033
|
const updatedGeoZoneIds: string[] = [] |
|
1034
|
|
|
1035
|
data_.forEach((serviceZone) => { |
|
1036
|
if (serviceZone.geo_zones) { |
|
1037
|
const existingServiceZone = serviceZoneMap.get(serviceZone.id!)! |
|
1038
|
const existingGeoZones = existingServiceZone.geo_zones |
|
1039
|
const updatedGeoZones = serviceZone.geo_zones |
|
1040
|
const existingGeoZoneIdsForServiceZone = existingGeoZones.map( |
|
1041
|
(g) => g.id |
|
1042
|
) |
|
1043
|
const toDeleteGeoZoneIds = getSetDifference( |
|
1044
|
new Set(existingGeoZoneIdsForServiceZone), |
|
1045
|
new Set( |
|
1046
|
updatedGeoZones |
|
1047
|
.map((g) => "id" in g && g.id) |
|
1048
|
.filter((id): id is string => !!id) |
|
1049
|
) |
|
1050
|
) |
|
1051
|
|
|
1052
|
existingGeoZoneIds.push(...existingGeoZoneIdsForServiceZone) |
|
1053
|
|
|
1054
|
if (toDeleteGeoZoneIds.size) { |
|
1055
|
geoZoneIdsToDelete.push(...Array.from(toDeleteGeoZoneIds)) |
|
1056
|
} |
|
1057
|
|
|
1058
|
const geoZonesMap = new Map( |
|
1059
|
existingServiceZone.geo_zones.map((geoZone) => [geoZone.id, geoZone]) |
|
1060
|
) |
|
1061
|
const geoZonesSet = new Set( |
|
1062
|
existingGeoZones |
|
1063
|
.map((g) => "id" in g && g.id) |
|
1064
|
.filter((id): id is string => !!id) |
|
1065
|
) |
|
1066
|
const expectedGeoZoneSet = new Set( |
|
1067
|
serviceZone.geo_zones |
|
1068
|
.map((g) => "id" in g && g.id) |
|
1069
|
.filter((id): id is string => !!id) |
|
1070
|
) |
|
1071
|
const missingGeoZoneIds = getSetDifference( |
|
1072
|
expectedGeoZoneSet, |
|
1073
|
geoZonesSet |
|
1074
|
) |
|
1075
|
|
|
1076
|
if (missingGeoZoneIds.size) { |
|
1077
|
throw new MedusaError( |
|
1078
|
MedusaError.Types.NOT_FOUND, |
|
1079
|
`The following geo zones does not exists: ${Array.from( |
|
1080
|
missingGeoZoneIds |
|
1081
|
).join(", ")}` |
|
1082
|
) |
|
1083
|
} |
|
1084
|
|
|
1085
|
serviceZone.geo_zones = serviceZone.geo_zones.map((geoZone) => { |
|
1086
|
if (!("id" in geoZone)) { |
|
1087
|
FulfillmentModuleService.validateGeoZones([geoZone]) |
|
1088
|
return geoZone |
|
1089
|
} |
|
1090
|
const existing = geoZonesMap.get(geoZone.id)! |
|
1091
|
|
|
1092
|
// If only the id is provided we dont consider it as an update |
|
1093
|
if ( |
|
1094
|
Object.keys(geoZone).length > 1 && |
|
1095
|
!deepEqualObj(existing, geoZone) |
|
1096
|
) { |
|
1097
|
updatedGeoZoneIds.push(geoZone.id) |
|
1098
|
} |
|
1099
|
|
|
1100
|
return { ...existing, ...geoZone } |
|
1101
|
}) |
|
1102
|
} |
|
1103
|
}) |
|
1104
|
|
|
1105
|
if (geoZoneIdsToDelete.length) { |
|
1106
|
await this.geoZoneService_.delete( |
|
1107
|
{ |
|
1108
|
id: geoZoneIdsToDelete, |
|
1109
|
}, |
|
1110
|
sharedContext |
|
1111
|
) |
|
1112
|
} |
|
1113
|
|
|
1114
|
const updatedServiceZones = await this.serviceZoneService_.update( |
|
1115
|
data_, |
|
1116
|
sharedContext |
|
1117
|
) |
|
1118
|
|
|
1119
|
return Array.isArray(data) ? updatedServiceZones : updatedServiceZones[0] |
|
1120
|
} |
|
1121
|
|
|
1122
|
upsertServiceZones( |
|
1123
|
data: FulfillmentTypes.UpsertServiceZoneDTO, |
|
1124
|
sharedContext?: Context |
|
1125
|
): Promise<FulfillmentTypes.ServiceZoneDTO> |
|
1126
|
upsertServiceZones( |
|
1127
|
data: FulfillmentTypes.UpsertServiceZoneDTO[], |
|
1128
|
sharedContext?: Context |
|
1129
|
): Promise<FulfillmentTypes.ServiceZoneDTO[]> |
|
1130
|
|
|
1131
|
@InjectManager() |
|
1132
|
@EmitEvents() |
|
1133
|
async upsertServiceZones( |
|
1134
|
data: |
|
1135
|
| FulfillmentTypes.UpsertServiceZoneDTO |
|
1136
|
| FulfillmentTypes.UpsertServiceZoneDTO[], |
|
1137
|
@MedusaContext() sharedContext: Context = {} |
|
1138
|
): Promise< |
|
1139
|
FulfillmentTypes.ServiceZoneDTO | FulfillmentTypes.ServiceZoneDTO[] |
|
1140
|
> { |
|
1141
|
const upsertServiceZones = await this.upsertServiceZones_( |
|
1142
|
data, |
|
1143
|
sharedContext |
|
1144
|
) |
|
1145
|
|
|
1146
|
const allServiceZones = await this.baseRepository_.serialize< |
|
1147
|
FulfillmentTypes.ServiceZoneDTO[] | FulfillmentTypes.ServiceZoneDTO |
|
1148
|
>(upsertServiceZones) |
|
1149
|
|
|
1150
|
return Array.isArray(data) ? allServiceZones : allServiceZones[0] |
|
1151
|
} |
|
1152
|
|
|
1153
|
@InjectTransactionManager() |
|
1154
|
async upsertServiceZones_( |
|
1155
|
data: |
|
1156
|
| FulfillmentTypes.UpsertServiceZoneDTO[] |
|
1157
|
| FulfillmentTypes.UpsertServiceZoneDTO, |
|
1158
|
@MedusaContext() sharedContext: Context = {} |
|
1159
|
): Promise< |
|
1160
|
InferEntityType<typeof ServiceZone>[] | InferEntityType<typeof ServiceZone> |
|
1161
|
> { |
|
1162
|
const input = Array.isArray(data) ? data : [data] |
|
1163
|
const forUpdate = input.filter( |
|
1164
|
(serviceZone): serviceZone is FulfillmentTypes.UpdateServiceZoneDTO => |
|
1165
|
!!serviceZone.id |
|
1166
|
) |
|
1167
|
const forCreate = input.filter( |
|
1168
|
(serviceZone): serviceZone is FulfillmentTypes.CreateServiceZoneDTO => |
|
1169
|
!serviceZone.id |
|
1170
|
) |
|
1171
|
|
|
1172
|
const created: InferEntityType<typeof ServiceZone>[] = [] |
|
1173
|
const updated: InferEntityType<typeof ServiceZone>[] = [] |
|
1174
|
|
|
1175
|
if (forCreate.length) { |
|
1176
|
const createdServiceZones = await this.createServiceZones_( |
|
1177
|
forCreate, |
|
1178
|
sharedContext |
|
1179
|
) |
|
1180
|
|
|
1181
|
const toPush = Array.isArray(createdServiceZones) |
|
1182
|
? createdServiceZones |
|
1183
|
: [createdServiceZones] |
|
1184
|
|
|
1185
|
created.push(...toPush) |
|
1186
|
} |
|
1187
|
|
|
1188
|
if (forUpdate.length) { |
|
1189
|
const updatedServiceZones = await this.updateServiceZones_( |
|
1190
|
forUpdate, |
|
1191
|
sharedContext |
|
1192
|
) |
|
1193
|
|
|
1194
|
const toPush = Array.isArray(updatedServiceZones) |
|
1195
|
? updatedServiceZones |
|
1196
|
: [updatedServiceZones] |
|
1197
|
|
|
1198
|
updated.push(...toPush) |
|
1199
|
} |
|
1200
|
|
|
1201
|
return [...created, ...updated] |
|
1202
|
} |
|
1203
|
|
|
1204
|
// @ts-ignore |
|
1205
|
updateShippingOptions( |
|
1206
|
id: string, |
|
1207
|
data: FulfillmentTypes.UpdateShippingOptionDTO, |
|
1208
|
sharedContext?: Context |
|
1209
|
): Promise<FulfillmentTypes.ShippingOptionDTO> |
|
1210
|
// @ts-expect-error |
|
1211
|
updateShippingOptions( |
|
1212
|
selector: FulfillmentTypes.FilterableShippingOptionProps, |
|
1213
|
data: FulfillmentTypes.UpdateShippingOptionDTO, |
|
1214
|
sharedContext?: Context |
|
1215
|
): Promise<FulfillmentTypes.ShippingOptionDTO[]> |
|
1216
|
|
|
1217
|
@InjectManager() |
|
1218
|
@EmitEvents() |
|
1219
|
// @ts-expect-error |
|
1220
|
async updateShippingOptions( |
|
1221
|
idOrSelector: string | FulfillmentTypes.FilterableShippingOptionProps, |
|
1222
|
data: FulfillmentTypes.UpdateShippingOptionDTO, |
|
1223
|
@MedusaContext() sharedContext: Context = {} |
|
1224
|
): Promise< |
|
1225
|
FulfillmentTypes.ShippingOptionDTO[] | FulfillmentTypes.ShippingOptionDTO |
|
1226
|
> { |
|
1227
|
const normalizedInput: UpdateShippingOptionsInput[] = [] |
|
1228
|
|
|
1229
|
if (isString(idOrSelector)) { |
|
1230
|
normalizedInput.push({ id: idOrSelector, ...data }) |
|
1231
|
} else { |
|
1232
|
const shippingOptions = await this.shippingOptionService_.list( |
|
1233
|
idOrSelector, |
|
1234
|
{}, |
|
1235
|
sharedContext |
|
1236
|
) |
|
1237
|
shippingOptions.forEach((shippingOption) => { |
|
1238
|
normalizedInput.push({ id: shippingOption.id, ...data }) |
|
1239
|
}) |
|
1240
|
} |
|
1241
|
|
|
1242
|
const updatedShippingOptions = await this.updateShippingOptions_( |
|
1243
|
normalizedInput, |
|
1244
|
sharedContext |
|
1245
|
) |
|
1246
|
|
|
1247
|
const serialized = await this.baseRepository_.serialize< |
|
1248
|
FulfillmentTypes.ShippingOptionDTO | FulfillmentTypes.ShippingOptionDTO[] |
|
1249
|
>(updatedShippingOptions) |
|
1250
|
|
|
1251
|
return isString(idOrSelector) ? serialized[0] : serialized |
|
1252
|
} |
|
1253
|
|
|
1254
|
@InjectTransactionManager() |
|
1255
|
async updateShippingOptions_( |
|
1256
|
data: UpdateShippingOptionsInput[] | UpdateShippingOptionsInput, |
|
1257
|
@MedusaContext() sharedContext: Context = {} |
|
1258
|
): Promise< |
|
1259
|
| InferEntityType<typeof ShippingOption> |
|
1260
|
| InferEntityType<typeof ShippingOption>[] |
|
1261
|
> { |
|
1262
|
const dataArray = Array.isArray(data) |
|
1263
|
? data.map((d) => deepCopy(d)) |
|
1264
|
: [deepCopy(data)] |
|
1265
|
|
|
1266
|
if (!dataArray.length) { |
|
1267
|
return [] |
|
1268
|
} |
|
1269
|
|
|
1270
|
const shippingOptionIds = dataArray.map((s) => s.id) |
|
1271
|
if (!shippingOptionIds.length) { |
|
1272
|
return [] |
|
1273
|
} |
|
1274
|
|
|
1275
|
const shippingOptions = await this.shippingOptionService_.list( |
|
1276
|
{ |
|
1277
|
id: shippingOptionIds, |
|
1278
|
}, |
|
1279
|
{ |
|
1280
|
relations: ["rules", "type"], |
|
1281
|
take: shippingOptionIds.length, |
|
1282
|
}, |
|
1283
|
sharedContext |
|
1284
|
) |
|
1285
|
|
|
1286
|
const existingShippingOptions = new Map( |
|
1287
|
shippingOptions.map((s) => [s.id, s]) |
|
1288
|
) |
|
1289
|
|
|
1290
|
FulfillmentModuleService.validateMissingShippingOptions_( |
|
1291
|
shippingOptions, |
|
1292
|
dataArray |
|
1293
|
) |
|
1294
|
|
|
1295
|
const ruleIdsToDelete: string[] = [] |
|
1296
|
const updatedRuleIds: string[] = [] |
|
1297
|
const existingRuleIds: string[] = [] |
|
1298
|
|
|
1299
|
dataArray.forEach((shippingOption) => { |
|
1300
|
const existingShippingOption = existingShippingOptions.get( |
|
1301
|
shippingOption.id |
|
1302
|
)! // Guaranteed to exist since the validation above have been performed |
|
1303
|
|
|
1304
|
// `type_id` doesn't exist on the entity/table, `type_id` argument is mapped to `shipping_option_type_id` |
|
1305
|
if (shippingOption.type_id) { |
|
1306
|
shippingOption.shipping_option_type_id = shippingOption.type_id |
|
1307
|
delete shippingOption.type_id |
|
1308
|
} |
|
1309
|
|
|
1310
|
if (!shippingOption.rules) { |
|
1311
|
return |
|
1312
|
} |
|
1313
|
|
|
1314
|
const existingRules = existingShippingOption.rules |
|
1315
|
|
|
1316
|
existingRuleIds.push(...existingRules.map((r) => r.id)) |
|
1317
|
|
|
1318
|
FulfillmentModuleService.validateMissingShippingOptionRules( |
|
1319
|
existingShippingOption, |
|
1320
|
shippingOption |
|
1321
|
) |
|
1322
|
|
|
1323
|
const existingRulesMap: Map< |
|
1324
|
string, |
|
1325
|
| FulfillmentTypes.UpdateShippingOptionRuleDTO |
|
1326
|
| InferEntityType<typeof ShippingOptionRule> |
|
1327
|
> = new Map(existingRules.map((rule) => [rule.id, rule])) |
|
1328
|
|
|
1329
|
const updatedRules = shippingOption.rules |
|
1330
|
.map((rule) => { |
|
1331
|
if ("id" in rule) { |
|
1332
|
const existingRule = (existingRulesMap.get(rule.id) ?? |
|
1333
|
{}) as FulfillmentTypes.UpdateShippingOptionRuleDTO |
|
1334
|
|
|
1335
|
if (existingRulesMap.get(rule.id)) { |
|
1336
|
updatedRuleIds.push(rule.id) |
|
1337
|
} |
|
1338
|
|
|
1339
|
// @ts-ignore |
|
1340
|
delete rule.created_at |
|
1341
|
// @ts-ignore |
|
1342
|
delete rule.updated_at |
|
1343
|
// @ts-ignore |
|
1344
|
delete rule.deleted_at |
|
1345
|
|
|
1346
|
const ruleData: FulfillmentTypes.UpdateShippingOptionRuleDTO = { |
|
1347
|
...existingRule, |
|
1348
|
...rule, |
|
1349
|
} |
|
1350
|
|
|
1351
|
existingRulesMap.set(rule.id, ruleData) |
|
1352
|
return ruleData |
|
1353
|
} |
|
1354
|
|
|
1355
|
return |
|
1356
|
}) |
|
1357
|
.filter(Boolean) as FulfillmentTypes.UpdateShippingOptionRuleDTO[] |
|
1358
|
|
|
1359
|
validateAndNormalizeRules(updatedRules) |
|
1360
|
|
|
1361
|
const toDeleteRuleIds = arrayDifference( |
|
1362
|
updatedRuleIds, |
|
1363
|
Array.from(existingRulesMap.keys()) |
|
1364
|
) as string[] |
|
1365
|
|
|
1366
|
if (toDeleteRuleIds.length) { |
|
1367
|
ruleIdsToDelete.push(...toDeleteRuleIds) |
|
1368
|
} |
|
1369
|
|
|
1370
|
shippingOption.rules = shippingOption.rules.map((rule) => { |
|
1371
|
if (!("id" in rule)) { |
|
1372
|
validateAndNormalizeRules([rule]) |
|
1373
|
return rule |
|
1374
|
} |
|
1375
|
return existingRulesMap.get(rule.id)! |
|
1376
|
}) |
|
1377
|
}) |
|
1378
|
|
|
1379
|
if (ruleIdsToDelete.length) { |
|
1380
|
await this.shippingOptionRuleService_.delete( |
|
1381
|
ruleIdsToDelete, |
|
1382
|
sharedContext |
|
1383
|
) |
|
1384
|
} |
|
1385
|
|
|
1386
|
const updatedShippingOptions = await this.shippingOptionService_.update( |
|
1387
|
dataArray, |
|
1388
|
sharedContext |
|
1389
|
) |
|
1390
|
|
|
1391
|
return Array.isArray(data) |
|
1392
|
? updatedShippingOptions |
|
1393
|
: updatedShippingOptions[0] |
|
1394
|
} |
|
1395
|
|
|
1396
|
async upsertShippingOptions( |
|
1397
|
data: FulfillmentTypes.UpsertShippingOptionDTO[], |
|
1398
|
sharedContext?: Context |
|
1399
|
): Promise<FulfillmentTypes.ShippingOptionDTO[]> |
|
1400
|
async upsertShippingOptions( |
|
1401
|
data: FulfillmentTypes.UpsertShippingOptionDTO, |
|
1402
|
sharedContext?: Context |
|
1403
|
): Promise<FulfillmentTypes.ShippingOptionDTO> |
|
1404
|
|
|
1405
|
@InjectManager() |
|
1406
|
@EmitEvents() |
|
1407
|
async upsertShippingOptions( |
|
1408
|
data: |
|
1409
|
| FulfillmentTypes.UpsertShippingOptionDTO[] |
|
1410
|
| FulfillmentTypes.UpsertShippingOptionDTO, |
|
1411
|
@MedusaContext() sharedContext: Context = {} |
|
1412
|
): Promise< |
|
1413
|
FulfillmentTypes.ShippingOptionDTO[] | FulfillmentTypes.ShippingOptionDTO |
|
1414
|
> { |
|
1415
|
const upsertedShippingOptions = await this.upsertShippingOptions_( |
|
1416
|
data, |
|
1417
|
sharedContext |
|
1418
|
) |
|
1419
|
|
|
1420
|
const allShippingOptions = await this.baseRepository_.serialize< |
|
1421
|
FulfillmentTypes.ShippingOptionDTO[] | FulfillmentTypes.ShippingOptionDTO |
|
1422
|
>(upsertedShippingOptions) |
|
1423
|
|
|
1424
|
return Array.isArray(data) ? allShippingOptions : allShippingOptions[0] |
|
1425
|
} |
|
1426
|
|
|
1427
|
@InjectTransactionManager() |
|
1428
|
async upsertShippingOptions_( |
|
1429
|
data: |
|
1430
|
| FulfillmentTypes.UpsertShippingOptionDTO[] |
|
1431
|
| FulfillmentTypes.UpsertShippingOptionDTO, |
|
1432
|
@MedusaContext() sharedContext: Context = {} |
|
1433
|
): Promise<InferEntityType<typeof ShippingOption>[]> { |
|
1434
|
const input = Array.isArray(data) ? data : [data] |
|
1435
|
const forUpdate = input.filter( |
|
1436
|
(shippingOption): shippingOption is UpdateShippingOptionsInput => |
|
1437
|
!!shippingOption.id |
|
1438
|
) |
|
1439
|
const forCreate = input.filter( |
|
1440
|
( |
|
1441
|
shippingOption |
|
1442
|
): shippingOption is FulfillmentTypes.CreateShippingOptionDTO => |
|
1443
|
!shippingOption.id |
|
1444
|
) |
|
1445
|
|
|
1446
|
let created: InferEntityType<typeof ShippingOption>[] = [] |
|
1447
|
let updated: InferEntityType<typeof ShippingOption>[] = [] |
|
1448
|
|
|
1449
|
if (forCreate.length) { |
|
1450
|
const createdShippingOptions = await this.createShippingOptions_( |
|
1451
|
forCreate, |
|
1452
|
sharedContext |
|
1453
|
) |
|
1454
|
const toPush = Array.isArray(createdShippingOptions) |
|
1455
|
? createdShippingOptions |
|
1456
|
: [createdShippingOptions] |
|
1457
|
created.push(...toPush) |
|
1458
|
} |
|
1459
|
if (forUpdate.length) { |
|
1460
|
const updatedShippingOptions = await this.updateShippingOptions_( |
|
1461
|
forUpdate, |
|
1462
|
sharedContext |
|
1463
|
) |
|
1464
|
const toPush = Array.isArray(updatedShippingOptions) |
|
1465
|
? updatedShippingOptions |
|
1466
|
: [updatedShippingOptions] |
|
1467
|
updated.push(...toPush) |
|
1468
|
} |
|
1469
|
|
|
1470
|
return [...created, ...updated] |
|
1471
|
} |
|
1472
|
|
|
1473
|
async upsertShippingOptionTypes( |
|
1474
|
data: FulfillmentTypes.UpsertShippingOptionTypeDTO[], |
|
1475
|
sharedContext?: Context |
|
1476
|
): Promise<FulfillmentTypes.ShippingOptionTypeDTO[]> |
|
1477
|
async upsertShippingOptionTypes( |
|
1478
|
data: FulfillmentTypes.UpsertShippingOptionTypeDTO, |
|
1479
|
sharedContext?: Context |
|
1480
|
): Promise<FulfillmentTypes.ShippingOptionTypeDTO> |
|
1481
|
|
|
1482
|
@InjectManager() |
|
1483
|
@EmitEvents() |
|
1484
|
async upsertShippingOptionTypes( |
|
1485
|
data: |
|
1486
|
| FulfillmentTypes.UpsertShippingOptionTypeDTO[] |
|
1487
|
| FulfillmentTypes.UpsertShippingOptionTypeDTO, |
|
1488
|
@MedusaContext() sharedContext: Context = {} |
|
1489
|
): Promise< |
|
1490
|
| FulfillmentTypes.ShippingOptionTypeDTO[] |
|
1491
|
| FulfillmentTypes.ShippingOptionTypeDTO |
|
1492
|
> { |
|
1493
|
const results = await this.updateShippingOptionTypes_(data, sharedContext) |
|
1494
|
|
|
1495
|
const allTypes = await this.baseRepository_.serialize< |
|
1496
|
| FulfillmentTypes.ShippingOptionTypeDTO[] |
|
1497
|
| FulfillmentTypes.ShippingOptionTypeDTO |
|
1498
|
>(results) |
|
1499
|
|
|
1500
|
return Array.isArray(data) ? allTypes : allTypes[0] |
|
1501
|
} |
|
1502
|
|
|
1503
|
@InjectTransactionManager() |
|
1504
|
protected async updateShippingOptionTypes_( |
|
1505
|
data: |
|
1506
|
| FulfillmentTypes.UpsertShippingOptionTypeDTO[] |
|
1507
|
| FulfillmentTypes.UpsertShippingOptionTypeDTO, |
|
1508
|
sharedContext: Context |
|
1509
|
): Promise<InferEntityType<typeof ShippingOptionType>[]> { |
|
1510
|
const input = Array.isArray(data) ? data : [data] |
|
1511
|
|
|
1512
|
const results = await this.shippingOptionTypeService_.upsert( |
|
1513
|
input, |
|
1514
|
sharedContext |
|
1515
|
) |
|
1516
|
|
|
1517
|
return results |
|
1518
|
} |
|
1519
|
|
|
1520
|
// @ts-expect-error |
|
1521
|
updateShippingOptionTypes( |
|
1522
|
id: string, |
|
1523
|
data: FulfillmentTypes.UpdateShippingOptionTypeDTO, |
|
1524
|
sharedContext?: Context |
|
1525
|
): Promise<FulfillmentTypes.ShippingOptionTypeDTO> |
|
1526
|
// @ts-expect-error |
|
1527
|
updateShippingOptionTypes( |
|
1528
|
selector: FulfillmentTypes.FilterableShippingOptionTypeProps, |
|
1529
|
data: FulfillmentTypes.UpdateShippingOptionTypeDTO, |
|
1530
|
sharedContext?: Context |
|
1531
|
): Promise<FulfillmentTypes.ShippingOptionTypeDTO[]> |
|
1532
|
|
|
1533
|
@InjectManager() |
|
1534
|
@EmitEvents() |
|
1535
|
// @ts-expect-error |
|
1536
|
async updateShippingOptionTypes( |
|
1537
|
idOrSelector: string | FulfillmentTypes.FilterableShippingOptionTypeProps, |
|
1538
|
data: FulfillmentTypes.UpdateShippingOptionTypeDTO, |
|
1539
|
@MedusaContext() sharedContext: Context = {} |
|
1540
|
): Promise< |
|
1541
|
| FulfillmentTypes.ShippingOptionTypeDTO[] |
|
1542
|
| FulfillmentTypes.ShippingOptionTypeDTO |
|
1543
|
> { |
|
1544
|
let normalizedInput: FulfillmentTypes.UpdateShippingOptionTypeDTO[] = [] |
|
1545
|
if (isString(idOrSelector)) { |
|
1546
|
// Check if the type exists in the first place |
|
1547
|
await this.shippingOptionTypeService_.retrieve( |
|
1548
|
idOrSelector, |
|
1549
|
{}, |
|
1550
|
sharedContext |
|
1551
|
) |
|
1552
|
normalizedInput = [{ id: idOrSelector, ...data }] |
|
1553
|
} else { |
|
1554
|
const types = await this.shippingOptionTypeService_.list( |
|
1555
|
idOrSelector, |
|
1556
|
{}, |
|
1557
|
sharedContext |
|
1558
|
) |
|
1559
|
|
|
1560
|
normalizedInput = types.map((type) => ({ |
|
1561
|
id: type.id, |
|
1562
|
...data, |
|
1563
|
})) |
|
1564
|
} |
|
1565
|
|
|
1566
|
const types = await this.shippingOptionTypeService_.update( |
|
1567
|
normalizedInput, |
|
1568
|
sharedContext |
|
1569
|
) |
|
1570
|
|
|
1571
|
const updatedTypes = await this.baseRepository_.serialize< |
|
1572
|
FulfillmentTypes.ShippingOptionTypeDTO[] |
|
1573
|
>(types) |
|
1574
|
|
|
1575
|
return isString(idOrSelector) ? updatedTypes[0] : updatedTypes |
|
1576
|
} |
|
1577
|
|
|
1578
|
// @ts-expect-error |
|
1579
|
updateShippingProfiles( |
|
1580
|
selector: FulfillmentTypes.FilterableShippingProfileProps, |
|
1581
|
data: FulfillmentTypes.UpdateShippingProfileDTO, |
|
1582
|
sharedContext?: Context |
|
1583
|
): Promise<FulfillmentTypes.ShippingProfileDTO[]> |
|
1584
|
// @ts-expect-error |
|
1585
|
updateShippingProfiles( |
|
1586
|
id: string, |
|
1587
|
data: FulfillmentTypes.UpdateShippingProfileDTO, |
|
1588
|
sharedContext?: Context |
|
1589
|
): Promise<FulfillmentTypes.ShippingProfileDTO> |
|
1590
|
|
|
1591
|
@InjectManager() |
|
1592
|
@EmitEvents() |
|
1593
|
// @ts-expect-error |
|
1594
|
async updateShippingProfiles( |
|
1595
|
idOrSelector: string | FulfillmentTypes.FilterableShippingProfileProps, |
|
1596
|
data: FulfillmentTypes.UpdateShippingProfileDTO, |
|
1597
|
@MedusaContext() sharedContext: Context = {} |
|
1598
|
): Promise< |
|
1599
|
FulfillmentTypes.ShippingProfileDTO | FulfillmentTypes.ShippingProfileDTO[] |
|
1600
|
> { |
|
1601
|
const profiles = await this.updateShippingProfiles_( |
|
1602
|
idOrSelector, |
|
1603
|
data, |
|
1604
|
sharedContext |
|
1605
|
) |
|
1606
|
|
|
1607
|
const updatedProfiles = await this.baseRepository_.serialize< |
|
1608
|
FulfillmentTypes.ShippingProfileDTO[] |
|
1609
|
>(profiles) |
|
1610
|
|
|
1611
|
return isString(idOrSelector) ? updatedProfiles[0] : updatedProfiles |
|
1612
|
} |
|
1613
|
|
|
1614
|
@InjectTransactionManager() |
|
1615
|
protected async updateShippingProfiles_( |
|
1616
|
idOrSelector: string | FulfillmentTypes.FilterableShippingProfileProps, |
|
1617
|
data: FulfillmentTypes.UpdateShippingProfileDTO, |
|
1618
|
@MedusaContext() sharedContext: Context = {} |
|
1619
|
): Promise<InferEntityType<typeof ShippingProfile>[]> { |
|
1620
|
let normalizedInput: ({ |
|
1621
|
id: string |
|
1622
|
} & FulfillmentTypes.UpdateShippingProfileDTO)[] = [] |
|
1623
|
if (isString(idOrSelector)) { |
|
1624
|
await this.shippingProfileService_.retrieve( |
|
1625
|
idOrSelector, |
|
1626
|
{}, |
|
1627
|
sharedContext |
|
1628
|
) |
|
1629
|
normalizedInput = [{ id: idOrSelector, ...data }] |
|
1630
|
} else { |
|
1631
|
const profiles = await this.shippingProfileService_.list( |
|
1632
|
idOrSelector, |
|
1633
|
{}, |
|
1634
|
sharedContext |
|
1635
|
) |
|
1636
|
|
|
1637
|
normalizedInput = profiles.map((profile) => ({ |
|
1638
|
id: profile.id, |
|
1639
|
...data, |
|
1640
|
})) |
|
1641
|
} |
|
1642
|
|
|
1643
|
const profiles = await this.shippingProfileService_.update( |
|
1644
|
normalizedInput, |
|
1645
|
sharedContext |
|
1646
|
) |
|
1647
|
|
|
1648
|
return profiles |
|
1649
|
} |
|
1650
|
|
|
1651
|
async upsertShippingProfiles( |
|
1652
|
data: FulfillmentTypes.UpsertShippingProfileDTO[], |
|
1653
|
sharedContext?: Context |
|
1654
|
): Promise<FulfillmentTypes.ShippingProfileDTO[]> |
|
1655
|
async upsertShippingProfiles( |
|
1656
|
data: FulfillmentTypes.UpsertShippingProfileDTO, |
|
1657
|
sharedContext?: Context |
|
1658
|
): Promise<FulfillmentTypes.ShippingProfileDTO> |
|
1659
|
|
|
1660
|
@InjectManager() |
|
1661
|
@EmitEvents() |
|
1662
|
async upsertShippingProfiles( |
|
1663
|
data: |
|
1664
|
| FulfillmentTypes.UpsertShippingProfileDTO[] |
|
1665
|
| FulfillmentTypes.UpsertShippingProfileDTO, |
|
1666
|
@MedusaContext() sharedContext: Context = {} |
|
1667
|
): Promise< |
|
1668
|
FulfillmentTypes.ShippingProfileDTO[] | FulfillmentTypes.ShippingProfileDTO |
|
1669
|
> { |
|
1670
|
const profiles = await this.upsertShippingProfiles_(data, sharedContext) |
|
1671
|
|
|
1672
|
return await this.baseRepository_.serialize< |
|
1673
|
| FulfillmentTypes.ShippingProfileDTO[] |
|
1674
|
| FulfillmentTypes.ShippingProfileDTO |
|
1675
|
>(Array.isArray(data) ? profiles : profiles[0]) |
|
1676
|
} |
|
1677
|
|
|
1678
|
@InjectTransactionManager() |
|
1679
|
protected async upsertShippingProfiles_( |
|
1680
|
data: |
|
1681
|
| FulfillmentTypes.UpsertShippingProfileDTO[] |
|
1682
|
| FulfillmentTypes.UpsertShippingProfileDTO, |
|
1683
|
@MedusaContext() sharedContext: Context = {} |
|
1684
|
): Promise< |
|
1685
|
| InferEntityType<typeof ShippingProfile>[] |
|
1686
|
| InferEntityType<typeof ShippingProfile> |
|
1687
|
> { |
|
1688
|
const input = Array.isArray(data) ? data : [data] |
|
1689
|
const forUpdate = input.filter((prof) => !!prof.id) |
|
1690
|
const forCreate = input.filter( |
|
1691
|
(prof): prof is FulfillmentTypes.CreateShippingProfileDTO => !prof.id |
|
1692
|
) |
|
1693
|
|
|
1694
|
let created: InferEntityType<typeof ShippingProfile>[] = [] |
|
1695
|
let updated: InferEntityType<typeof ShippingProfile>[] = [] |
|
1696
|
|
|
1697
|
if (forCreate.length) { |
|
1698
|
created = await this.shippingProfileService_.create( |
|
1699
|
forCreate, |
|
1700
|
sharedContext |
|
1701
|
) |
|
1702
|
} |
|
1703
|
if (forUpdate.length) { |
|
1704
|
updated = await this.shippingProfileService_.update( |
|
1705
|
forUpdate, |
|
1706
|
sharedContext |
|
1707
|
) |
|
1708
|
} |
|
1709
|
|
|
1710
|
return [...created, ...updated] |
|
1711
|
} |
|
1712
|
|
|
1713
|
// @ts-expect-error |
|
1714
|
updateGeoZones( |
|
1715
|
data: FulfillmentTypes.UpdateGeoZoneDTO[], |
|
1716
|
sharedContext?: Context |
|
1717
|
): Promise<FulfillmentTypes.GeoZoneDTO[]> |
|
1718
|
// @ts-expect-error |
|
1719
|
updateGeoZones( |
|
1720
|
data: FulfillmentTypes.UpdateGeoZoneDTO, |
|
1721
|
sharedContext?: Context |
|
1722
|
): Promise<FulfillmentTypes.GeoZoneDTO> |
|
1723
|
|
|
1724
|
@InjectManager() |
|
1725
|
@EmitEvents() |
|
1726
|
// @ts-expect-error |
|
1727
|
async updateGeoZones( |
|
1728
|
data: |
|
1729
|
| FulfillmentTypes.UpdateGeoZoneDTO |
|
1730
|
| FulfillmentTypes.UpdateGeoZoneDTO[], |
|
1731
|
@MedusaContext() sharedContext: Context = {} |
|
1732
|
): Promise<FulfillmentTypes.GeoZoneDTO | FulfillmentTypes.GeoZoneDTO[]> { |
|
1733
|
const data_ = Array.isArray(data) ? data : [data] |
|
1734
|
|
|
1735
|
if (!data_.length) { |
|
1736
|
return [] |
|
1737
|
} |
|
1738
|
|
|
1739
|
FulfillmentModuleService.validateGeoZones(data_) |
|
1740
|
|
|
1741
|
const updatedGeoZones = await this.geoZoneService_.update( |
|
1742
|
data_, |
|
1743
|
sharedContext |
|
1744
|
) |
|
1745
|
|
|
1746
|
const serialized = await this.baseRepository_.serialize< |
|
1747
|
FulfillmentTypes.GeoZoneDTO[] |
|
1748
|
>(updatedGeoZones) |
|
1749
|
|
|
1750
|
return Array.isArray(data) ? serialized : serialized[0] |
|
1751
|
} |
|
1752
|
|
|
1753
|
// @ts-expect-error |
|
1754
|
updateShippingOptionRules( |
|
1755
|
data: FulfillmentTypes.UpdateShippingOptionRuleDTO[], |
|
1756
|
sharedContext?: Context |
|
1757
|
): Promise<FulfillmentTypes.ShippingOptionRuleDTO[]> |
|
1758
|
// @ts-expect-error |
|
1759
|
updateShippingOptionRules( |
|
1760
|
data: FulfillmentTypes.UpdateShippingOptionRuleDTO, |
|
1761
|
sharedContext?: Context |
|
1762
|
): Promise<FulfillmentTypes.ShippingOptionRuleDTO> |
|
1763
|
|
|
1764
|
@InjectManager() |
|
1765
|
@EmitEvents() |
|
1766
|
// @ts-expect-error |
|
1767
|
async updateShippingOptionRules( |
|
1768
|
data: |
|
1769
|
| FulfillmentTypes.UpdateShippingOptionRuleDTO[] |
|
1770
|
| FulfillmentTypes.UpdateShippingOptionRuleDTO, |
|
1771
|
@MedusaContext() sharedContext: Context = {} |
|
1772
|
): Promise< |
|
1773
|
| FulfillmentTypes.ShippingOptionRuleDTO[] |
|
1774
|
| FulfillmentTypes.ShippingOptionRuleDTO |
|
1775
|
> { |
|
1776
|
const updatedShippingOptionRules = await this.updateShippingOptionRules_( |
|
1777
|
data, |
|
1778
|
sharedContext |
|
1779
|
) |
|
1780
|
|
|
1781
|
return await this.baseRepository_.serialize< |
|
1782
|
| FulfillmentTypes.ShippingOptionRuleDTO |
|
1783
|
| FulfillmentTypes.ShippingOptionRuleDTO[] |
|
1784
|
>(updatedShippingOptionRules) |
|
1785
|
} |
|
1786
|
|
|
1787
|
@InjectTransactionManager() |
|
1788
|
async updateShippingOptionRules_( |
|
1789
|
data: |
|
1790
|
| FulfillmentTypes.UpdateShippingOptionRuleDTO[] |
|
1791
|
| FulfillmentTypes.UpdateShippingOptionRuleDTO, |
|
1792
|
@MedusaContext() sharedContext: Context = {} |
|
1793
|
): Promise< |
|
1794
|
| InferEntityType<typeof ShippingOptionRule> |
|
1795
|
| InferEntityType<typeof ShippingOptionRule>[] |
|
1796
|
> { |
|
1797
|
const data_ = Array.isArray(data) ? data : [data] |
|
1798
|
|
|
1799
|
if (!data_.length) { |
|
1800
|
return [] |
|
1801
|
} |
|
1802
|
|
|
1803
|
validateAndNormalizeRules(data_ as unknown as Record<string, unknown>[]) |
|
1804
|
|
|
1805
|
const updatedShippingOptionRules = |
|
1806
|
await this.shippingOptionRuleService_.update(data_, sharedContext) |
|
1807
|
|
|
1808
|
return Array.isArray(data) |
|
1809
|
? updatedShippingOptionRules |
|
1810
|
: updatedShippingOptionRules[0] |
|
1811
|
} |
|
1812
|
|
|
1813
|
@InjectManager() |
|
1814
|
@EmitEvents() |
|
1815
|
async updateFulfillment( |
|
1816
|
id: string, |
|
1817
|
data: FulfillmentTypes.UpdateFulfillmentDTO, |
|
1818
|
@MedusaContext() sharedContext: Context = {} |
|
1819
|
): Promise<FulfillmentTypes.FulfillmentDTO> { |
|
1820
|
const fulfillment = await this.updateFulfillment_(id, data, sharedContext) |
|
1821
|
|
|
1822
|
return await this.baseRepository_.serialize<FulfillmentTypes.FulfillmentDTO>( |
|
1823
|
fulfillment |
|
1824
|
) |
|
1825
|
} |
|
1826
|
|
|
1827
|
@InjectTransactionManager() |
|
1828
|
protected async updateFulfillment_( |
|
1829
|
id: string, |
|
1830
|
data: FulfillmentTypes.UpdateFulfillmentDTO, |
|
1831
|
@MedusaContext() sharedContext: Context |
|
1832
|
): Promise<InferEntityType<typeof Fulfillment>> { |
|
1833
|
const existingFulfillment: InferEntityType<typeof Fulfillment> = |
|
1834
|
await this.fulfillmentService_.retrieve( |
|
1835
|
id, |
|
1836
|
{ |
|
1837
|
relations: ["items", "labels"], |
|
1838
|
}, |
|
1839
|
sharedContext |
|
1840
|
) |
|
1841
|
|
|
1842
|
const updatedLabelIds: string[] = [] |
|
1843
|
|
|
1844
|
/** |
|
1845
|
* @note |
|
1846
|
* Since the relation is a one to many, the deletion, update and creation of labels |
|
1847
|
* is handled b the orm. That means that we dont have to perform any manual deletions or update. |
|
1848
|
* For some reason we use to have upsert and replace handled manually but we could simplify all that just like |
|
1849
|
* we do below which will create the label, update some and delete the one that does not exists in the new data. |
|
1850
|
* |
|
1851
|
* There is a bit of logic as we need to reassign the data of those we want to keep |
|
1852
|
* and we also need to emit the events later on. |
|
1853
|
*/ |
|
1854
|
if (isDefined(data.labels) && isPresent(data.labels)) { |
|
1855
|
for (let label of data.labels) { |
|
1856
|
if (!("id" in label)) { |
|
1857
|
continue |
|
1858
|
} |
|
1859
|
|
|
1860
|
const existingLabel = existingFulfillment.labels.find( |
|
1861
|
({ id }) => id === label.id |
|
1862
|
)! |
|
1863
|
|
|
1864
|
if ( |
|
1865
|
!existingLabel || |
|
1866
|
Object.keys(label).length === 1 || |
|
1867
|
deepEqualObj(existingLabel, label) |
|
1868
|
) { |
|
1869
|
continue |
|
1870
|
} |
|
1871
|
|
|
1872
|
updatedLabelIds.push(label.id) |
|
1873
|
const labelData = { ...label } |
|
1874
|
Object.assign(label, existingLabel, labelData) |
|
1875
|
} |
|
1876
|
} |
|
1877
|
|
|
1878
|
const [fulfillment] = await this.fulfillmentService_.update( |
|
1879
|
[{ id, ...data }], |
|
1880
|
sharedContext |
|
1881
|
) |
|
1882
|
|
|
1883
|
return fulfillment |
|
1884
|
} |
|
1885
|
|
|
1886
|
@InjectManager() |
|
1887
|
@EmitEvents() |
|
1888
|
async cancelFulfillment( |
|
1889
|
id: string, |
|
1890
|
@MedusaContext() sharedContext: Context = {} |
|
1891
|
): Promise<FulfillmentDTO> { |
|
1892
|
const canceledAt = new Date() |
|
1893
|
|
|
1894
|
let fulfillment = await this.fulfillmentService_.retrieve( |
|
1895
|
id, |
|
1896
|
{}, |
|
1897
|
sharedContext |
|
1898
|
) |
|
1899
|
|
|
1900
|
FulfillmentModuleService.canCancelFulfillmentOrThrow(fulfillment) |
|
1901
|
|
|
1902
|
// Make this action idempotent |
|
1903
|
if (!fulfillment.canceled_at) { |
|
1904
|
try { |
|
1905
|
await this.fulfillmentProviderService_.cancelFulfillment( |
|
1906
|
fulfillment.provider_id!, // TODO: should we add a runtime check on provider_id being provided?, |
|
1907
|
fulfillment.data ?? {} |
|
1908
|
) |
|
1909
|
} catch (error) { |
|
1910
|
throw error |
|
1911
|
} |
|
1912
|
|
|
1913
|
fulfillment = await this.fulfillmentService_.update( |
|
1914
|
{ |
|
1915
|
id, |
|
1916
|
canceled_at: canceledAt, |
|
1917
|
}, |
|
1918
|
sharedContext |
|
1919
|
) |
|
1920
|
} |
|
1921
|
|
|
1922
|
const result = await this.baseRepository_.serialize<FulfillmentDTO>( |
|
1923
|
fulfillment |
|
1924
|
) |
|
1925
|
|
|
1926
|
return result |
|
1927
|
} |
|
1928
|
|
|
1929
|
async retrieveFulfillmentOptions( |
|
1930
|
providerId: string |
|
1931
|
): Promise<FulfillmentOption[]> { |
|
1932
|
return await this.fulfillmentProviderService_.getFulfillmentOptions( |
|
1933
|
providerId |
|
1934
|
) |
|
1935
|
} |
|
1936
|
|
|
1937
|
async validateFulfillmentData( |
|
1938
|
providerId: string, |
|
1939
|
optionData: Record<string, unknown>, |
|
1940
|
data: Record<string, unknown>, |
|
1941
|
context: ValidateFulfillmentDataContext |
|
1942
|
): Promise<Record<string, unknown>> { |
|
1943
|
return await this.fulfillmentProviderService_.validateFulfillmentData( |
|
1944
|
providerId, |
|
1945
|
optionData, |
|
1946
|
data, |
|
1947
|
context |
|
1948
|
) |
|
1949
|
} |
|
1950
|
|
|
1951
|
// TODO: seems not to be used, what is the purpose of this method? |
|
1952
|
async validateFulfillmentOption( |
|
1953
|
providerId: string, |
|
1954
|
data: Record<string, unknown> |
|
1955
|
): Promise<boolean> { |
|
1956
|
return await this.fulfillmentProviderService_.validateOption( |
|
1957
|
providerId, |
|
1958
|
data |
|
1959
|
) |
|
1960
|
} |
|
1961
|
|
|
1962
|
@InjectManager() |
|
1963
|
async validateShippingOption( |
|
1964
|
shippingOptionId: string, |
|
1965
|
context: Record<string, unknown> = {}, |
|
1966
|
@MedusaContext() sharedContext: Context = {} |
|
1967
|
) { |
|
1968
|
const shippingOptions = await this.listShippingOptionsForContext( |
|
1969
|
{ id: shippingOptionId, context }, |
|
1970
|
{ |
|
1971
|
relations: ["rules"], |
|
1972
|
}, |
|
1973
|
sharedContext |
|
1974
|
) |
|
1975
|
|
|
1976
|
return !!shippingOptions.length |
|
1977
|
} |
|
1978
|
|
|
1979
|
@InjectManager() |
|
1980
|
async validateShippingOptionsForPriceCalculation( |
|
1981
|
shippingOptionsData: FulfillmentTypes.CreateShippingOptionDTO[], |
|
1982
|
@MedusaContext() sharedContext: Context = {} |
|
1983
|
): Promise<boolean[]> { |
|
1984
|
const nonCalculatedOptions = shippingOptionsData.filter( |
|
1985
|
(option) => option.price_type !== "calculated" |
|
1986
|
) |
|
1987
|
|
|
1988
|
if (nonCalculatedOptions.length) { |
|
1989
|
throw new MedusaError( |
|
1990
|
MedusaError.Types.INVALID_DATA, |
|
1991
|
`Cannot calculate price for non-calculated shipping options: ${nonCalculatedOptions |
|
1992
|
.map((o) => o.name) |
|
1993
|
.join(", ")}` |
|
1994
|
) |
|
1995
|
} |
|
1996
|
|
|
1997
|
const promises = shippingOptionsData.map((option) => |
|
1998
|
this.fulfillmentProviderService_.canCalculate(option.provider_id, option) |
|
1999
|
) |
|
2000
|
|
|
2001
|
return await promiseAll(promises) |
|
2002
|
} |
|
2003
|
|
|
2004
|
async calculateShippingOptionsPrices( |
|
2005
|
shippingOptionsData: FulfillmentTypes.CalculateShippingOptionPriceDTO[] |
|
2006
|
): Promise<CalculatedShippingOptionPrice[]> { |
|
2007
|
const promises = shippingOptionsData.map((data) => |
|
2008
|
this.fulfillmentProviderService_.calculatePrice( |
|
2009
|
data.provider_id, |
|
2010
|
data.optionData, |
|
2011
|
data.data, |
|
2012
|
data.context |
|
2013
|
) |
|
2014
|
) |
|
2015
|
|
|
2016
|
return await promiseAll(promises) |
|
2017
|
} |
|
2018
|
|
|
2019
|
@InjectTransactionManager() |
|
2020
|
@EmitEvents() |
|
2021
|
// @ts-expect-error |
|
2022
|
async deleteShippingProfiles( |
|
2023
|
ids: string | string[], |
|
2024
|
@MedusaContext() sharedContext: Context = {} |
|
2025
|
) { |
|
2026
|
const shippingProfileIds = Array.isArray(ids) ? ids : [ids] |
|
2027
|
await this.validateShippingProfileDeletion( |
|
2028
|
shippingProfileIds, |
|
2029
|
sharedContext |
|
2030
|
) |
|
2031
|
|
|
2032
|
return await super.deleteShippingProfiles(shippingProfileIds, sharedContext) |
|
2033
|
} |
|
2034
|
|
|
2035
|
@InjectTransactionManager() |
|
2036
|
@EmitEvents() |
|
2037
|
// @ts-expect-error |
|
2038
|
async softDeleteShippingProfiles< |
|
2039
|
TReturnableLinkableKeys extends string = string |
|
2040
|
>( |
|
2041
|
ids: string[], |
|
2042
|
config?: SoftDeleteReturn<TReturnableLinkableKeys>, |
|
2043
|
@MedusaContext() sharedContext: Context = {} |
|
2044
|
): Promise<Record<string, string[]> | void> { |
|
2045
|
await this.validateShippingProfileDeletion(ids, sharedContext) |
|
2046
|
|
|
2047
|
return await super.softDeleteShippingProfiles(ids, config, sharedContext) |
|
2048
|
} |
|
2049
|
|
|
2050
|
protected async validateShippingProfileDeletion( |
|
2051
|
ids: string[], |
|
2052
|
sharedContext: Context |
|
2053
|
) { |
|
2054
|
const shippingProfileIds = Array.isArray(ids) ? ids : [ids] |
|
2055
|
const shippingProfiles = await this.shippingProfileService_.list( |
|
2056
|
{ id: shippingProfileIds }, |
|
2057
|
{ |
|
2058
|
relations: ["shipping_options.id"], |
|
2059
|
}, |
|
2060
|
sharedContext |
|
2061
|
) |
|
2062
|
|
|
2063
|
const undeletableShippingProfiles = shippingProfiles.filter( |
|
2064
|
(profile) => profile.shipping_options.length > 0 |
|
2065
|
) |
|
2066
|
if (undeletableShippingProfiles.length) { |
|
2067
|
const undeletableShippingProfileIds = undeletableShippingProfiles.map( |
|
2068
|
(profile) => profile.id |
|
2069
|
) |
|
2070
|
|
|
2071
|
throw new MedusaError( |
|
2072
|
MedusaError.Types.INVALID_DATA, |
|
2073
|
`Cannot delete Shipping Profiles ${undeletableShippingProfileIds} with associated Shipping Options. Delete Shipping Options first and try again.` |
|
2074
|
) |
|
2075
|
} |
|
2076
|
} |
|
2077
|
|
|
2078
|
protected static canCancelFulfillmentOrThrow( |
|
2079
|
fulfillment: InferEntityType<typeof Fulfillment> |
|
2080
|
) { |
|
2081
|
if (fulfillment.shipped_at) { |
|
2082
|
throw new MedusaError( |
|
2083
|
MedusaError.Types.INVALID_DATA, |
|
2084
|
`Fulfillment with id ${fulfillment.id} already shipped` |
|
2085
|
) |
|
2086
|
} |
|
2087
|
|
|
2088
|
if (fulfillment.delivered_at) { |
|
2089
|
throw new MedusaError( |
|
2090
|
MedusaError.Types.INVALID_DATA, |
|
2091
|
`Fulfillment with id ${fulfillment.id} already delivered` |
|
2092
|
) |
|
2093
|
} |
|
2094
|
|
|
2095
|
return true |
|
2096
|
} |
|
2097
|
|
|
2098
|
protected static validateMissingShippingOptions_( |
|
2099
|
shippingOptions: InferEntityType<typeof ShippingOption>[], |
|
2100
|
shippingOptionsData: UpdateShippingOptionsInput[] |
|
2101
|
) { |
|
2102
|
const missingShippingOptionIds = arrayDifference( |
|
2103
|
shippingOptionsData.map((s) => s.id), |
|
2104
|
shippingOptions.map((s) => s.id) |
|
2105
|
) |
|
2106
|
|
|
2107
|
if (missingShippingOptionIds.length) { |
|
2108
|
throw new MedusaError( |
|
2109
|
MedusaError.Types.NOT_FOUND, |
|
2110
|
`The following shipping options do not exist: ${Array.from( |
|
2111
|
missingShippingOptionIds |
|
2112
|
).join(", ")}` |
|
2113
|
) |
|
2114
|
} |
|
2115
|
} |
|
2116
|
|
|
2117
|
protected static validateMissingShippingOptionRules( |
|
2118
|
shippingOption: InferEntityType<typeof ShippingOption>, |
|
2119
|
shippingOptionUpdateData: FulfillmentTypes.UpdateShippingOptionDTO |
|
2120
|
) { |
|
2121
|
if (!shippingOptionUpdateData.rules) { |
|
2122
|
return |
|
2123
|
} |
|
2124
|
|
|
2125
|
const existingRules = shippingOption.rules |
|
2126
|
|
|
2127
|
const rulesSet = new Set(existingRules.map((r) => r.id)) |
|
2128
|
// Only validate the rules that have an id to validate that they really exists in the shipping option |
|
2129
|
const expectedRuleSet = new Set( |
|
2130
|
shippingOptionUpdateData.rules |
|
2131
|
.map((r) => "id" in r && r.id) |
|
2132
|
.filter((id): id is string => !!id) |
|
2133
|
) |
|
2134
|
const nonAlreadyExistingRules = getSetDifference(expectedRuleSet, rulesSet) |
|
2135
|
|
|
2136
|
if (nonAlreadyExistingRules.size) { |
|
2137
|
throw new MedusaError( |
|
2138
|
MedusaError.Types.NOT_FOUND, |
|
2139
|
`The following rules does not exists: ${Array.from( |
|
2140
|
nonAlreadyExistingRules |
|
2141
|
).join(", ")} on shipping option ${shippingOptionUpdateData.id}` |
|
2142
|
) |
|
2143
|
} |
|
2144
|
} |
|
2145
|
|
|
2146
|
protected static validateGeoZones( |
|
2147
|
geoZones: ( |
|
2148
|
| (Partial<FulfillmentTypes.CreateGeoZoneDTO> & { type: string }) |
|
2149
|
| (Partial<FulfillmentTypes.UpdateGeoZoneDTO> & { type: string }) |
|
2150
|
)[] |
|
2151
|
) { |
|
2152
|
const requirePropForType = { |
|
2153
|
country: ["country_code"], |
|
2154
|
province: ["country_code", "province_code"], |
|
2155
|
city: ["country_code", "province_code", "city"], |
|
2156
|
zip: ["country_code", "province_code", "city", "postal_expression"], |
|
2157
|
} |
|
2158
|
|
|
2159
|
for (const geoZone of geoZones) { |
|
2160
|
if (!requirePropForType[geoZone.type]) { |
|
2161
|
throw new MedusaError( |
|
2162
|
MedusaError.Types.INVALID_DATA, |
|
2163
|
`Invalid geo zone type: ${geoZone.type}` |
|
2164
|
) |
|
2165
|
} |
|
2166
|
|
|
2167
|
for (const prop of requirePropForType[geoZone.type]) { |
|
2168
|
if (!geoZone[prop]) { |
|
2169
|
throw new MedusaError( |
|
2170
|
MedusaError.Types.INVALID_DATA, |
|
2171
|
`Missing required property ${prop} for geo zone type ${geoZone.type}` |
|
2172
|
) |
|
2173
|
} |
|
2174
|
} |
|
2175
|
} |
|
2176
|
} |
|
2177
|
|
|
2178
|
protected static normalizeListShippingOptionsForContextParams( |
|
2179
|
filters: FulfillmentTypes.FilterableShippingOptionForContextProps, |
|
2180
|
config: FindConfig<ShippingOptionDTO> = {} |
|
2181
|
) { |
|
2182
|
let { |
|
2183
|
fulfillment_set_id, |
|
2184
|
fulfillment_set_type, |
|
2185
|
address, |
|
2186
|
context, |
|
2187
|
...where |
|
2188
|
} = filters |
|
2189
|
|
|
2190
|
const normalizedConfig = { ...config } |
|
2191
|
normalizedConfig.relations = [ |
|
2192
|
"rules", |
|
2193
|
"type", |
|
2194
|
"shipping_profile", |
|
2195
|
"provider", |
|
2196
|
...(normalizedConfig.relations ?? []), |
|
2197
|
] |
|
2198
|
|
|
2199
|
normalizedConfig.take = |
|
2200
|
normalizedConfig.take ?? (context ? null : undefined) |
|
2201
|
|
|
2202
|
let normalizedFilters = { ...where } |
|
2203
|
|
|
2204
|
if (fulfillment_set_id || fulfillment_set_type) { |
|
2205
|
const fulfillmentSetConstraints = {} |
|
2206
|
|
|
2207
|
if (fulfillment_set_id) { |
|
2208
|
fulfillmentSetConstraints["id"] = fulfillment_set_id |
|
2209
|
} |
|
2210
|
|
|
2211
|
if (fulfillment_set_type) { |
|
2212
|
fulfillmentSetConstraints["type"] = fulfillment_set_type |
|
2213
|
} |
|
2214
|
|
|
2215
|
normalizedFilters = { |
|
2216
|
...normalizedFilters, |
|
2217
|
service_zone: { |
|
2218
|
...(normalizedFilters.service_zone ?? {}), |
|
2219
|
fulfillment_set: { |
|
2220
|
...(normalizedFilters.service_zone?.fulfillment_set ?? {}), |
|
2221
|
...fulfillmentSetConstraints, |
|
2222
|
}, |
|
2223
|
}, |
|
2224
|
} |
|
2225
|
|
|
2226
|
normalizedConfig.relations.push("service_zone.fulfillment_set") |
|
2227
|
} |
|
2228
|
|
|
2229
|
if (address) { |
|
2230
|
const geoZoneConstraints = |
|
2231
|
FulfillmentModuleService.buildGeoZoneConstraintsFromAddress(address) |
|
2232
|
|
|
2233
|
if (geoZoneConstraints.length) { |
|
2234
|
normalizedFilters = { |
|
2235
|
...normalizedFilters, |
|
2236
|
service_zone: { |
|
2237
|
...(normalizedFilters.service_zone ?? {}), |
|
2238
|
geo_zones: { |
|
2239
|
$or: geoZoneConstraints.map((geoZoneConstraint) => ({ |
|
2240
|
// Apply eventually provided constraints on the geo zone along side the address constraints |
|
2241
|
...(normalizedFilters.service_zone?.geo_zones ?? {}), |
|
2242
|
...geoZoneConstraint, |
|
2243
|
})), |
|
2244
|
}, |
|
2245
|
}, |
|
2246
|
} |
|
2247
|
|
|
2248
|
normalizedConfig.relations.push("service_zone.geo_zones") |
|
2249
|
} |
|
2250
|
} |
|
2251
|
|
|
2252
|
normalizedConfig.relations = Array.from(new Set(normalizedConfig.relations)) |
|
2253
|
|
|
2254
|
return { |
|
2255
|
filters: normalizedFilters, |
|
2256
|
config: normalizedConfig, |
|
2257
|
context, |
|
2258
|
} |
|
2259
|
} |
|
2260
|
|
|
2261
|
/** |
|
2262
|
* Build the constraints for the geo zones based on the address properties |
|
2263
|
* available and the hierarchy of required properties. |
|
2264
|
* We build a OR constraint from the narrowest to the broadest |
|
2265
|
* e.g. if we have a postal expression we build a constraint for the postal expression require props of type zip |
|
2266
|
* and a constraint for the city required props of type city |
|
2267
|
* and a constraint for the province code required props of type province |
|
2268
|
* and a constraint for the country code required props of type country |
|
2269
|
* example: |
|
2270
|
* { |
|
2271
|
* $or: [ |
|
2272
|
* { |
|
2273
|
* type: "zip", |
|
2274
|
* country_code: "SE", |
|
2275
|
* province_code: "AB", |
|
2276
|
* city: "Stockholm", |
|
2277
|
* postal_expression: "12345" |
|
2278
|
* }, |
|
2279
|
* { |
|
2280
|
* type: "city", |
|
2281
|
* country_code: "SE", |
|
2282
|
* province_code: "AB", |
|
2283
|
* city: "Stockholm" |
|
2284
|
* }, |
|
2285
|
* { |
|
2286
|
* type: "province", |
|
2287
|
* country_code: "SE", |
|
2288
|
* province_code: "AB" |
|
2289
|
* }, |
|
2290
|
* { |
|
2291
|
* type: "country", |
|
2292
|
* country_code: "SE" |
|
2293
|
* } |
|
2294
|
* ] |
|
2295
|
* } |
|
2296
|
*/ |
|
2297
|
protected static buildGeoZoneConstraintsFromAddress( |
|
2298
|
address: FulfillmentTypes.FilterableShippingOptionForContextProps["address"] |
|
2299
|
) { |
|
2300
|
/** |
|
2301
|
* Define the hierarchy of required properties for the geo zones. |
|
2302
|
*/ |
|
2303
|
const geoZoneRequirePropertyHierarchy = { |
|
2304
|
postal_expression: { |
|
2305
|
props: ["country_code", "province_code", "city", "postal_expression"], |
|
2306
|
type: "zip", |
|
2307
|
}, |
|
2308
|
city: { |
|
2309
|
props: ["country_code", "province_code", "city"], |
|
2310
|
type: "city", |
|
2311
|
}, |
|
2312
|
province_code: { |
|
2313
|
props: ["country_code", "province_code"], |
|
2314
|
type: "province", |
|
2315
|
}, |
|
2316
|
country_code: { |
|
2317
|
props: ["country_code"], |
|
2318
|
type: "country", |
|
2319
|
}, |
|
2320
|
} |
|
2321
|
|
|
2322
|
/** |
|
2323
|
* The following changes assume that the lowest level check (e.g postal expression) can't exist multiple times in the higher level (e.g country) |
|
2324
|
* In case we encounter situations where it is possible to have multiple postal expressions for the same country we need to change the logic back |
|
2325
|
* to this pr https://github.com/medusajs/medusa/pull/8066 |
|
2326
|
*/ |
|
2327
|
|
|
2328
|
const geoZoneConstraints = Object.entries(geoZoneRequirePropertyHierarchy) |
|
2329
|
.map(([prop, { props, type }]) => { |
|
2330
|
if (address![prop]) { |
|
2331
|
return { |
|
2332
|
type, |
|
2333
|
...props.reduce((geoZoneConstraint, prop) => { |
|
2334
|
if (isPresent(address![prop])) { |
|
2335
|
geoZoneConstraint[prop] = address![prop] |
|
2336
|
} |
|
2337
|
return geoZoneConstraint |
|
2338
|
}, {} as Record<string, string | undefined>), |
|
2339
|
} |
|
2340
|
} |
|
2341
|
return null |
|
2342
|
}) |
|
2343
|
.filter((v) => !!v) |
|
2344
|
|
|
2345
|
return geoZoneConstraints |
|
2346
|
} |
|
2347
|
} |