|
| 1 | +/** |
| 2 | + * Alerts API |
| 3 | + * Notification and subscription management |
| 4 | + */ |
| 5 | + |
| 6 | +import { get, post, del, ENDPOINTS } from './client'; |
| 7 | +import type { |
| 8 | + ApiResponse, |
| 9 | + GeneralAlert, |
| 10 | + AlertFilterParams, |
| 11 | + Department, |
| 12 | + Subscription, |
| 13 | +} from '../types/api'; |
| 14 | +import { getAlertsFromRSS } from './external/rss-parser'; |
| 15 | +import { getCareerAlertsFromHTML } from './external/html-parser'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Get filtered alerts by category |
| 19 | + * Fetch alerts with optional category filter |
| 20 | + * Falls back to external sources (RSS/HTML) for categories that fail via API |
| 21 | + */ |
| 22 | +export async function getAlerts( |
| 23 | + params?: AlertFilterParams |
| 24 | +): Promise<ApiResponse<GeneralAlert[]>> { |
| 25 | + try { |
| 26 | + // If specific category is requested, try API first |
| 27 | + if (params?.category) { |
| 28 | + const result = await get<GeneralAlert[]>(ENDPOINTS.ALERTS.BASE, { params }); |
| 29 | + |
| 30 | + // If API succeeded, return the result |
| 31 | + if (result.success && result.status === 200) { |
| 32 | + return result; |
| 33 | + } |
| 34 | + |
| 35 | + // If API failed for this category, fallback to external sources |
| 36 | + console.warn(`API failed for category ${params.category}, falling back to external sources`); |
| 37 | + |
| 38 | + const [rssAlerts, careerAlerts] = await Promise.all([ |
| 39 | + getAlertsFromRSS(), |
| 40 | + getCareerAlertsFromHTML(6000), |
| 41 | + ]); |
| 42 | + |
| 43 | + const allAlerts = [...rssAlerts, ...careerAlerts]; |
| 44 | + const filteredAlerts = allAlerts.filter(alert => alert.category === params.category); |
| 45 | + |
| 46 | + return { |
| 47 | + success: true, |
| 48 | + data: filteredAlerts, |
| 49 | + status: 200, |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + // If no category specified, try API first for all categories |
| 54 | + const result = await get<GeneralAlert[]>(ENDPOINTS.ALERTS.BASE); |
| 55 | + |
| 56 | + // If API succeeded, return the result |
| 57 | + if (result.success && result.status === 200) { |
| 58 | + return result; |
| 59 | + } |
| 60 | + |
| 61 | + // If API failed, fallback to external sources for all categories |
| 62 | + console.warn("API failed, falling back to external sources"); |
| 63 | + |
| 64 | + const [rssAlerts, careerAlerts] = await Promise.all([ |
| 65 | + getAlertsFromRSS(), |
| 66 | + getCareerAlertsFromHTML(6000), |
| 67 | + ]); |
| 68 | + |
| 69 | + const allAlerts = [...rssAlerts, ...careerAlerts]; |
| 70 | + |
| 71 | + return { |
| 72 | + success: true, |
| 73 | + data: allAlerts, |
| 74 | + status: 200, |
| 75 | + }; |
| 76 | + } catch (error) { |
| 77 | + // If API and external sources fail, return error |
| 78 | + console.error("API and external sources failed:", error); |
| 79 | + return { |
| 80 | + success: false, |
| 81 | + error: { |
| 82 | + code: "FETCH_FAILED", |
| 83 | + message: "공지사항을 불러오는데 실패했습니다.", |
| 84 | + }, |
| 85 | + }; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Get my alerts |
| 91 | + * Fetch alerts from subscribed departments |
| 92 | + */ |
| 93 | +export async function getMyAlerts(): Promise<ApiResponse<GeneralAlert[]>> { |
| 94 | + return get<GeneralAlert[]>(ENDPOINTS.ALERTS.MY); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Get all available departments for subscription |
| 99 | + */ |
| 100 | +export async function getSubscriptions(): Promise<ApiResponse<Department[]>> { |
| 101 | + return get<Department[]>(ENDPOINTS.ALERTS.SUBSCRIPTION); |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Get my subscribed departments |
| 106 | + */ |
| 107 | +export async function getMySubscriptions(): Promise<ApiResponse<Subscription[]>> { |
| 108 | + return get<Subscription[]>(ENDPOINTS.ALERTS.MY_SUBSCRIPTION); |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Subscribe to a department |
| 113 | + * Start receiving alerts from the department |
| 114 | + */ |
| 115 | +export async function subscribeDepartment( |
| 116 | + departmentId: number |
| 117 | +): Promise<ApiResponse<Subscription>> { |
| 118 | + return post<Subscription>(ENDPOINTS.ALERTS.SUBSCRIBE(departmentId)); |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Unsubscribe from a department |
| 123 | + * Stop receiving alerts from the department |
| 124 | + */ |
| 125 | +export async function unsubscribeDepartment( |
| 126 | + departmentId: number |
| 127 | +): Promise<ApiResponse<{ message: string }>> { |
| 128 | + return del<{ message: string }>(ENDPOINTS.ALERTS.UNSUBSCRIBE(departmentId)); |
| 129 | +} |
0 commit comments