Newer
Older
import { formatToUTCDate } from '$lib/utils/date';
import type { fetchType } from '$lib/utils/types';
export async function getStudiesAPI(fetch: fetchType): Promise<any[]> {
const response = await fetch('/api/studies');
if (!response.ok) return [];
return await response.json();
}
export async function getStudyAPI(fetch: fetchType, id: number): Promise<any | undefined> {
const response = await fetch(`/api/studies/${id}`);
if (!response.ok) return;
return await response.json();
}
export async function deleteStudyAPI(fetch: fetchType, id: number): Promise<boolean> {
const response = await fetch(`/api/studies/${id}`, {
method: 'DELETE'
});
return response.ok;
}
export async function createStudyAPI(
fetch: fetchType,
title: string,
description: string,
startDate: Date,
endDate: Date,
nbSession: number,
test_ids: number[],
consentParticipation: string,
consentPrivacy: string,
consentRights: string,
studyOrganisation: string,
studyAddress: string,
studyContact: string,
studyPIemail: string,
): Promise<number | null> {
const response = await fetch('/api/studies', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title,
description,
start_date: formatToUTCDate(startDate),
end_date: formatToUTCDate(endDate),
nb_session: nbSession,
test_ids,
consent_participation: consentParticipation,
consent_privacy: consentPrivacy,
consent_rights: consentRights,
study_data_organisation: studyOrganisation,
study_data_address: studyAddress,
study_data_contact: studyContact,
study_data_email: studyPIemail,
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
})
});
if (!response.ok) return null;
return parseInt(await response.text());
}
export async function patchStudyAPI(
fetch: fetchType,
study_id: number,
data: any
): Promise<boolean> {
const response = await fetch(`/api/studies/${study_id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
return response.ok;
}
export async function addUserToStudyAPI(
fetch: fetchType,
study_id: number,
user_id: number
): Promise<boolean> {
const response = await fetch(`/api/studies/${study_id}/users/${user_id}`, {
method: 'POST'
});
return response.ok;
}
export async function removeUserToStudyAPI(
fetch: fetchType,
study_id: number,
user_id: number
): Promise<boolean> {
const response = await fetch(`/api/studies/${study_id}/users/${user_id}`, {
method: 'DELETE'
});
return response.ok;
}
export async function createTestTypingAPI(
fetch: fetchType,
entries: typingEntry[],
code: string
): Promise<number | null> {
const response = await fetch(`/api/studies/typing`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ entries, code })
});
if (!response.ok) return null;
return parseInt(await response.text());
}