import { InternshipInfoDTO, InternshipRegistrationUpdate, SubmissionState } from "@/api/dto/internship-registration"; import { axios } from "@/api/index"; import { Nullable } from "@/helpers"; const INTERNSHIP_REGISTRATION_ENDPOINT = '/internshipRegistration'; const INTERNSHIP_ENDPOINT = '/internship'; export type ValidationMessage = { key: string; parameters: { [name: string]: string }, } export class ValidationError extends Error { public readonly messages: ValidationMessage[]; constructor(messages: ValidationMessage[], message: string = "There were validation errors.") { super(message); Object.setPrototypeOf(this, ValidationError.prototype); this.messages = messages; } } interface ApiError { key: string; parameters: { [name: string]: string }, } interface UpdateResponse { status: SubmissionState; errors?: ApiError[]; } export async function update(internship: Nullable): Promise { const response = (await axios.put(INTERNSHIP_REGISTRATION_ENDPOINT, internship)).data; if (response.status == SubmissionState.Draft) { throw new ValidationError(response.errors || []); } return response.status; } export async function get(): Promise { const response = await axios.get(INTERNSHIP_ENDPOINT); return response.data; }