feat(ADM-018): completed feature
This commit is contained in:
13
project/src/modules/pricing/domain/errors.ts
Normal file
13
project/src/modules/pricing/domain/errors.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
export class PriceNotFoundError extends Error {
|
||||
constructor() {
|
||||
super('Variant price not found');
|
||||
this.name = 'PriceNotFoundError';
|
||||
}
|
||||
}
|
||||
|
||||
export class InvalidPriceError extends Error {
|
||||
constructor(message = 'Invalid price command') {
|
||||
super(message);
|
||||
this.name = 'InvalidPriceError';
|
||||
}
|
||||
}
|
||||
17
project/src/modules/pricing/domain/ports.ts
Normal file
17
project/src/modules/pricing/domain/ports.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import type {
|
||||
PriceCalculation,
|
||||
PriceCalculationCommand,
|
||||
SetVariantPriceCommand,
|
||||
VariantPrice,
|
||||
} from './price.js';
|
||||
|
||||
export interface PricingService {
|
||||
calculate(input: PriceCalculationCommand): Promise<PriceCalculation>;
|
||||
getVariantPrice(variantId: string): Promise<VariantPrice | undefined>;
|
||||
setVariantPrice(input: SetVariantPriceCommand): Promise<VariantPrice>;
|
||||
}
|
||||
|
||||
export interface PricingRepository {
|
||||
findByVariantId(variantId: string): Promise<VariantPrice | undefined>;
|
||||
setVariantPrice(input: SetVariantPriceCommand): Promise<VariantPrice>;
|
||||
}
|
||||
46
project/src/modules/pricing/domain/price.ts
Normal file
46
project/src/modules/pricing/domain/price.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
export type VatRate = 'general' | 'reduced';
|
||||
|
||||
export const VAT_BASIS_POINTS: Record<VatRate, number> = {
|
||||
general: 2100,
|
||||
reduced: 1000,
|
||||
};
|
||||
|
||||
export interface VariantPrice {
|
||||
variantId: string;
|
||||
netUnitAmountCents: number;
|
||||
offerCents: number | null;
|
||||
costCents: number | null;
|
||||
vatRate: VatRate;
|
||||
currency: 'EUR';
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
export interface SetVariantPriceCommand {
|
||||
variantId: string;
|
||||
netUnitAmountCents: number;
|
||||
offerCents?: number | null;
|
||||
costCents?: number | null;
|
||||
vatRate: VatRate;
|
||||
}
|
||||
|
||||
export interface PriceCalculationCommand {
|
||||
variantId: string;
|
||||
quantity: number;
|
||||
}
|
||||
|
||||
export interface PriceCalculation {
|
||||
variantId: string;
|
||||
quantity: number;
|
||||
currency: 'EUR';
|
||||
vatRate: VatRate;
|
||||
vatBasisPoints: number;
|
||||
netUnitAmountCents: number;
|
||||
netSubtotalCents: number;
|
||||
vatAmountCents: number;
|
||||
totalCents: number;
|
||||
}
|
||||
|
||||
export function calculateVatAmountCents(netSubtotalCents: number, vatRate: VatRate): number {
|
||||
return Math.round((netSubtotalCents * VAT_BASIS_POINTS[vatRate]) / 10_000);
|
||||
}
|
||||
Reference in New Issue
Block a user