Skip to content

Commit

Permalink
feat(RSS-ECOMM-4_98): add level to product (#313)
Browse files Browse the repository at this point in the history
feat: add level to product
  • Loading branch information
YulikK authored May 26, 2024
1 parent 20ec774 commit 43da852
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
18 changes: 16 additions & 2 deletions src/shared/API/product/model/ProductModel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Category, Product, SizeType, localization } from '@/shared/types/product.ts';
import type { Category, LevelType, Product, SizeType, localization } from '@/shared/types/product.ts';
import type {
Attribute as AttributeResponse,
CategoryPagedQueryResponse,
Expand All @@ -12,7 +12,7 @@ import type {
} from '@commercetools/platform-sdk';

import { PRICE_FRACTIONS } from '@/shared/constants/product.ts';
import getSize from '@/shared/utils/size.ts';
import { getLevel, getSize } from '@/shared/utils/size.ts';

import {
Attribute,
Expand Down Expand Up @@ -109,6 +109,13 @@ export class ProductModel {
return [];
}

private adaptLevel(attribute: AttributeResponse): LevelType | null {
if (Array.isArray(attribute.value) && attribute.value.length && isAttributePlainEnumValue(attribute.value[0])) {
return getLevel(attribute.value[0].key);
}
return null;
}

private adaptPrice(variant: ProductVariant): number {
let price = 0;

Expand All @@ -132,6 +139,7 @@ export class ProductModel {
id: product.id || '',
images: [],
key: product.key ?? '',
level: null,
name: [],
slug: [],
variant: [],
Expand All @@ -154,6 +162,7 @@ export class ProductModel {
const variants = [...response.variants, response.masterVariant];
variants.forEach((variant) => {
let size: SizeType | null = null;
let level: LevelType | null = null;

if (variant.attributes?.length) {
variant.attributes.forEach((attribute) => {
Expand All @@ -163,6 +172,11 @@ export class ProductModel {
if (attribute.name === Attribute.SIZE) {
size = this.adaptSize(attribute);
}
if (attribute.name === Attribute.LEVEL) {
level = this.adaptLevel(attribute);
const productEl = product;
productEl.level = level;
}
});
}

Expand Down
1 change: 1 addition & 0 deletions src/shared/API/types/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Category, Product, SizeType } from '@/shared/types/product.ts';

export const Attribute = {
FULL_DESCRIPTION: 'full_description',
LEVEL: 'level',
SIZE: 'size',
} as const;

Expand Down
8 changes: 8 additions & 0 deletions src/shared/types/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ export const SIZE = {
XL: 'XL',
} as const;

export const LEVEL = {
1: '1',
2: '2',
3: '3',
} as const;

export type SizeType = (typeof SIZE)[keyof typeof SIZE];
export type LevelType = (typeof LEVEL)[keyof typeof LEVEL];

export interface Variant {
discount: number;
Expand All @@ -34,6 +41,7 @@ export interface Product {
id: string;
images: string[];
key: string;
level: LevelType | null;
name: localization[];
slug: localization[];
variant: Variant[];
Expand Down
18 changes: 15 additions & 3 deletions src/shared/utils/size.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { SizeType } from '../types/product.ts';
import type { LevelType, SizeType } from '../types/product.ts';

import { SIZE } from '../types/product.ts';
import { LEVEL, SIZE } from '../types/product.ts';

export default function getSize(sizeString: string): SizeType | null {
export function getSize(sizeString: string): SizeType | null {
const sizeValues = Object.values(SIZE);

const foundValue = sizeValues.find((value) => value.toLowerCase() === sizeString.toLowerCase());
Expand All @@ -13,3 +13,15 @@ export default function getSize(sizeString: string): SizeType | null {

return null;
}

export function getLevel(levelString: string): LevelType | null {
const levelValues = Object.values(LEVEL);

const foundValue = levelValues.find((value) => value.toLowerCase() === levelString.toLowerCase());

if (foundValue) {
return foundValue;
}

return null;
}

0 comments on commit 43da852

Please sign in to comment.