**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>
291 lines
8.7 KiB
TypeScript
291 lines
8.7 KiB
TypeScript
import {
|
|
Fulfillment,
|
|
FulfillmentSet,
|
|
GeoZone,
|
|
ServiceZone,
|
|
ShippingOption,
|
|
ShippingOptionRule,
|
|
ShippingOptionType,
|
|
} from "@models"
|
|
import { Context, InferEntityType } from "@medusajs/framework/types"
|
|
import {
|
|
CommonEvents,
|
|
FulfillmentEvents,
|
|
moduleEventBuilderFactory,
|
|
Modules,
|
|
} from "@medusajs/framework/utils"
|
|
|
|
export const eventBuilders = {
|
|
createdFulfillment: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.CREATED,
|
|
object: "fulfillment",
|
|
eventName: FulfillmentEvents.FULFILLMENT_CREATED,
|
|
}),
|
|
updatedFulfillment: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.UPDATED,
|
|
object: "fulfillment",
|
|
eventName: FulfillmentEvents.FULFILLMENT_UPDATED,
|
|
}),
|
|
createdFulfillmentAddress: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.CREATED,
|
|
object: "fulfillment_address",
|
|
eventName: FulfillmentEvents.FULFILLMENT_ADDRESS_CREATED,
|
|
}),
|
|
createdFulfillmentItem: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.CREATED,
|
|
object: "fulfillment_item",
|
|
eventName: FulfillmentEvents.FULFILLMENT_ITEM_CREATED,
|
|
}),
|
|
createdFulfillmentLabel: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.CREATED,
|
|
object: "fulfillment_label",
|
|
eventName: FulfillmentEvents.FULFILLMENT_LABEL_CREATED,
|
|
}),
|
|
updatedFulfillmentLabel: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.UPDATED,
|
|
object: "fulfillment_label",
|
|
eventName: FulfillmentEvents.FULFILLMENT_LABEL_UPDATED,
|
|
}),
|
|
deletedFulfillmentLabel: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.DELETED,
|
|
object: "fulfillment_label",
|
|
eventName: FulfillmentEvents.FULFILLMENT_LABEL_DELETED,
|
|
}),
|
|
createdShippingProfile: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.CREATED,
|
|
object: "shipping_profile",
|
|
eventName: FulfillmentEvents.SHIPPING_PROFILE_CREATED,
|
|
}),
|
|
createdShippingOptionType: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.CREATED,
|
|
object: "shipping_option_type",
|
|
eventName: FulfillmentEvents.SHIPPING_OPTION_TYPE_CREATED,
|
|
}),
|
|
updatedShippingOptionType: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.UPDATED,
|
|
object: "shipping_option_type",
|
|
eventName: FulfillmentEvents.SHIPPING_OPTION_TYPE_UPDATED,
|
|
}),
|
|
deletedShippingOptionType: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.DELETED,
|
|
object: "shipping_option_type",
|
|
eventName: FulfillmentEvents.SHIPPING_OPTION_TYPE_DELETED,
|
|
}),
|
|
createdShippingOptionRule: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.CREATED,
|
|
object: "shipping_option_rule",
|
|
eventName: FulfillmentEvents.SHIPPING_OPTION_RULE_CREATED,
|
|
}),
|
|
updatedShippingOptionRule: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.UPDATED,
|
|
object: "shipping_option_rule",
|
|
eventName: FulfillmentEvents.SHIPPING_OPTION_RULE_UPDATED,
|
|
}),
|
|
deletedShippingOptionRule: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.DELETED,
|
|
object: "shipping_option_rule",
|
|
eventName: FulfillmentEvents.SHIPPING_OPTION_RULE_DELETED,
|
|
}),
|
|
createdShippingOption: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.CREATED,
|
|
object: "shipping_option",
|
|
eventName: FulfillmentEvents.SHIPPING_OPTION_CREATED,
|
|
}),
|
|
updatedShippingOption: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.UPDATED,
|
|
object: "shipping_option",
|
|
eventName: FulfillmentEvents.SHIPPING_OPTION_UPDATED,
|
|
}),
|
|
createdFulfillmentSet: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.CREATED,
|
|
object: "fulfillment_set",
|
|
eventName: FulfillmentEvents.FULFILLMENT_SET_CREATED,
|
|
}),
|
|
updatedFulfillmentSet: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.UPDATED,
|
|
object: "fulfillment_set",
|
|
eventName: FulfillmentEvents.FULFILLMENT_SET_UPDATED,
|
|
}),
|
|
deletedFulfillmentSet: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.DELETED,
|
|
object: "fulfillment_set",
|
|
eventName: FulfillmentEvents.FULFILLMENT_SET_DELETED,
|
|
}),
|
|
createdServiceZone: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.CREATED,
|
|
object: "service_zone",
|
|
eventName: FulfillmentEvents.SERVICE_ZONE_CREATED,
|
|
}),
|
|
updatedServiceZone: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.UPDATED,
|
|
object: "service_zone",
|
|
eventName: FulfillmentEvents.SERVICE_ZONE_UPDATED,
|
|
}),
|
|
deletedServiceZone: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.DELETED,
|
|
object: "service_zone",
|
|
eventName: FulfillmentEvents.SERVICE_ZONE_DELETED,
|
|
}),
|
|
createdGeoZone: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.CREATED,
|
|
object: "geo_zone",
|
|
eventName: FulfillmentEvents.GEO_ZONE_CREATED,
|
|
}),
|
|
updatedGeoZone: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.UPDATED,
|
|
object: "geo_zone",
|
|
eventName: FulfillmentEvents.GEO_ZONE_UPDATED,
|
|
}),
|
|
deletedGeoZone: moduleEventBuilderFactory({
|
|
source: Modules.FULFILLMENT,
|
|
action: CommonEvents.DELETED,
|
|
object: "geo_zone",
|
|
eventName: FulfillmentEvents.GEO_ZONE_DELETED,
|
|
}),
|
|
}
|
|
|
|
export function buildCreatedFulfillmentEvents({
|
|
fulfillments,
|
|
sharedContext,
|
|
}: {
|
|
fulfillments: InferEntityType<typeof Fulfillment>[]
|
|
sharedContext: Context
|
|
}) {
|
|
if (!fulfillments.length) {
|
|
return
|
|
}
|
|
|
|
const fulfillments_: { id: string }[] = []
|
|
const addresses: { id: string }[] = []
|
|
const items: { id: string }[] = []
|
|
const labels: { id: string }[] = []
|
|
|
|
fulfillments.forEach((fulfillment) => {
|
|
fulfillments_.push({ id: fulfillment.id })
|
|
|
|
if (fulfillment.delivery_address) {
|
|
addresses.push({ id: fulfillment.delivery_address.id })
|
|
}
|
|
|
|
if (fulfillment.items) {
|
|
items.push(...fulfillment.items)
|
|
}
|
|
|
|
if (fulfillment.labels) {
|
|
labels.push(...fulfillment.labels)
|
|
}
|
|
})
|
|
|
|
eventBuilders.createdFulfillment({ data: fulfillments_, sharedContext })
|
|
eventBuilders.createdFulfillmentAddress({ data: addresses, sharedContext })
|
|
eventBuilders.createdFulfillmentItem({ data: items, sharedContext })
|
|
eventBuilders.createdFulfillmentLabel({ data: labels, sharedContext })
|
|
}
|
|
|
|
export function buildCreatedShippingOptionEvents({
|
|
shippingOptions,
|
|
sharedContext,
|
|
}: {
|
|
shippingOptions: InferEntityType<typeof ShippingOption>[]
|
|
sharedContext: Context
|
|
}) {
|
|
if (!shippingOptions.length) {
|
|
return
|
|
}
|
|
|
|
const options: { id: string }[] = []
|
|
const types: InferEntityType<typeof ShippingOptionType>[] = []
|
|
const rules: InferEntityType<typeof ShippingOptionRule>[] = []
|
|
|
|
shippingOptions.forEach((shippingOption) => {
|
|
options.push({ id: shippingOption.id })
|
|
|
|
if (shippingOption.type) {
|
|
types.push(shippingOption.type)
|
|
}
|
|
|
|
if (shippingOption.rules) {
|
|
rules.push(...shippingOption.rules)
|
|
}
|
|
})
|
|
|
|
eventBuilders.createdShippingOption({ data: options, sharedContext })
|
|
eventBuilders.createdShippingOptionType({ data: types, sharedContext })
|
|
eventBuilders.createdShippingOptionRule({ data: rules, sharedContext })
|
|
}
|
|
|
|
export function buildCreatedFulfillmentSetEvents({
|
|
fulfillmentSets,
|
|
sharedContext,
|
|
}: {
|
|
fulfillmentSets: InferEntityType<typeof FulfillmentSet>[]
|
|
sharedContext: Context
|
|
}): void {
|
|
if (!fulfillmentSets.length) {
|
|
return
|
|
}
|
|
|
|
const serviceZones: InferEntityType<typeof ServiceZone>[] = []
|
|
|
|
fulfillmentSets.forEach((fulfillmentSet) => {
|
|
if (!fulfillmentSet.service_zones?.length) {
|
|
return
|
|
}
|
|
|
|
serviceZones.push(...fulfillmentSet.service_zones)
|
|
})
|
|
|
|
eventBuilders.createdFulfillmentSet({ data: fulfillmentSets, sharedContext })
|
|
|
|
buildCreatedServiceZoneEvents({ serviceZones, sharedContext })
|
|
}
|
|
|
|
export function buildCreatedServiceZoneEvents({
|
|
serviceZones,
|
|
sharedContext,
|
|
}: {
|
|
serviceZones: InferEntityType<typeof ServiceZone>[]
|
|
sharedContext: Context
|
|
}): void {
|
|
if (!serviceZones.length) {
|
|
return
|
|
}
|
|
|
|
const geoZones: InferEntityType<typeof GeoZone>[] = []
|
|
|
|
serviceZones.forEach((serviceZone) => {
|
|
if (!serviceZone.geo_zones.length) {
|
|
return
|
|
}
|
|
|
|
geoZones.push(...serviceZone.geo_zones)
|
|
})
|
|
|
|
eventBuilders.createdServiceZone({ data: serviceZones, sharedContext })
|
|
eventBuilders.createdGeoZone({ data: geoZones, sharedContext })
|
|
}
|