-
Notifications
You must be signed in to change notification settings - Fork 0
function: total static price #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
codeicey
wants to merge
11
commits into
main
Choose a base branch
from
feat/prices
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e42351e
function: total static price
codeicey 632524e
function: total dynamic price
codeicey f6ca401
Total price calculation based on UnitCodes
codeicey bd6c747
Different price per day calculation
codeicey b12a45b
Test cases on total price calculation
codeicey 32ba0c2
PriceSpecification type and capitalization update
codeicey 9b8c884
Prices function and tests correction updates
codeicey ac32339
Bug fix, add mile unitCode
codeicey b59f39d
Statute mile unitCode
codeicey c9106db
Prices test updates
codeicey e90c569
Add conversion for statute mile to kilometers.
codeicey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "extends": [ | ||
| "eslint:recommended", | ||
| "plugin:@typescript-eslint/recommended", | ||
| "prettier" | ||
| ], | ||
| "parserOptions": { | ||
| "ecmaVersion": 2020, | ||
| "sourceType": "module" | ||
| }, | ||
| "plugins": ["prettier", "@typescript-eslint"], | ||
| "rules": { | ||
| "eqeqeq": ["warn", "allow-null"], | ||
| "@typescript-eslint/no-unused-vars": [ | ||
| "error", | ||
| { | ||
| "ignoreRestSiblings": true, | ||
| "varsIgnorePattern": "^_", | ||
| "argsIgnorePattern": "^_" | ||
| } | ||
| ], | ||
| "no-var": "error", | ||
| "no-with": "warn", | ||
| "prefer-const": "error" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| *.log | ||
| dist | ||
| node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| { | ||
| "name": "@js-ligo/prices", | ||
| "version": "1.0.0", | ||
| "description": "", | ||
| "type": "module", | ||
| "main": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "exports": { | ||
| ".": "./dist/index.js" | ||
| }, | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "scripts": { | ||
| "build:clean": "del dist", | ||
| "build:js": "swc src -d ./dist --config-file ../../.swcrc", | ||
| "build:types": "tsc --emitDeclarationOnly --skipLibCheck", | ||
| "build": "pnpm build:clean && pnpm build:types && pnpm build:js", | ||
| "test": "node --experimental-vm-modules ../../node_modules/jest/bin/jest.js", | ||
| "lint": "eslint src/* test/* --fix", | ||
| "prepare": "pnpm build" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "MIT", | ||
| "dependencies": { | ||
| "@js-ligo/vocab": "workspace:^0.3.0" | ||
| }, | ||
| "jest": { | ||
| "extensionsToTreatAsEsm": [ | ||
| ".ts" | ||
| ], | ||
| "moduleNameMapper": { | ||
| "^(\\.{1,2}/.*)\\.js$": "$1" | ||
| }, | ||
| "transform": { | ||
| "^.+\\.(t|j)s$": [ | ||
| "@swc/jest", | ||
| { | ||
| "root": "../.." | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| "devDependencies": { | ||
| "@typescript-eslint/parser": "^4.9.1", | ||
| "eslint": "^6.0.0", | ||
| "jest-environment-node": "^29.0.3", | ||
| "multiformats": "^9.9.0", | ||
| "node-fetch": "*", | ||
| "typescript": "^4.7.4" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from "./prices"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| /* eslint-disable eqeqeq */ | ||
| import { | ||
| LigoAgreementState, | ||
| PriceSpecification, | ||
| RentalCarReservation, | ||
| } from "@js-ligo/vocab"; | ||
|
|
||
| export class Prices { | ||
| /** | ||
| * Find Total Price | ||
| // Case 1: Base Price per day | ||
| // Case 2: Base price per kilometer | ||
| // Case 3: Base price per day + X$ per kilometer over Y range | ||
| // Case 4: Base price per hour | ||
| // Case 5: Different price per day | ||
| // Case 6: Monthly Subscription of X$ | ||
| // Case 7: Discount for Y+ days | ||
| */ | ||
| async calculateTotalPrice( | ||
| _priceSpecifications: PriceSpecification[], | ||
| _rentalCarReservation: RentalCarReservation, | ||
| _ligoAgreementState: LigoAgreementState | ||
| ) { | ||
| let sum = 0; | ||
| for (let i = 0; i < _priceSpecifications.length; i++) { | ||
| if (_priceSpecifications[i].referenceQuantity?.unitCode === "DAY") { | ||
| sum = | ||
| sum + | ||
| (await this._totalPriceDays( | ||
| _priceSpecifications[i], | ||
| _rentalCarReservation | ||
| )); | ||
| } else if ( | ||
| _priceSpecifications[i].referenceQuantity?.unitCode === "KMT" || | ||
| _priceSpecifications[i].referenceQuantity?.unitCode === "SMI" | ||
| ) { | ||
| sum = | ||
| sum + | ||
| (await this._totalPriceKM( | ||
| _priceSpecifications[i], | ||
| _ligoAgreementState | ||
| )); | ||
| } else if ( | ||
| _priceSpecifications[i].referenceQuantity?.unitCode === "HUR" | ||
| ) { | ||
| sum = | ||
| sum + | ||
| (await this._totalPriceHour( | ||
| _priceSpecifications[i], | ||
| _rentalCarReservation | ||
| )); | ||
| } else if ( | ||
| _priceSpecifications[i].referenceQuantity?.unitCode === "MON" | ||
| ) { | ||
| sum = sum + (await this._totalMonthlySub(_priceSpecifications[i])); | ||
| } | ||
| } | ||
| return sum; | ||
| } | ||
|
|
||
| private async _totalPriceDays( | ||
| _priceSpecification: PriceSpecification, | ||
| _rentalCarReservation: RentalCarReservation | ||
| ) { | ||
| //Filter ValidFrom and ValidThrough | ||
| const validF = new Date( | ||
| _priceSpecification.validFrom | ||
| ? _priceSpecification.validFrom | ||
| : _rentalCarReservation.pickupTime | ||
| ); | ||
| const validT = new Date( | ||
| _priceSpecification.validThrough | ||
| ? _priceSpecification.validThrough | ||
| : _rentalCarReservation.dropoffTime | ||
| ); | ||
| const dropoffT = new Date(_rentalCarReservation.dropoffTime); | ||
| const pickupT = new Date(_rentalCarReservation.pickupTime); | ||
| const a = validF >= pickupT ? validF : pickupT; | ||
| const b = validT <= dropoffT ? validT : dropoffT; | ||
|
|
||
| // For discount maxValue | ||
| if (_priceSpecification.eligibleQuantity?.maxValue != null) { | ||
| return ( | ||
| _priceSpecification.eligibleQuantity?.maxValue * | ||
| _priceSpecification.price | ||
| ); | ||
| } | ||
| // For discount minValue | ||
| if (_priceSpecification.eligibleQuantity?.minValue != null) { | ||
| return ( | ||
| ((await this._dateDiffInDays(a, b)) - | ||
| _priceSpecification.eligibleQuantity?.minValue) * | ||
| _priceSpecification.price | ||
| ); | ||
| } | ||
| // Normal days | ||
| return (await this._dateDiffInDays(a, b)) * _priceSpecification.price; | ||
| } | ||
|
|
||
| private async _totalPriceKM( | ||
| _priceSpecification: PriceSpecification, | ||
| _ligoAgreementState: LigoAgreementState | ||
| ) { | ||
| if ( | ||
| _ligoAgreementState.startOdometer?.value != null && | ||
| _ligoAgreementState.endOdometer?.value != null | ||
| ) { | ||
| if (_priceSpecification.eligibleQuantity?.minValue != null) { | ||
| if ( | ||
| Math.abs( | ||
| _ligoAgreementState.endOdometer.value - | ||
| _ligoAgreementState.startOdometer.value | ||
| ) >= _priceSpecification.eligibleQuantity?.minValue | ||
| ) { | ||
| if (_priceSpecification.referenceQuantity?.unitCode == "SMI") { | ||
| return ( | ||
| Math.abs( | ||
| Math.abs( | ||
| _ligoAgreementState.endOdometer.value - | ||
| _ligoAgreementState.startOdometer.value | ||
| ) - _priceSpecification.eligibleQuantity?.minValue | ||
| ) * | ||
| 1.609344 * | ||
| _priceSpecification.price // 1 SMI = 1.609344 KMT | ||
| ); | ||
| } else { | ||
| return ( | ||
| Math.abs( | ||
| Math.abs( | ||
| _ligoAgreementState.endOdometer.value - | ||
| _ligoAgreementState.startOdometer.value | ||
| ) - _priceSpecification.eligibleQuantity?.minValue | ||
| ) * _priceSpecification.price | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| { | ||
| if (_priceSpecification.referenceQuantity?.unitCode == "SMI") { | ||
| return ( | ||
| Math.abs( | ||
| _ligoAgreementState.endOdometer.value - | ||
| _ligoAgreementState.startOdometer.value | ||
| ) * | ||
| 1.609344 * // 1 SMI = 1.609344 KMT | ||
| _priceSpecification.price | ||
| ); | ||
| } else { | ||
| return ( | ||
| Math.abs( | ||
| _ligoAgreementState.endOdometer.value - | ||
| _ligoAgreementState.startOdometer.value | ||
| ) * _priceSpecification.price | ||
| ); | ||
| } | ||
| } | ||
| } else return 0; | ||
| } | ||
|
|
||
| private async _totalPriceHour( | ||
| _priceSpecification: PriceSpecification, | ||
| _rentalCarReservation: RentalCarReservation | ||
| ) { | ||
| const a = new Date(_rentalCarReservation.dropoffTime); | ||
| const b = new Date(_rentalCarReservation.pickupTime); | ||
| return ( | ||
| (await Math.abs(await this._hourDiffInDays(a, b))) * | ||
| _priceSpecification.price | ||
| ); | ||
| } | ||
|
|
||
| private async _totalMonthlySub(_priceSpecification: PriceSpecification) { | ||
| if (_priceSpecification.billingIncrement != null) { | ||
| return _priceSpecification.price * _priceSpecification.billingIncrement; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| private async _dateDiffInDays(a: Date, b: Date) { | ||
| const _MS_PER_DAY = 1000 * 60 * 60 * 24; | ||
| // Discard the time and time-zone information. | ||
| const utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate()); | ||
| const utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate()); | ||
| return Math.floor((utc2 - utc1) / _MS_PER_DAY); | ||
| } | ||
|
|
||
| private async _hourDiffInDays(a: Date, b: Date) { | ||
| const _MS_PER_HOUR = 1000 * 60 * 60; | ||
| const utc1 = Date.UTC( | ||
| a.getFullYear(), | ||
| a.getMonth(), | ||
| a.getDate(), | ||
| a.getHours() | ||
| ); | ||
| const utc2 = Date.UTC( | ||
| b.getFullYear(), | ||
| b.getMonth(), | ||
| b.getDate(), | ||
| b.getHours() | ||
| ); | ||
| return Math.floor((utc2 - utc1) / _MS_PER_HOUR); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this function assuming that the
unitCodefor thestartOdometerandendOdometeris the same as thePriceSpecification? This isn't a safe assumption. What should be done if the units are not the same?To keep it simple, I think a per distance fee should only be charged if there is a
PriceSpecificationfor the correct unit and leave it up to the host to published a per distance fee for each unit separately.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added conversion for SMI to KMT. Let me know your thoughts on it.