fix: Shipping profile deletion with options (#8910)

* fix(fulfillment): Prevent deletion of profiles associated to options

* add tests

* typo

* address feedback
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2024-08-30 17:04:29 +02:00
1 parent dea5af085a
commit 3da01035f3
3 files changed
+150 -2

No files matched your search

@@ -10,6 +10,7 @@ import {
ModuleJoinerConfig,
ModulesSdkTypes,
ShippingOptionDTO,
SoftDeleteReturn,
UpdateFulfillmentSetDTO,
UpdateServiceZoneDTO,
} from "@medusajs/types"
@@ -1953,6 +1954,63 @@ export default class FulfillmentModuleService
return !!shippingOptions.length
}
@InjectTransactionManager()
// @ts-expect-error
async deleteShippingProfiles(
ids: string | string[],
@MedusaContext() sharedContext: Context = {}
) {
const shippingProfileIds = Array.isArray(ids) ? ids : [ids]
await this.validateShippingProfileDeletion(
shippingProfileIds,
sharedContext
)
return await super.deleteShippingProfiles(shippingProfileIds, sharedContext)
}
@InjectTransactionManager()
// @ts-expect-error
async softDeleteShippingProfiles<
TReturnableLinkableKeys extends string = string
>(
ids: string[],
config?: SoftDeleteReturn<TReturnableLinkableKeys>,
@MedusaContext() sharedContext: Context = {}
): Promise<Record<string, string[]> | void> {
await this.validateShippingProfileDeletion(ids, sharedContext)
return await super.softDeleteShippingProfiles(ids, config, sharedContext)
}
protected async validateShippingProfileDeletion(
ids: string[],
sharedContext: Context
) {
const shippingProfileIds = Array.isArray(ids) ? ids : [ids]
const shippingProfiles = await this.shippingProfileService_.list(
{ id: shippingProfileIds },
{
relations: ["shipping_options.id"],
},
sharedContext
)
const undeletableShippingProfiles = shippingProfiles.filter(
(profile) => profile.shipping_options.length > 0
)
if (undeletableShippingProfiles.length) {
const undeletableShippingProfileIds = undeletableShippingProfiles.map(
(profile) => profile.id
)
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Cannot delete Shipping Profiles ${undeletableShippingProfileIds} with associated Shipping Options. Delete Shipping Options first and try again.`
)
}
}
protected static canCancelFulfillmentOrThrow(fulfillment: Fulfillment) {
if (fulfillment.shipped_at) {
throw new MedusaError(