Преглед на файлове

fix(core): Add missing reverse side relations (#2781)

Eugene Nitsenko преди 1 година
родител
ревизия
bdf2329b9b
променени са 25 файла, в които са добавени 105 реда и са изтрити 33 реда
  1. 22 3
      packages/core/src/entity/channel/channel.entity.ts
  2. 5 1
      packages/core/src/entity/customer-group/customer-group.entity.ts
  3. 1 1
      packages/core/src/entity/customer/customer.entity.ts
  4. 1 1
      packages/core/src/entity/facet/facet.entity.ts
  5. 1 1
      packages/core/src/entity/order-line-reference/order-line-reference.entity.ts
  6. 21 6
      packages/core/src/entity/order-line/order-line.entity.ts
  7. 1 1
      packages/core/src/entity/payment-method/payment-method.entity.ts
  8. 4 0
      packages/core/src/entity/product-variant/product-variant.entity.ts
  9. 1 1
      packages/core/src/entity/product/product.entity.ts
  10. 1 2
      packages/core/src/entity/promotion/promotion.entity.ts
  11. 1 1
      packages/core/src/entity/refund/refund.entity.ts
  12. 1 1
      packages/core/src/entity/role/role.entity.ts
  13. 5 1
      packages/core/src/entity/seller/seller.entity.ts
  14. 1 1
      packages/core/src/entity/session/authenticated-session.entity.ts
  15. 5 1
      packages/core/src/entity/shipping-line/shipping-line.entity.ts
  16. 1 1
      packages/core/src/entity/shipping-method/shipping-method.entity.ts
  17. 6 2
      packages/core/src/entity/stock-location/stock-location.entity.ts
  18. 1 1
      packages/core/src/entity/stock-movement/allocation.entity.ts
  19. 1 1
      packages/core/src/entity/stock-movement/cancellation.entity.ts
  20. 1 1
      packages/core/src/entity/stock-movement/sale.entity.ts
  21. 1 1
      packages/core/src/entity/stock-movement/stock-movement.entity.ts
  22. 4 0
      packages/core/src/entity/tax-category/tax-category.entity.ts
  23. 3 3
      packages/core/src/entity/tax-rate/tax-rate.entity.ts
  24. 4 0
      packages/core/src/entity/user/user.entity.ts
  25. 12 1
      packages/core/src/entity/zone/zone.entity.ts

+ 22 - 3
packages/core/src/entity/channel/channel.entity.ts

@@ -2,6 +2,7 @@ import { CurrencyCode, LanguageCode } from '@vendure/common/lib/generated-types'
 import { DeepPartial, ID } from '@vendure/common/lib/shared-types';
 import { Column, Entity, Index, ManyToMany, ManyToOne } from 'typeorm';
 
+import { Customer, PaymentMethod, Promotion, Role, ShippingMethod, StockLocation } from '..';
 import { VendureEntity } from '../base/base.entity';
 import { Collection } from '../collection/collection.entity';
 import { CustomChannelFields } from '../custom-entity-fields';
@@ -61,7 +62,7 @@ export class Channel extends VendureEntity {
     description: string;
 
     @Index()
-    @ManyToOne(type => Seller)
+    @ManyToOne(type => Seller, seller => seller.channels)
     seller?: Seller;
 
     @EntityId({ nullable: true })
@@ -73,11 +74,11 @@ export class Channel extends VendureEntity {
     availableLanguageCodes: LanguageCode[];
 
     @Index()
-    @ManyToOne(type => Zone)
+    @ManyToOne(type => Zone, zone => zone.defaultTaxZoneChannels)
     defaultTaxZone: Zone;
 
     @Index()
-    @ManyToOne(type => Zone)
+    @ManyToOne(type => Zone, zone => zone.defaultShippingZoneChannels)
     defaultShippingZone: Zone;
 
     @Column('varchar')
@@ -123,6 +124,24 @@ export class Channel extends VendureEntity {
     @ManyToMany(type => Collection, collection => collection.channels, { onDelete: 'CASCADE' })
     collections: Collection[];
 
+    @ManyToMany(type => Promotion, promotion => promotion.channels, { onDelete: 'CASCADE' })
+    promotions: Promotion[];
+
+    @ManyToMany(type => PaymentMethod, paymentMethod => paymentMethod.channels, { onDelete: 'CASCADE' })
+    paymentMethods: PaymentMethod[];
+
+    @ManyToMany(type => ShippingMethod, shippingMethod => shippingMethod.channels, { onDelete: 'CASCADE' })
+    shippingMethods: ShippingMethod[];
+
+    @ManyToMany(type => Customer, customer => customer.channels, { onDelete: 'CASCADE' })
+    customers: Customer[];
+
+    @ManyToMany(type => Role, role => role.channels, { onDelete: 'CASCADE' })
+    roles: Role[];
+
+    @ManyToMany(type => StockLocation, stockLocation => stockLocation.channels, { onDelete: 'CASCADE' })
+    stockLocations: StockLocation[];
+
     private generateToken(): string {
         const randomString = () => Math.random().toString(36).substr(3, 10);
         return `${randomString()}${randomString()}`;

+ 5 - 1
packages/core/src/entity/customer-group/customer-group.entity.ts

@@ -1,10 +1,11 @@
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToMany } from 'typeorm';
+import { Column, Entity, ManyToMany, OneToMany } from 'typeorm';
 
 import { HasCustomFields } from '../../config/custom-field/custom-field-types';
 import { VendureEntity } from '../base/base.entity';
 import { CustomCustomerGroupFields } from '../custom-entity-fields';
 import { Customer } from '../customer/customer.entity';
+import { TaxRate } from '../tax-rate/tax-rate.entity';
 
 /**
  * @description
@@ -26,4 +27,7 @@ export class CustomerGroup extends VendureEntity implements HasCustomFields {
 
     @Column(type => CustomCustomerGroupFields)
     customFields: CustomCustomerGroupFields;
+
+    @OneToMany(type => TaxRate, taxRate => taxRate.zone)
+    taxRates: TaxRate[];
 }

+ 1 - 1
packages/core/src/entity/customer/customer.entity.ts

@@ -58,7 +58,7 @@ export class Customer extends VendureEntity implements ChannelAware, HasCustomFi
     @Column(type => CustomCustomerFields)
     customFields: CustomCustomerFields;
 
-    @ManyToMany(type => Channel)
+    @ManyToMany(type => Channel, channel => channel.customers)
     @JoinTable()
     channels: Channel[];
 }

+ 1 - 1
packages/core/src/entity/facet/facet.entity.ts

@@ -45,7 +45,7 @@ export class Facet extends VendureEntity implements Translatable, HasCustomField
     @Column(type => CustomFacetFields)
     customFields: CustomFacetFields;
 
-    @ManyToMany(type => Channel)
+    @ManyToMany(type => Channel, channel => channel.facets)
     @JoinTable()
     channels: Channel[];
 }

+ 1 - 1
packages/core/src/entity/order-line-reference/order-line-reference.entity.ts

@@ -19,7 +19,7 @@ export abstract class OrderLineReference extends VendureEntity {
     quantity: number;
 
     @Index()
-    @ManyToOne(type => OrderLine, { onDelete: 'CASCADE' })
+    @ManyToOne(type => OrderLine, line => line.linesReferences, { onDelete: 'CASCADE' })
     orderLine: OrderLine;
 
     @EntityId()

+ 21 - 6
packages/core/src/entity/order-line/order-line.entity.ts

@@ -1,7 +1,7 @@
 import { Adjustment, AdjustmentType, Discount, TaxLine } from '@vendure/common/lib/generated-types';
 import { DeepPartial, ID } from '@vendure/common/lib/shared-types';
 import { summate } from '@vendure/common/lib/shared-utils';
-import { Column, Entity, Index, ManyToOne, OneToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne, OneToMany, OneToOne } from 'typeorm';
 
 import { Calculated } from '../../common/calculated-decorator';
 import { roundMoney } from '../../common/round-money';
@@ -14,9 +14,12 @@ import { CustomOrderLineFields } from '../custom-entity-fields';
 import { EntityId } from '../entity-id.decorator';
 import { Money } from '../money.decorator';
 import { Order } from '../order/order.entity';
+import { OrderLineReference } from '../order-line-reference/order-line-reference.entity';
 import { ProductVariant } from '../product-variant/product-variant.entity';
 import { ShippingLine } from '../shipping-line/shipping-line.entity';
+import { Allocation } from '../stock-movement/allocation.entity';
 import { Cancellation } from '../stock-movement/cancellation.entity';
+import { Sale } from '../stock-movement/sale.entity';
 import { TaxCategory } from '../tax-category/tax-category.entity';
 
 /**
@@ -49,7 +52,10 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * This is determined by the configured {@link ShippingLineAssignmentStrategy}.
      */
     @Index()
-    @ManyToOne(type => ShippingLine, { nullable: true, onDelete: 'SET NULL' })
+    @ManyToOne(type => ShippingLine, shippingLine => shippingLine.orderLines, {
+        nullable: true,
+        onDelete: 'SET NULL',
+    })
     shippingLine?: ShippingLine;
 
     @EntityId({ nullable: true })
@@ -60,7 +66,7 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * The {@link ProductVariant} which is being ordered.
      */
     @Index()
-    @ManyToOne(type => ProductVariant)
+    @ManyToOne(type => ProductVariant, productVariant => productVariant.lines, { onDelete: 'CASCADE' })
     productVariant: ProductVariant;
 
     @EntityId()
@@ -71,13 +77,19 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
     taxCategory: TaxCategory;
 
     @Index()
-    @ManyToOne(type => Asset)
+    @ManyToOne(type => Asset, asset => asset.featuredInVariants, { onDelete: 'SET NULL' })
     featuredAsset: Asset;
 
     @Index()
     @ManyToOne(type => Order, order => order.lines, { onDelete: 'CASCADE' })
     order: Order;
 
+    @OneToMany(type => OrderLineReference, lineRef => lineRef.orderLine)
+    linesReferences: OrderLineReference[];
+
+    @OneToMany(type => Sale, sale => sale.orderLine)
+    sales: Sale[];
+
     @Column()
     quantity: number;
 
@@ -118,8 +130,11 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
     @Column('simple-json')
     taxLines: TaxLine[];
 
-    @OneToOne(type => Cancellation, cancellation => cancellation.orderLine)
-    cancellation: Cancellation;
+    @OneToMany(type => Cancellation, cancellation => cancellation.orderLine)
+    cancellations: Cancellation[];
+
+    @OneToMany(type => Allocation, allocation => allocation.orderLine)
+    allocations: Allocation[];
 
     @Column(type => CustomOrderLineFields)
     customFields: CustomOrderLineFields;

+ 1 - 1
packages/core/src/entity/payment-method/payment-method.entity.ts

@@ -39,7 +39,7 @@ export class PaymentMethod extends VendureEntity implements Translatable, Channe
 
     @Column('simple-json') handler: ConfigurableOperation;
 
-    @ManyToMany(type => Channel)
+    @ManyToMany(type => Channel, channel => channel.paymentMethods)
     @JoinTable()
     channels: Channel[];
 

+ 4 - 0
packages/core/src/entity/product-variant/product-variant.entity.ts

@@ -14,6 +14,7 @@ import { Collection } from '../collection/collection.entity';
 import { CustomProductVariantFields } from '../custom-entity-fields';
 import { EntityId } from '../entity-id.decorator';
 import { FacetValue } from '../facet-value/facet-value.entity';
+import { OrderLine } from '../order-line/order-line.entity';
 import { Product } from '../product/product.entity';
 import { ProductOption } from '../product-option/product-option.entity';
 import { StockLevel } from '../stock-level/stock-level.entity';
@@ -170,4 +171,7 @@ export class ProductVariant
     @ManyToMany(type => Channel, channel => channel.productVariants)
     @JoinTable()
     channels: Channel[];
+
+    @OneToMany(type => OrderLine, orderLine => orderLine.productVariant)
+    lines: OrderLine[];
 }

+ 1 - 1
packages/core/src/entity/product/product.entity.ts

@@ -63,7 +63,7 @@ export class Product
     @JoinTable()
     facetValues: FacetValue[];
 
-    @ManyToMany(type => Channel)
+    @ManyToMany(type => Channel, channel => channel.products)
     @JoinTable()
     channels: Channel[];
 

+ 1 - 2
packages/core/src/entity/promotion/promotion.entity.ts

@@ -20,7 +20,6 @@ import { Channel } from '../channel/channel.entity';
 import { CustomPromotionFields } from '../custom-entity-fields';
 import { Order } from '../order/order.entity';
 import { OrderLine } from '../order-line/order-line.entity';
-import { PaymentMethodTranslation } from '../payment-method/payment-method-translation.entity';
 import { ShippingLine } from '../shipping-line/shipping-line.entity';
 
 import { PromotionTranslation } from './promotion-translation.entity';
@@ -107,7 +106,7 @@ export class Promotion
 
     @Column() enabled: boolean;
 
-    @ManyToMany(type => Channel)
+    @ManyToMany(type => Channel, channel => channel.promotions)
     @JoinTable()
     channels: Channel[];
 

+ 1 - 1
packages/core/src/entity/refund/refund.entity.ts

@@ -48,7 +48,7 @@ export class Refund extends VendureEntity {
     lines: RefundLine[];
 
     @Index()
-    @ManyToOne(type => Payment)
+    @ManyToOne(type => Payment, payment => payment.refunds)
     @JoinColumn()
     payment: Payment;
 

+ 1 - 1
packages/core/src/entity/role/role.entity.ts

@@ -25,7 +25,7 @@ export class Role extends VendureEntity implements ChannelAware {
 
     @Column('simple-array') permissions: Permission[];
 
-    @ManyToMany(type => Channel)
+    @ManyToMany(type => Channel, channel => channel.roles)
     @JoinTable()
     channels: Channel[];
 }

+ 5 - 1
packages/core/src/entity/seller/seller.entity.ts

@@ -1,6 +1,7 @@
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity } from 'typeorm';
+import { Column, Entity, OneToMany } from 'typeorm';
 
+import { Channel } from '..';
 import { SoftDeletable } from '../../common/types/common-types';
 import { HasCustomFields } from '../../config/custom-field/custom-field-types';
 import { VendureEntity } from '../base/base.entity';
@@ -26,4 +27,7 @@ export class Seller extends VendureEntity implements SoftDeletable, HasCustomFie
 
     @Column(type => CustomSellerFields)
     customFields: CustomSellerFields;
+
+    @OneToMany(type => Channel, channel => channel.seller)
+    channels: Channel[];
 }

+ 1 - 1
packages/core/src/entity/session/authenticated-session.entity.ts

@@ -22,7 +22,7 @@ export class AuthenticatedSession extends Session {
      * The {@link User} who has authenticated to create this session.
      */
     @Index()
-    @ManyToOne(type => User)
+    @ManyToOne(type => User, user => user.sessions)
     user: User;
 
     /**

+ 5 - 1
packages/core/src/entity/shipping-line/shipping-line.entity.ts

@@ -1,8 +1,9 @@
 import { Adjustment, AdjustmentType, Discount, TaxLine } from '@vendure/common/lib/generated-types';
 import { DeepPartial, ID } from '@vendure/common/lib/shared-types';
 import { summate } from '@vendure/common/lib/shared-utils';
-import { Column, Entity, Index, ManyToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne, OneToMany } from 'typeorm';
 
+import { OrderLine } from '..';
 import { Calculated } from '../../common/calculated-decorator';
 import { roundMoney } from '../../common/round-money';
 import { grossPriceOf, netPriceOf } from '../../common/tax-utils';
@@ -49,6 +50,9 @@ export class ShippingLine extends VendureEntity {
     @Column('simple-json')
     taxLines: TaxLine[];
 
+    @OneToMany(type => OrderLine, orderLine => orderLine.shippingLine)
+    orderLines: OrderLine[];
+
     @Calculated()
     get price(): number {
         return this.listPriceIncludesTax ? netPriceOf(this.listPrice, this.taxRate) : this.listPrice;

+ 1 - 1
packages/core/src/entity/shipping-method/shipping-method.entity.ts

@@ -62,7 +62,7 @@ export class ShippingMethod
     @Column()
     fulfillmentHandlerCode: string;
 
-    @ManyToMany(type => Channel)
+    @ManyToMany(type => Channel, channel => channel.shippingMethods)
     @JoinTable()
     channels: Channel[];
 

+ 6 - 2
packages/core/src/entity/stock-location/stock-location.entity.ts

@@ -1,11 +1,12 @@
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, JoinTable, ManyToMany } from 'typeorm';
+import { Column, Entity, JoinTable, ManyToMany, OneToMany } from 'typeorm';
 
 import { ChannelAware } from '../../common/types/common-types';
 import { HasCustomFields } from '../../config/custom-field/custom-field-types';
 import { VendureEntity } from '../base/base.entity';
 import { Channel } from '../channel/channel.entity';
 import { CustomStockLocationFields } from '../custom-entity-fields';
+import { StockMovement } from '../stock-movement/stock-movement.entity';
 
 /**
  * @description
@@ -32,7 +33,10 @@ export class StockLocation extends VendureEntity implements HasCustomFields, Cha
     @Column(type => CustomStockLocationFields)
     customFields: CustomStockLocationFields;
 
-    @ManyToMany(type => Channel)
+    @ManyToMany(type => Channel, channel => channel.stockLocations)
     @JoinTable()
     channels: Channel[];
+
+    @OneToMany(type => StockMovement, movement => movement.stockLocation)
+    stockMovements: StockMovement[];
 }

+ 1 - 1
packages/core/src/entity/stock-movement/allocation.entity.ts

@@ -23,6 +23,6 @@ export class Allocation extends StockMovement {
     }
 
     @Index()
-    @ManyToOne(type => OrderLine)
+    @ManyToOne(type => OrderLine, orderLine => orderLine.allocations)
     orderLine: OrderLine;
 }

+ 1 - 1
packages/core/src/entity/stock-movement/cancellation.entity.ts

@@ -22,6 +22,6 @@ export class Cancellation extends StockMovement {
     }
 
     // @Index() omitted as it would conflict with the orderLineId index from the Allocation entity
-    @ManyToOne(type => OrderLine)
+    @ManyToOne(type => OrderLine, orderLine => orderLine.cancellations)
     orderLine: OrderLine;
 }

+ 1 - 1
packages/core/src/entity/stock-movement/sale.entity.ts

@@ -22,6 +22,6 @@ export class Sale extends StockMovement {
     }
 
     // @Index() omitted as it would conflict with the orderLineId index from the Allocation entity
-    @ManyToOne(type => OrderLine)
+    @ManyToOne(type => OrderLine, line => line.sales)
     orderLine: OrderLine;
 }

+ 1 - 1
packages/core/src/entity/stock-movement/stock-movement.entity.ts

@@ -27,7 +27,7 @@ export abstract class StockMovement extends VendureEntity {
     productVariant: ProductVariant;
 
     @Index()
-    @ManyToOne(type => StockLocation, { onDelete: 'CASCADE' })
+    @ManyToOne(type => StockLocation, stockLocation => stockLocation.stockMovements, { onDelete: 'CASCADE' })
     stockLocation: StockLocation;
 
     @EntityId()

+ 4 - 0
packages/core/src/entity/tax-category/tax-category.entity.ts

@@ -1,6 +1,7 @@
 import { DeepPartial } from '@vendure/common/lib/shared-types';
 import { Column, Entity, OneToMany } from 'typeorm';
 
+import { TaxRate } from '..';
 import { HasCustomFields } from '../../config/custom-field/custom-field-types';
 import { VendureEntity } from '../base/base.entity';
 import { CustomTaxCategoryFields } from '../custom-entity-fields';
@@ -27,4 +28,7 @@ export class TaxCategory extends VendureEntity implements HasCustomFields {
 
     @OneToMany(type => ProductVariant, productVariant => productVariant.taxCategory)
     productVariants: ProductVariant[];
+
+    @OneToMany(type => TaxRate, taxRate => taxRate.category)
+    taxRates: TaxRate[];
 }

+ 3 - 3
packages/core/src/entity/tax-rate/tax-rate.entity.ts

@@ -35,15 +35,15 @@ export class TaxRate extends VendureEntity implements HasCustomFields {
     @Column({ type: 'decimal', precision: 5, scale: 2, transformer: new DecimalTransformer() }) value: number;
 
     @Index()
-    @ManyToOne(type => TaxCategory)
+    @ManyToOne(type => TaxCategory, taxCategory => taxCategory.taxRates)
     category: TaxCategory;
 
     @Index()
-    @ManyToOne(type => Zone)
+    @ManyToOne(type => Zone, zone => zone.taxRates)
     zone: Zone;
 
     @Index()
-    @ManyToOne(type => CustomerGroup, { nullable: true })
+    @ManyToOne(type => CustomerGroup, customerGroup => customerGroup.taxRates, { nullable: true })
     customerGroup?: CustomerGroup;
 
     @Column(type => CustomTaxRateFields)

+ 4 - 0
packages/core/src/entity/user/user.entity.ts

@@ -9,6 +9,7 @@ import { NativeAuthenticationMethod } from '../authentication-method/native-auth
 import { VendureEntity } from '../base/base.entity';
 import { CustomUserFields } from '../custom-entity-fields';
 import { Role } from '../role/role.entity';
+import { AuthenticatedSession } from '../session/authenticated-session.entity';
 
 /**
  * @description
@@ -45,6 +46,9 @@ export class User extends VendureEntity implements HasCustomFields, SoftDeletabl
     @Column(type => CustomUserFields)
     customFields: CustomUserFields;
 
+    @OneToMany(type => AuthenticatedSession, session => session.user)
+    sessions: AuthenticatedSession[];
+
     getNativeAuthenticationMethod(): NativeAuthenticationMethod;
     getNativeAuthenticationMethod(strict?: boolean): NativeAuthenticationMethod | undefined;
 

+ 12 - 1
packages/core/src/entity/zone/zone.entity.ts

@@ -1,11 +1,13 @@
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, JoinTable, ManyToMany } from 'typeorm';
+import { Column, Entity, JoinTable, ManyToMany, OneToMany } from 'typeorm';
 
 import { HasCustomFields } from '../../config/custom-field/custom-field-types';
 import { VendureEntity } from '../base/base.entity';
+import { Channel } from '../channel/channel.entity';
 import { CustomZoneFields } from '../custom-entity-fields';
 import { Country } from '../region/country.entity';
 import { Region } from '../region/region.entity';
+import { TaxRate } from '../tax-rate/tax-rate.entity';
 
 /**
  * @description
@@ -28,4 +30,13 @@ export class Zone extends VendureEntity implements HasCustomFields {
 
     @Column(type => CustomZoneFields)
     customFields: CustomZoneFields;
+
+    @OneToMany(type => Channel, country => country.defaultShippingZone)
+    defaultShippingZoneChannels: Channel[];
+
+    @OneToMany(type => Channel, country => country.defaultTaxZone)
+    defaultTaxZoneChannels: Channel[];
+
+    @OneToMany(type => TaxRate, taxRate => taxRate.zone)
+    taxRates: TaxRate[];
 }