generated from nhcarrigan/template
178 lines
4.7 KiB
TypeScript
178 lines
4.7 KiB
TypeScript
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<boolean> {
|
|
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<Appeal>): Promise<SuccessResponse | ErrorResponse> {
|
|
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<Commission>): Promise<SuccessResponse | ErrorResponse> {
|
|
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<Contact>): Promise<SuccessResponse | ErrorResponse> {
|
|
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<Event>): Promise<SuccessResponse | ErrorResponse> {
|
|
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<Meeting>): Promise<SuccessResponse | ErrorResponse> {
|
|
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<Mentorship>): Promise<SuccessResponse | ErrorResponse> {
|
|
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<Staff>): Promise<SuccessResponse | ErrorResponse> {
|
|
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<DataResponse | ErrorResponse> {
|
|
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<SuccessResponse | ErrorResponse> {
|
|
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;
|
|
}
|
|
}
|