feat(core-flows, fulfillment): Add create return specific method and add more tests (#7357)

* feat(core-flows, fulfillment): Add create return specific method and add more tests

* fix defautl providers in tests fixtures

* more tests

* wip fixes

* fix flow and tests

* cleanup
This commit is contained in:
Adrien de Peretti
2024-05-21 13:48:59 +02:00
committed by GitHub
parent 35dc3c5cf7
commit c4fde7ea5c
9 changed files with 318 additions and 91 deletions

View File

@@ -14,6 +14,10 @@ export class FulfillmentProviderServiceFixtures extends AbstractFulfillmentProvi
async getFulfillmentOptions(): Promise<any> {
return {}
}
async createReturnFulfillment(fulfillment): Promise<any> {
return {}
}
}
export const services = [FulfillmentProviderServiceFixtures]

View File

@@ -137,6 +137,63 @@ moduleIntegrationTestRunner({
})
)
})
it("should create a return fulfillment", async () => {
const shippingProfile = await service.createShippingProfiles({
name: "test",
type: "default",
})
const fulfillmentSet = await service.create({
name: "test",
type: "test-type",
})
const serviceZone = await service.createServiceZones({
name: "test",
fulfillment_set_id: fulfillmentSet.id,
})
const shippingOption = await service.createShippingOptions(
generateCreateShippingOptionsData({
provider_id: providerId,
service_zone_id: serviceZone.id,
shipping_profile_id: shippingProfile.id,
})
)
const fulfillment = await service.createReturnFulfillment(
generateCreateFulfillmentData({
provider_id: providerId,
shipping_option_id: shippingOption.id,
})
)
expect(fulfillment).toEqual(
expect.objectContaining({
id: expect.any(String),
packed_at: null,
shipped_at: null,
delivered_at: null,
canceled_at: null,
data: null,
provider_id: providerId,
shipping_option_id: shippingOption.id,
metadata: null,
delivery_address: expect.objectContaining({
id: expect.any(String),
}),
items: [
expect.objectContaining({
id: expect.any(String),
}),
],
labels: [
expect.objectContaining({
id: expect.any(String),
}),
],
})
)
})
})
describe("on cancel", () => {

View File

@@ -193,9 +193,7 @@ export default class FulfillmentModuleService<
return await this.baseRepository_.serialize<
FulfillmentTypes.ShippingOptionDTO[]
>(shippingOptions, {
populate: true,
})
>(shippingOptions)
}
@InjectManager("baseRepository_")
@@ -211,10 +209,7 @@ export default class FulfillmentModuleService<
)
return await this.baseRepository_.serialize<FulfillmentTypes.FulfillmentDTO>(
fulfillment,
{
populate: true,
}
fulfillment
)
}
@@ -232,9 +227,7 @@ export default class FulfillmentModuleService<
return await this.baseRepository_.serialize<
FulfillmentTypes.FulfillmentDTO[]
>(fulfillments, {
populate: true,
})
>(fulfillments)
}
@InjectManager("baseRepository_")
@@ -251,10 +244,7 @@ export default class FulfillmentModuleService<
return [
await this.baseRepository_.serialize<FulfillmentTypes.FulfillmentDTO[]>(
fulfillments,
{
populate: true,
}
fulfillments
),
count,
]
@@ -283,9 +273,7 @@ export default class FulfillmentModuleService<
return await this.baseRepository_.serialize<
FulfillmentTypes.FulfillmentSetDTO | FulfillmentTypes.FulfillmentSetDTO[]
>(createdFulfillmentSets, {
populate: true,
})
>(createdFulfillmentSets)
}
@InjectTransactionManager("baseRepository_")
@@ -351,9 +339,7 @@ export default class FulfillmentModuleService<
return await this.baseRepository_.serialize<
FulfillmentTypes.ServiceZoneDTO | FulfillmentTypes.ServiceZoneDTO[]
>(createdServiceZones, {
populate: true,
})
>(createdServiceZones)
}
@InjectTransactionManager("baseRepository_")
@@ -410,9 +396,7 @@ export default class FulfillmentModuleService<
return await this.baseRepository_.serialize<
FulfillmentTypes.ShippingOptionDTO | FulfillmentTypes.ShippingOptionDTO[]
>(createdShippingOptions, {
populate: true,
})
>(createdShippingOptions)
}
@InjectTransactionManager("baseRepository_")
@@ -469,9 +453,7 @@ export default class FulfillmentModuleService<
return await this.baseRepository_.serialize<
| FulfillmentTypes.ShippingProfileDTO
| FulfillmentTypes.ShippingProfileDTO[]
>(createdShippingProfiles, {
populate: true,
})
>(createdShippingProfiles)
}
@InjectTransactionManager("baseRepository_")
@@ -523,10 +505,7 @@ export default class FulfillmentModuleService<
)
return await this.baseRepository_.serialize<FulfillmentTypes.GeoZoneDTO[]>(
Array.isArray(data) ? createdGeoZones : createdGeoZones[0],
{
populate: true,
}
Array.isArray(data) ? createdGeoZones : createdGeoZones[0]
)
}
@@ -557,9 +536,7 @@ export default class FulfillmentModuleService<
return await this.baseRepository_.serialize<
| FulfillmentTypes.ShippingOptionRuleDTO
| FulfillmentTypes.ShippingOptionRuleDTO[]
>(createdShippingOptionRules, {
populate: true,
})
>(createdShippingOptionRules)
}
@InjectTransactionManager("baseRepository_")
@@ -627,10 +604,43 @@ export default class FulfillmentModuleService<
}
return await this.baseRepository_.serialize<FulfillmentTypes.FulfillmentDTO>(
fulfillment,
{
populate: true,
}
fulfillment
)
}
@InjectManager("baseRepository_")
async createReturnFulfillment(
data: FulfillmentTypes.CreateFulfillmentDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<FulfillmentTypes.FulfillmentDTO> {
const { order, ...fulfillmentDataToCreate } = data
const fulfillment = await this.fulfillmentService_.create(
fulfillmentDataToCreate,
sharedContext
)
let fulfillmentThirdPartyData!: any
try {
fulfillmentThirdPartyData =
await this.fulfillmentProviderService_.createReturn(
fulfillment.provider_id,
fulfillment as Record<any, any>
)
await this.fulfillmentService_.update(
{
id: fulfillment.id,
data: fulfillmentThirdPartyData ?? {},
},
sharedContext
)
} catch (error) {
await this.fulfillmentService_.delete(fulfillment.id, sharedContext)
throw error
}
return await this.baseRepository_.serialize<FulfillmentTypes.FulfillmentDTO>(
fulfillment
)
}
@@ -654,9 +664,7 @@ export default class FulfillmentModuleService<
return await this.baseRepository_.serialize<
FulfillmentTypes.FulfillmentSetDTO | FulfillmentTypes.FulfillmentSetDTO[]
>(updatedFulfillmentSets, {
populate: true,
})
>(updatedFulfillmentSets)
}
@InjectTransactionManager("baseRepository_")
@@ -865,9 +873,7 @@ export default class FulfillmentModuleService<
return await this.baseRepository_.serialize<
FulfillmentTypes.ServiceZoneDTO | FulfillmentTypes.ServiceZoneDTO[]
>(toReturn, {
populate: true,
})
>(toReturn)
}
@InjectTransactionManager("baseRepository_")
@@ -1110,9 +1116,7 @@ export default class FulfillmentModuleService<
const serialized = await this.baseRepository_.serialize<
FulfillmentTypes.ShippingOptionDTO | FulfillmentTypes.ShippingOptionDTO[]
>(updatedShippingOptions, {
populate: true,
})
>(updatedShippingOptions)
return isString(idOrSelector) ? serialized[0] : serialized
}
@@ -1359,9 +1363,7 @@ export default class FulfillmentModuleService<
const serialized = await this.baseRepository_.serialize<
FulfillmentTypes.GeoZoneDTO[]
>(updatedGeoZones, {
populate: true,
})
>(updatedGeoZones)
return Array.isArray(data) ? serialized : serialized[0]
}
@@ -1393,9 +1395,7 @@ export default class FulfillmentModuleService<
return await this.baseRepository_.serialize<
| FulfillmentTypes.ShippingOptionRuleDTO
| FulfillmentTypes.ShippingOptionRuleDTO[]
>(updatedShippingOptionRules, {
populate: true,
})
>(updatedShippingOptionRules)
}
@InjectTransactionManager("baseRepository_")
@@ -1434,10 +1434,7 @@ export default class FulfillmentModuleService<
const serialized =
await this.baseRepository_.serialize<FulfillmentTypes.FulfillmentDTO>(
fulfillment,
{
populate: true,
}
fulfillment
)
return Array.isArray(serialized) ? serialized[0] : serialized
@@ -1478,9 +1475,7 @@ export default class FulfillmentModuleService<
)
}
const result = await this.baseRepository_.serialize(fulfillment, {
populate: true,
})
const result = await this.baseRepository_.serialize(fulfillment)
return Array.isArray(result) ? result[0] : result
}

View File

@@ -101,4 +101,12 @@ export default class FulfillmentProviderService extends ModulesSdkUtils.internal
const provider = this.retrieveProviderRegistration(providerId)
return await provider.cancelFulfillment(fulfillment)
}
async createReturn(
providerId: string,
fulfillment: Record<string, unknown>,
) {
const provider = this.retrieveProviderRegistration(providerId)
return await provider.createReturnFulfillment(fulfillment)
}
}

View File

@@ -1,6 +1,6 @@
import { Modules } from "@medusajs/modules-sdk"
import { ModuleJoinerConfig } from "@medusajs/types"
import { MapToConfig } from "@medusajs/utils"
import { MapToConfig, pluralize } from "@medusajs/utils"
import { LineItem, ReturnReason } from "@models"
import Order from "./models/order"
@@ -37,6 +37,7 @@ export const joinerConfig: ModuleJoinerConfig = {
name: ["return_reason", "return_reasons"],
args: {
entity: ReturnReason.name,
methodSuffix: pluralize(ReturnReason.name),
},
},
],

View File

@@ -29,16 +29,20 @@ export class ManualFulfillmentService extends AbstractFulfillmentProviderService
return data
}
async validateOption(data: Record<string, unknown>): Promise<boolean> {
async validateOption(data: Record<string, any>): Promise<boolean> {
return true
}
async createFulfillment(): Promise<Record<string, unknown>> {
async createFulfillment(): Promise<Record<string, any>> {
// No data is being sent anywhere
return {}
}
async cancelFulfillment(fulfillment: Record<string, unknown>): Promise<any> {
async cancelFulfillment(): Promise<any> {
return {}
}
async createReturnFulfillment(): Promise<any> {
return {}
}
}