Переглянути джерело

feat(core): Use decimal type for OrderItem.taxRate

Relates to #234.

BREAKING CHANGE: The `OrderItem.taxRate` column type in the database has been changed from `int` to `decimal`. You will need to perform a migration to update this column and depending on your database type, you may need to manually edit the migration script in order to preserve the old values.
Michael Bromley 6 роки тому
батько
коміт
92650ec8ae

+ 3 - 1
packages/core/src/entity/order-item/order-item.entity.ts

@@ -9,6 +9,7 @@ import { Fulfillment } from '../fulfillment/fulfillment.entity';
 import { OrderLine } from '../order-line/order-line.entity';
 import { Refund } from '../refund/refund.entity';
 import { Cancellation } from '../stock-movement/cancellation.entity';
+import { DecimalTransformer } from '../value-transformers';
 
 /**
  * @description
@@ -29,7 +30,8 @@ export class OrderItem extends VendureEntity {
 
     @Column() unitPriceIncludesTax: boolean;
 
-    @Column() taxRate: number;
+    @Column({ type: 'decimal', precision: 5, scale: 2, transformer: new DecimalTransformer() })
+    taxRate: number;
 
     @Column('simple-json') pendingAdjustments: Adjustment[];
 

+ 2 - 1
packages/core/src/entity/tax-rate/tax-rate.entity.ts

@@ -6,6 +6,7 @@ import { AdjustmentSource } from '../../common/types/adjustment-source';
 import { idsAreEqual } from '../../common/utils';
 import { CustomerGroup } from '../customer-group/customer-group.entity';
 import { TaxCategory } from '../tax-category/tax-category.entity';
+import { DecimalTransformer } from '../value-transformers';
 import { Zone } from '../zone/zone.entity';
 
 /**
@@ -30,7 +31,7 @@ export class TaxRate extends AdjustmentSource {
 
     @Column() enabled: boolean;
 
-    @Column({ type: 'decimal', precision: 5, scale: 2 }) value: number;
+    @Column({ type: 'decimal', precision: 5, scale: 2, transformer: new DecimalTransformer() }) value: number;
 
     @ManyToOne(type => TaxCategory)
     category: TaxCategory;

+ 14 - 0
packages/core/src/entity/value-transformers.ts

@@ -0,0 +1,14 @@
+import { ValueTransformer } from 'typeorm';
+
+/**
+ * Decimal types are returned as strings (e.g. "20.00") by some DBs, e.g. MySQL & Postgres
+ */
+export class DecimalTransformer implements ValueTransformer {
+    to(value: any): any {
+        return value;
+    }
+
+    from(value: any): any {
+        return Number.parseFloat(value);
+    }
+}