File size: 5,039 Bytes
9a92a42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { Request, Response } from 'express';
import axios from 'axios';

const SACHET_BASE_URL = 'https://sachet.ndma.gov.in/cap_public_website';

/**
 * Proxy endpoint for fetching earthquake alerts from SACHET API
 */
export const fetchEarthquakeAlerts = async (req: Request, res: Response): Promise<Response> => {
    try {
        const response = await axios.get(`${SACHET_BASE_URL}/FetchEarthquakeAlerts`, {
            timeout: 10000,
            headers: {
                'Accept': 'application/json',
                'User-Agent': 'Prashikshak-Backend/1.0'
            }
        });

        return res.status(200).json({
            success: true,
            data: response.data
        });
    } catch (error: any) {
        console.error('Error fetching earthquake alerts:', error.message);
        return res.status(error.response?.status || 500).json({
            success: false,
            message: 'Failed to fetch earthquake alerts',
            error: error.message
        });
    }
};

/**
 * Proxy endpoint for fetching IMD Nowcast alerts from SACHET API
 */
export const fetchNowcastAlerts = async (req: Request, res: Response): Promise<Response> => {
    try {
        const response = await axios.get(`${SACHET_BASE_URL}/FetchIMDNowcastAlerts`, {
            timeout: 10000,
            headers: {
                'Accept': 'application/json',
                'User-Agent': 'Prashikshak-Backend/1.0'
            }
        });

        return res.status(200).json({
            success: true,
            data: response.data
        });
    } catch (error: any) {
        console.error('Error fetching nowcast alerts:', error.message);
        return res.status(error.response?.status || 500).json({
            success: false,
            message: 'Failed to fetch nowcast alerts',
            error: error.message
        });
    }
};

/**
 * Proxy endpoint for fetching all alert details from SACHET API
 */
export const fetchAllAlertDetails = async (req: Request, res: Response): Promise<Response> => {
    try {
        const response = await axios.get(`${SACHET_BASE_URL}/FetchAllAlertDetails`, {
            timeout: 10000,
            headers: {
                'Accept': 'application/json',
                'User-Agent': 'Prashikshak-Backend/1.0'
            }
        });

        return res.status(200).json({
            success: true,
            data: response.data
        });
    } catch (error: any) {
        console.error('Error fetching all alert details:', error.message);
        return res.status(error.response?.status || 500).json({
            success: false,
            message: 'Failed to fetch all alert details',
            error: error.message
        });
    }
};

/**
 * Proxy endpoint for fetching statewise filter data from SACHET API
 */
export const fetchStatewiseFilter = async (req: Request, res: Response): Promise<Response> => {
    try {
        const response = await axios.get(`${SACHET_BASE_URL}/../locales/en/statewiseFilter.json`, {
            timeout: 10000,
            headers: {
                'Accept': 'application/json',
                'User-Agent': 'Prashikshak-Backend/1.0'
            }
        });

        return res.status(200).json({
            success: true,
            data: response.data
        });
    } catch (error: any) {
        console.error('Error fetching statewise filter:', error.message);
        return res.status(error.response?.status || 500).json({
            success: false,
            message: 'Failed to fetch statewise filter',
            error: error.message
        });
    }
};

/**
 * Fetch all alerts in a single request (optimized endpoint)
 */
export const fetchAllAlerts = async (req: Request, res: Response): Promise<Response> => {
    try {
        const [earthquakeRes, nowcastRes, generalRes] = await Promise.all([
            axios.get(`${SACHET_BASE_URL}/FetchEarthquakeAlerts`, {
                timeout: 10000,
                headers: { 'Accept': 'application/json', 'User-Agent': 'Prashikshak-Backend/1.0' }
            }),
            axios.get(`${SACHET_BASE_URL}/FetchIMDNowcastAlerts`, {
                timeout: 10000,
                headers: { 'Accept': 'application/json', 'User-Agent': 'Prashikshak-Backend/1.0' }
            }),
            axios.get(`${SACHET_BASE_URL}/FetchAllAlertDetails`, {
                timeout: 10000,
                headers: { 'Accept': 'application/json', 'User-Agent': 'Prashikshak-Backend/1.0' }
            })
        ]);

        return res.status(200).json({
            success: true,
            data: {
                earthquakes: earthquakeRes.data,
                nowcast: nowcastRes.data,
                general: generalRes.data
            }
        });
    } catch (error: any) {
        console.error('Error fetching all alerts:', error.message);
        return res.status(error.response?.status || 500).json({
            success: false,
            message: 'Failed to fetch all alerts',
            error: error.message
        });
    }
};