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 {
if (Number.isNaN(damage)) damage = 0;
const dim = matchDimensions(dimensions, product.dimensions);
return (
dim.w ? dimensionArea(dim) / dimensionArea(product.dimensions) * product.pricePerUnit
: (dim.l / product.dimensions.l) * product.pricePerUnit
) * (1.0 - damage);
}
constructor(public pricePerUnit: number, public dimensions: dimensions_t, public attributes: ProductAttributes = {}, export function priceDisplay(price : number) {
id?: Id, return price.toLocaleString(undefined, {
) { minimumFractionDigits: 2,
this.id = id || uuid.v4().toString(); maximumFractionDigits: 2,
} })
}
public priceFor(dimensions: dimensions_t, damage : number): number { export function pricePerUnitDisplay(product : Product) {
if (Number.isNaN(damage)) damage = 0; const p = priceDisplay(product.pricePerUnit);
const dim = matchDimensions(dimensions, this.dimensions); const { l, u } = product.dimensions;
return ( const w = (product.dimensions as area_t).w || null;
dim.w ? dimensionArea(dim) / dimensionArea(this.dimensions) * this.pricePerUnit
: (dim.l / this.dimensions.l) * this.pricePerUnit
) * (1.0 - damage);
}
get priceDisplay() {
return this.pricePerUnit.toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})
}
get pricePerUnitDisplay() {
const p = this.priceDisplay;
const { l, u } = this.dimensions;
const w = (this.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,
) )
} );
} }