New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@aitodotai/aito-price-tool

Package Overview
Dependencies
Maintainers
6
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aitodotai/aito-price-tool - npm Package Compare versions

Comparing version

to
2.0.0

21

dist/src/index.d.ts

@@ -1,4 +0,10 @@

export declare type Product = 'SANDBOX' | 'DEVELOPER' | 'PRODUCTION' | 'PLUS_ONE_GB';
import Dinero from 'dinero.js';
export declare enum Product {
Sandbox = "SANDBOX",
Developer = "DEVELOPER",
Production = "PRODUCTION",
PlusOneGigabyte = "PLUS_ONE_GB"
}
export interface IProductConfig {
price: number;
price: Dinero.Dinero;
}

@@ -11,3 +17,12 @@ /**

};
export declare function calculatePrice(products: Product[], months: number): number;
/**
*
*/
export interface ITotalPrice {
totalAmount: Dinero.Dinero;
vatAmount: Dinero.Dinero;
vatPercentage: number;
productAmount: Dinero.Dinero;
}
export declare function calculatePrice(products: Product[], months: number, vatPercentage?: number): ITotalPrice;
//# sourceMappingURL=index.d.ts.map
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// tslint:disable:object-literal-sort-keys
var dinero_js_1 = __importDefault(require("dinero.js"));
var Product;
(function (Product) {
Product["Sandbox"] = "SANDBOX";
Product["Developer"] = "DEVELOPER";
Product["Production"] = "PRODUCTION";
Product["PlusOneGigabyte"] = "PLUS_ONE_GB";
})(Product = exports.Product || (exports.Product = {}));
var EURO = 100;
var ZERO_PRICE = dinero_js_1.default({ amount: 0, currency: 'EUR' });
/**

@@ -9,8 +21,18 @@ * Prices given in cents per month

exports.PRODUCT_CONFIG = {
SANDBOX: { price: 0 * EURO },
DEVELOPER: { price: 39 * EURO },
PRODUCTION: { price: 249 * EURO },
PLUS_ONE_GB: { price: 49 * EURO },
SANDBOX: { price: ZERO_PRICE },
DEVELOPER: { price: dinero_js_1.default({ amount: 39 * EURO, currency: 'EUR' }) },
PRODUCTION: { price: dinero_js_1.default({ amount: 249 * EURO, currency: 'EUR' }) },
PLUS_ONE_GB: { price: dinero_js_1.default({ amount: 49 * EURO, currency: 'EUR' }) },
};
var add = function (accumulator, current) { return accumulator + current; };
/**
* Value added tax should be given in percentage, not fraction
* @param vatPercent
*/
function assertVatIsPercent(vatPercent) {
if (vatPercent > 0 && vatPercent < 1) {
throw new Error("Vat percent should be given as percent, not fraction. " + vatPercent);
}
return true;
}
function assertProductSetup(products) {

@@ -21,3 +43,3 @@ if (products === void 0) { products = []; }

}
if (products.indexOf('PLUS_ONE_GB') !== -1 && products.indexOf('PRODUCTION') === -1) {
if (products.indexOf(Product.PlusOneGigabyte) !== -1 && products.indexOf(Product.Production) === -1) {
throw new Error("Additional data can only be bought for the production subscription (" + products.join(', ') + ")");

@@ -33,11 +55,27 @@ }

}
function calculatePrice(products, months) {
function calculatePrice(products, months, vatPercentage) {
if (vatPercentage === void 0) { vatPercentage = 0; }
assertProductSetup(products);
assertUsageTime(months);
assertVatIsPercent(vatPercentage);
if (products.length === 0) {
return 0;
return {
totalAmount: ZERO_PRICE,
vatAmount: ZERO_PRICE,
productAmount: ZERO_PRICE,
vatPercentage: vatPercentage,
};
}
return products.map(function (p) { return exports.PRODUCT_CONFIG[p].price; }).reduce(add, 0) * months;
var basePrice = products.map(function (p) { return exports.PRODUCT_CONFIG[p].price; }).
reduce(function (a, b) { return a.add(b); }, ZERO_PRICE);
var productPriceOverMonths = basePrice.multiply(months);
var vatAmount = basePrice.multiply(vatPercentage, 'HALF_UP').divide(100, 'HALF_UP');
return {
totalAmount: productPriceOverMonths.add(vatAmount),
vatAmount: vatAmount,
productAmount: productPriceOverMonths,
vatPercentage: vatPercentage,
};
}
exports.calculatePrice = calculatePrice;
//# sourceMappingURL=index.js.map

75

dist/test/pricing.spec.js
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {

@@ -10,28 +13,40 @@ if (mod && mod.__esModule) return mod;

Object.defineProperty(exports, "__esModule", { value: true });
var dinero_js_1 = __importDefault(require("dinero.js"));
var chai_1 = require("chai");
require("mocha");
var index_1 = require("../src/index");
var Calculator = __importStar(require("../src/index"));
describe('Pricing module', function () {
var twoFourNine = dinero_js_1.default({ amount: 249 * 100, currency: 'EUR' });
var thirtyNine = dinero_js_1.default({ amount: 39 * 100, currency: 'EUR' });
var fourtyNine = dinero_js_1.default({ amount: 49 * 100, currency: 'EUR' });
var zeroPrice = dinero_js_1.default({ amount: 0, currency: 'EUR' });
var invalidProductSetups = [
['SANDBOX', 'PLUS_ONE_GB'],
['DEVELOPER', 'PLUS_ONE_GB'],
['PLUS_ONE_GB'],
['PLUS_ONE_GB', 'PLUS_ONE_GB'],
['PLUS_ONE_GB', 'PLUS_ONE_GB', 'PLUS_ONE_GB'],
[index_1.Product.Sandbox, index_1.Product.PlusOneGigabyte],
[index_1.Product.Developer, index_1.Product.PlusOneGigabyte],
[index_1.Product.PlusOneGigabyte],
[index_1.Product.PlusOneGigabyte, index_1.Product.PlusOneGigabyte],
[index_1.Product.PlusOneGigabyte, index_1.Product.PlusOneGigabyte, index_1.Product.PlusOneGigabyte],
];
it('should give prize 0 for empty array', function () {
chai_1.expect(Calculator.calculatePrice([], 1)).to.equal(0);
chai_1.expect(Calculator.calculatePrice([], 10000)).to.equal(0);
var price = Calculator.calculatePrice([], 1);
chai_1.expect(price.totalAmount.equalsTo(zeroPrice)).to.equal(true);
price = Calculator.calculatePrice([], 10000);
chai_1.expect(price.totalAmount.equalsTo(zeroPrice)).to.equal(true);
});
it('should charge nothing for sandbox', function () {
chai_1.expect(Calculator.calculatePrice(['SANDBOX'], 1)).to.equal(0);
chai_1.expect(Calculator.calculatePrice(['SANDBOX'], 10)).to.equal(0);
chai_1.expect(Calculator.calculatePrice(['SANDBOX'], 100000000)).to.equal(0);
var price = Calculator.calculatePrice([index_1.Product.Sandbox], 1);
chai_1.expect(price.totalAmount.equalsTo(zeroPrice)).to.equal(true);
price = Calculator.calculatePrice([index_1.Product.Sandbox], 10);
chai_1.expect(price.totalAmount.equalsTo(zeroPrice)).to.equal(true);
price = Calculator.calculatePrice([index_1.Product.Sandbox], 100000000);
chai_1.expect(price.totalAmount.equalsTo(zeroPrice)).to.equal(true);
});
it('should not charge anything for no time', function () {
chai_1.expect(Calculator.calculatePrice(['PRODUCTION'], 0)).to.equal(0);
chai_1.expect(Calculator.calculatePrice([index_1.Product.Production], 0).
totalAmount.equalsTo(zeroPrice)).to.equal(true);
});
it('should throw error for invalid time', function () {
try {
Calculator.calculatePrice(['SANDBOX'], -1);
Calculator.calculatePrice([index_1.Product.Sandbox], -1);
chai_1.assert.fail("Accepted negative time (-1)");

@@ -58,14 +73,38 @@ }

it('should calculate prices for single products', function () {
chai_1.expect(Calculator.calculatePrice(['PRODUCTION'], 1)).to.equal(24900);
var price = Calculator.calculatePrice([index_1.Product.Production], 1);
chai_1.expect(price.totalAmount.equalsTo(twoFourNine)).to.equal(true);
chai_1.expect(price.vatAmount.equalsTo(zeroPrice)).to.equal(true);
chai_1.expect(price.vatPercentage).to.equal(0);
});
it('should calculate prices for combined products', function () {
chai_1.expect(Calculator.calculatePrice(['PRODUCTION', 'PLUS_ONE_GB'], 1)).to.equal(24900 + 4900);
chai_1.expect(Calculator.calculatePrice(['PRODUCTION', 'DEVELOPER'], 1)).to.equal(24900 + 3900);
var prodPlusGigabyte = Calculator.calculatePrice([index_1.Product.Production, index_1.Product.PlusOneGigabyte], 1);
chai_1.expect(prodPlusGigabyte.productAmount.equalsTo(twoFourNine.add(fourtyNine))).
to.equal(true);
var prodPlusDeveloper = Calculator.calculatePrice([index_1.Product.Production, index_1.Product.Developer], 1);
chai_1.expect(prodPlusDeveloper.productAmount.equalsTo(twoFourNine.add(thirtyNine))).
to.equal(true);
});
it('should calculate prices for random ordered combined products', function () {
chai_1.expect(Calculator.calculatePrice(['PLUS_ONE_GB', 'PRODUCTION', 'PLUS_ONE_GB', 'DEVELOPER', 'SANDBOX'], 1)).to.be.greaterThan(0);
chai_1.expect(Calculator.calculatePrice(['SANDBOX', 'DEVELOPER', 'DEVELOPER'], 1)).to.be.greaterThan(0);
chai_1.expect(Calculator.calculatePrice(['PLUS_ONE_GB', 'PRODUCTION', 'PRODUCTION', 'PRODUCTION', 'PRODUCTION'], 1)).to.be.greaterThan(0);
chai_1.expect(Calculator.calculatePrice([index_1.Product.PlusOneGigabyte, index_1.Product.Production, index_1.Product.PlusOneGigabyte,
index_1.Product.Developer, index_1.Product.Sandbox], 1).productAmount.getAmount()).
to.be.greaterThan(0);
chai_1.expect(Calculator.calculatePrice([index_1.Product.Sandbox, index_1.Product.Developer, index_1.Product.Developer], 1).
productAmount.getAmount()).to.be.greaterThan(0);
chai_1.expect(Calculator.calculatePrice([index_1.Product.PlusOneGigabyte, index_1.Product.Production, index_1.Product.Production, index_1.Product.Production, index_1.Product.Production], 1).productAmount.getAmount()).to.be.greaterThan(0);
});
it('should return the vat amount and percentage as part of the response', function () {
var price = Calculator.calculatePrice([index_1.Product.Production], 1, 24);
chai_1.expect(price.productAmount.equalsTo(twoFourNine)).to.equal(true);
chai_1.expect(price.vatPercentage).to.equal(24);
chai_1.expect(price.vatAmount.getAmount()).to.equal(5976);
chai_1.expect(price.vatAmount.getCurrency()).to.equal('EUR');
chai_1.expect(price.totalAmount.getAmount()).to.equal(24900 + 5976);
});
it('should round vat sum up by default', function () {
var price = Calculator.calculatePrice([index_1.Product.Developer], 1, 1.5);
chai_1.expect(price.vatPercentage).to.equal(1.5);
chai_1.expect(price.vatAmount.getAmount()).to.equal(59);
chai_1.expect(price.vatAmount.getCurrency()).to.equal('EUR');
});
});
//# sourceMappingURL=pricing.spec.js.map
{
"name": "@aitodotai/aito-price-tool",
"version": "1.0.2",
"version": "2.0.0",
"description": "Calculates Aito invoices",

@@ -46,3 +46,7 @@ "main": "dist/src/index.js",

"typescript": "^3.2.2"
},
"dependencies": {
"@types/dinero.js": "^1.6.2",
"dinero.js": "^1.8.1"
}
}

@@ -0,1 +1,3 @@

import Dinero from 'dinero.js'
import {

@@ -7,23 +9,59 @@ expect,

import { Product } from '../dist/src/index'
import * as Calculator from '../dist/src/index'
describe('Pricing module', () => {
const twoFourNine = Dinero({
amount: 249 * 100,
currency: 'EUR'
})
const thirtyNine = Dinero({
amount: 39 * 100,
currency: 'EUR'
})
const fourtyNine = Dinero({
amount: 49 * 100,
currency: 'EUR'
})
const zeroPrice = Dinero({
amount: 0,
currency: 'EUR'
})
const invalidProductSetups = [
['SANDBOX', 'PLUS_ONE_GB'],
['DEVELOPER', 'PLUS_ONE_GB'],
['PLUS_ONE_GB'],
['PLUS_ONE_GB', 'PLUS_ONE_GB'],
['PLUS_ONE_GB', 'PLUS_ONE_GB', 'PLUS_ONE_GB'],
[Product.Sandbox, Product.PlusOneGigabyte],
[Product.Developer, Product.PlusOneGigabyte],
[Product.PlusOneGigabyte],
[Product.PlusOneGigabyte, Product.PlusOneGigabyte],
[Product.PlusOneGigabyte, Product.PlusOneGigabyte, Product.PlusOneGigabyte],
]
it('should give prize 0 for empty array', () => {
expect(Calculator.calculatePrice([])).to.equal(0)
let price = Calculator.calculatePrice([], 1)
expect(price.totalAmount.equalsTo(zeroPrice)).to.equal(true)
price = Calculator.calculatePrice([], 10000)
expect(price.totalAmount.equalsTo(zeroPrice)).to.equal(true)
})
it('should charge nothing for sandbox', () => {
let price = Calculator.calculatePrice([Product.Sandbox], 1)
expect(price.totalAmount.equalsTo(zeroPrice)).to.equal(true)
price = Calculator.calculatePrice([Product.Sandbox], 10)
expect(price.totalAmount.equalsTo(zeroPrice)).to.equal(true)
price = Calculator.calculatePrice([Product.Sandbox], 100000000)
expect(price.totalAmount.equalsTo(zeroPrice)).to.equal(true)
})
it('should not charge anything for no time', () => {
expect(Calculator.calculatePrice([Product.Production], 0).totalAmount.equalsTo(zeroPrice)).to.equal(true)
})
it('should throw error for invalid time', () => {
try {
expect(Calculator.calculatePrice(['SANDBOX'], -1)).to.be.within(333, 334)
fail(`Accepted negative time (-1)`)
Calculator.calculatePrice([Product.Sandbox], -1)
assert.fail(`Accepted negative time (-1)`)
} catch (error) {

@@ -48,15 +86,32 @@ expect(error.message).to.contain('negative time')

it('should calculate prices for single products', () => {
expect(Calculator.calculatePrice(['PRODUCTION'], 1)).to.equal(24900)
const price = Calculator.calculatePrice([Product.Production], 1)
expect(price.totalAmount.equalsTo(twoFourNine)).to.equal(true)
expect(price.vatAmount.equalsTo(zeroPrice)).to.equal(true)
expect(price.vatPercentage).to.equal(0)
})
it('should calculate prices for combined products', () => {
expect(Calculator.calculatePrice(['PRODUCTION', 'PLUS_ONE_GB'], 1)).to.equal(24900 + 4900)
expect(Calculator.calculatePrice(['PRODUCTION', 'DEVELOPER'], 1)).to.equal(24900 + 3900)
const prodPlusGigabyte = Calculator.calculatePrice([Product.Production, Product.PlusOneGigabyte], 1)
expect(prodPlusGigabyte.productAmount.equalsTo(twoFourNine.add(fourtyNine))).
to.equal(true)
const prodPlusDeveloper = Calculator.calculatePrice([Product.Production, Product.Developer], 1)
expect(prodPlusDeveloper.productAmount.equalsTo(twoFourNine.add(thirtyNine))).
to.equal(true)
})
it('should calculate prices for random ordered combined products', () => {
expect(Calculator.calculatePrice(['PLUS_ONE_GB', 'PRODUCTION', 'PLUS_ONE_GB', 'DEVELOPER', 'SANDBOX'], 1)).to.be.greaterThan(0)
expect(Calculator.calculatePrice(['SANDBOX', 'DEVELOPER', 'DEVELOPER'], 1)).to.be.greaterThan(0)
expect(Calculator.calculatePrice(['PLUS_ONE_GB', 'PRODUCTION', 'PRODUCTION', 'PRODUCTION', 'PRODUCTION'], 1)).to.be.greaterThan(0)
expect(Calculator.calculatePrice(
[Product.PlusOneGigabyte, Product.Production, Product.PlusOneGigabyte, Product.Developer, Product.Sandbox], 1).productAmount.getAmount()).
to.be.greaterThan(0)
expect(Calculator.calculatePrice(
[Product.Sandbox, Product.Developer, Product.Developer], 1).productAmount.getAmount()).to.be.greaterThan(0)
expect(Calculator.calculatePrice(
[Product.PlusOneGigabyte, Product.Production, Product.Production, Product.Production, Product.Production],
1).productAmount.getAmount()).to.be.greaterThan(0)
})
})

@@ -0,1 +1,3 @@

import Dinero from 'dinero.js'
import {

@@ -7,27 +9,42 @@ assert,

import { Product } from '../src/index'
import * as Calculator from '../src/index'
describe('Pricing module', () => {
const twoFourNine = Dinero({ amount: 249 * 100, currency: 'EUR' })
const thirtyNine = Dinero({ amount: 39 * 100, currency: 'EUR' })
const fourtyNine = Dinero({ amount: 49 * 100, currency: 'EUR' })
const zeroPrice = Dinero({ amount: 0, currency: 'EUR'})
const invalidProductSetups: Calculator.Product[][] = [
['SANDBOX', 'PLUS_ONE_GB'],
['DEVELOPER', 'PLUS_ONE_GB'],
['PLUS_ONE_GB'],
['PLUS_ONE_GB', 'PLUS_ONE_GB'],
['PLUS_ONE_GB', 'PLUS_ONE_GB', 'PLUS_ONE_GB'],
[Product.Sandbox, Product.PlusOneGigabyte],
[Product.Developer, Product.PlusOneGigabyte],
[Product.PlusOneGigabyte],
[Product.PlusOneGigabyte, Product.PlusOneGigabyte],
[Product.PlusOneGigabyte, Product.PlusOneGigabyte, Product.PlusOneGigabyte],
]
it('should give prize 0 for empty array', () => {
expect(Calculator.calculatePrice([], 1)).to.equal(0)
expect(Calculator.calculatePrice([], 10000)).to.equal(0)
let price = Calculator.calculatePrice([], 1)
expect(price.totalAmount.equalsTo(zeroPrice)).to.equal(true)
price = Calculator.calculatePrice([], 10000)
expect(price.totalAmount.equalsTo(zeroPrice)).to.equal(true)
})
it('should charge nothing for sandbox', () => {
expect(Calculator.calculatePrice(['SANDBOX'], 1)).to.equal(0)
expect(Calculator.calculatePrice(['SANDBOX'], 10)).to.equal(0)
expect(Calculator.calculatePrice(['SANDBOX'], 100000000)).to.equal(0)
let price = Calculator.calculatePrice([Product.Sandbox], 1)
expect(price.totalAmount.equalsTo(zeroPrice)).to.equal(true)
price = Calculator.calculatePrice([Product.Sandbox], 10)
expect(price.totalAmount.equalsTo(zeroPrice)).to.equal(true)
price = Calculator.calculatePrice([Product.Sandbox], 100000000)
expect(price.totalAmount.equalsTo(zeroPrice)).to.equal(true)
})
it('should not charge anything for no time', () => {
expect(Calculator.calculatePrice(['PRODUCTION'], 0)).to.equal(0)
expect(Calculator.calculatePrice([Product.Production], 0).
totalAmount.equalsTo(zeroPrice)).to.equal(true)
})

@@ -37,3 +54,3 @@

try {
Calculator.calculatePrice(['SANDBOX'], -1)
Calculator.calculatePrice([Product.Sandbox], -1)
assert.fail(`Accepted negative time (-1)`)

@@ -59,8 +76,18 @@ } catch (error) {

it('should calculate prices for single products', () => {
expect(Calculator.calculatePrice(['PRODUCTION'], 1)).to.equal(24900)
const price = Calculator.calculatePrice([Product.Production], 1)
expect(price.totalAmount.equalsTo(twoFourNine)).to.equal(true)
expect(price.vatAmount.equalsTo(zeroPrice)).to.equal(true)
expect(price.vatPercentage).to.equal(0)
})
it('should calculate prices for combined products', () => {
expect(Calculator.calculatePrice(['PRODUCTION', 'PLUS_ONE_GB'], 1)).to.equal(24900 + 4900)
expect(Calculator.calculatePrice(['PRODUCTION', 'DEVELOPER'], 1)).to.equal(24900 + 3900)
const prodPlusGigabyte = Calculator.calculatePrice([Product.Production, Product.PlusOneGigabyte], 1)
expect(prodPlusGigabyte.productAmount.equalsTo(twoFourNine.add(fourtyNine))).
to.equal(true)
const prodPlusDeveloper = Calculator.calculatePrice([Product.Production, Product.Developer], 1)
expect(prodPlusDeveloper.productAmount.equalsTo(twoFourNine.add(thirtyNine))).
to.equal(true)
})

@@ -70,11 +97,34 @@

expect(Calculator.calculatePrice(
['PLUS_ONE_GB', 'PRODUCTION', 'PLUS_ONE_GB', 'DEVELOPER', 'SANDBOX'], 1)).to.be.greaterThan(0)
[Product.PlusOneGigabyte, Product.Production, Product.PlusOneGigabyte,
Product.Developer, Product.Sandbox], 1).productAmount.getAmount()).
to.be.greaterThan(0)
expect(Calculator.calculatePrice(
['SANDBOX', 'DEVELOPER', 'DEVELOPER'], 1)).to.be.greaterThan(0)
expect(Calculator.calculatePrice([Product.Sandbox, Product.Developer, Product.Developer], 1).
productAmount.getAmount()).to.be.greaterThan(0)
expect(Calculator.calculatePrice(
['PLUS_ONE_GB', 'PRODUCTION', 'PRODUCTION', 'PRODUCTION', 'PRODUCTION'],
1)).to.be.greaterThan(0)
[Product.PlusOneGigabyte, Product.Production, Product.Production, Product.Production, Product.Production],
1).productAmount.getAmount()).to.be.greaterThan(0)
})
it('should return the vat amount and percentage as part of the response', () => {
const price = Calculator.calculatePrice([Product.Production], 1, 24)
expect(price.productAmount.equalsTo(twoFourNine)).to.equal(true)
expect(price.vatPercentage).to.equal(24)
expect(price.vatAmount.getAmount()).to.equal(5976)
expect(price.vatAmount.getCurrency()).to.equal('EUR')
expect(price.totalAmount.getAmount()).to.equal(24900 + 5976)
})
it('should round vat sum up by default', () => {
const price = Calculator.calculatePrice([Product.Developer], 1, 1.5)
expect(price.vatPercentage).to.equal(1.5)
expect(price.vatAmount.getAmount()).to.equal(59)
expect(price.vatAmount.getCurrency()).to.equal('EUR')
})
})

@@ -6,3 +6,3 @@ {

"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
"lib": ["es6"], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */

@@ -9,0 +9,0 @@ // "checkJs": true, /* Report errors in .js files. */

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet