Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.0 kB
2
Indexable
Never
import { IEventInfo, IEventsInfo, ITicketOffer } from '../interfaces/event.interface';
import { IEvents365Response, IEventResponse, ITicketResponse } from '../interfaces/sports-events-365-response.interface';

export namespace EventsUtils {

    export function organizeEvents(events: IEvents365Response): IEventsInfo {
        const { page, perpage, totalpages, totalrecords } = events.all.control || {};
        const metadata = { page, perpage, totalpages, totalrecords };
        const organizedEvents: any = events?.all?.data?.map((eventResponse: IEventResponse) => {
          return organizeSingleEvent(eventResponse);
        });
        return { metadata, events: organizedEvents };
      }
    
      export function organizeSingleEvent(eventData: IEventResponse): IEventInfo {
        const { id, caption, home_team_caption, home_team_id, away_team_caption, away_team_id, sport, sportid, tournament, tournamentid, date, time_of_event: time, final_date, final_time, dateTime_boundries, venue, venue_img, venue_address, city, cityid, country, countryid } = eventData || {};
        const dateInfo = { date, time, final_date, final_time, dateTime_boundries };
        const sportInfo = { name: sport, id: sportid };
        const tournamentInfo = { name: tournament, id: tournamentid };
        const homeTeamInfo = { name: home_team_caption, id: home_team_id };
        const awayTeamInfo = { name: away_team_caption, id: away_team_id };
        const locationInfo = parseLocationInfo(country, countryid, city, cityid, venue, venue_img, venue_address);
        const organizedTicketsData: ITicketOffer[] = eventData.ticketdata.map((ticketResponse: ITicketResponse) => {
          return organizeEventTicket(ticketResponse);
        });        
        const organizedSingleEvent: IEventInfo = { id, caption, home_team: homeTeamInfo, away_team: awayTeamInfo, sport: sportInfo, tournament: tournamentInfo, date: dateInfo, location: locationInfo, tickets: organizedTicketsData };
        return organizedSingleEvent;
      }

      function organizeEventTicket(ticketResponse: ITicketResponse): ITicketOffer {
        const { ItemID, Section: full_section_name, splittedCategoryName, Price: price, Currency: currency, max_qty, available_selling_quantities, Tags, stock_limit, stock_qty, purchase_alert, immediate_confirmation, shipping_methods, required_purchase_fields } = ticketResponse || {};
        const section = { full_section_name, mainSection: splittedCategoryName.main, locationInSection: splittedCategoryName.secondary, packageDescription: splittedCategoryName.packageDetails };
        const priceInfo = { price, currency };
        const ticketsConstraints = { max_qty, available_selling_quantities, stock_limit, stock_qty, purchase_alert }
        return { ItemID, section, priceInfo, ticketsConstraints, immediate_confirmation, required_purchase_fields, shipping_methods };
      }

      export function filterTicketsForEvent(ticketOffers: ITicketOffer[], numberOfTickets: number): ITicketOffer[] {
        const filteredTicketOffers = [];
        const alreadyAddedSections = [];
        for (let ticketOffer of ticketOffers) {
            if(!alreadyAddedSections.includes(ticketOffer.section.full_section_name) && ticketOffer.ticketsConstraints.available_selling_quantities.includes(numberOfTickets)) {
                filteredTicketOffers.push(ticketOffer);
                alreadyAddedSections.push(ticketOffer.section.full_section_name);
            }
        }
        return filteredTicketOffers;
      }
    
      function parseLocationInfo(country: string, countryid: string, city: string, cityid: string, venue: string, venue_img: string, venue_address: string) {
        const countryInfo = { name: country, id: countryid };
        const cityInfo = { name: city, id: cityid };
        const venueInfo = { venue, img: venue_img, address: venue_address };
        return { country: countryInfo, city: cityInfo, venue: venueInfo };
    }