diff --git a/src/pages/CartPage/model/CartPageModel.ts b/src/pages/CartPage/model/CartPageModel.ts index 7d3e44c9..29b57dac 100644 --- a/src/pages/CartPage/model/CartPageModel.ts +++ b/src/pages/CartPage/model/CartPageModel.ts @@ -3,6 +3,7 @@ import type { Page } from '@/shared/types/page.ts'; import SummaryModel from '@/entities/Summary/model/SummaryModel.ts'; import getCartModel from '@/shared/API/cart/model/CartModel.ts'; +import getCustomerModel from '@/shared/API/customer/model/CustomerModel.ts'; import LoaderModel from '@/shared/Loader/model/LoaderModel.ts'; import getStore from '@/shared/Store/Store.ts'; import { setCurrentPage } from '@/shared/Store/actions.ts'; @@ -17,6 +18,7 @@ import ProductOrderModel from '@/widgets/ProductOrder/model/ProductOrderModel.ts import CartPageView from '../view/CartPageView.ts'; +const HAPPY_BIRTHDAY = 'HAPPY-BIRTHDAY-10'; const TITLE_SUMMARY = { cart: { en: 'Cart Discount', ru: 'Скидка на корзину' }, product: { en: 'Product Discount', ru: 'Скидка на продукты' }, @@ -49,6 +51,9 @@ class CartPageModel implements Page { private async addDiscountHandler(discountCode: string): Promise { if (discountCode.trim()) { + if (discountCode.trim() === HAPPY_BIRTHDAY) { + await this.checkBirthday(); + } const loader = new LoaderModel(LOADER_SIZE.SMALL).getHTML(); this.view.getCouponButton().append(loader); await getCartModel() @@ -94,6 +99,31 @@ class CartPageModel implements Page { this.view.updateTotal(this.cart); } + private async checkBirthday(): Promise { + if (!getStore().getState().isUserLoggedIn) { + throw showErrorMessage(SERVER_MESSAGE_KEYS.COUPON_NEED_LOGIN); + } + const customer = await getCustomerModel().getCurrentUser(); + if (customer?.birthDate) { + if (customer?.birthDate) { + const currentDate = new Date(); + currentDate.setHours(0, 0, 0, 0); + const birthDate = new Date(customer.birthDate); + birthDate.setFullYear(currentDate.getFullYear()); + birthDate.setHours(0, 0, 0, 0); + const startBirthdayPeriod = new Date(birthDate); + startBirthdayPeriod.setDate(birthDate.getDate() - 3); + const endBirthdayPeriod = new Date(birthDate); + endBirthdayPeriod.setDate(birthDate.getDate() + 3); + + if (currentDate >= startBirthdayPeriod && currentDate <= endBirthdayPeriod) { + return; + } + throw showErrorMessage(SERVER_MESSAGE_KEYS.COUPON_WRONG_DATE); + } + } + } + private async clearCart(): Promise { await getCartModel() .clearCart() diff --git a/src/shared/constants/messages.ts b/src/shared/constants/messages.ts index 66c81264..e7356a9d 100644 --- a/src/shared/constants/messages.ts +++ b/src/shared/constants/messages.ts @@ -21,6 +21,8 @@ export const SERVER_MESSAGE: Record> ADDRESS_DELETED: 'Address has been deleted successfully', ADDRESS_STATUS_CHANGED: 'Address status has been changed successfully', BAD_REQUEST: 'Sorry, something went wrong. Try again later.', + COUPON_NEED_LOGIN: 'You must be logged in to apply this promo code', + COUPON_WRONG_DATE: 'You can only apply this promo code 3 days before and after your birthday', INCORRECT_PASSWORD: 'Please, enter a correct password', INVALID_COUPON: 'Invalid coupon', INVALID_EMAIL: "User with this email doesn't exist. Please, register first", @@ -49,6 +51,8 @@ export const SERVER_MESSAGE: Record> ADDRESS_DELETED: 'Адрес успешно удален', ADDRESS_STATUS_CHANGED: 'Статус адреса успешно изменен', BAD_REQUEST: 'Извините, что-то пошло не так. Попробуйте позже.', + COUPON_NEED_LOGIN: 'Вам нужно войти, чтобы применить этот промокод', + COUPON_WRONG_DATE: 'Вы можете применить этот промокод только за 3 дня до и после дня рождения', INCORRECT_PASSWORD: 'Пожалуйста, введите правильный пароль', INVALID_COUPON: 'Неверный купон', INVALID_EMAIL: 'Пользователь с таким адресом не существует. Пожалуйста, сначала зарегистрируйтесь', @@ -79,6 +83,8 @@ export const SERVER_MESSAGE_KEYS: Record = { ADDRESS_DELETED: 'ADDRESS_DELETED', ADDRESS_STATUS_CHANGED: 'ADDRESS_STATUS_CHANGED', BAD_REQUEST: 'BAD_REQUEST', + COUPON_NEED_LOGIN: 'COUPON_NEED_LOGIN', + COUPON_WRONG_DATE: 'COUPON_WRONG_DATE', INCORRECT_PASSWORD: 'INCORRECT_PASSWORD', INVALID_COUPON: 'INVALID_COUPON', INVALID_EMAIL: 'INVALID_EMAIL',