chore(fulfillment, utils): Migrate module to DML (#10617)

**What**
- Allow to provide `foreignKeyName` option for hasOne and belongsTo relationships
  - `model.hasOne(() => OtherEntity, { foreignKey: true, foreignKeyName: 'other_entity_something_id' })`
  - The above will also output a generated type that takes into consideration the custom fk name 🔽 
- Update types to account for defined custom foreign key name
- Fix joiner config linkable generation to account for custom linkable keys that provide a public API for their model but are not part of the list of the models included in the MedusaService
  - This was supposed to be handled correctly but the implementation was not considering that custom linkable keys could reference models not part of the one provided to medusa service
- Migrate fulfillment module to DML
- Fix has one with fk behaviour and hooks (the relation should be assigned but not the fk)
- Fix has one belongsTo hooks (the relation should be assigned but not the fk)
- Fix hasOneWithFk and belongsTo non persisted fk to be selectable
- Allow to define `belongsTo` without other side definition for `ManyToOne` with no counter part defined
  - Meaning that if a user defined `belongsTo` on one side andnot mapped by and no counter part on the other entity it will be considered as a `ManyToOne`
- `orphanRemoval` on `OneToOne` have been removed, this means that when assigning a new object relation to an entity, the previous one gets deconected but not deleted automatically. This prevent removing data un volountarely

**NOTE**
As per our convention here are some information to keep in mind

**HasOne <> BelongsTo**
Define `OneToOne`, The foreign key is owned by the belongs to and the relation needs to be provided to cascade if wanted

**HasMany <> BelongsTo**
Define `OneToMane` <> `ManyToOne`, the foreign key is owned by the many to one and for those relation no cascade will be performed, the foreign key must be provided. For the `HasMany` the cascade is available

**HasOne (with FK)**
Will act similarly to belongs to with **HasOne <> BelongsTo**

Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
This commit is contained in:
Adrien de Peretti
2024-12-19 16:40:11 +00:00
committed by GitHub
co-authored by Carlos R. L. Rodrigues
parent 65007c49f6
commit 100da64242
39 changed files with 1154 additions and 1858 deletions
@@ -42,6 +42,7 @@ async function list(
"service_zones.shipping_options.provider",
"service_zones.shipping_options.type",
"service_zones.shipping_options.rules",
"service_zones.shipping_options.shipping_profile",
"service_zones.shipping_options.fulfillments.labels",
"service_zones.shipping_options.fulfillments.items",
"service_zones.shipping_options.fulfillments.delivery_address",
@@ -112,20 +113,23 @@ moduleIntegrationTestRunner({
service: FulfillmentModuleService,
}).linkable
expect(Object.keys(linkable)).toEqual([
"fulfillmentAddress",
"fulfillmentItem",
"fulfillmentLabel",
"fulfillmentProvider",
"fulfillmentSet",
"fulfillment",
"geoZone",
"serviceZone",
"shippingOptionRule",
"shippingOptionType",
"shippingOption",
"shippingProfile",
])
expect(Object.keys(linkable)).toHaveLength(12)
expect(Object.keys(linkable)).toEqual(
expect.arrayContaining([
"fulfillmentAddress",
"fulfillmentItem",
"fulfillmentLabel",
"fulfillmentProvider",
"fulfillmentSet",
"fulfillment",
"geoZone",
"serviceZone",
"shippingOptionRule",
"shippingOptionType",
"shippingOption",
"shippingProfile",
])
)
Object.keys(linkable).forEach((key) => {
delete linkable[key].toJSON
@@ -20,7 +20,7 @@ import {
} from "../../__fixtures__"
import { FulfillmentProviderServiceFixtures } from "../../__fixtures__/providers"
jest.setTimeout(100000)
jest.setTimeout(1000000)
const moduleOptions = {
providers: [
@@ -709,7 +709,18 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
const existingRule = shippingOption.rules[0]!
const updateData: UpdateShippingOptionDTO = {
const updateData: UpdateShippingOptionDTO & {
type: {
code: string
description: string
label: string
}
rules: {
attribute: string
operator: string
value: string
}[]
} = {
id: shippingOption.id,
name: "updated-test",
price_type: "calculated",
@@ -767,9 +778,9 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
}),
expect.objectContaining({
id: expect.any(String),
attribute: updateData.rules[1].attribute,
operator: updateData.rules[1].operator,
value: updateData.rules[1].value,
attribute: updateData.rules![1].attribute,
operator: updateData.rules![1].operator,
value: updateData.rules![1].value,
}),
]),
})
@@ -789,13 +800,15 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
)
const types = await service.listShippingOptionTypes()
expect(types).toHaveLength(1)
expect(types[0]).toEqual(
expect.objectContaining({
code: updateData.type.code,
description: updateData.type.description,
label: updateData.type.label,
})
expect(types).toHaveLength(2)
expect(types).toEqual(
expect.arrayContaining([
expect.objectContaining({
code: updateData.type.code,
description: updateData.type.description,
label: updateData.type.label,
}),
])
)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(5)
@@ -1059,7 +1072,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
)
const types = await service.listShippingOptionTypes()
expect(types).toHaveLength(2)
expect(types).toHaveLength(4)
expect(types).toEqual(
expect.arrayContaining([
expect.objectContaining({
@@ -1090,7 +1103,7 @@ moduleIntegrationTestRunner<IFulfillmentModuleService>({
type: "default",
})
const shippingOptionData = {
const shippingOptionData: UpdateShippingOptionDTO = {
id: "sp_jdafwfleiwuonl",
name: "test",
price_type: "flat",