Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc verification #296

Merged
merged 7 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions callbacks/admin/student/documents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import axios, { AxiosResponse } from "axios";

import { errorNotification, successNotification } from "@callbacks/notifcation";
import {
ADMIN_STUDENT_URL,
ErrorType,
SERVER_ERROR,
setConfig,
} from "@callbacks/constants";

const instance = axios.create({
baseURL: ADMIN_STUDENT_URL,
timeout: 15000,
timeoutErrorMessage: SERVER_ERROR,
});

// eslint-disable-next-line no-unused-vars
interface nullBool {
Bool: boolean;
Valid: boolean;
}

export interface AllStudentDocumentsResponse {
ID: number;
CreatedAt: string | null;
UpdatedAt: string | null;
DeletedAt: string | null;
sid: number;
type: string;
path: string;
verified: boolean;
action_taken_by: string;
}

export interface StudentDocumentsResponse {
ID: number; // rsid
CreatedAt: string;
UpdatedAt: string;
DeletedAt: string;
path: string;
verified: nullBool;
action_taken_by: string;
}
export interface VerifyBody {
verified: boolean;
}

export interface SuccessResponse {
sucess: boolean;
}

const responseBody = <T>(response: AxiosResponse<T>) => response.data;

const adminDocumentsRequest = {
getAll: (token: string) =>
instance
.get<AllStudentDocumentsResponse[]>(`/documents`, setConfig(token))
.then(responseBody)
.catch((err: ErrorType) => {
errorNotification(
"Error in fetching data",
err.response?.data?.error || err.message
);
return [] as AllStudentDocumentsResponse[];
}),
get: (token: string, sid: string) =>
instance
.get<StudentDocumentsResponse[]>(`/${sid}/documents`, setConfig(token))
.then(responseBody)
.catch((err: ErrorType) => {
errorNotification(
"Error in fetching data",
err.response?.data?.error || err.message
);
return [] as StudentDocumentsResponse[];
}),
getByType: (token: string, type: string) =>
instance
.get<AllStudentDocumentsResponse[]>(
`/documents/type/${type}`,
setConfig(token)
)
.then(responseBody)
.catch((err: ErrorType) => {
errorNotification(
"Error in fetching data",
err.response?.data?.error || err.message
);
return [] as AllStudentDocumentsResponse[];
}),
getBySid: (token: string, sid: string | string[]) =>
instance
.get<AllStudentDocumentsResponse[]>(`/${sid}/documents`, setConfig(token))
.then(responseBody)
.catch((err: ErrorType) => {
errorNotification(
"Error in fetching data",
err.response?.data?.error || err.message
);
return [] as AllStudentDocumentsResponse[];
}),
putVerify: (token: string, docid: number, body: VerifyBody) =>
instance
.put<SuccessResponse, AxiosResponse<SuccessResponse>>(
`/document/${docid}/verify`,
body,
setConfig(token)
)
.then(() => {
let stat = body.verified ? "accepted" : "rejected";
successNotification("Success", `Documents ${stat}`);
return true;
})
.catch((err: ErrorType) => {
errorNotification("Error", err.response?.data?.error || err.message);
return false;
}),
};

export default adminDocumentsRequest;
116 changes: 116 additions & 0 deletions callbacks/student/rc/documents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import axios, { AxiosResponse } from "axios";

import { errorNotification, successNotification } from "@callbacks/notifcation";

import {
CDN_URL,
ErrorType,
SERVER_ERROR,
STUDENT_URL,
StatusResponse,
responseBody,
setConfig,
} from "../../constants";

const cdn_instance = axios.create({
baseURL: CDN_URL,
timeout: 15000,
timeoutErrorMessage: SERVER_ERROR,
});

const instance = axios.create({
baseURL: STUDENT_URL,
timeout: 15000,
timeoutErrorMessage: SERVER_ERROR,
});
export interface DocumentResponse {
message: string;
filename: string;
}

export interface DocumentsBackendParams {
path: string;
type: string;
sid: number;
}

// eslint-disable-next-line no-unused-vars
interface nullBool {
Bool: boolean;
Valid: boolean;
}

export interface AllStudentDocumentsResponse {
ID: number;
CreatedAt: string | null;
UpdatedAt: string | null;
DeletedAt: string | null;
sid: number;
type: string;
path: string;
verified: nullBool;
action_taken_by: string;
}

const documentRequest = {
post: (
body: FormData,
token: string,
doc: AllStudentDocumentsResponse,
sid: number
) =>
cdn_instance
.post<
DocumentResponse,
AxiosResponse<DocumentResponse, FormData>,
FormData
>("/upload", body, {
headers: {
token,
},
})
.then(
// second api call to backend instance
(response: AxiosResponse<DocumentResponse>) =>
instance
.post<
StatusResponse,
AxiosResponse<DocumentResponse, DocumentsBackendParams>,
DocumentsBackendParams
>(
`/document`,
{
path: response.data.filename,
type: doc.type,
sid,
},
setConfig(token)
)
.then(() => {
successNotification("Success", "Documents uploaded");
return responseBody;
})
.catch((err: ErrorType) => {
errorNotification(
"Error",
err.response?.data?.error || err.message
);
return { status: "error" } as StatusResponse;
})
)
.catch((err: ErrorType) => {
errorNotification("Upload Failed", err.response?.data?.error);
return { message: "", filename: "" } as DocumentResponse;
}),

get: (token: string) =>
instance
.get<AllStudentDocumentsResponse[]>(`/documents`, setConfig(token))
.then(responseBody)
.catch((err: ErrorType) => {
errorNotification("Error", err.response?.data?.error || err.message);
return [] as AllStudentDocumentsResponse[];
}),
};

export default documentRequest;
Loading
Loading