2024-06-27 23:31:59 +02:00
|
|
|
import uuid from "react-native-uuid";
|
2024-06-28 23:53:48 +02:00
|
|
|
import convert, { Area, Length } from "convert";
|
2024-06-30 18:37:27 +02:00
|
|
|
import { Transform } from "class-transformer";
|
2024-06-28 23:53:48 +02:00
|
|
|
|
2024-06-27 16:06:39 +02:00
|
|
|
export type Id = string;
|
|
|
|
|
|
|
|
export type Currency = "USD";
|
|
|
|
|
|
|
|
export type ProductAttributes = {
|
2024-06-27 23:31:59 +02:00
|
|
|
id?: string,
|
2024-06-27 16:06:39 +02:00
|
|
|
name?: string,
|
|
|
|
image?: string,
|
|
|
|
description?: string,
|
|
|
|
depth?: string,
|
2024-06-27 23:31:59 +02:00
|
|
|
currency?: Currency,
|
2024-06-28 23:53:48 +02:00
|
|
|
// [index:string]: any,
|
2024-06-27 16:06:39 +02:00
|
|
|
}
|
2024-06-28 23:53:48 +02:00
|
|
|
export type length_t = {
|
|
|
|
l: number, u: Length
|
|
|
|
}
|
|
|
|
|
|
|
|
export type area_t = length_t & {
|
|
|
|
w: number,
|
|
|
|
}
|
|
|
|
|
|
|
|
export type dimensions_t = area_t | length_t;
|
2024-06-27 16:06:39 +02:00
|
|
|
|
2024-06-30 18:37:27 +02:00
|
|
|
export type ProductData = {
|
|
|
|
id?: Id,
|
|
|
|
pricePerUnit: number,
|
|
|
|
dimensions: dimensions_t,
|
|
|
|
attributes?: ProductAttributes,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2024-06-28 23:53:48 +02:00
|
|
|
export type product_type_t = "area" | "length";
|
|
|
|
|
|
|
|
export const isArea = (d: dimensions_t) => ("width" in d);
|
|
|
|
export const isLength = (d: dimensions_t) => (!("width" in d));
|
|
|
|
export const dimensionType = (d: dimensions_t) => isArea(d) ? "area" : "length"
|
|
|
|
|
2024-06-30 18:37:27 +02:00
|
|
|
export function matchDimensions(d1: dimensions_t, d2: dimensions_t) {
|
|
|
|
if (!
|
|
|
|
(
|
|
|
|
(isArea(d1) && isArea(d2)) ||
|
|
|
|
(isLength(d1) && isLength(d2))
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new Error(`Dimension mismatch: ${JSON.stringify(d1)} / ${JSON.stringify(d1)}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
l: convert(d1.l, d1.u).to(d2.u),
|
|
|
|
u: d2.u,
|
|
|
|
...(
|
|
|
|
"w" in d1 ?
|
|
|
|
{ w: convert(d1.w, d1.u).to(d2.u), }
|
|
|
|
: {}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function dimensionArea(d: dimensions_t) {
|
|
|
|
return "w" in d ? d.w * d.l : 0;
|
|
|
|
}
|
|
|
|
|
2024-06-28 23:53:48 +02:00
|
|
|
export class Product {
|
2024-06-30 18:37:27 +02:00
|
|
|
|
2024-07-01 21:23:45 +02:00
|
|
|
public id?: Id;
|
2024-06-30 18:37:27 +02:00
|
|
|
|
|
|
|
constructor(public pricePerUnit: number, public dimensions: dimensions_t, public attributes: ProductAttributes = {},
|
|
|
|
id?: Id,
|
|
|
|
) {
|
|
|
|
this.id = id || uuid.v4().toString();
|
2024-06-27 16:06:39 +02:00
|
|
|
}
|
|
|
|
|
2024-07-01 21:23:45 +02:00
|
|
|
public priceFor(dimensions: dimensions_t, damage : number): number {
|
|
|
|
if (Number.isNaN(damage)) damage = 0;
|
2024-06-30 18:37:27 +02:00
|
|
|
const dim = matchDimensions(dimensions, this.dimensions);
|
|
|
|
return (
|
|
|
|
dim.w ? dimensionArea(dim) / dimensionArea(this.dimensions) * this.pricePerUnit
|
|
|
|
: (dim.l / this.dimensions.l) * this.pricePerUnit
|
2024-07-01 21:23:45 +02:00
|
|
|
) * (1.0 - damage);
|
2024-06-27 16:06:39 +02:00
|
|
|
}
|
|
|
|
|
2024-06-29 02:04:30 +02:00
|
|
|
get priceDisplay() {
|
2024-06-30 18:37:27 +02:00
|
|
|
return this.pricePerUnit.toLocaleString(undefined, {
|
|
|
|
minimumFractionDigits: 2,
|
|
|
|
maximumFractionDigits: 2,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
get pricePerUnitDisplay() {
|
|
|
|
const p = this.priceDisplay;
|
2024-07-01 21:23:45 +02:00
|
|
|
const { l, u } = this.dimensions;
|
2024-06-30 18:37:27 +02:00
|
|
|
const w = (this.dimensions as area_t).w || null;
|
2024-07-01 21:23:45 +02:00
|
|
|
const d = w ? `${l}${u} x ${w}${u}` : `${l}${u}`;
|
2024-06-30 18:37:27 +02:00
|
|
|
return `$${p} per ${d}`
|
2024-06-29 02:04:30 +02:00
|
|
|
}
|
|
|
|
|
2024-06-27 23:31:59 +02:00
|
|
|
get attributesAsList() {
|
|
|
|
return Object.entries(this.attributes).map(([key, value]) => {
|
2024-06-28 23:53:48 +02:00
|
|
|
return { key, value }
|
2024-06-27 23:31:59 +02:00
|
|
|
})
|
2024-06-27 16:06:39 +02:00
|
|
|
}
|
|
|
|
|
2024-06-28 23:53:48 +02:00
|
|
|
public removeAttribute(key: string) {
|
2024-06-30 18:37:27 +02:00
|
|
|
this.attributes = Object.fromEntries(
|
|
|
|
Object.entries(this.attributes).filter(
|
|
|
|
([k, v]) => {
|
|
|
|
k == key;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-01 21:23:45 +02:00
|
|
|
get asObject(): ProductData {
|
2024-06-30 18:37:27 +02:00
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
pricePerUnit: this.pricePerUnit,
|
2024-07-01 21:23:45 +02:00
|
|
|
dimensions: this.dimensions,
|
2024-06-30 18:37:27 +02:00
|
|
|
attributes: this.attributes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-01 21:23:45 +02:00
|
|
|
static fromObject({ id, pricePerUnit, dimensions, attributes }: ProductData) {
|
2024-06-30 18:37:27 +02:00
|
|
|
return new Product(
|
|
|
|
pricePerUnit,
|
|
|
|
dimensions,
|
|
|
|
attributes,
|
|
|
|
id,
|
|
|
|
)
|
2024-06-27 16:06:39 +02:00
|
|
|
}
|
|
|
|
}
|