Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
12 kB
2
Indexable
Never
const multer = require('multer');
const { GetAllSettings } = require('../services/appSettings-services');
const { Exclude } = require('../utils');
const { GetHighlights, GetHighlightTypes } = require('../services/highlight-service');
const { GetNewsWeb, GetNewsTypes, GetSingleNews } = require('../services/news-service');
const { GetSingleMatch, GetAllMatchWeb, GetSingleMatchWeb } = require('../services/match-service');
const { GetAllTeams, GetAllPlayers, GetAllLeagues } = require('../services/popular-services');
const { verifyApiKey } = require('./middleware/userAuth');
const { GetGeneralSettingWeb } = require('../services/general-settings-service');
const IP = require('ip');
const uploadForm = multer();

module.exports = app => {
    // Get all settings
    app.post('/api/v1/settings', uploadForm.none(), verifyApiKey, async (req, res, next) => {
        try {
            const platform = req.body.platform;
            const publicIP =
                req.headers['cf-connecting-ip'] ||
                req.headers['x-real-ip'] ||
                req.headers['x-forwarded-for'] ||
                req.socket.remoteAddress ||
                '';

            const data = await GetAllSettings();

            if (!platform) {
                // No platform provided, return all settings data
                res.json({
                    status: true,
                    ip: publicIP,
                    data: data
                });
            } else if (platform.toLowerCase() === 'ios') {
                // Platform is ios, exclude android_settings
                const settingData = Exclude(data, ['android_settings']);

                const iosSettings = { settings: settingData.ios_settings, ...settingData };
                delete iosSettings.ios_settings;

                res.json({
                    status: true,
                    ip: publicIP,
                    message: 'Settings fetched successfully for Ios',
                    data: iosSettings
                });
            } else if (platform.toLowerCase() === 'android') {
                // Platform is android, exclude ios_settings
                const settingData = Exclude(data, ['ios_settings']);

                const androidSettings = { settings: settingData.android_settings, ...settingData };
                delete androidSettings.android_settings;

                res.json({
                    status: true,
                    ip: publicIP,
                    message: 'Settings fetched successfully for Android',
                    data: androidSettings
                });
            } else {
                // Invalid platform provided
                res.json({
                    status: false,
                    ip: publicIP,
                    message: 'Invalid platform key',
                    data: null
                });
            }
        } catch (error) {
            res.status(500).json({ status: false, message: 'Something went wrong or invalid request, Try again', error: error });
        }
    });

    // Get general settings for client site
    app.post('/api/v1/general_settings', uploadForm.none(), verifyApiKey, async (req, res, next) => {
        try {
            const data = await GetGeneralSettingWeb();
            res.json({
                status: true,
                message: 'General settings fetched successfully',
                data: data
            });
        } catch (error) {
            res.status(500).json({ status: false, message: 'Something went wrong or invalid request, Try again' });
        }
    });

    //Get all highlights
    app.post('/api/v1/highlights', uploadForm.none(), verifyApiKey, async (req, res, next) => {
        try {
            const data = await GetHighlights();
            res.json({
                status: true,
                message: 'Highlights fetched successfully',
                data: data
            });
        } catch (error) {
            res.status(500).json({ status: false, message: 'Something went wrong or invalid request, Try again', error: error });
        }
    });

    // Get all highlight types
    app.post('/api/v1/highlight_types', uploadForm.none(), verifyApiKey, async (req, res, next) => {
        try {
            const data = await GetHighlightTypes();
            const sortedArray = data.sort((a, b) => {
                return a.position - b.position;
            });
            res.json({
                status: true,
                message: 'Highlight types fetched successfully',
                data: sortedArray
            });
        } catch (error) {
            res.status(500).json({ status: false, message: 'Something went wrong or invalid request, Try again', error: error });
        }
    });

    // Get all news
    app.post('/api/v1/all_news', uploadForm.none(), verifyApiKey, async (req, res, next) => {
        try {
            const data = await GetNewsWeb();
            res.json({
                status: true,
                message: 'News fetched successfully',
                data: data
            });
        } catch (error) {
            res.status(500).json({ status: false, message: 'Something went wrong or invalid request, Try again', error: error });
        }
    });

    //Get single news
    app.post('/api/v1/news/:newsId', uploadForm.none(), verifyApiKey, async (req, res, next) => {
        try {
            const id = req.params.newsId;
            const data = await GetSingleNews(id);
            res.json({
                status: true,
                message: 'News fetched successfully',
                data: data
            });
        } catch (error) {
            res.status(500).json({ status: false, message: 'Something went wrong or invalid request, Try again', error: error });
        }
    });

    // Get all news type
    app.post('/api/v1/news_types', uploadForm.none(), verifyApiKey, async (req, res, next) => {
        try {
            const data = await GetNewsTypes();
            const sortedArray = data.sort((a, b) => {
                return a.position - b.position;
            });
            res.json({
                status: true,
                message: 'News types fetched successfully',
                data: sortedArray
            });
        } catch (error) {
            res.status(500).json({ status: false, message: 'Something went wrong or invalid request, Try again', error: error });
        }
    });

    //Get all matches
    app.post('/api/v1/matches', uploadForm.none(), verifyApiKey, async (req, res, next) => {
        try {
            const data = await GetAllMatchWeb();
            res.json({
                message: 'All matches fetched successfully',
                data: data
            });
        } catch (error) {
            res.status(500).json({ status: false, message: 'Something went wrong or invalid request, Try again', error: error });
        }
    });

    //Get a single match
    app.post('/api/v1/match/:matchId', uploadForm.none(), verifyApiKey, async (req, res) => {
        try {
            const platform = req.body.platform;
            const id = req.params.matchId;

            const publicIP =
                req.headers['cf-connecting-ip'] ||
                req.headers['x-real-ip'] ||
                req.headers['x-forwarded-for'] ||
                req.socket.remoteAddress ||
                '';

            const data = await GetSingleMatchWeb({ id, publicIP });
            if (!platform) {
                // No platform provided
                res.json({
                    status: true,
                    message: 'Match fetched successfully',
                    data: data
                });
            } else if (platform.toLowerCase() === 'ios') {
                // Platform is ios, filter ios and both
                let filteredIosStreamingSources = data?.streaming_sources.filter(
                    source => source?.platform === 'ios' || source?.platform === 'both'
                );
                let iosData = {
                    ...data,
                    streaming_sources: filteredIosStreamingSources
                };
                res.json({
                    status: true,
                    message: 'Match fetched with ios platform successfully',
                    data: iosData
                });
            } else if (platform.toLowerCase() === 'android') {
                // Platform is android, filter android and both
                let filteredAndStreamingSources = data?.streaming_sources.filter(
                    source => source?.platform === 'android' || source?.platform === 'both'
                );
                let androidData = {
                    ...data,
                    streaming_sources: filteredAndStreamingSources
                };
                res.json({
                    status: true,
                    message: 'Match fetched with android successfully',
                    data: androidData
                });
            } else {
                // Invalid platform provided
                res.json({
                    status: false,
                    message: 'Invalid platform key',
                    data: null
                });
            }
        } catch (error) {
            res.status(500).json({ status: false, message: 'Something went wrong or invalid request, Try again', error: error });
        }
    });

    //Get all teams
    app.post('/api/v1/popular_teams', uploadForm.none(), verifyApiKey, async (req, res, next) => {
        try {
            const data = await GetAllTeams();

            const sortedArray = data.sort((a, b) => {
                return a.position - b.position;
            });
            res.json({
                status: true,
                message: 'All teams fetched successfully',
                data: sortedArray
            });
        } catch (error) {
            res.status(500).json({ status: false, message: 'Something went wrong or invalid request, Try again', error: error });
            next(error);
        }
    });

    //Get all players
    app.post('/api/v1/popular_players', uploadForm.none(), verifyApiKey, async (req, res, next) => {
        try {
            const data = await GetAllPlayers();
            const sortedArray = data.sort((a, b) => {
                return a.position - b.position;
            });
            res.json({
                status: true,
                message: 'All players fetched successfully',
                data: sortedArray
            });
        } catch (error) {
            res.status(500).json({ status: false, message: 'Something went wrong or invalid request, Try again', error: error });
            next(error);
        }
    });

    //Get all leagues
    app.post('/api/v1/popular_leagues', uploadForm.none(), verifyApiKey, async (req, res, next) => {
        try {
            const data = await GetAllLeagues();
            const sortedArray = data.sort((a, b) => {
                return a.position - b.position;
            });
            res.json({
                status: true,
                message: 'All leagues fetched successfully',
                data: sortedArray
            });
        } catch (error) {
            res.status(500).json({ status: false, message: 'Something went wrong or invalid request, Try again', error: error });
            next(error);
        }
    });
};