2024-06-27 23:31:59 +02:00
|
|
|
import uuid from "react-native-uuid";
|
2024-07-06 00:00:08 +02:00
|
|
|
import { Area } from "convert";
|
2024-06-30 18:37:27 +02:00
|
|
|
import { Transform } from "class-transformer";
|
2024-07-06 00:00:08 +02:00
|
|
|
import { dimensions_t, area_t } from "./dimensions";
|
|
|
|
import { matchDimensions } from "./dimensions";
|
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-07-06 00:00:08 +02:00
|
|
|
export function dimensionArea(d: dimensions_t) {
|
|
|
|
return "w" in d ? d.w * d.l : 0;
|
2024-06-28 23:53:48 +02:00
|
|
|
}
|
|
|
|
|
2024-06-30 18:37:27 +02:00
|
|
|
export type ProductData = {
|
2024-07-06 00:00:08 +02:00
|
|
|
id?: Id;
|
|
|
|
pricePerUnit: number;
|
|
|
|
dimensions: dimensions_t;
|
|
|
|
attributes?: ProductAttributes;
|
2024-06-30 18:37:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|