|
1
|
import { |
|
2
|
Context, |
|
3
|
CreateStockLocationInput, |
|
4
|
DAL, |
|
5
|
FilterableStockLocationProps, |
|
6
|
IEventBusService, |
|
7
|
InferEntityType, |
|
8
|
InternalModuleDeclaration, |
|
9
|
IStockLocationService, |
|
10
|
ModuleJoinerConfig, |
|
11
|
ModulesSdkTypes, |
|
12
|
StockLocationAddressInput, |
|
13
|
StockLocationTypes, |
|
14
|
UpdateStockLocationAddressInput, |
|
15
|
UpdateStockLocationInput, |
|
16
|
UpsertStockLocationAddressInput, |
|
17
|
UpsertStockLocationInput, |
|
18
|
} from "@medusajs/framework/types" |
|
19
|
import { |
|
20
|
EmitEvents, |
|
21
|
InjectManager, |
|
22
|
InjectTransactionManager, |
|
23
|
isString, |
|
24
|
MedusaContext, |
|
25
|
MedusaService, |
|
26
|
Modules, |
|
27
|
promiseAll, |
|
28
|
} from "@medusajs/framework/utils" |
|
29
|
import { joinerConfig } from "../joiner-config" |
|
30
|
import { StockLocation, StockLocationAddress } from "../models" |
|
31
|
|
|
32
|
type InjectedDependencies = { |
|
33
|
[Modules.EVENT_BUS]: IEventBusService |
|
34
|
baseRepository: DAL.RepositoryService |
|
35
|
stockLocationService: ModulesSdkTypes.IMedusaInternalService<any> |
|
36
|
stockLocationAddressService: ModulesSdkTypes.IMedusaInternalService<any> |
|
37
|
} |
|
38
|
|
|
39
|
/** |
|
40
|
* Service for managing stock locations. |
|
41
|
*/ |
|
42
|
export default class StockLocationModuleService |
|
43
|
extends MedusaService<{ |
|
44
|
StockLocation: { dto: StockLocationTypes.StockLocationDTO } |
|
45
|
StockLocationAddress: { dto: StockLocationTypes.StockLocationAddressDTO } |
|
46
|
}>({ StockLocation, StockLocationAddress }) |
|
47
|
implements IStockLocationService |
|
48
|
{ |
|
49
|
protected readonly eventBusModuleService_: IEventBusService |
|
50
|
protected baseRepository_: DAL.RepositoryService |
|
51
|
protected readonly stockLocationService_: ModulesSdkTypes.IMedusaInternalService< |
|
52
|
InferEntityType<typeof StockLocation> |
|
53
|
> |
|
54
|
protected readonly stockLocationAddressService_: ModulesSdkTypes.IMedusaInternalService< |
|
55
|
InferEntityType<typeof StockLocationAddress> |
|
56
|
> |
|
57
|
|
|
58
|
constructor( |
|
59
|
{ |
|
60
|
[Modules.EVENT_BUS]: eventBusModuleService, |
|
61
|
baseRepository, |
|
62
|
stockLocationService, |
|
63
|
stockLocationAddressService, |
|
64
|
}: InjectedDependencies, |
|
65
|
protected readonly moduleDeclaration?: InternalModuleDeclaration |
|
66
|
) { |
|
67
|
// @ts-ignore |
|
68
|
super(...arguments) |
|
69
|
|
|
70
|
this.baseRepository_ = baseRepository |
|
71
|
this.stockLocationService_ = stockLocationService |
|
72
|
this.stockLocationAddressService_ = stockLocationAddressService |
|
73
|
this.eventBusModuleService_ = eventBusModuleService |
|
74
|
} |
|
75
|
|
|
76
|
__joinerConfig(): ModuleJoinerConfig { |
|
77
|
return joinerConfig |
|
78
|
} |
|
79
|
|
|
80
|
// @ts-expect-error |
|
81
|
createStockLocations( |
|
82
|
data: CreateStockLocationInput, |
|
83
|
context: Context |
|
84
|
): Promise<StockLocationTypes.StockLocationDTO> |
|
85
|
// @ts-expect-error |
|
86
|
createStockLocations( |
|
87
|
data: CreateStockLocationInput[], |
|
88
|
context: Context |
|
89
|
): Promise<StockLocationTypes.StockLocationDTO[]> |
|
90
|
|
|
91
|
@InjectManager() |
|
92
|
@EmitEvents() |
|
93
|
// @ts-expect-error |
|
94
|
async createStockLocations( |
|
95
|
data: CreateStockLocationInput | CreateStockLocationInput[], |
|
96
|
@MedusaContext() context: Context = {} |
|
97
|
): Promise< |
|
98
|
StockLocationTypes.StockLocationDTO | StockLocationTypes.StockLocationDTO[] |
|
99
|
> { |
|
100
|
const input = Array.isArray(data) ? data : [data] |
|
101
|
|
|
102
|
const created = await this.createStockLocations_(input, context) |
|
103
|
|
|
104
|
const serialized = await this.baseRepository_.serialize< |
|
105
|
| StockLocationTypes.StockLocationDTO |
|
106
|
| StockLocationTypes.StockLocationDTO[] |
|
107
|
>(created) |
|
108
|
|
|
109
|
return Array.isArray(data) ? serialized : serialized[0] |
|
110
|
} |
|
111
|
|
|
112
|
@InjectTransactionManager() |
|
113
|
async createStockLocations_( |
|
114
|
data: CreateStockLocationInput[], |
|
115
|
@MedusaContext() context: Context = {} |
|
116
|
): Promise<InferEntityType<typeof StockLocation>[]> { |
|
117
|
return await this.stockLocationService_.create(data, context) |
|
118
|
} |
|
119
|
|
|
120
|
async upsertStockLocations( |
|
121
|
data: UpsertStockLocationInput, |
|
122
|
context?: Context |
|
123
|
): Promise<StockLocationTypes.StockLocationDTO> |
|
124
|
async upsertStockLocations( |
|
125
|
data: UpsertStockLocationInput[], |
|
126
|
context?: Context |
|
127
|
): Promise<StockLocationTypes.StockLocationDTO[]> |
|
128
|
|
|
129
|
@InjectManager() |
|
130
|
@EmitEvents() |
|
131
|
async upsertStockLocations( |
|
132
|
data: UpsertStockLocationInput | UpsertStockLocationInput[], |
|
133
|
@MedusaContext() context: Context = {} |
|
134
|
): Promise< |
|
135
|
StockLocationTypes.StockLocationDTO | StockLocationTypes.StockLocationDTO[] |
|
136
|
> { |
|
137
|
const input = Array.isArray(data) ? data : [data] |
|
138
|
|
|
139
|
const result = await this.upsertStockLocations_(input, context) |
|
140
|
|
|
141
|
return await this.baseRepository_.serialize< |
|
142
|
| StockLocationTypes.StockLocationDTO[] |
|
143
|
| StockLocationTypes.StockLocationDTO |
|
144
|
>(Array.isArray(data) ? result : result[0]) |
|
145
|
} |
|
146
|
|
|
147
|
@InjectTransactionManager() |
|
148
|
async upsertStockLocations_( |
|
149
|
input: UpsertStockLocationInput[], |
|
150
|
@MedusaContext() context: Context = {} |
|
151
|
) { |
|
152
|
const toUpdate = input.filter( |
|
153
|
(location): location is UpdateStockLocationInput => !!location.id |
|
154
|
) as UpdateStockLocationInput[] |
|
155
|
const toCreate = input.filter( |
|
156
|
(location) => !location.id |
|
157
|
) as CreateStockLocationInput[] |
|
158
|
|
|
159
|
const operations: Promise< |
|
160
|
| InferEntityType<typeof StockLocation>[] |
|
161
|
| InferEntityType<typeof StockLocation> |
|
162
|
>[] = [] |
|
163
|
|
|
164
|
if (toCreate.length) { |
|
165
|
operations.push(this.createStockLocations_(toCreate, context)) |
|
166
|
} |
|
167
|
if (toUpdate.length) { |
|
168
|
operations.push(this.updateStockLocations_(toUpdate, context)) |
|
169
|
} |
|
170
|
|
|
171
|
return (await promiseAll(operations)).flat() |
|
172
|
} |
|
173
|
|
|
174
|
// @ts-expect-error |
|
175
|
updateStockLocations( |
|
176
|
id: string, |
|
177
|
input: UpdateStockLocationInput, |
|
178
|
context?: Context |
|
179
|
): Promise<StockLocationTypes.StockLocationDTO> |
|
180
|
// @ts-expect-error |
|
181
|
updateStockLocations( |
|
182
|
selector: FilterableStockLocationProps, |
|
183
|
input: UpdateStockLocationInput, |
|
184
|
context?: Context |
|
185
|
): Promise<StockLocationTypes.StockLocationDTO[]> |
|
186
|
|
|
187
|
/** |
|
188
|
* Updates an existing stock location. |
|
189
|
* @param stockLocationId - The ID of the stock location to update. |
|
190
|
* @param updateData - The update data for the stock location. |
|
191
|
* @param context |
|
192
|
* @returns The updated stock location. |
|
193
|
*/ |
|
194
|
@InjectManager() |
|
195
|
@EmitEvents() |
|
196
|
// @ts-expect-error |
|
197
|
async updateStockLocations( |
|
198
|
idOrSelector: string | FilterableStockLocationProps, |
|
199
|
data: UpdateStockLocationInput | UpdateStockLocationInput[], |
|
200
|
@MedusaContext() context: Context = {} |
|
201
|
): Promise< |
|
202
|
StockLocationTypes.StockLocationDTO | StockLocationTypes.StockLocationDTO[] |
|
203
|
> { |
|
204
|
let normalizedInput: |
|
205
|
| (UpdateStockLocationInput & { id: string })[] |
|
206
|
| { data: any; selector: FilterableStockLocationProps } = [] |
|
207
|
if (isString(idOrSelector)) { |
|
208
|
normalizedInput = [{ id: idOrSelector, ...data }] |
|
209
|
} else { |
|
210
|
normalizedInput = { data, selector: idOrSelector } |
|
211
|
} |
|
212
|
const updated = await this.updateStockLocations_(normalizedInput, context) |
|
213
|
|
|
214
|
const serialized = await this.baseRepository_.serialize< |
|
215
|
| StockLocationTypes.StockLocationDTO |
|
216
|
| StockLocationTypes.StockLocationDTO[] |
|
217
|
>(updated) |
|
218
|
|
|
219
|
return Array.isArray(data) ? serialized : serialized[0] |
|
220
|
} |
|
221
|
|
|
222
|
@InjectTransactionManager() |
|
223
|
async updateStockLocations_( |
|
224
|
data: |
|
225
|
| UpdateStockLocationInput[] |
|
226
|
| UpdateStockLocationInput |
|
227
|
| { data: any; selector: FilterableStockLocationProps }, |
|
228
|
@MedusaContext() context: Context = {} |
|
229
|
): Promise< |
|
230
|
| InferEntityType<typeof StockLocation>[] |
|
231
|
| InferEntityType<typeof StockLocation> |
|
232
|
> { |
|
233
|
return await this.stockLocationService_.update(data, context) |
|
234
|
} |
|
235
|
|
|
236
|
// @ts-expect-error |
|
237
|
updateStockLocationAddresses( |
|
238
|
data: StockLocationAddressInput & { id: string }, |
|
239
|
context?: Context |
|
240
|
): Promise<StockLocationTypes.StockLocationAddressDTO> |
|
241
|
// @ts-expect-error |
|
242
|
updateStockLocationAddresses( |
|
243
|
data: (StockLocationAddressInput & { id: string })[], |
|
244
|
context?: Context |
|
245
|
): Promise<StockLocationTypes.StockLocationAddressDTO[]> |
|
246
|
|
|
247
|
@InjectManager() |
|
248
|
@EmitEvents() |
|
249
|
// @ts-expect-error |
|
250
|
async updateStockLocationAddresses( |
|
251
|
data: |
|
252
|
| (StockLocationAddressInput & { id: string }) |
|
253
|
| (StockLocationAddressInput & { id: string })[], |
|
254
|
@MedusaContext() context: Context = {} |
|
255
|
) { |
|
256
|
const input = Array.isArray(data) ? data : [data] |
|
257
|
|
|
258
|
const updated = await this.updateStockLocationAddresses_(input, context) |
|
259
|
|
|
260
|
const serialized = await this.baseRepository_.serialize< |
|
261
|
| StockLocationTypes.StockLocationAddressDTO |
|
262
|
| StockLocationTypes.StockLocationAddressDTO[] |
|
263
|
>(updated) |
|
264
|
|
|
265
|
return Array.isArray(data) ? serialized : serialized[0] |
|
266
|
} |
|
267
|
|
|
268
|
@InjectTransactionManager() |
|
269
|
private async updateStockLocationAddresses_( |
|
270
|
input: (StockLocationAddressInput & { id: string })[], |
|
271
|
@MedusaContext() context: Context |
|
272
|
) { |
|
273
|
return await this.stockLocationAddressService_.update(input, context) |
|
274
|
} |
|
275
|
|
|
276
|
async upsertStockLocationAddresses( |
|
277
|
data: UpsertStockLocationAddressInput, |
|
278
|
context?: Context |
|
279
|
): Promise<StockLocationTypes.StockLocationAddressDTO> |
|
280
|
async upsertStockLocationAddresses( |
|
281
|
data: UpsertStockLocationAddressInput[], |
|
282
|
context?: Context |
|
283
|
): Promise<StockLocationTypes.StockLocationAddressDTO[]> |
|
284
|
|
|
285
|
@InjectManager() |
|
286
|
@EmitEvents() |
|
287
|
async upsertStockLocationAddresses( |
|
288
|
data: UpsertStockLocationAddressInput | UpsertStockLocationAddressInput[], |
|
289
|
@MedusaContext() context: Context = {} |
|
290
|
): Promise< |
|
291
|
| StockLocationTypes.StockLocationAddressDTO |
|
292
|
| StockLocationTypes.StockLocationAddressDTO[] |
|
293
|
> { |
|
294
|
const input = Array.isArray(data) ? data : [data] |
|
295
|
|
|
296
|
const result = await this.upsertStockLocationAddresses_(input, context) |
|
297
|
|
|
298
|
return await this.baseRepository_.serialize< |
|
299
|
| StockLocationTypes.StockLocationAddressDTO[] |
|
300
|
| StockLocationTypes.StockLocationAddressDTO |
|
301
|
>(Array.isArray(data) ? result : result[0]) |
|
302
|
} |
|
303
|
|
|
304
|
@InjectTransactionManager() |
|
305
|
async upsertStockLocationAddresses_( |
|
306
|
input: UpsertStockLocationAddressInput[], |
|
307
|
@MedusaContext() context: Context = {} |
|
308
|
) { |
|
309
|
const toUpdate = input.filter( |
|
310
|
(location): location is UpdateStockLocationAddressInput => !!location.id |
|
311
|
) as UpdateStockLocationAddressInput[] |
|
312
|
const toCreate = input.filter( |
|
313
|
(location) => !location.id |
|
314
|
) as StockLocationAddressInput[] |
|
315
|
|
|
316
|
const operations: Promise< |
|
317
|
| InferEntityType<typeof StockLocationAddress>[] |
|
318
|
| InferEntityType<typeof StockLocationAddress> |
|
319
|
>[] = [] |
|
320
|
|
|
321
|
if (toCreate.length) { |
|
322
|
operations.push( |
|
323
|
this.stockLocationAddressService_.create(toCreate, context) |
|
324
|
) |
|
325
|
} |
|
326
|
if (toUpdate.length) { |
|
327
|
operations.push( |
|
328
|
this.stockLocationAddressService_.update(toUpdate, context) |
|
329
|
) |
|
330
|
} |
|
331
|
|
|
332
|
return (await promiseAll(operations)).flat() |
|
333
|
} |
|
334
|
} |