Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
8.7 kB
2
Indexable
Never
import { Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { firstValueFrom } from 'rxjs';
import { EndpointsEnum } from './endpoints.enum';
import { IEventsByDateRangeCriteria, IEventsByCompetitorCriteria, IEventsByTournamentCriteria } from 'src/events/interfaces/events-criteria.interface';
import { IEvents365Response } from 'src/events/interfaces/sports-events-365-response.interface';
import { IOrderTicketsDetails, IOrdersStatuses } from 'src/events/interfaces/order.interface';
import { IEvents365OrderResponse } from 'src/events/interfaces/order-confirmation.interface';

@Injectable()
export class SportsEvents365Service {
    private baseApiUrl: string = 'https://api.sportsevents365.com';
    private sitekey: string = '0d244ef567f5410aa2ad0cd8ccc67a7e';
    private user: string = 'RAZ';
    private password: string = '2^APqH766*5H';
    private responseMethod: string = 'json';

    constructor(private readonly httpService: HttpService) {
    }

    public async orderTickets(orderTicketsDetails: IOrderTicketsDetails): Promise<IEvents365OrderResponse> {
        try {
            const response = await firstValueFrom(this.httpService.post(
                `${this.baseApiUrl}/${EndpointsEnum.BOOKING}/`,
                orderTicketsDetails,
                {
                    params: {
                      sitekey: this.sitekey
                    }
                }
            ));
            return response.data;
        }
        catch(err) {
            console.error(err);
        }
    }
    
    public async getOrdersStatuses(ordersStatuses: IOrdersStatuses): Promise<any> {
        const { fnc } = ordersStatuses || {};
        try {
            const response = await firstValueFrom(this.httpService.post(
                `${this.baseApiUrl}/${EndpointsEnum.BOOKING}/`,
                { 
                    // source: this.user,
                    // site_key: this.sitekey,
                    // headers: { },
                    // // auth: {
                    // //     username: this.user,
                    // //     password: this.password
                    // // },
                    // params: { 
                    //     // res_method: this.responseMethod,
                    //     source: this.user,
                    //     site_key: this.sitekey,
                        // fnc: fnc
                    // }
                },
                {
                    params: {
                        source: this.user,
                        site_key: this.sitekey
                      }
                }
            ));
            return response.data;
        }
        catch(err) {
            console.error(err);
        }
      }  

    public async searchEventsByDateRange(eventsCriteria: IEventsByDateRangeCriteria): Promise<IEvents365Response> {
        const { country, city, fromDate, toDate, sportId, page } = eventsCriteria || {};
        
        try {
            const response = await firstValueFrom(this.httpService.get(
                `${this.baseApiUrl}/${EndpointsEnum.SEARCH}/`,
                { 
                    headers: { },
                    auth: {
                        username: this.user,
                        password: this.password
                    },
                    params: { 
                        q: `lq,${country},${city},${sportId},CUSTOM,${fromDate},${toDate}`,
                        res_method: this.responseMethod,
                        page, sitekey: this.sitekey
                    }
                },
            ));
            return response.data;
        }
        catch(err) {
            console.error(err);
        }
      }  

      public async searchEventsByCompetitor(competitorId: number, eventsCriteria: IEventsByCompetitorCriteria): Promise<IEvents365Response> {
        const { fromDate, toDate, sportId, page } = eventsCriteria || {};
        
        try {
            const response = await firstValueFrom(this.httpService.get(
                `${this.baseApiUrl}/${EndpointsEnum.SEARCH}/`,
                { 
                    headers: { },
                    auth: {
                        username: this.user,
                        password: this.password
                    },
                    params: { 
                        q: `cq,${competitorId},${sportId},CUSTOM,${fromDate},${toDate}`,
                        res_method: this.responseMethod,
                        page, sitekey: this.sitekey
                    }
                },
            ));
            return response.data;
        }
        catch(err) {
            console.error(err);
        }
      }  

      public async searchEventsByTournament(tournamentId: number, eventsCriteria: IEventsByTournamentCriteria): Promise<IEvents365Response> {
        const { fromDate, toDate, page } = eventsCriteria || {};
        
        try {
            const response = await firstValueFrom(this.httpService.get(
                `${this.baseApiUrl}/${EndpointsEnum.SEARCH}/`,
                { 
                    headers: { },
                    auth: {
                        username: this.user,
                        password: this.password
                    },
                    params: { 
                        q: `tq,${tournamentId},CUSTOM,${fromDate},${toDate}`,
                        res_method: this.responseMethod,
                        page, sitekey: this.sitekey
                    }
                },
            ));
            return response.data;
        }
        catch(err) {
            console.error(err);
        }
      }  

      public async searchEventsByID(eventID: number): Promise<any> {        
        try {
            const response = await firstValueFrom(this.httpService.get(
                `${this.baseApiUrl}/${EndpointsEnum.SEARCH}/`,
                { 
                    headers: { },
                    auth: {
                        username: this.user,
                        password: this.password
                    },
                    params: { 
                        q: `eq,${eventID}`,
                        res_method: this.responseMethod,
                        sitekey: this.sitekey
                    }
                },
            ));
            return response.data;
        }
        catch(err) {
            console.error(err);
        }
      }  

      public async getSports(): Promise<any> {
        try {
            const response = await firstValueFrom(this.httpService.get(
                `${this.baseApiUrl}/${EndpointsEnum.SEARCH}/`,
                { 
                    headers: { },
                    auth: {
                        username: this.user,
                        password: this.password
                    },
                    params: { 
                        q: "getSports",
                        res_method: this.responseMethod,
                        sitekey: this.sitekey  
                    }
                },
            ));
            return response.data;
        }
        catch(err) {
            console.error(err);
        }
      }

      public async getCountries(): Promise<any> {
        try {
            const response = await firstValueFrom(this.httpService.get(
                `${this.baseApiUrl}/${EndpointsEnum.SEARCH}/`,
                { 
                    headers: { },
                    auth: {
                        username: this.user,
                        password: this.password
                    },
                    params: { 
                        q: "getCountries",
                        res_method: this.responseMethod,
                        sitekey: this.sitekey  
                    }
                },
            ));
            return response.data;
        }
        catch(err) {
            console.error(err);
        }
      }

      public async getCities(countryid: number): Promise<any> {
        try {
            const response = await firstValueFrom(this.httpService.get(
                `${this.baseApiUrl}/${EndpointsEnum.SEARCH}/`,
                { 
                    headers: { },
                    auth: {
                        username: this.user,
                        password: this.password
                    },
                    params: { 
                        q: "getCities",
                        countryid: countryid,
                        res_method: this.responseMethod,
                        sitekey: this.sitekey  
                    }
                },
            ));
            return response.data;
        }
        catch(err) {
            console.error(err);
        }
      }  
}