diff --git a/src/models/wine.ts b/src/models/wine.ts index 9186097..ac967f4 100644 --- a/src/models/wine.ts +++ b/src/models/wine.ts @@ -4,7 +4,9 @@ import { Viticulture, BottleClosure, VintageOptions, - WineRegion + StandardDrinks, + WineRegion, + WineVolume } from '../types' import { Alpha2Code } from 'i18n-iso-countries' import { CurrencyCode } from 'currency-codes-ts/dist/types' @@ -23,8 +25,9 @@ export class Wine { public producerId: ObjectId, // product producer public varietal: string, // if more than one, list as 'blend' public vintage: number | VintageOptions, // year, nv (non-vintage) or mv (multi-vintage) + public volume: WineVolume, // bottle volume public alcohol: number, // alcohol percentage - public standardDrinks100ml: StandardDrinks100ml, // an amount of standard drinks per 100ml in AU, UK and US + public standardDrinks: StandardDrinks, // an amount of standard drinks per 100ml and bottle in AU, UK and US public viticulture: Viticulture, // viticulture public sulfites: number, // parts per million public filtered: boolean, // is wine filtered (fined (egg or fish)) diff --git a/src/routes/wines.router.ts b/src/routes/wines.router.ts index b7ca3f3..9c032da 100644 --- a/src/routes/wines.router.ts +++ b/src/routes/wines.router.ts @@ -5,7 +5,8 @@ import { wineValidation, handleReqError, handleReqSuccess, - alcoholToStandardDrinks + alcoholToStandardDrinks, + volumeToMl } from '../utils' import Joi from 'joi' @@ -76,7 +77,10 @@ winesRouter.post('/', async (req: Request, res: Response) => { } } - wine.standardDrinks100ml = alcoholToStandardDrinks(wine.alcohol) + wine.standardDrinks = alcoholToStandardDrinks( + wine.alcohol, + volumeToMl(wine.volume) + ) const result = await collections.wines?.insertOne(wine) diff --git a/src/types/product.ts b/src/types/product.ts index d562f86..76949fd 100644 --- a/src/types/product.ts +++ b/src/types/product.ts @@ -66,8 +66,7 @@ export enum VintageOptions { MV = 'mv' } -export interface StandardDrinks100ml { - AU: number - UK: number - US: number +export interface StandardDrinks { + '100ml': { AU: number; UK: number; US: number } + bottle: { AU: number; UK: number; US: number } } diff --git a/src/types/wine.ts b/src/types/wine.ts index 1d85afa..f5e490b 100644 --- a/src/types/wine.ts +++ b/src/types/wine.ts @@ -22,3 +22,15 @@ export interface WineRegion { | string[] | { [key: string]: string[] | { [key: string]: string[] } } } + +export enum WineVolume { + '0.05L' = '0.05L', + '0.187L' = '0.187L', + '0.375L' = '0.375L', + '0.5L' = '0.5L', + '0.75L' = '0.75L', + '1.5L' = '1.5L', + '3L' = '3L', + '6L' = '6L', + '12L' = '12L' +} diff --git a/src/utils/alcohol.ts b/src/utils/alcohol.ts index 3cf3f3a..05d4ed2 100644 --- a/src/utils/alcohol.ts +++ b/src/utils/alcohol.ts @@ -1,10 +1,36 @@ -import { StandardDrinks100ml } from '../types' +import { StandardDrinks, WineVolume } from '../types' import { roundToOneDecimal } from './' export const alcoholToStandardDrinks = ( - alcohol: number -): StandardDrinks100ml => ({ - UK: roundToOneDecimal(10 * alcohol), - AU: roundToOneDecimal(7.91 * alcohol), - US: roundToOneDecimal(5.64 * alcohol) -}) + alcohol: number, + bottle: number +): StandardDrinks => { + const UK100ml = roundToOneDecimal(10 * alcohol) + const AU100ml = roundToOneDecimal(7.91 * alcohol) + const US100ml = roundToOneDecimal(5.64 * alcohol) + + const bottleMultiplier = bottle / 100 + + return { + '100ml': { + UK: UK100ml, + AU: AU100ml, + US: US100ml + }, + bottle: { + UK: UK100ml * bottleMultiplier, + AU: AU100ml * bottleMultiplier, + US: US100ml * bottleMultiplier + } + } +} + +export const volumeToMl = (volume: WineVolume): number => { + if (volume.endsWith('L')) { + const volumeMl = volume.replace('L', '') + + return Number(volumeMl) * 1000 + } + + throw new Error('Not supported volume type') +} diff --git a/src/utils/validation/wine.ts b/src/utils/validation/wine.ts index 1f651f1..5a61c40 100644 --- a/src/utils/validation/wine.ts +++ b/src/utils/validation/wine.ts @@ -4,7 +4,8 @@ import { VintageOptions, BottleClosure, Viticulture, - WineRegion + WineRegion, + WineVolume } from '../../types' import { wineRegionsMap, isObject } from '../' @@ -261,6 +262,9 @@ export const wineValidation = (data: unknown): Joi.ValidationResult => vintage: Joi.alternatives() .try(Joi.string().valid(...Object.values(VintageOptions)), Joi.number()) .required(), + volume: Joi.string() + .valid(...Object.values(WineVolume)) + .required(), alcohol: Joi.number().min(0).max(0.99).required(), viticulture: Joi.string() .valid(...Object.values(Viticulture))