feat(fulfillment): Soft deletes (#6630)

**What**
- Ensure soft delete works properly according to the soft delete configuration and validate all relation of the entire data model
- Add is_enabled to the providers in order to manage new providers to enabled or disabled
- include joiner config update

FIXES CORE-1853
FIXES CORE-1830
FIXES CORE-1719
This commit is contained in:
Adrien de Peretti
2024-03-11 15:56:08 +00:00
committed by GitHub
parent 78e5ec459a
commit d9d5afc3cf
23 changed files with 985 additions and 696 deletions
+36 -13
View File
@@ -2,7 +2,7 @@ import { moduleProviderLoader } from "@medusajs/modules-sdk"
import { LoaderOptions, ModuleProvider, ModulesSdkTypes } from "@medusajs/types"
import { asFunction, asValue, Lifetime } from "awilix"
import { FulfillmentIdentifiersRegistrationName } from "@types"
import { lowerCaseFirst } from "@medusajs/utils"
import { lowerCaseFirst, promiseAll } from "@medusajs/utils"
import { FulfillmentProviderService } from "@services"
import { ContainerRegistrationKeys } from "@medusajs/utils/src"
@@ -56,7 +56,8 @@ async function syncDatabaseProviders({ container }) {
FulfillmentProviderService.name
)
const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
const logger = container.resolve(ContainerRegistrationKeys.LOGGER) ?? console
try {
const providerIdentifiers: string[] = (
container.resolve(FulfillmentIdentifiersRegistrationName) ?? []
@@ -65,22 +66,44 @@ async function syncDatabaseProviders({ container }) {
const providerService: ModulesSdkTypes.InternalModuleService<any> =
container.resolve(providerServiceRegistrationKey)
const providers = await providerService.list({
id: providerIdentifiers,
})
const providers = await providerService.list({})
const loadedProvidersMap = new Map(providers.map((p) => [p.id, p]))
const providersToCreate: any[] = []
for (const identifier of providerIdentifiers) {
if (loadedProvidersMap.has(identifier)) {
continue
}
const providersToCreate = providerIdentifiers.filter(
(id) => !loadedProvidersMap.has(id)
)
const providersToEnabled = providerIdentifiers.filter((id) =>
loadedProvidersMap.has(id)
)
const providersToDisable = providers.filter(
(p) => !providerIdentifiers.includes(p.id)
)
providersToCreate.push({ id: identifier })
const promises: Promise<any>[] = []
if (providersToCreate.length) {
promises.push(
providerService.create(providersToCreate.map((id) => ({ id })))
)
}
await providerService.create(providersToCreate)
if (providersToEnabled.length) {
promises.push(
providerService.update(
providersToEnabled.map((id) => ({ id, is_enabled: true }))
)
)
}
if (providersToDisable.length) {
promises.push(
providerService.update(
providersToDisable.map((p) => ({ id: p.id, is_enabled: false }))
)
)
}
await promiseAll(promises)
} catch (error) {
logger.error(`Error syncing providers: ${error.message}`)
}