Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {presets: ['@babel/preset-env']}
Empty file added server/jestSetupFile.js
Empty file.
2,678 changes: 1,712 additions & 966 deletions server/package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,20 @@
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-prettier": "^3.4.0",
"fs": "^0.0.1-security",
"jest": "^29.0.0",
"jest": "^29.7.0",
"path": "^0.12.7",
"prettier": "^2.3.2",
"ts-jest": "^29.1.1",
"ts-jest": "^29.2.4",
"ts-loader": "^9.2.5",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
},
"dependencies": {
"@apollo/server": "^4.9.3",
"@babel/preset-env": "^7.25.3",
"@graphql-tools/schema": "^10.0.0",
"apollo-server-express": "^3.1.2",
"babel-jest": "^29.7.0",
"bcrypt": "^5.0.1",
"cors": "^2.8.5",
"crypto": "^1.0.1",
Expand Down
13 changes: 13 additions & 0 deletions server/src/utils/discount/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export enum TypeOfUser {
STANDARD = 'STANDARD',
PREMIUM = 'PREMIUM',
GOLD = 'GOLD',
FREE = 'FREE',
}

export enum TypeOfProduct {
CAR = 'CAR',
TOY = 'TOY',
FOOD = 'FOOD',
}

43 changes: 43 additions & 0 deletions server/src/utils/discount/discountHelper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, it, expect, beforeAll} from "@jest/globals"
import { TypeOfProduct, TypeOfUser } from "./const"
import { DiscountUnionType, getTotalDiscount } from "./discountHelper"

describe('Check user and product discounts', () => {
let discountUserGold : DiscountUnionType;
let discountUserPremium: DiscountUnionType;

let discountProductFood: DiscountUnionType;
let discountProductToy: DiscountUnionType;

beforeAll(() => {
discountUserGold = {
type: 'user',
discountType: TypeOfUser.GOLD,
}
discountUserPremium = {
type: 'user',
discountType: TypeOfUser.PREMIUM,
}

discountProductFood = {
type: 'product',
discountType: TypeOfProduct.FOOD,
}
discountProductToy = {
type: 'product',
discountType: TypeOfProduct.TOY,
}

})

it('Check user Gold and product Food', () => {
let allDiscounts : DiscountUnionType[] = [discountUserGold, discountProductFood];
expect(getTotalDiscount(allDiscounts)).toBe(22);
});

it('Check user Premium and product Toy', () => {
let allDiscounts : DiscountUnionType[] = [discountUserPremium, discountProductToy];
expect(getTotalDiscount(allDiscounts)).toBe(17);
});

})
74 changes: 74 additions & 0 deletions server/src/utils/discount/discountHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { TypeOfProduct, TypeOfUser } from "./const";
// Вспомогательные функции для вычисления скидки пользователя
// с учетом статуса пользователя и категории товара

// Получение размера скидки по типу пользователя
export const getDiscountByTypeOfUser = (type: TypeOfUser): number => {
switch (type) {
case TypeOfUser.STANDARD:
return 0;
case TypeOfUser.PREMIUM:
return 8;
case TypeOfUser.GOLD:
return 16;
case TypeOfUser.FREE:
return 100;
default:
return 0;
}
}

const getDiscountByTypeOfProductTypeGuard = (type: never): never => {
throw new Error('Function getDiscountByTypeOfProduct called with new type that it knows nothing about')
}

// Получение размера скидки по типу продукта
export const getDiscountByTypeOfProduct = (type: TypeOfProduct): number => {
switch (type) {
case TypeOfProduct.CAR:
return 3;
case TypeOfProduct.FOOD:
return 6;
case TypeOfProduct.TOY:
return 9;
default:
return getDiscountByTypeOfProductTypeGuard(type);
}
}

export interface DiscountUnionType {
type: 'user' | 'product',
discountType: TypeOfUser | TypeOfProduct,
}

const getTotalDiscountTypeGuard = (type: never): never => {
throw new Error('Function getTotalDiscount called with new type that it knows nothing about');
}

// Получение общей скидки
export const getTotalDiscount = (allDiscounts: DiscountUnionType[]) => {
let totalDiscount = 0;

allDiscounts.forEach(d => {
switch (d.type) {
case 'user': {
totalDiscount += getDiscountByTypeOfUser(d.discountType as TypeOfUser);
break;
}

case 'product': {
totalDiscount += getDiscountByTypeOfProduct(d.discountType as TypeOfProduct);
break;
}
default:
getTotalDiscountTypeGuard(d.type);
}
});

if(totalDiscount > 100){
totalDiscount = 100;
}

return totalDiscount;
}