|
| 1 | +import { UserType, ProductType } from './interfaces'; |
| 2 | + |
| 3 | +class AccountService { |
| 4 | + private userDiscounts: { userType: UserType; discount: number }[]; |
| 5 | + private productsList: { id: number; title: ProductType }[]; |
| 6 | + private userProductsDiscount: { userType: UserType; productId: number; discount: number }[]; |
| 7 | + |
| 8 | + constructor() { |
| 9 | + this.userDiscounts = [ |
| 10 | + { userType: UserType.Standard, discount: 0 }, |
| 11 | + { userType: UserType.Premium, discount: 0.1 }, |
| 12 | + { userType: UserType.Gold, discount: 0.2 }, |
| 13 | + { userType: UserType.Free, discount: 0.3 }, |
| 14 | + ]; |
| 15 | + this.productsList = [ |
| 16 | + { id: 1, title: ProductType.Car }, |
| 17 | + { id: 2, title: ProductType.Food }, |
| 18 | + { id: 3, title: ProductType.Toy }, |
| 19 | + ]; |
| 20 | + this.userProductsDiscount = [ |
| 21 | + { userType: UserType.Standard, productId: 1, discount: 0 }, |
| 22 | + { userType: UserType.Standard, productId: 2, discount: 0 }, |
| 23 | + { userType: UserType.Standard, productId: 3, discount: 0 }, |
| 24 | + { userType: UserType.Premium, productId: 1, discount: 0.05 }, |
| 25 | + { userType: UserType.Premium, productId: 2, discount: 0.1 }, |
| 26 | + { userType: UserType.Premium, productId: 3, discount: 0.02 }, |
| 27 | + { userType: UserType.Gold, productId: 1, discount: 0.1 }, |
| 28 | + { userType: UserType.Gold, productId: 2, discount: 0.2 }, |
| 29 | + { userType: UserType.Gold, productId: 3, discount: 0.04 }, |
| 30 | + { userType: UserType.Free, productId: 1, discount: 0.2 }, |
| 31 | + { userType: UserType.Free, productId: 2, discount: 0.3 }, |
| 32 | + { userType: UserType.Free, productId: 3, discount: 0.12 }, |
| 33 | + ]; |
| 34 | + } |
| 35 | + |
| 36 | + // Метод получения списка скидок по статусам профилей |
| 37 | + public getUserDiscounts() { |
| 38 | + return new Promise((resolve) => { |
| 39 | + setTimeout(() => { |
| 40 | + resolve(this.userDiscounts); |
| 41 | + }, 1000); |
| 42 | + }); |
| 43 | + } |
| 44 | + // Метод получения списка товаров |
| 45 | + public getProductsList() { |
| 46 | + return new Promise((resolve) => { |
| 47 | + setTimeout(() => { |
| 48 | + resolve(this.productsList); |
| 49 | + }, 1000); |
| 50 | + }); |
| 51 | + } |
| 52 | + // Метод получения скидок на определенный товар по типам профилей |
| 53 | + public getUserProductsDiscountList() { |
| 54 | + return new Promise((resolve) => { |
| 55 | + setTimeout(() => { |
| 56 | + resolve(this.userProductsDiscount); |
| 57 | + }, 1000); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + // Метод получения id товара по названию |
| 62 | + public getProductId(productTitle: ProductType): number { |
| 63 | + const productId = this.productsList.find((id) => id.title === productTitle); |
| 64 | + return productId?.id; |
| 65 | + } |
| 66 | + |
| 67 | + // Метод получения скидки для пользователя по статусам |
| 68 | + public getDiscount(userType: UserType): number { |
| 69 | + const userDiscount = this.userDiscounts.find((discount) => discount.userType === userType); |
| 70 | + return userDiscount?.discount; |
| 71 | + } |
| 72 | + // Метод получения скидки на определенный товар для конкретного статуса |
| 73 | + public getUserProductDiscount(userType: UserType, productTitle: ProductType): number { |
| 74 | + const discountItem = this.userProductsDiscount.find( |
| 75 | + (item) => item.userType === userType && item.productId === this.getProductId(productTitle) |
| 76 | + ); |
| 77 | + return discountItem?.discount; |
| 78 | + } |
| 79 | + // Метод формирования общей скидки (скидки суммируются) |
| 80 | + public getCommonDiscount(userType: UserType, productTitle: ProductType): number { |
| 81 | + const commonDiscount = this.getDiscount(userType) * 100 + this.getUserProductDiscount(userType, productTitle) * 100; |
| 82 | + return commonDiscount; |
| 83 | + } |
| 84 | + // Метод формирования конечной цены |
| 85 | + calculateFinalPrice(initialPrice: number, userType: UserType, productTitle: ProductType): number { |
| 86 | + const discountAmount = (initialPrice / 100) * this.getCommonDiscount(userType, productTitle); |
| 87 | + return initialPrice - discountAmount; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export default AccountService; |
0 commit comments