Procházet zdrojové kódy

refactor(server): Remove configurable rounding strategy

No clear reason why this is needed, so we should avoid the extra complexity and possibility of strange bugs being introduced by using differing methods of rounding.
Michael Bromley před 7 roky
rodič
revize
4fc3bea73e

+ 0 - 5
server/src/config/config.service.ts

@@ -13,7 +13,6 @@ import { AssetStorageStrategy } from './asset-storage-strategy/asset-storage-str
 import { EntityIdStrategy } from './entity-id-strategy/entity-id-strategy';
 import { EntityIdStrategy } from './entity-id-strategy/entity-id-strategy';
 import { PromotionAction } from './promotion/promotion-action';
 import { PromotionAction } from './promotion/promotion-action';
 import { PromotionCondition } from './promotion/promotion-condition';
 import { PromotionCondition } from './promotion/promotion-condition';
-import { RoundingStrategy } from './rounding-strategy/rounding-strategy';
 import { AuthOptions, getConfig, VendureConfig } from './vendure-config';
 import { AuthOptions, getConfig, VendureConfig } from './vendure-config';
 import { VendurePlugin } from './vendure-plugin/vendure-plugin';
 import { VendurePlugin } from './vendure-plugin/vendure-plugin';
 
 
@@ -59,10 +58,6 @@ export class ConfigService implements VendureConfig {
         return this.activeConfig.cors;
         return this.activeConfig.cors;
     }
     }
 
 
-    get roundingStrategy(): RoundingStrategy {
-        return this.activeConfig.roundingStrategy;
-    }
-
     get entityIdStrategy(): EntityIdStrategy {
     get entityIdStrategy(): EntityIdStrategy {
         return this.activeConfig.entityIdStrategy;
         return this.activeConfig.entityIdStrategy;
     }
     }

+ 0 - 2
server/src/config/default-config.ts

@@ -10,7 +10,6 @@ import { NoAssetStorageStrategy } from './asset-storage-strategy/no-asset-storag
 import { AutoIncrementIdStrategy } from './entity-id-strategy/auto-increment-id-strategy';
 import { AutoIncrementIdStrategy } from './entity-id-strategy/auto-increment-id-strategy';
 import { defaultPromotionActions } from './promotion/default-promotion-actions';
 import { defaultPromotionActions } from './promotion/default-promotion-actions';
 import { defaultPromotionConditions } from './promotion/default-promotion-conditions';
 import { defaultPromotionConditions } from './promotion/default-promotion-conditions';
-import { HalfUpRoundingStrategy } from './rounding-strategy/half-up-rounding-strategy';
 import { VendureConfig } from './vendure-config';
 import { VendureConfig } from './vendure-config';
 
 
 /**
 /**
@@ -33,7 +32,6 @@ export const defaultConfig: ReadOnlyRequired<VendureConfig> = {
         sessionDuration: '7d',
         sessionDuration: '7d',
     },
     },
     apiPath: API_PATH,
     apiPath: API_PATH,
-    roundingStrategy: new HalfUpRoundingStrategy(),
     entityIdStrategy: new AutoIncrementIdStrategy(),
     entityIdStrategy: new AutoIncrementIdStrategy(),
     assetNamingStrategy: new DefaultAssetNamingStrategy(),
     assetNamingStrategy: new DefaultAssetNamingStrategy(),
     assetStorageStrategy: new NoAssetStorageStrategy(),
     assetStorageStrategy: new NoAssetStorageStrategy(),

+ 0 - 16
server/src/config/rounding-strategy/half-even-rounding-strategy.ts

@@ -1,16 +0,0 @@
-import { RoundingStrategy } from './rounding-strategy';
-
-/**
- * The Half-even rounding strategy (also known as Banker's Rounding) will round a decimal of .5
- * to the nearest even number. This is intended to counteract the upward bias introduced by the
- * more well-known "round 0.5 upwards" method.
- *
- * Based on https://stackoverflow.com/a/49080858/772859
- */
-export class HalfEvenRoundingStrategy implements RoundingStrategy {
-    round(input: number): number {
-        const r = Math.round(input);
-        const br = Math.abs(input) % 1 === 0.5 ? (r % 2 === 0 ? r : r - 1) : r;
-        return br;
-    }
-}

+ 0 - 10
server/src/config/rounding-strategy/half-up-rounding-strategy.ts

@@ -1,10 +0,0 @@
-import { RoundingStrategy } from './rounding-strategy';
-
-/**
- * Rounds decimals of 0.5 up to the next integer in the direction of + infinity.
- */
-export class HalfUpRoundingStrategy implements RoundingStrategy {
-    round(input: number): number {
-        return Math.round(input);
-    }
-}

+ 0 - 7
server/src/config/rounding-strategy/rounding-strategy.ts

@@ -1,7 +0,0 @@
-/**
- * Sets the method used to round monetary amounts which contain
- * fractions of a cent / penny.
- */
-export interface RoundingStrategy {
-    round(input: number): number;
-}

+ 0 - 6
server/src/config/vendure-config.ts

@@ -14,7 +14,6 @@ import { EntityIdStrategy } from './entity-id-strategy/entity-id-strategy';
 import { mergeConfig } from './merge-config';
 import { mergeConfig } from './merge-config';
 import { PromotionAction } from './promotion/promotion-action';
 import { PromotionAction } from './promotion/promotion-action';
 import { PromotionCondition } from './promotion/promotion-condition';
 import { PromotionCondition } from './promotion/promotion-condition';
-import { RoundingStrategy } from './rounding-strategy/rounding-strategy';
 import { VendurePlugin } from './vendure-plugin/vendure-plugin';
 import { VendurePlugin } from './vendure-plugin/vendure-plugin';
 
 
 export interface AuthOptions {
 export interface AuthOptions {
@@ -90,11 +89,6 @@ export interface VendureConfig {
      * Configuration for authorization.
      * Configuration for authorization.
      */
      */
     authOptions: AuthOptions;
     authOptions: AuthOptions;
-    /**
-     * Defines the strategy used in rounding fractions of cents when performing
-     * calculations of moneytary amounts.
-     */
-    roundingStrategy?: RoundingStrategy;
     /**
     /**
      * Defines the strategy used for both storing the primary keys of entities
      * Defines the strategy used for both storing the primary keys of entities
      * in the database, and the encoding & decoding of those ids when exposing
      * in the database, and the encoding & decoding of those ids when exposing

+ 1 - 3
server/src/entity/promotion/promotion.entity.ts

@@ -16,7 +16,6 @@ export class Promotion extends AdjustmentSource {
     type = AdjustmentType.PROMOTION;
     type = AdjustmentType.PROMOTION;
     private readonly allConditions: { [code: string]: PromotionCondition } = {};
     private readonly allConditions: { [code: string]: PromotionCondition } = {};
     private readonly allActions: { [code: string]: PromotionAction } = {};
     private readonly allActions: { [code: string]: PromotionAction } = {};
-    private readonly round: (input: number) => number;
 
 
     constructor(input?: DeepPartial<Promotion>) {
     constructor(input?: DeepPartial<Promotion>) {
         super(input);
         super(input);
@@ -25,7 +24,6 @@ export class Promotion extends AdjustmentSource {
             {},
             {},
         );
         );
         this.allActions = getConfig().promotionActions.reduce((hash, o) => ({ ...hash, [o.code]: o }), {});
         this.allActions = getConfig().promotionActions.reduce((hash, o) => ({ ...hash, [o.code]: o }), {});
-        this.round = getConfig().roundingStrategy.round;
     }
     }
 
 
     @Column() name: string;
     @Column() name: string;
@@ -45,7 +43,7 @@ export class Promotion extends AdjustmentSource {
 
 
         for (const action of this.actions) {
         for (const action of this.actions) {
             const promotionAction = this.allActions[action.code];
             const promotionAction = this.allActions[action.code];
-            amount += this.round(promotionAction.execute(orderItem, orderLine, action.args));
+            amount += Math.round(promotionAction.execute(orderItem, orderLine, action.args));
         }
         }
         if (amount !== 0) {
         if (amount !== 0) {
             return {
             return {

+ 2 - 5
server/src/entity/tax-rate/tax-rate.entity.ts

@@ -4,7 +4,6 @@ import { Column, Entity, ManyToOne } from 'typeorm';
 
 
 import { AdjustmentSource } from '../../common/types/adjustment-source';
 import { AdjustmentSource } from '../../common/types/adjustment-source';
 import { idsAreEqual } from '../../common/utils';
 import { idsAreEqual } from '../../common/utils';
-import { getConfig } from '../../config/vendure-config';
 import { CustomerGroup } from '../customer-group/customer-group.entity';
 import { CustomerGroup } from '../customer-group/customer-group.entity';
 import { TaxCategory } from '../tax-category/tax-category.entity';
 import { TaxCategory } from '../tax-category/tax-category.entity';
 import { Zone } from '../zone/zone.entity';
 import { Zone } from '../zone/zone.entity';
@@ -12,11 +11,9 @@ import { Zone } from '../zone/zone.entity';
 @Entity()
 @Entity()
 export class TaxRate extends AdjustmentSource {
 export class TaxRate extends AdjustmentSource {
     readonly type = AdjustmentType.TAX;
     readonly type = AdjustmentType.TAX;
-    private readonly round: (input: number) => number;
 
 
     constructor(input?: DeepPartial<TaxRate>) {
     constructor(input?: DeepPartial<TaxRate>) {
         super(input);
         super(input);
-        this.round = getConfig().roundingStrategy.round;
     }
     }
 
 
     @Column() name: string;
     @Column() name: string;
@@ -38,7 +35,7 @@ export class TaxRate extends AdjustmentSource {
      * Returns the tax component of a given gross price.
      * Returns the tax component of a given gross price.
      */
      */
     taxComponentOf(grossPrice: number): number {
     taxComponentOf(grossPrice: number): number {
-        return this.round(grossPrice - grossPrice / ((100 + this.value) / 100));
+        return Math.round(grossPrice - grossPrice / ((100 + this.value) / 100));
     }
     }
 
 
     /**
     /**
@@ -52,7 +49,7 @@ export class TaxRate extends AdjustmentSource {
      * Returns the tax applicable to the given net price.
      * Returns the tax applicable to the given net price.
      */
      */
     taxPayableOn(netPrice: number): number {
     taxPayableOn(netPrice: number): number {
-        return this.round(netPrice * (this.value / 100));
+        return Math.round(netPrice * (this.value / 100));
     }
     }
 
 
     /**
     /**