2025-12-05 10:48:01 +01:00
|
|
|
import { supabase, getAccessToken } from '$lib/utils/functions/supabase';
|
2023-10-19 08:31:57 +00:00
|
|
|
import type { ExerciseSubmissions } from '$lib/utils/types';
|
2021-08-18 19:47:02 +03:00
|
|
|
|
|
|
|
|
export function fetchSubmissionStatus() {
|
|
|
|
|
return supabase.from('submissionstatus').select(`*`);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-05 10:48:01 +01:00
|
|
|
export async function fetchSubmissions(course_id: string) {
|
|
|
|
|
const accessToken = await getAccessToken();
|
|
|
|
|
|
|
|
|
|
const response = await fetch(`/api/courses/submissions?courseId=${course_id}`, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
Authorization: accessToken
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
const error = await response.text();
|
|
|
|
|
return {
|
|
|
|
|
data: null,
|
|
|
|
|
error: { message: error }
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { success, data, message } = await response.json();
|
|
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
|
return {
|
|
|
|
|
data: null,
|
|
|
|
|
error: { message }
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { data, error: null };
|
2021-08-18 19:47:02 +03:00
|
|
|
}
|
2021-09-04 22:27:00 +03:00
|
|
|
|
2023-10-19 08:31:57 +00:00
|
|
|
export async function fetchSubmission({
|
|
|
|
|
courseId,
|
|
|
|
|
exerciseId,
|
|
|
|
|
submittedBy
|
|
|
|
|
}: {
|
|
|
|
|
exerciseId: string;
|
|
|
|
|
courseId?: string;
|
|
|
|
|
submittedBy?: string;
|
|
|
|
|
}) {
|
2025-12-05 10:48:01 +01:00
|
|
|
const accessToken = await getAccessToken();
|
2023-10-19 08:31:57 +00:00
|
|
|
|
2025-12-05 10:48:01 +01:00
|
|
|
const params = new URLSearchParams({ exerciseId });
|
|
|
|
|
if (courseId) params.append('courseId', courseId);
|
|
|
|
|
if (submittedBy) params.append('submittedBy', submittedBy);
|
|
|
|
|
|
|
|
|
|
const response = await fetch(`/api/courses/submission?${params.toString()}`, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
Authorization: accessToken
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
const error = await response.text();
|
|
|
|
|
return {
|
|
|
|
|
data: null,
|
|
|
|
|
error: { message: error }
|
|
|
|
|
};
|
2023-10-19 08:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-05 10:48:01 +01:00
|
|
|
const { success, data, message } = await response.json();
|
|
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
|
return {
|
|
|
|
|
data: null,
|
|
|
|
|
error: { message }
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { data, error: null };
|
2021-09-04 22:27:00 +03:00
|
|
|
}
|
|
|
|
|
|
2023-10-18 08:09:52 +00:00
|
|
|
export async function updateSubmission(
|
2025-11-01 15:49:29 +01:00
|
|
|
{ id, status_id, total, feedback }: { id: string; status_id?: number; total?: number; feedback?: string },
|
2024-08-03 20:58:07 +01:00
|
|
|
otherArgs?: Record<string, string>
|
2023-10-18 08:09:52 +00:00
|
|
|
) {
|
2023-10-19 08:31:57 +00:00
|
|
|
const toUpdate: {
|
|
|
|
|
status_id?: number;
|
|
|
|
|
total?: number;
|
2024-05-13 19:35:06 +01:00
|
|
|
feedback?: string;
|
2023-10-19 08:31:57 +00:00
|
|
|
} = {
|
2024-05-13 19:35:06 +01:00
|
|
|
status_id,
|
|
|
|
|
feedback
|
2021-09-04 22:27:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (typeof total === 'number') {
|
|
|
|
|
toUpdate.total = total;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-11 19:27:11 +03:00
|
|
|
return supabase.from('submission').update(toUpdate, otherArgs).match({ id });
|
2021-09-04 22:27:00 +03:00
|
|
|
}
|
|
|
|
|
|
2024-08-03 20:58:07 +01:00
|
|
|
export async function deleteSubmission(id: string) {
|
|
|
|
|
return supabase.from('submission').delete().match({ id });
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
export async function updateQuestionAnswer(update: Record<string, string>, match: Record<string, string>) {
|
2021-09-04 22:27:00 +03:00
|
|
|
return supabase.from('question_answer').update(update).match(match);
|
|
|
|
|
}
|