Files
classroomio/apps/dashboard/src/lib/utils/services/submissions/index.ts
T

109 lines
2.5 KiB
TypeScript
Raw Normal View History

import { supabase, getAccessToken } from '$lib/utils/functions/supabase';
import type { ExerciseSubmissions } from '$lib/utils/types';
2021-08-18 19:47:02 +03:00
export function fetchSubmissionStatus() {
return supabase.from('submissionstatus').select(`*`);
}
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
export async function fetchSubmission({
courseId,
exerciseId,
submittedBy
}: {
exerciseId: string;
courseId?: string;
submittedBy?: string;
}) {
const accessToken = await getAccessToken();
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 }
};
}
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
}
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>
) {
const toUpdate: {
status_id?: number;
total?: number;
2024-05-13 19:35:06 +01:00
feedback?: string;
} = {
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);
}