refactor products as type.

This commit is contained in:
Jordan Hewitt 2024-07-17 06:54:55 -07:00
parent 49266bbc97
commit d2368b30e5

View File

@ -1,6 +1,4 @@
import uuid from "react-native-uuid"; import uuid from "react-native-uuid";
import { Area } from "convert";
import { Transform } from "class-transformer";
import { dimensions_t, area_t } from "./dimensions"; import { dimensions_t, area_t } from "./dimensions";
import { matchDimensions } from "./dimensions"; import { matchDimensions } from "./dimensions";
@ -15,85 +13,66 @@ export type ProductAttributes = {
description?: string, description?: string,
depth?: string, depth?: string,
currency?: Currency, currency?: Currency,
// [index:string]: any, [index:string]: any,
} }
export function dimensionArea(d: dimensions_t) { export function dimensionArea(d: dimensions_t) {
return "w" in d ? d.w * d.l : 0; return "w" in d ? d.w * d.l : 0;
} }
export type ProductData = { export type product_type_t = "lumber" | "area_rug"
export type Product = {
id?: Id; id?: Id;
pricePerUnit: number; pricePerUnit: number;
dimensions: dimensions_t; dimensions: dimensions_t;
type: product_type_t;
attributes?: ProductAttributes; attributes?: ProductAttributes;
}; };
export type LumberProduct = Product & {
type: "lumber"
}
export class Product { export type AreaRugProduct = Product & {
type: "lumber"
}
public id?: Id; export function productPriceFor(product : Product, dimensions: dimensions_t, damage: number): number {
constructor(public pricePerUnit: number, public dimensions: dimensions_t, public attributes: ProductAttributes = {},
id?: Id,
) {
this.id = id || uuid.v4().toString();
}
public priceFor(dimensions: dimensions_t, damage : number): number {
if (Number.isNaN(damage)) damage = 0; if (Number.isNaN(damage)) damage = 0;
const dim = matchDimensions(dimensions, this.dimensions); const dim = matchDimensions(dimensions, product.dimensions);
return ( return (
dim.w ? dimensionArea(dim) / dimensionArea(this.dimensions) * this.pricePerUnit dim.w ? dimensionArea(dim) / dimensionArea(product.dimensions) * product.pricePerUnit
: (dim.l / this.dimensions.l) * this.pricePerUnit : (dim.l / product.dimensions.l) * product.pricePerUnit
) * (1.0 - damage); ) * (1.0 - damage);
} }
get priceDisplay() { export function priceDisplay(price : number) {
return this.pricePerUnit.toLocaleString(undefined, { return price.toLocaleString(undefined, {
minimumFractionDigits: 2, minimumFractionDigits: 2,
maximumFractionDigits: 2, maximumFractionDigits: 2,
}) })
} }
get pricePerUnitDisplay() { export function pricePerUnitDisplay(product : Product) {
const p = this.priceDisplay; const p = priceDisplay(product.pricePerUnit);
const { l, u } = this.dimensions; const { l, u } = product.dimensions;
const w = (this.dimensions as area_t).w || null; const w = (product.dimensions as area_t).w || null;
const d = w ? `${l}${u} x ${w}${u}` : `${l}${u}`; const d = w ? `${l}${u} x ${w}${u}` : `${l}${u}`;
return `$${p} per ${d}` return `$${p} per ${d}`
} }
get attributesAsList() { export function attributesAsList(attributes : {[key:string]: any}) {
return Object.entries(this.attributes).map(([key, value]) => { return Object.entries(attributes).map(([key, value]) => {
return { key, value } return { key, value }
}) })
} }
public removeAttribute(key: string) { export function removeAttribute(attributes: {[key: string]: any}, key: string) {
this.attributes = Object.fromEntries( return Object.fromEntries(
Object.entries(this.attributes).filter( Object.entries(attributes).filter(
([k, v]) => { ([k, v]) => {
k == key; k == key;
} }
) )
); );
}
get asObject(): ProductData {
return {
id: this.id,
pricePerUnit: this.pricePerUnit,
dimensions: this.dimensions,
attributes: this.attributes,
}
}
static fromObject({ id, pricePerUnit, dimensions, attributes }: ProductData) {
return new Product(
pricePerUnit,
dimensions,
attributes,
id,
)
}
} }