Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(RSS-ECOMM-5_93): birthday check #363

Merged
merged 1 commit into from
Jun 12, 2024
Merged
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
30 changes: 30 additions & 0 deletions src/pages/CartPage/model/CartPageModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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: 'Скидка на продукты' },
Expand Down Expand Up @@ -49,6 +51,9 @@ class CartPageModel implements Page {

private async addDiscountHandler(discountCode: string): Promise<void> {
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()
Expand Down Expand Up @@ -94,6 +99,31 @@ class CartPageModel implements Page {
this.view.updateTotal(this.cart);
}

private async checkBirthday(): Promise<void> {
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<void> {
await getCartModel()
.clearCart()
Expand Down
6 changes: 6 additions & 0 deletions src/shared/constants/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const SERVER_MESSAGE: Record<LanguageChoiceType, Record<string, string>>
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",
Expand Down Expand Up @@ -49,6 +51,8 @@ export const SERVER_MESSAGE: Record<LanguageChoiceType, Record<string, string>>
ADDRESS_DELETED: 'Адрес успешно удален',
ADDRESS_STATUS_CHANGED: 'Статус адреса успешно изменен',
BAD_REQUEST: 'Извините, что-то пошло не так. Попробуйте позже.',
COUPON_NEED_LOGIN: 'Вам нужно войти, чтобы применить этот промокод',
COUPON_WRONG_DATE: 'Вы можете применить этот промокод только за 3 дня до и после дня рождения',
INCORRECT_PASSWORD: 'Пожалуйста, введите правильный пароль',
INVALID_COUPON: 'Неверный купон',
INVALID_EMAIL: 'Пользователь с таким адресом не существует. Пожалуйста, сначала зарегистрируйтесь',
Expand Down Expand Up @@ -79,6 +83,8 @@ export const SERVER_MESSAGE_KEYS: Record<string, string> = {
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',
Expand Down
Loading