/* eslint-disable consistent-return */ /* eslint-disable react/no-danger */ /* eslint-disable jsx-a11y/no-static-element-interactions */ /* eslint-disable jsx-a11y/click-events-have-key-events */ /* eslint-disable unused-imports/no-unused-vars */ /* eslint-disable no-underscore-dangle */ /* eslint-disable @typescript-eslint/naming-convention */ /* eslint-disable array-callback-return */ /* eslint-disable @typescript-eslint/no-loop-func */ /* eslint-disable guard-for-in */ /* eslint-disable no-restricted-syntax */ /* eslint-disable no-plusplus */ import { format, parse } from "date-fns"; import _ from "lodash-es"; import { useRouter } from "next/navigation"; import { useMemo } from "react"; import { useMediaQuery } from "usehooks-ts"; import Form from "@/components/form/form"; import FormItem from "@/components/form/form-item"; import PolicyCheck from "@/components/widget/policy-check/policy-check"; import type { ITravelPlannerForm } from "@/features/travel-planner/components/travel-planner/travel-planner-form"; import { localeStorageHelper } from "@/libs/helpers/locale-storage-helper"; import type { IHotelAddToCartResponse } from "@/services/eva-display/hotel/hotel-add-to-cart/hotel-add-to-card-services.types"; import { useHotelBookingRequestMutation } from "@/services/eva-display/hotel/hotel-booking-request/hotel-booking-request.service"; import { useHotelValidateMutation } from "@/services/eva-display/hotel/hotel-validate/hotel-validate.service"; import type { IBookingPassenger } from "@/services/eva-display/hotel/hotel-validate/hotel-validate-service.types"; import { useUserDepartmentQuery } from "@/services/eva-display/user-department/user-department.service"; import { useNotifyStore } from "@/store/notify-store/use-notify-store"; import { requiredRule } from "@/validations/form-rules/rules/required-rule"; import HotelSummaryReservationDetail from "./components/hotel-summary-reservation-detail"; import HotelSummaryRoom from "./components/hotel-summary-room"; interface HotelSummaryContentProps { data: IHotelAddToCartResponse; customers: ITravelPlannerForm["customer"]; rooms: ITravelPlannerForm["room"]; searchId: string; productKey: string; travelReasonId: number; } // interface IEmployeeCustomer { // id: string; // } // interface IGuestCustomer { // id: string; // gender: string; // name: string; // surname: string; // phone: string; // email: string; // tc: string; // birthDate: string; // key: string; // } const HotelSummaryContent = (props: HotelSummaryContentProps) => { const router = useRouter(); const isDesktop = useMediaQuery("(min-width: 768px)"); const useDepartmentQuery = useUserDepartmentQuery(); const validateMutation = useHotelValidateMutation(); const { setNotify } = useNotifyStore(); const bookingRequestMutation = useHotelBookingRequestMutation({ onSuccess(data) { if (!data.success) { setNotify({ title: "Bir Hata Oluştu!", message: data.errors[0]?.message ?? "Bir hata oluştu", variant: "error", }); return; } router.push( `/booking-request-success?bookingId=${data.result.requestId}`, ); }, onError() {}, }); const getDefaultValues = () => { const defaultFormValues: any = {}; let customerTurnIndex = 0; props.rooms.forEach((room, roomIndex) => { const customerCount = room.employee?.count ?? 0; Array.from(Array(customerCount), (_customerItem, customerIndex) => { defaultFormValues[`${roomIndex}-${customerIndex}-employee`] = props.customers[customerTurnIndex]?.value; customerTurnIndex++; return null; }); }); return defaultFormValues; }; const defaultValues = useMemo(getDefaultValues, [ props.customers, props.rooms, ]); const DEFAULT_COUNTRY_VALUE = { Id: 277, ParentId: null, ParentRegion: null, Label: "", }; function formatBirthDate(birthDate: string) { const parsedTarih = parse(birthDate, "dd.MM.yyyy", new Date()); return format(parsedTarih, "yyyy-MM-dd'T'HH:mm:ss.SSS"); } function getPassengerFromRoomUser(roomPassenger: any, index: number) { let _phoneNumber: string = roomPassenger.phone?.replace(/\s/g, ""); if (_phoneNumber && !_phoneNumber.startsWith("90")) { _phoneNumber = `90${_phoneNumber}`; } return { Id: 0, IsMaster: index === 0, Address: { AddressName: "", Street: "", City: "", Country: DEFAULT_COUNTRY_VALUE, PostalCode: "", }, Email: roomPassenger.email ?? "mail@com.com", Prefix: "", Suffix: "", PhoneNumbers: [_phoneNumber ?? "944444444444"], Country: DEFAULT_COUNTRY_VALUE, LoyaltyCode: "", TcKimlikNo: roomPassenger.tc ?? "30390364086", // "30390364086", SpecialServiceRequests: [], ZipCode: "", SecretCustomer: null, PassportTypeId: 0, MernisQueryStatus: 0, Key: roomPassenger.psKey ?? "Key", Name: roomPassenger.name ?? "", Surname: roomPassenger.surname ?? "", Age: 0, PassengerType: +roomPassenger.passengerType ?? 0, Birthday: roomPassenger.birthDate ? formatBirthDate(roomPassenger.birthDate) : "1990-01-01T00:00:00.000", Gender: !roomPassenger.gender ? 1 : +roomPassenger.gender, CrmCode: "", MilesCode: "", HesCode: "", PassportNumber: "", }; } const handleSubmit = async (values: any) => { const roomsPassengers: any[] = []; const passengersListStoraged = localeStorageHelper.get("passengers"); if (!passengersListStoraged) return null; const passengerList = passengersListStoraged.passengers; const { searchProductKey } = passengersListStoraged; for (const _roomItem in props.rooms) { Object.entries(values).map(([formKey, formValue]) => { const formKeySplitted = formKey.split("-"); const roomIndex = formKeySplitted[0]; const customerIndex = formKeySplitted[1]; const customerType = formKeySplitted[2]; const customerField = formKeySplitted[3]; const customerFieldValue = formValue; if (roomIndex && customerIndex && customerType) { if (["guest", "child"].includes(customerType) && customerField) { const customer = { ...roomsPassengers[+roomIndex]?.[customerIndex], [customerField]: customerFieldValue, psKey: passengerList[+customerIndex].key, customerType, }; if (!roomsPassengers[+roomIndex]) { roomsPassengers[+roomIndex] = [customer]; } else { roomsPassengers[+roomIndex][customerIndex] = customer; } } else if (customerType === "employee") { const customer = { id: customerFieldValue, psKey: passengerList[+customerIndex].key, customerType: "employee", }; if (!roomsPassengers[+roomIndex]) { roomsPassengers[+roomIndex] = [customer]; } else { roomsPassengers[+roomIndex][customerIndex] = customer; } } } }); } const formPassengers = _.flattenDepth(roomsPassengers, 1).flatMap( (roomsPassenger) => { if ( roomsPassenger.customerType === "guest" || roomsPassenger.customerType === "child" ) { return { ...roomsPassenger, passengerType: roomsPassenger.customerType === "guest" ? 0 : 1, }; } const userItem = useDepartmentQuery.data?.find( (x) => x.value === roomsPassenger.id, ); if (userItem) { return { phone: userItem.properties.phoneNumber, email: userItem.properties.email, psKey: roomsPassenger.psKey, name: userItem.name.split(" ").slice(0, -1).join(" "), surname: userItem.name.split(" ").pop(), passengerType: 0, }; } return {}; }, ); const passengers: IBookingPassenger[] = formPassengers.map( (formPassengerItem, formPassengerIndex) => getPassengerFromRoomUser(formPassengerItem, formPassengerIndex), ); // const billingPassenger = passengers[0]!; bookingRequestMutation.mutate({ CorpCreateHotelRequest: { SearchProductKey: searchProductKey, SearchId: props.searchId, CartId: props.data.cartId, DetailProductKey: props.productKey, isPreBook: true, KvkkStatus: true, PassengerInformations: passengers as any, LinkId: "", TravelReasonId: props.travelReasonId, RequesterUserIds: ["00000000-0000-0000-0000-000000000000"], RequestId: null, }, }); return true; }; return (
); }; export default HotelSummaryContent;