import { Injectable } from "@angular/core"; import type { Appeal, Commission, Contact, ErrorResponse, Event, Meeting, Mentorship, SuccessResponse, Staff, DataResponse } from "@repo/types"; /** * */ @Injectable({ providedIn: "root", }) export class ApiService { public url = "https://forms-api.nhcarrigan.com"; /** * */ constructor() { } /** * @param token */ public async validateToken(token: string | null): Promise { if (!token) { return false; } const ipRequest = await fetch("https://api.ipify.org?format=json"); const ipRes = await ipRequest.json(); const { ip } = ipRes; const request = await fetch(`${this.url}/validate-token`, { body: JSON.stringify({ ip, token }), headers: { "Content-type": "application/json", }, method: "POST", }); const response = await request.json(); return response.valid; } /** * @param appeal */ public async submitAppeal(appeal: Partial): Promise { const request = await fetch(`${this.url}/submit/appeals`, { body: JSON.stringify(appeal), headers: { "Content-type": "application/json", }, method: "POST", }); const response = await request.json(); return response; } /** * @param commission */ public async submitCommission(commission: Partial): Promise { const request = await fetch(`${this.url}/submit/commissions`, { body: JSON.stringify(commission), headers: { "Content-type": "application/json", }, method: "POST", }); const response = await request.json(); return response; } /** * @param contact */ public async submitContact(contact: Partial): Promise { const request = await fetch(`${this.url}/submit/contacts`, { body: JSON.stringify(contact), headers: { "Content-type": "application/json", }, method: "POST", }); const response = await request.json(); return response; } /** * @param event */ public async submitEvent(event: Partial): Promise { const request = await fetch(`${this.url}/submit/events`, { body: JSON.stringify(event), headers: { "Content-type": "application/json", }, method: "POST", }); const response = await request.json(); return response; } /** * @param meeting */ public async submitMeeting(meeting: Partial): Promise { const request = await fetch(`${this.url}/submit/meetings`, { body: JSON.stringify(meeting), headers: { "Content-type": "application/json", }, method: "POST", }); const response = await request.json(); return response; } /** * @param mentorship */ public async submitMentorship(mentorship: Partial): Promise { const request = await fetch(`${this.url}/submit/mentorships`, { body: JSON.stringify(mentorship), headers: { "Content-type": "application/json", }, method: "POST", }); const response = await request.json(); return response; } /** * @param staff */ public async submitStaff(staff: Partial): Promise { const request = await fetch(`${this.url}/submit/staff`, { body: JSON.stringify(staff), headers: { "Content-type": "application/json", }, method: "POST", }); const response = await request.json(); return response; } /** * @param type * @param token */ public async getData(type: "appeals" | "commissions" | "contacts" | "events" | "meetings" | "mentorships" | "staff", token: string): Promise { const request = await fetch(`${this.url}/list/${type}`, { headers: { "Authorization": token, "Content-type": "application/json", }, method: "GET", }); const response = await request.json(); return response; } /** * @param type * @param id * @param token */ public async markReviewed(type: "appeals" | "commissions" | "contacts" | "events" | "meetings" | "mentorships" | "staff", id: string, token: string): Promise { const request = await fetch(`${this.url}/review/${type}`, { body: JSON.stringify({ submissionId: id }), headers: { "Authorization": token, "Content-type": "application/json", }, method: "PUT", }); const response = await request.json(); return response; } }