Skip to content

Commit

Permalink
Improved product types for sendanor/project-sendanor.fi#133
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaakko Heusala committed Jul 19, 2024
1 parent 61dc13d commit 888b377
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 62 deletions.
83 changes: 49 additions & 34 deletions store/types/product/NewProductDTO.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022-2023. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
// Copyright (c) 2022-2024. Sendanor <info@sendanor.fi>. All rights reserved.

import { isBoolean } from "../../../types/Boolean";
import { isString } from "../../../types/String";
Expand All @@ -9,51 +9,60 @@ import { hasNoOtherKeysInDevelopment } from "../../../types/OtherKeys";
export interface NewProductDTO {
readonly productGroupId : string;
readonly priceTypeId : string;
readonly slug : string;
readonly number : number;
readonly slug : string;
readonly name : string;
readonly description : string;
readonly expensePrice : number;
readonly discountPercent : number;
readonly discountFrom : string;
readonly discountTo : string;
readonly price : number;
readonly vatPercent : number;
readonly onHold : boolean;
readonly isPublic : boolean;
readonly stockEnabled : boolean;
readonly stockAmount : number;
readonly stockSold : number;
readonly stockAmount : number;
readonly stockEnabled : boolean;
readonly onHold : boolean;
readonly published : boolean;
}

export function createNewProductDTO (
productGroupId : string,
priceTypeId : string,
number : number,
name : string,
description : string,
expensePrice : number,
price : number,
vatPercent : number,
onHold : boolean,
isPublic : boolean,
stockEnabled : boolean,
stockAmount : number,
stockSold : number,
slug : string,
productGroupId : string,
priceTypeId : string,
number : number,
slug : string,
name : string,
description : string,
expensePrice : number,
discountPercent : number,
discountFrom : string,
discountTo : string,
price : number,
vatPercent : number,
stockSold : number,
stockAmount : number,
stockEnabled : boolean,
onHold : boolean,
published : boolean,
): NewProductDTO {
return {
productGroupId,
priceTypeId,
number,
name,
slug,
name,
description,
expensePrice,
discountPercent,
discountFrom,
discountTo,
price,
vatPercent,
onHold,
isPublic,
stockEnabled,
stockSold,
stockAmount,
stockEnabled,
onHold,
published,
};
}

Expand All @@ -64,32 +73,38 @@ export function isNewProductDTO (value: any): value is NewProductDTO {
'productGroupId',
'priceTypeId',
'number',
'slug',
'name',
'description',
'expensePrice',
'discountPercent',
'discountFrom',
'discountTo',
'price',
'vatPercent',
'onHold',
'isPublic',
'stockEnabled',
'stockAmount',
'stockSold',
'slug',
'stockAmount',
'stockEnabled',
'onHold',
'published',
])
&& isString(value?.productGroupId)
&& isString(value?.priceTypeId)
&& isNumber(value?.number)
&& isString(value?.slug)
&& isString(value?.name)
&& isString(value?.description)
&& isNumber(value?.expensePrice)
&& isNumber(value?.discountPercent)
&& isString(value?.discountFrom)
&& isString(value?.discountTo)
&& isNumber(value?.price)
&& isNumber(value?.vatPercent)
&& isBoolean(value?.onHold)
&& isBoolean(value?.isPublic)
&& isBoolean(value?.stockEnabled)
&& isNumber(value?.stockAmount)
&& isNumber(value?.stockSold)
&& isString(value?.slug)
&& isNumber(value?.stockAmount)
&& isBoolean(value?.stockEnabled)
&& isBoolean(value?.onHold)
&& isBoolean(value?.published)
);
}

Expand Down
80 changes: 80 additions & 0 deletions store/types/product/NewProductGroupDTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) 2022-2024. Sendanor <info@sendanor.fi>. All rights reserved.

import { isBoolean } from "../../../types/Boolean";
import { isNumber } from "../../../types/Number";
import {
isString,
} from "../../../types/String";
import { isRegularObject } from "../../../types/RegularObject";
import { hasNoOtherKeysInDevelopment } from "../../../types/OtherKeys";

export interface NewProductGroupDTO {
readonly slug : string;
readonly type : string;
readonly name : string;
readonly description : string;
readonly stockSold : number;
readonly stockAmount : number;
readonly stockEnabled : boolean;
readonly onHold : boolean;
readonly published : boolean;
}

export function createNewProductGroupDTO (
slug : string,
type : string,
name : string,
description : string,
stockSold : number,
stockAmount : number,
stockEnabled : boolean,
onHold : boolean,
published : boolean,
): NewProductGroupDTO {
return {
slug,
type,
name,
description,
stockSold,
stockAmount,
stockEnabled,
onHold,
published,
};
}

export function isNewProductGroupDTO (value: any): value is NewProductGroupDTO {
return (
isRegularObject(value)
&& hasNoOtherKeysInDevelopment(value, [
'slug',
'type',
'name',
'description',
'stockSold',
'stockAmount',
'stockEnabled',
'onHold',
'published',
])
&& isString(value?.slug)
&& isString(value?.type)
&& isString(value?.name)
&& isString(value?.description)
&& isNumber(value?.stockSold)
&& isNumber(value?.stockAmount)
&& isBoolean(value?.stockEnabled)
&& isBoolean(value?.onHold)
&& isBoolean(value?.published)
);
}

export function stringifyNewProductGroupDTO (value: NewProductGroupDTO): string {
return `NewProductGroupDTO(${value})`;
}

export function parseNewProductGroupDTO (value: any): NewProductGroupDTO | undefined {
if ( isNewProductGroupDTO(value) ) return value;
return undefined;
}
76 changes: 48 additions & 28 deletions store/types/product/ProductDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,43 @@ export interface ProductDTO {
readonly updated : string;
readonly creation : string;
readonly number : number;
readonly slug : string;
readonly name : string;
readonly description : string;
readonly expensePrice : number;
readonly discountPercent : number;
readonly discountFrom : string;
readonly discountTo : string;
readonly price : number;
readonly vatPercent : number;
readonly stockSold : number;
readonly stockAmount : number;
readonly stockEnabled : boolean;
readonly onHold : boolean;
readonly published : boolean;
readonly stockEnabled : boolean;
readonly stockAmount : number;
readonly stockSold : number;
}

export function createProductDTO (
productId : string,
productGroupId : string,
priceTypeId : string,
updated : string,
creation : string,
number : number,
name : string,
description : string,
expensePrice : number,
price : number,
vatPercent : number,
onHold : boolean,
published : boolean,
stockEnabled : boolean,
stockAmount : number,
stockSold : number,
productId : string,
productGroupId : string,
priceTypeId : string,
updated : string,
creation : string,
number : number,
slug : string,
name : string,
description : string,
expensePrice : number,
discountPercent : number,
discountFrom : string,
discountTo : string,
price : number,
vatPercent : number,
stockSold : number,
stockAmount : number,
stockEnabled : boolean,
onHold : boolean,
published : boolean,
): ProductDTO {
return {
productId,
Expand All @@ -50,16 +58,20 @@ export function createProductDTO (
updated,
creation,
number,
slug,
name,
description,
expensePrice,
discountPercent,
discountFrom,
discountTo,
price,
vatPercent,
stockSold,
stockAmount,
stockEnabled,
onHold,
published,
stockEnabled,
stockAmount,
stockSold,
};
}

Expand All @@ -73,33 +85,41 @@ export function isProductDTO (value: any): value is ProductDTO {
'updated',
'creation',
'number',
'slug',
'name',
'description',
'expensePrice',
'discountPercent',
'discountFrom',
'discountTo',
'price',
'vatPercent',
'stockSold',
'stockAmount',
'stockEnabled',
'onHold',
'published',
'stockEnabled',
'stockAmount',
'stockSold',
])
&& isString(value?.productId)
&& isString(value?.productGroupId)
&& isString(value?.priceTypeId)
&& isString(value?.updated)
&& isString(value?.creation)
&& isNumber(value?.number)
&& isString(value?.slug)
&& isString(value?.name)
&& isString(value?.description)
&& isNumber(value?.expensePrice)
&& isNumber(value?.discountPercent)
&& isString(value?.discountFrom)
&& isString(value?.discountTo)
&& isNumber(value?.price)
&& isNumber(value?.vatPercent)
&& isNumber(value?.stockSold)
&& isNumber(value?.stockAmount)
&& isBoolean(value?.stockEnabled)
&& isBoolean(value?.onHold)
&& isBoolean(value?.published)
&& isBoolean(value?.stockEnabled)
&& isNumber(value?.stockAmount)
&& isNumber(value?.stockSold)
);
}

Expand Down

0 comments on commit 888b377

Please sign in to comment.