|
1
|
import { |
|
2
|
Context, |
|
3
|
DAL, |
|
4
|
FindConfig, |
|
5
|
IEventBusModuleService, |
|
6
|
ILinkModule, |
|
7
|
InternalModuleDeclaration, |
|
8
|
ModuleJoinerConfig, |
|
9
|
RestoreReturn, |
|
10
|
SoftDeleteReturn, |
|
11
|
} from "@medusajs/framework/types" |
|
12
|
import { |
|
13
|
CommonEvents, |
|
14
|
EmitEvents, |
|
15
|
InjectManager, |
|
16
|
InjectTransactionManager, |
|
17
|
isDefined, |
|
18
|
mapObjectTo, |
|
19
|
MapToConfig, |
|
20
|
MedusaContext, |
|
21
|
MedusaError, |
|
22
|
moduleEventBuilderFactory, |
|
23
|
Modules, |
|
24
|
ModulesSdkUtils, |
|
25
|
} from "@medusajs/framework/utils" |
|
26
|
import { LinkService } from "@services" |
|
27
|
|
|
28
|
type InjectedDependencies = { |
|
29
|
baseRepository: DAL.RepositoryService |
|
30
|
linkService: LinkService<any> |
|
31
|
primaryKey: string | string[] |
|
32
|
foreignKey: string |
|
33
|
extraFields: string[] |
|
34
|
entityName: string |
|
35
|
serviceName: string |
|
36
|
[Modules.EVENT_BUS]?: IEventBusModuleService |
|
37
|
} |
|
38
|
|
|
39
|
export default class LinkModuleService implements ILinkModule { |
|
40
|
protected baseRepository_: DAL.RepositoryService |
|
41
|
protected readonly linkService_: LinkService<any> |
|
42
|
protected readonly eventBusModuleService_?: IEventBusModuleService |
|
43
|
protected readonly entityName_: string |
|
44
|
protected readonly serviceName_: string |
|
45
|
protected primaryKey_: string[] |
|
46
|
protected foreignKey_: string |
|
47
|
protected extraFields_: string[] |
|
48
|
|
|
49
|
constructor( |
|
50
|
{ |
|
51
|
baseRepository, |
|
52
|
linkService, |
|
53
|
primaryKey, |
|
54
|
foreignKey, |
|
55
|
extraFields, |
|
56
|
entityName, |
|
57
|
serviceName, |
|
58
|
[Modules.EVENT_BUS]: eventBusModuleService, |
|
59
|
}: InjectedDependencies, |
|
60
|
readonly moduleDeclaration: InternalModuleDeclaration |
|
61
|
) { |
|
62
|
this.baseRepository_ = baseRepository |
|
63
|
this.linkService_ = linkService |
|
64
|
this.eventBusModuleService_ = eventBusModuleService |
|
65
|
this.primaryKey_ = !Array.isArray(primaryKey) ? [primaryKey] : primaryKey |
|
66
|
this.foreignKey_ = foreignKey |
|
67
|
this.extraFields_ = extraFields |
|
68
|
this.entityName_ = entityName |
|
69
|
this.serviceName_ = serviceName |
|
70
|
} |
|
71
|
|
|
72
|
__joinerConfig(): ModuleJoinerConfig { |
|
73
|
return {} as ModuleJoinerConfig |
|
74
|
} |
|
75
|
|
|
76
|
private buildData( |
|
77
|
primaryKeyData: string | string[], |
|
78
|
foreignKeyData: string, |
|
79
|
extra: Record<string, unknown> = {} |
|
80
|
) { |
|
81
|
if (this.primaryKey_.length > 1) { |
|
82
|
if ( |
|
83
|
!Array.isArray(primaryKeyData) || |
|
84
|
primaryKeyData.length !== this.primaryKey_.length |
|
85
|
) { |
|
86
|
throw new MedusaError( |
|
87
|
MedusaError.Types.INVALID_DATA, |
|
88
|
`Primary key data must be an array ${this.primaryKey_.length} values` |
|
89
|
) |
|
90
|
} |
|
91
|
} |
|
92
|
|
|
93
|
const pk = this.primaryKey_.join(",") |
|
94
|
return { |
|
95
|
[pk]: primaryKeyData, |
|
96
|
[this.foreignKey_]: foreignKeyData, |
|
97
|
...extra, |
|
98
|
} |
|
99
|
} |
|
100
|
|
|
101
|
private isValidKeyName(name: string) { |
|
102
|
return this.primaryKey_.concat(this.foreignKey_).includes(name) |
|
103
|
} |
|
104
|
|
|
105
|
private validateFields(data: any | any[]) { |
|
106
|
const dataToValidate = Array.isArray(data) ? data : [data] |
|
107
|
dataToValidate.forEach((d) => { |
|
108
|
const keys = Object.keys(d) |
|
109
|
if (keys.some((k) => !this.isValidKeyName(k))) { |
|
110
|
throw new MedusaError( |
|
111
|
MedusaError.Types.INVALID_DATA, |
|
112
|
`Invalid field name provided. Valid field names are ${this.primaryKey_.concat( |
|
113
|
this.foreignKey_ |
|
114
|
)}` |
|
115
|
) |
|
116
|
} |
|
117
|
}) |
|
118
|
} |
|
119
|
|
|
120
|
@InjectManager() |
|
121
|
async retrieve( |
|
122
|
primaryKeyData: string | string[], |
|
123
|
foreignKeyData: string, |
|
124
|
@MedusaContext() sharedContext: Context = {} |
|
125
|
): Promise<unknown> { |
|
126
|
const filter = this.buildData(primaryKeyData, foreignKeyData) |
|
127
|
const queryOptions = ModulesSdkUtils.buildQuery<unknown>(filter) |
|
128
|
const entry = await this.linkService_.list(queryOptions, {}, sharedContext) |
|
129
|
|
|
130
|
if (!entry?.length) { |
|
131
|
const pk = this.primaryKey_.join(",") |
|
132
|
const errMessage = `${pk}[${primaryKeyData}] and ${this.foreignKey_}[${foreignKeyData}]` |
|
133
|
|
|
134
|
throw new MedusaError( |
|
135
|
MedusaError.Types.NOT_FOUND, |
|
136
|
`Entry ${errMessage} was not found` |
|
137
|
) |
|
138
|
} |
|
139
|
|
|
140
|
return (await this.baseRepository_.serialize(entry[0])) as unknown |
|
141
|
} |
|
142
|
|
|
143
|
@InjectManager() |
|
144
|
async list( |
|
145
|
filters: Record<string, unknown> = {}, |
|
146
|
config: FindConfig<unknown> = {}, |
|
147
|
@MedusaContext() sharedContext: Context = {} |
|
148
|
): Promise<unknown[]> { |
|
149
|
if (!isDefined(config.take)) { |
|
150
|
config.take = null |
|
151
|
} |
|
152
|
|
|
153
|
const rows = await this.linkService_.list(filters, config, sharedContext) |
|
154
|
|
|
155
|
return (await this.baseRepository_.serialize(rows)) as unknown[] |
|
156
|
} |
|
157
|
|
|
158
|
@InjectManager() |
|
159
|
async listAndCount( |
|
160
|
filters: Record<string, unknown> = {}, |
|
161
|
config: FindConfig<unknown> = {}, |
|
162
|
@MedusaContext() sharedContext: Context = {} |
|
163
|
): Promise<[unknown[], number]> { |
|
164
|
if (!isDefined(config.take)) { |
|
165
|
config.take = null |
|
166
|
} |
|
167
|
|
|
168
|
let [rows, count] = await this.linkService_.listAndCount( |
|
169
|
filters, |
|
170
|
config, |
|
171
|
sharedContext |
|
172
|
) |
|
173
|
|
|
174
|
rows = (await this.baseRepository_.serialize(rows)) as unknown[] |
|
175
|
|
|
176
|
return [rows, count] |
|
177
|
} |
|
178
|
|
|
179
|
// `EmitEvents` must sit outside `InjectTransactionManager` so the `ATTACHED` |
|
180
|
// event is flushed after the transaction commits. Emitting from inside the |
|
181
|
// transaction lets subscribers read before the rows are visible: the index |
|
182
|
// module re-fetches the link by id, gets nothing, and silently skips it. |
|
183
|
@InjectManager() |
|
184
|
@EmitEvents() |
|
185
|
@InjectTransactionManager() |
|
186
|
async create( |
|
187
|
primaryKeyOrBulkData: |
|
188
|
| string |
|
189
|
| string[] |
|
190
|
| [string | string[], string, Record<string, unknown>][], |
|
191
|
foreignKeyData?: string, |
|
192
|
extraFields?: Record<string, unknown>, |
|
193
|
@MedusaContext() sharedContext: Context = {} |
|
194
|
) { |
|
195
|
const data: unknown[] = [] |
|
196
|
if (foreignKeyData === undefined && Array.isArray(primaryKeyOrBulkData)) { |
|
197
|
for (const [primaryKey, foreignKey, extra] of primaryKeyOrBulkData) { |
|
198
|
data.push( |
|
199
|
this.buildData( |
|
200
|
primaryKey as string | string[], |
|
201
|
foreignKey as string, |
|
202
|
extra as Record<string, unknown> |
|
203
|
) |
|
204
|
) |
|
205
|
} |
|
206
|
} else { |
|
207
|
data.push( |
|
208
|
this.buildData( |
|
209
|
primaryKeyOrBulkData as string | string[], |
|
210
|
foreignKeyData!, |
|
211
|
extraFields |
|
212
|
) |
|
213
|
) |
|
214
|
} |
|
215
|
|
|
216
|
const links = await this.linkService_.create(data, sharedContext) |
|
217
|
|
|
218
|
moduleEventBuilderFactory({ |
|
219
|
action: CommonEvents.ATTACHED, |
|
220
|
object: this.entityName_, |
|
221
|
source: this.serviceName_, |
|
222
|
eventName: this.entityName_ + "." + CommonEvents.ATTACHED, |
|
223
|
})({ |
|
224
|
data: data as { id: string }[], |
|
225
|
sharedContext, |
|
226
|
}) |
|
227
|
|
|
228
|
return (await this.baseRepository_.serialize(links)) as unknown[] |
|
229
|
} |
|
230
|
|
|
231
|
@InjectManager() |
|
232
|
@EmitEvents() |
|
233
|
@InjectTransactionManager() |
|
234
|
async dismiss( |
|
235
|
primaryKeyOrBulkData: string | string[] | [string | string[], string][], |
|
236
|
foreignKeyData?: string, |
|
237
|
@MedusaContext() sharedContext: Context = {} |
|
238
|
) { |
|
239
|
const data: unknown[] = [] |
|
240
|
if (foreignKeyData === undefined && Array.isArray(primaryKeyOrBulkData)) { |
|
241
|
for (const [primaryKey, foreignKey] of primaryKeyOrBulkData) { |
|
242
|
data.push(this.buildData(primaryKey, foreignKey as string)) |
|
243
|
} |
|
244
|
} else { |
|
245
|
data.push( |
|
246
|
this.buildData( |
|
247
|
primaryKeyOrBulkData as string | string[], |
|
248
|
foreignKeyData! |
|
249
|
) |
|
250
|
) |
|
251
|
} |
|
252
|
|
|
253
|
const links = await this.linkService_.dismiss(data, sharedContext) |
|
254
|
|
|
255
|
moduleEventBuilderFactory({ |
|
256
|
action: CommonEvents.DETACHED, |
|
257
|
object: this.entityName_, |
|
258
|
source: this.serviceName_, |
|
259
|
eventName: this.entityName_ + "." + CommonEvents.DETACHED, |
|
260
|
})({ |
|
261
|
data: links.map((link) => ({ id: link.id })), |
|
262
|
sharedContext, |
|
263
|
}) |
|
264
|
|
|
265
|
return (await this.baseRepository_.serialize(links)) as unknown[] |
|
266
|
} |
|
267
|
|
|
268
|
@InjectManager() |
|
269
|
@EmitEvents() |
|
270
|
@InjectTransactionManager() |
|
271
|
async delete( |
|
272
|
data: any, |
|
273
|
@MedusaContext() sharedContext: Context = {} |
|
274
|
): Promise<void> { |
|
275
|
this.validateFields(data) |
|
276
|
|
|
277
|
await this.linkService_.delete(data, sharedContext) |
|
278
|
|
|
279
|
const allData = Array.isArray(data) ? data : [data] |
|
280
|
moduleEventBuilderFactory({ |
|
281
|
action: CommonEvents.DETACHED, |
|
282
|
object: this.entityName_, |
|
283
|
source: this.serviceName_, |
|
284
|
eventName: this.entityName_ + "." + CommonEvents.DETACHED, |
|
285
|
})({ |
|
286
|
data: allData as { id: string }[], |
|
287
|
sharedContext, |
|
288
|
}) |
|
289
|
} |
|
290
|
|
|
291
|
@InjectManager() |
|
292
|
@EmitEvents() |
|
293
|
@InjectTransactionManager() |
|
294
|
async softDelete( |
|
295
|
data: any, |
|
296
|
{ returnLinkableKeys }: SoftDeleteReturn = {}, |
|
297
|
@MedusaContext() sharedContext: Context = {} |
|
298
|
): Promise<Record<string, unknown[]> | void> { |
|
299
|
const inputArray = Array.isArray(data) ? data : [data] |
|
300
|
|
|
301
|
this.validateFields(inputArray) |
|
302
|
|
|
303
|
let [deletedEntities, cascadedEntitiesMap] = await this.softDelete_( |
|
304
|
inputArray, |
|
305
|
sharedContext |
|
306
|
) |
|
307
|
|
|
308
|
const pk = this.primaryKey_.join(",") |
|
309
|
const entityNameToLinkableKeysMap: MapToConfig = { |
|
310
|
LinkModel: [ |
|
311
|
{ mapTo: pk, valueFrom: pk }, |
|
312
|
{ mapTo: this.foreignKey_, valueFrom: this.foreignKey_ }, |
|
313
|
], |
|
314
|
} |
|
315
|
|
|
316
|
let mappedCascadedEntitiesMap |
|
317
|
if (returnLinkableKeys) { |
|
318
|
// Map internal table/column names to their respective external linkable keys |
|
319
|
// eg: product.id = product_id, variant.id = variant_id |
|
320
|
mappedCascadedEntitiesMap = mapObjectTo<Record<string, string[]>>( |
|
321
|
cascadedEntitiesMap, |
|
322
|
entityNameToLinkableKeysMap, |
|
323
|
{ |
|
324
|
pick: returnLinkableKeys, |
|
325
|
} |
|
326
|
) |
|
327
|
} |
|
328
|
|
|
329
|
moduleEventBuilderFactory({ |
|
330
|
action: CommonEvents.DETACHED, |
|
331
|
object: this.entityName_, |
|
332
|
source: this.serviceName_, |
|
333
|
eventName: this.entityName_ + "." + CommonEvents.DETACHED, |
|
334
|
})({ |
|
335
|
data: deletedEntities as { id: string }[], |
|
336
|
sharedContext, |
|
337
|
}) |
|
338
|
|
|
339
|
return mappedCascadedEntitiesMap ? mappedCascadedEntitiesMap : void 0 |
|
340
|
} |
|
341
|
|
|
342
|
@InjectTransactionManager() |
|
343
|
protected async softDelete_( |
|
344
|
data: any[], |
|
345
|
@MedusaContext() sharedContext: Context = {} |
|
346
|
): Promise<[object[], Record<string, string[]>]> { |
|
347
|
return await this.linkService_.softDelete(data, sharedContext) |
|
348
|
} |
|
349
|
|
|
350
|
@InjectManager() |
|
351
|
@EmitEvents() |
|
352
|
@InjectTransactionManager() |
|
353
|
async restore( |
|
354
|
data: any, |
|
355
|
{ returnLinkableKeys }: RestoreReturn = {}, |
|
356
|
@MedusaContext() sharedContext: Context = {} |
|
357
|
): Promise<Record<string, unknown[]> | void> { |
|
358
|
const inputArray = Array.isArray(data) ? data : [data] |
|
359
|
this.validateFields(inputArray) |
|
360
|
|
|
361
|
let [restoredEntities, cascadedEntitiesMap] = await this.restore_( |
|
362
|
inputArray, |
|
363
|
sharedContext |
|
364
|
) |
|
365
|
|
|
366
|
const pk = this.primaryKey_.join(",") |
|
367
|
const entityNameToLinkableKeysMap: MapToConfig = { |
|
368
|
LinkModel: [ |
|
369
|
{ mapTo: pk, valueFrom: pk }, |
|
370
|
{ mapTo: this.foreignKey_, valueFrom: this.foreignKey_ }, |
|
371
|
], |
|
372
|
} |
|
373
|
|
|
374
|
let mappedCascadedEntitiesMap |
|
375
|
if (returnLinkableKeys) { |
|
376
|
// Map internal table/column names to their respective external linkable keys |
|
377
|
// eg: product.id = product_id, variant.id = variant_id |
|
378
|
mappedCascadedEntitiesMap = mapObjectTo<Record<string, string[]>>( |
|
379
|
cascadedEntitiesMap, |
|
380
|
entityNameToLinkableKeysMap, |
|
381
|
{ |
|
382
|
pick: returnLinkableKeys, |
|
383
|
} |
|
384
|
) |
|
385
|
} |
|
386
|
|
|
387
|
moduleEventBuilderFactory({ |
|
388
|
action: CommonEvents.ATTACHED, |
|
389
|
object: this.entityName_, |
|
390
|
source: this.serviceName_, |
|
391
|
eventName: this.entityName_ + "." + CommonEvents.ATTACHED, |
|
392
|
})({ |
|
393
|
data: restoredEntities as { id: string }[], |
|
394
|
sharedContext, |
|
395
|
}) |
|
396
|
|
|
397
|
return mappedCascadedEntitiesMap ? mappedCascadedEntitiesMap : void 0 |
|
398
|
} |
|
399
|
|
|
400
|
@InjectTransactionManager() |
|
401
|
async restore_( |
|
402
|
data: any, |
|
403
|
@MedusaContext() sharedContext: Context = {} |
|
404
|
): Promise<[object[], Record<string, string[]>]> { |
|
405
|
return await this.linkService_.restore(data, sharedContext) |
|
406
|
} |
|
407
|
|
|
408
|
protected async emitEvents_(groupedEvents) { |
|
409
|
if (!this.eventBusModuleService_ || !groupedEvents) { |
|
410
|
return |
|
411
|
} |
|
412
|
|
|
413
|
const promises: Promise<void>[] = [] |
|
414
|
for (const group of Object.keys(groupedEvents)) { |
|
415
|
promises.push( |
|
416
|
this.eventBusModuleService_.emit(groupedEvents[group], { |
|
417
|
internal: true, |
|
418
|
}) |
|
419
|
) |
|
420
|
} |
|
421
|
|
|
422
|
await Promise.all(promises) |
|
423
|
} |
|
424
|
} |