From 2d7ed46cf3510fe7dad23801b45f1652b3f39b45 Mon Sep 17 00:00:00 2001 From: Sebastian Pravda Date: Sun, 4 Dec 2022 12:32:53 +0100 Subject: [PATCH] feat: admin API --- frontend/src/lib/@api/admin.ts | 79 ++++++++++++++++++++++++++++ frontend/src/lib/stores/admin.ts | 4 ++ frontend/src/lib/stores/candidate.ts | 16 ++++++ 3 files changed, 99 insertions(+) create mode 100644 frontend/src/lib/@api/admin.ts create mode 100644 frontend/src/lib/stores/admin.ts diff --git a/frontend/src/lib/@api/admin.ts b/frontend/src/lib/@api/admin.ts new file mode 100644 index 0000000..a88c79f --- /dev/null +++ b/frontend/src/lib/@api/admin.ts @@ -0,0 +1,79 @@ +import type { AdminLogin } from "$lib/stores/admin"; +import type { CandidateData, CandidatePreview, CreateCandidate, CreateCandidateLogin } from "$lib/stores/candidate"; +import axios from "axios"; +import { API_URL, errorHandler, type Fetch } from "."; + + +// Login as admin /admin/login +export const apiLogin = async (data: AdminLogin): Promise => { + try { + const res = await axios.post(API_URL + '/admin/login', data, { withCredentials: true }); + return data.adminId; + } catch (e: any) { + throw errorHandler(e, 'Login failed'); + } +}; + +// Create new candidate /admin/create +// return created candidate's applicationId, personalIdNumber and password +export const createCandidate = async (data: CreateCandidate): Promise => { + try { + const res = await axios.post(API_URL + '/admin/create', data, { withCredentials: true }); + return res.data; + } catch (e: any) { + throw errorHandler(e, 'Candidate creation failed'); + } +} + +// Reset candidate password /admin/candidate/{id}/reset_password +export const resetCandidatePassword = async (id: number): Promise => { + try { + const res = await axios.post(API_URL + '/admin/candidate/' + id + '/reset_password', + {}, + { withCredentials: true } + ); + return res.data; + } catch (e: any) { + throw errorHandler(e, 'Candidate creation failed'); + } +} + +// SSR compatible +// List all candidates /admin/list/candidates +export const apiListCandidates = async (fetchSsr?: Fetch): Promise<[CandidatePreview]> => { + try { + if (fetchSsr) { + const res = await fetchSsr(API_URL + '/admin/list/candidates', { + method: 'GET', + credentials: 'include' + }); + return await res.json(); + } + const res = await axios.get(API_URL + '/admin/list/candidates', { + withCredentials: true + }); + return res.data; + } catch (e: any) { + throw errorHandler(e, 'Failed to fetch submission progress'); + } +}; + +// SSR compatible +// Get candidate data /admin/candidate/{id} +export const apiFetchCandidate = async (id: number, fetchSsr?: Fetch): Promise => { + try { + if (fetchSsr) { + const res = await fetchSsr(API_URL + '/admin/candidate/' + id, { + method: 'GET', + credentials: 'include' + }); + return await res.json(); + } + const res = await axios.get(API_URL + '/admin/candidate/' + id, { + withCredentials: true + }); + return res.data; + } catch (e: any) { + throw errorHandler(e, 'Failed to fetch candidate data'); + } +} \ No newline at end of file diff --git a/frontend/src/lib/stores/admin.ts b/frontend/src/lib/stores/admin.ts new file mode 100644 index 0000000..b359230 --- /dev/null +++ b/frontend/src/lib/stores/admin.ts @@ -0,0 +1,4 @@ +export interface AdminLogin { + adminId: number; + password: string; +} \ No newline at end of file diff --git a/frontend/src/lib/stores/candidate.ts b/frontend/src/lib/stores/candidate.ts index 0f8c2b1..31886d0 100644 --- a/frontend/src/lib/stores/candidate.ts +++ b/frontend/src/lib/stores/candidate.ts @@ -18,9 +18,25 @@ export interface CandidateData { parentEmail?: string; } +export interface CandidatePreview { + applicationId: number; + name: string; + surname: string; + study: string; +} + export interface CandidateLogin { applicationId: number; password: string; } +export interface CreateCandidate { + applicationId: number; + personalIdNumber: string; +} + +export interface CreateCandidateLogin extends CreateCandidate { + password: string; +} + export const candidateData = writable({});