import { Identifiable, Identifier, Internship } from "@/data"; import moment, { Moment } from "moment-timezone"; import { encapsulate, Nullable, OneOrMany } from "@/helpers"; import { SubmissionStatus } from "@/state/reducer/submission"; import { axios } from "@/api"; import { prepare, query } from "@/routing"; import { InternshipDocument, InternshipDocumentDTO, internshipDocumentDtoTransformer, InternshipInfoDTO, internshipReportDtoTransformer, submissionStateDtoTransformer } from "@/api/dto/internship-registration"; import { OneWayTransformer, Transformer } from "@/serialization"; import { mentorDtoTransformer } from "@/api/dto/mentor"; import { internshipTypeDtoTransformer } from "@/api/dto/type"; import { studentDtoTransfer } from "@/api/dto/student"; import { programEntryDtoTransformer } from "@/api/dto/edition"; import { UploadType } from "@/api/upload"; import { Report } from "@/data/report"; export type InternshipSubmission = Nullable & { state: SubmissionStatus, changed: Moment | null, ipp: InternshipDocument | null, report: Report | null, evaluation: InternshipDocument, grade: number | null, approvals: InternshipDocument[], } const INTERNSHIP_MANAGEMENT_INDEX_ENDPOINT = "/management/internship"; const INTERNSHIP_MANAGEMENT_ENDPOINT = "/management/internship/:id"; const INTERNSHIP_GRADE_ENDPOINT = "/management/internship/:id/grade"; const INTERNSHIP_ACCEPT_ENDPOINT = "/management/internship/:id/registration/accept"; const INTERNSHIP_REJECT_ENDPOINT = "/management/internship/:id/registration/reject"; const internshipInfoDtoTransformer: OneWayTransformer = { transform(subject: InternshipInfoDTO, context?: never): InternshipSubmission { // @ts-ignore const ipp = subject.documentation.find(doc => doc.type === UploadType.Ipp); // @ts-ignore const evaluation = subject.documentation.find(doc => doc.type === UploadType.InternshipEvaluation); const report = subject.report; return { ...subject, changed: moment(subject.internshipRegistration.submissionDate), company: subject.internshipRegistration.company, startDate: moment(subject.internshipRegistration.start), endDate: moment(subject.internshipRegistration.end), hours: subject.internshipRegistration.declaredHours, id: subject.id, intern: subject.student && studentDtoTransfer.transform(subject.student), isAccepted: false, lengthInWeeks: 0, mentor: subject.internshipRegistration.mentor && mentorDtoTransformer.transform(subject.internshipRegistration.mentor), office: subject.internshipRegistration.branchAddress, program: (subject.internshipRegistration.subjects || []).map(subject => programEntryDtoTransformer.transform(subject.subject)), state: submissionStateDtoTransformer.transform(subject.internshipRegistration.state), type: subject.internshipRegistration.type && internshipTypeDtoTransformer.transform(subject.internshipRegistration.type), ipp: ipp && internshipDocumentDtoTransformer.transform(ipp), report: report && internshipReportDtoTransformer.transform(report), approvals: (subject.documentation.filter(doc => doc.type === UploadType.DeanConsent).map(subject => internshipDocumentDtoTransformer.transform(subject))), evaluation: evaluation && internshipDocumentDtoTransformer.transform(evaluation), }; }, } export async function all(edition: Identifiable): Promise { const result = await axios.get(query(INTERNSHIP_MANAGEMENT_INDEX_ENDPOINT, { EditionId: edition.id || "" })); return result.data.map(result => internshipInfoDtoTransformer.transform(result)) } export async function get(id: Identifier): Promise { const result = await axios.get(prepare(INTERNSHIP_MANAGEMENT_ENDPOINT, { id })) return internshipInfoDtoTransformer.transform(result.data); } export async function accept(internship: OneOrMany, comment?: string): Promise { const internships = encapsulate(internship) await Promise.all(internships.map(internship => axios.put( prepare(INTERNSHIP_ACCEPT_ENDPOINT, { id: internship.id || ""}), JSON.stringify(comment || ""), { headers: { 'Content-Type': 'application/json' } } ))) } export async function discard(internship: OneOrMany, comment: string): Promise { const internships = encapsulate(internship) await Promise.all(internships.map(internship => axios.put( prepare(INTERNSHIP_REJECT_ENDPOINT, { id: internship.id || ""}), JSON.stringify(comment), { headers: { 'Content-Type': 'application/json' } } ))) } export async function grade(internship: OneOrMany, grade: number): Promise { const internships = encapsulate(internship) await Promise.all(internships.map(internship => axios.put( prepare(INTERNSHIP_GRADE_ENDPOINT, { id: internship.id || ""}), JSON.stringify(grade), { headers: { 'Content-Type': 'application/json' } } ))) }