|
1
|
import { moduleProviderLoader } from "@medusajs/framework/modules-sdk" |
|
2
|
import { |
|
3
|
LoaderOptions, |
|
4
|
ModuleProvider, |
|
5
|
ModulesSdkTypes, |
|
6
|
} from "@medusajs/framework/types" |
|
7
|
import { |
|
8
|
ContainerRegistrationKeys, |
|
9
|
lowerCaseFirst, |
|
10
|
promiseAll, |
|
11
|
} from "@medusajs/framework/utils" |
|
12
|
import { FulfillmentProviderService } from "@services" |
|
13
|
import { FulfillmentIdentifiersRegistrationName } from "@types" |
|
14
|
import { Lifetime, asFunction, asValue } from "@medusajs/framework/awilix" |
|
15
|
|
|
16
|
const registrationFn = async (klass, container, pluginOptions) => { |
|
17
|
const key = FulfillmentProviderService.getRegistrationIdentifier( |
|
18
|
klass, |
|
19
|
pluginOptions.id |
|
20
|
) |
|
21
|
|
|
22
|
container.register({ |
|
23
|
["fp_" + key]: asFunction( |
|
24
|
(cradle) => new klass(cradle, pluginOptions.options), |
|
25
|
{ |
|
26
|
lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, |
|
27
|
} |
|
28
|
), |
|
29
|
}) |
|
30
|
|
|
31
|
container.registerAdd(FulfillmentIdentifiersRegistrationName, asValue(key)) |
|
32
|
} |
|
33
|
|
|
34
|
export default async ({ |
|
35
|
container, |
|
36
|
options, |
|
37
|
}: LoaderOptions< |
|
38
|
( |
|
39
|
| ModulesSdkTypes.ModuleServiceInitializeOptions |
|
40
|
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions |
|
41
|
) & { providers: ModuleProvider[] } |
|
42
|
>): Promise<void> => { |
|
43
|
container.registerAdd( |
|
44
|
FulfillmentIdentifiersRegistrationName, |
|
45
|
asValue(undefined) |
|
46
|
) |
|
47
|
|
|
48
|
// Local providers |
|
49
|
// TODO |
|
50
|
|
|
51
|
await moduleProviderLoader({ |
|
52
|
container, |
|
53
|
providers: options?.providers || [], |
|
54
|
registerServiceFn: registrationFn, |
|
55
|
}) |
|
56
|
|
|
57
|
await syncDatabaseProviders({ |
|
58
|
container, |
|
59
|
}) |
|
60
|
} |
|
61
|
|
|
62
|
async function syncDatabaseProviders({ container }) { |
|
63
|
const providerServiceRegistrationKey = lowerCaseFirst( |
|
64
|
FulfillmentProviderService.name |
|
65
|
) |
|
66
|
|
|
67
|
const logger = container.resolve(ContainerRegistrationKeys.LOGGER) ?? console |
|
68
|
|
|
69
|
try { |
|
70
|
const providerIdentifiers: string[] = ( |
|
71
|
container.resolve(FulfillmentIdentifiersRegistrationName) ?? [] |
|
72
|
).filter(Boolean) |
|
73
|
|
|
74
|
const providerService: ModulesSdkTypes.IMedusaInternalService<any> = |
|
75
|
container.resolve(providerServiceRegistrationKey) |
|
76
|
|
|
77
|
const providers = await providerService.list({}) |
|
78
|
const loadedProvidersMap = new Map(providers.map((p) => [p.id, p])) |
|
79
|
|
|
80
|
const providersToCreate = providerIdentifiers.filter( |
|
81
|
(id) => !loadedProvidersMap.has(id) |
|
82
|
) |
|
83
|
const providersToEnabled = providerIdentifiers.filter((id) => |
|
84
|
loadedProvidersMap.has(id) |
|
85
|
) |
|
86
|
const providersToDisable = providers.filter( |
|
87
|
(p) => !providerIdentifiers.includes(p.id) |
|
88
|
) |
|
89
|
|
|
90
|
const promises: Promise<any>[] = [] |
|
91
|
|
|
92
|
if (providersToCreate.length) { |
|
93
|
promises.push( |
|
94
|
providerService.create(providersToCreate.map((id) => ({ id }))) |
|
95
|
) |
|
96
|
} |
|
97
|
|
|
98
|
if (providersToEnabled.length) { |
|
99
|
promises.push( |
|
100
|
providerService.update( |
|
101
|
providersToEnabled.map((id) => ({ id, is_enabled: true })) |
|
102
|
) |
|
103
|
) |
|
104
|
} |
|
105
|
|
|
106
|
if (providersToDisable.length) { |
|
107
|
promises.push( |
|
108
|
providerService.update( |
|
109
|
providersToDisable.map((p) => ({ id: p.id, is_enabled: false })) |
|
110
|
) |
|
111
|
) |
|
112
|
} |
|
113
|
|
|
114
|
await promiseAll(promises) |
|
115
|
} catch (error) { |
|
116
|
logger.error(`Error syncing the fulfillment providers: ${error.message}`) |
|
117
|
} |
|
118
|
} |