forked from hb1998/CodeGaze
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathExam.API.ts
More file actions
108 lines (95 loc) · 3.66 KB
/
Exam.API.ts
File metadata and controls
108 lines (95 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { QUALIFYING_SCORE } from '../../../constants/common.constants';
import { ExamInsertDto, ExamUpdateDto } from '../../../types/Models';
import { supabase } from '../../API/supabase';
export class ExamAPIService {
static async getAll() {
const { data, error } = await supabase
.from('exam')
.select(`
*,
challenge(*),
assessment(result)
`);
if (error) {
throw error;
}
return data.map((exam) => {
const qualifyingScore = (exam.assessment?.reduce((acc, curr) => {
if (curr.result) {
const result = (<boolean[]>curr.result)
const passPercent = (result.filter((result) => result).length / result.length) * 100;
if (passPercent >= QUALIFYING_SCORE) {
acc += 1;
}
}
return acc;
}, 0) / exam.assessment?.length)
return {
...exam,
qualifyingScore: isNaN(qualifyingScore) || !qualifyingScore ? 0 : qualifyingScore.toFixed(2)
}
})
}
static async getById(id: string) {
const { data, error } = await supabase.from('exam').select('*, challenge(*)').eq('id', id).single();
if (error) {
throw error;
}
return data || null;
}
static async create(exam: ExamInsertDto) {
const { data, error } = await supabase.from('exam').insert(exam).select('*,challenge(*)');
if (error) {
throw error;
}
return data ? data[0] : null;
}
static async update(exam: ExamUpdateDto) {
const { data, error } = await supabase.from('exam').update(exam).eq('id', exam.id);
if (error) {
throw error;
}
return data ? data[0] : null;
}
static async delete(id: string): Promise<void> {
const { error } = await supabase.from('exam').delete().eq('id', id);
if (error) {
throw error;
}
}
static async updateExamChallenges(examId: string, challengeIds: Set<string>) {
// Extract the array of challenge IDs from the exam_challenge data
const { data: examChallengeData, error: examChallengeError } = await supabase
.from('exam_challenge')
.select('challenge_id')
.eq('exam_id', examId);
if (examChallengeError) {
throw examChallengeError;
}
const currentChallengeIds = examChallengeData.map((row) => row.challenge_id);
const newChallengeIds = Array.from(challengeIds).filter((id) => !currentChallengeIds.includes(id));
const deletedChallengeIds = currentChallengeIds.filter((id) => !challengeIds.has(id));
// Insert new challenges into the exam_challenge table
const newChallengeRecords = newChallengeIds.map((challengeId) => ({
exam_id: examId,
challenge_id: challengeId,
}));
if (newChallengeRecords.length > 0) {
const { error: insertError } = await supabase.from('exam_challenge').insert(newChallengeRecords);
if (insertError) {
throw insertError;
}
}
// Delete challenges from the exam_challenge table
if (deletedChallengeIds.length > 0) {
const { error: deleteError } = await supabase
.from('exam_challenge')
.delete()
.in('challenge_id', deletedChallengeIds)
.eq('exam_id', examId);
if (deleteError) {
throw deleteError;
}
}
}
}