Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@
"import": "./dist/index.js"
}
},
"files": ["dist"],
"files": [
"dist"
],
"scripts": {
"build": "tsc",
"test": "vitest run",
"test:watch": "vitest"
},
"keywords": ["pricing", "engine", "calculator", "business-logic"],
"keywords": [
"pricing",
"engine",
"calculator",
"business-logic"
],
"author": "lukTS",
"license": "MIT"
"license": "MIT",
"dependencies": {
"zod": "^4.4.3"
}
}
5 changes: 4 additions & 1 deletion packages/core/src/engine.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { CalculationInputSchema, PricingEngineConfigSchema } from './schemas.js';
import type { CalculationInput, CalculationResult, PricingEngineConfig } from './types.js';

export class PricingEngine {
private rules: PricingEngineConfig['rules'];

constructor(config: PricingEngineConfig) {
PricingEngineConfigSchema.parse(config);
this.rules = config.rules;
}

calculate(input: CalculationInput): CalculationResult {
const rule = this.rules.find((r) => r.name === input.rule);
CalculationInputSchema.parse(input);

const rule = this.rules.find((r) => r.name === input.rule);
if (!rule) {
throw new Error(`Unknown rule: "${input.rule}"`);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './types.js';
export * from './engine.js';
export * from './engine.js';
export * from './schemas.js';
30 changes: 30 additions & 0 deletions packages/core/src/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { z } from 'zod';

export const PricingRuleConfigSchema = z.object({
name: z.string().min(1, 'Rule name is required'),
type: z.string().min(1, 'Rule type is required'),
unitPrice: z.number().positive('unitPrice must be positive'),
unit: z.string().min(1, 'Unit is required'),
minCharge: z.number().positive('minCharge must be positive').optional(),
});

export const CalculationDimensionsSchema = z.object({
width: z.number().positive('Width must be positive'),
height: z.number().positive('Height must be positive'),
});

export const CalculationInputSchema = z.object({
rule: z.string().min(1, 'Rule name is required'),
dimensions: CalculationDimensionsSchema,
quantity: z.number().int().positive('Quantity must be a positive integer'),
});

export const PricingEngineConfigSchema = z.object({
rules: z
.array(PricingRuleConfigSchema)
.min(1, 'At least one rule is required')
.refine(
(rules) => new Set(rules.map((r) => r.name)).size === rules.length,
'Rule names must be unique',
),
});
117 changes: 117 additions & 0 deletions packages/core/tests/schemas.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { describe, expect, it } from "vitest";
import { PricingEngine } from '../src/engine.js';

describe('PricingRuleConfigSchema', () => {
it('throws on empty rule name', () => {
expect(() => new PricingEngine({
rules: [ { name: '', type: 'area', unitPrice: 12.5, unit: 'm2', minCharge: 50 } ]
})).toThrow();
});

it('throws on zero unitPrice', () => {
expect(() => new PricingEngine({
rules: [ {name: 'test', type: 'area', unitPrice: 0, unit: 'm2', minCharge: 50} ]
})).toThrow();
});

it('throws on negative unitPrice', () => {
expect(() => new PricingEngine({
rules: [{ name: 'test', type: 'area', unitPrice: -30, unit: 'm2', minCharge: 50 }],
})).toThrow();
});

it('throws on empty unit', () => {
expect(() => new PricingEngine({
rules: [{ name: 'test', type: 'area', unitPrice: 30, unit: '', minCharge: 50 }]
})).toThrow();
});

it('throws on zero minCharge', () => {
expect(() => new PricingEngine({
rules: [{ name: 'test', type: 'area', unitPrice: 30, unit: 'm2', minCharge: 0 }],
})).toThrow();
});

it('throws on negative minCharge', () => {
expect(() => new PricingEngine({
rules: [{ name: 'test', type: 'area', unitPrice: 30, unit: 'm2', minCharge: -50 }]
})).toThrow();
});

it('throws if no rules are provided', () => {
expect(() => new PricingEngine({ rules: [] })).toThrow();
});

it('throws if rule names are not unique', () => {
expect(() => new PricingEngine({
rules: [
{ name: 'duplicate', type: 'area', unitPrice: 30, unit: 'm2', minCharge: 50 },
{ name: 'duplicate', type: 'area', unitPrice: 25, unit: 'm2' },
]
})).toThrow();
});
});

describe('CalculationInputSchema', () => {
const engine = new PricingEngine({
rules: [
{ name: 'flat-surface', type: 'area', unitPrice: 12.5, unit: 'm2' },
{ name: 'premium', type: 'area', unitPrice: 30, unit: 'm2', minCharge: 50 },
],
});

it('throws on empty rule name in input', () => {
expect(() => engine.calculate({
rule: '',
dimensions: { width: 2, height: 3 },
quantity: 1,
})).toThrow();
});

it('throws on zero quantity', () => {
expect(() => engine.calculate({
rule: 'flat-surface',
dimensions: { width: 2, height: 3 },
quantity: 0,
})).toThrow();
});

it('throws on negative quantity', () => {
expect(() => engine.calculate({
rule: 'flat-surface',
dimensions: { width: 2, height: 3 },
quantity: -1,
})).toThrow();
});

it('throws on negative width', () => {
expect(() => engine.calculate({
rule: 'flat-surface',
dimensions: { width: -1, height: 3 },
quantity: 1,
})).toThrow();
});

it('throws on negative height', () => {
expect(() => engine.calculate({
rule: 'flat-surface',
dimensions: { width: 2, height: -3 },
quantity: 1,
})).toThrow();
});
it('throws on zero width', () => {
expect(() => engine.calculate({
rule: 'flat-surface',
dimensions: { width: 0, height: 3 },
quantity: 1,
})).toThrow();
});

it('throws on zero height', () => {
expect(() => engine.calculate({
rule: 'flat-surface',
dimensions: { width: 2, height: 0 },
quantity: 1,
})).toThrow();
});
});
11 changes: 10 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading