-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
139 lines (129 loc) · 7.3 KB
/
api.js
File metadata and controls
139 lines (129 loc) · 7.3 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// API Configuration - use relative URLs so session cookies are always sent
const API_BASE_URL = '/api';
const AUTH_BASE_URL = '/api/auth';
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
async function apiCall(url, method = 'GET', data = null) {
const options = {
method,
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': getCookie('csrftoken'),
},
credentials: 'include',
};
if (data && method !== 'GET') options.body = JSON.stringify(data);
const response = await fetch(url, options);
let result;
try { result = await response.json(); } catch (_) { result = { detail: response.statusText || 'Request failed' }; }
if (!response.ok) {
const err = typeof result === 'object' && result !== null ? { ...result, status: response.status } : { detail: result, status: response.status };
throw err;
}
return result;
}
const AuthAPI = {
register: (data) => apiCall(`${AUTH_BASE_URL}/register/`, 'POST', data),
login: (email, password, role) => apiCall(`${AUTH_BASE_URL}/login/`, 'POST', { email, password, role }),
logout: () => apiCall(`${AUTH_BASE_URL}/logout/`, 'POST'),
getCurrentUser: () => apiCall(`${AUTH_BASE_URL}/current-user/`),
updateProfile: (profileData) => apiCall(`${AUTH_BASE_URL}/update-profile/`, 'PATCH', profileData),
updateSettings: (settingsData) => apiCall(`${AUTH_BASE_URL}/update-settings/`, 'PATCH', settingsData),
adminAccessToken: (email, accessToken) => apiCall(`${AUTH_BASE_URL}/admin-secret-key/`, 'POST', { email, access_token: accessToken, secret_key: accessToken }),
adminSecretKey: (email, secretKey) => apiCall(`${AUTH_BASE_URL}/admin-secret-key/`, 'POST', { email, access_token: secretKey, secret_key: secretKey }),
getAdminAccessToken: () => apiCall(`${AUTH_BASE_URL}/admin-access-token/`),
uploadProfileImage: (formData) => fetch(`${AUTH_BASE_URL}/upload-profile-image/`, {
method: 'POST',
headers: { 'X-CSRFToken': getCookie('csrftoken') },
credentials: 'include',
body: formData
}).then(async res => {
let data = {};
try { data = await res.json(); } catch (_) {}
if (!res.ok) throw { ...data, status: res.status };
return data;
}),
getUserStats: () => apiCall(`${AUTH_BASE_URL}/user-stats/`),
getFacultyList: () => apiCall(`${AUTH_BASE_URL}/faculty-list/`),
getAdminAudit: () => apiCall(`${AUTH_BASE_URL}/admin-login-audit/`),
};
const AccountDeleteAPI = {
request: (confirmationPhrase, disclaimerAccepted) =>
apiCall(`${AUTH_BASE_URL}/request-delete-account/`, 'POST', {
confirmation_phrase: confirmationPhrase,
disclaimer_accepted: disclaimerAccepted,
}),
confirm: (confirmationPhrase, password, disclaimerAccepted) =>
apiCall(`${AUTH_BASE_URL}/confirm-delete-account/`, 'POST', {
confirmation_phrase: confirmationPhrase,
password,
disclaimer_accepted: disclaimerAccepted,
}),
};
const UsersAPI = {
list: (role) => apiCall(`${AUTH_BASE_URL}/users/${role ? '?role=' + encodeURIComponent(role) : ''}`),
byUuid: (id) => apiCall(`${AUTH_BASE_URL}/users/by-uuid/${encodeURIComponent(id)}/`),
getPermissions: (id) => apiCall(`${AUTH_BASE_URL}/users/${id}/permissions/`),
updatePermissions: (id, revokedPermissions) => apiCall(`${AUTH_BASE_URL}/users/${id}/permissions/`, 'PATCH', { revoked_permissions: revokedPermissions }),
delete: (id) => apiCall(`${AUTH_BASE_URL}/users/${id}/`, 'DELETE'),
};
const FacultyRequestsAPI = {
listPending: () => apiCall(`${AUTH_BASE_URL}/faculty-requests/`),
approve: (id) => apiCall(`${AUTH_BASE_URL}/faculty-requests/${id}/approve/`, 'POST', {}),
reject: (id) => apiCall(`${AUTH_BASE_URL}/faculty-requests/${id}/reject/`, 'POST', {}),
};
const TicketsAPI = {
getAll: () => apiCall(`${API_BASE_URL}/tickets/`),
getById: (id) => apiCall(`${API_BASE_URL}/tickets/${id}/`),
create: (ticketData) => apiCall(`${API_BASE_URL}/tickets/`, 'POST', { ...ticketData, status: ticketData.status || 'submitted' }),
update: (id, ticketData) => apiCall(`${API_BASE_URL}/tickets/${id}/`, 'PATCH', ticketData),
reply: (id, message) => apiCall(`${API_BASE_URL}/tickets/${id}/reply/`, 'POST', { message }),
updateStatus: (id, status) => apiCall(`${API_BASE_URL}/tickets/${id}/update_status/`, 'PATCH', { status }),
accept: (id) => apiCall(`${API_BASE_URL}/tickets/${id}/accept/`, 'POST'),
escalate: (id) => apiCall(`${API_BASE_URL}/tickets/${id}/escalate/`, 'POST'),
submit: (id) => apiCall(`${API_BASE_URL}/tickets/${id}/submit/`, 'POST'),
withdraw: (id) => apiCall(`${API_BASE_URL}/tickets/${id}/withdraw/`, 'POST'),
reopen: (id) => apiCall(`${API_BASE_URL}/tickets/${id}/reopen/`, 'POST'),
uploadAttachment: (id, formData) => fetch(`${API_BASE_URL}/tickets/${id}/attachments/`, {
method: 'POST',
headers: { 'X-CSRFToken': getCookie('csrftoken') },
credentials: 'include',
body: formData
}).then(r => r.ok ? r.json() : r.json().then(j => { throw { ...j, status: r.status }; })),
reassign: (id, assignedFacultyId) => apiCall(`${API_BASE_URL}/tickets/${id}/reassign/`, 'POST', { assigned_faculty_id: assignedFacultyId }),
forceClose: (id) => apiCall(`${API_BASE_URL}/tickets/${id}/force_close/`, 'POST'),
getDashboardStats: () => apiCall(`${API_BASE_URL}/dashboard-stats/`),
otherIssues: () => apiCall(`${API_BASE_URL}/tickets/other/`),
track: (ticketId) => apiCall(`${API_BASE_URL}/tickets/track/?ticket_id=${encodeURIComponent(ticketId)}`),
};
const SearchAPI = { search: (q) => apiCall(`${API_BASE_URL}/search/?q=${encodeURIComponent(q)}`) };
const CategoriesAPI = {
list: () => apiCall(`${API_BASE_URL}/categories/`),
create: (data) => apiCall(`${API_BASE_URL}/categories/create/`, 'POST', data),
update: (id, data) => apiCall(`${API_BASE_URL}/categories/${id}/`, 'PATCH', data),
remove: (id) => apiCall(`${API_BASE_URL}/categories/${id}/`, 'DELETE'),
};
const DepartmentsAPI = { list: () => apiCall(`${API_BASE_URL}/departments/`), stats: () => apiCall(`${API_BASE_URL}/department-stats/`) };
const SubjectsAPI = { list: (departmentId) => apiCall(`${API_BASE_URL}/subjects/${departmentId != null && departmentId !== '' ? '?department_id=' + encodeURIComponent(departmentId) : ''}`) };
const NotificationsAPI = {
list: () => apiCall(`${API_BASE_URL}/notifications/`),
markRead: (id) => apiCall(`${API_BASE_URL}/notifications/${id}/read/`, 'POST'),
markUnread: (id) => apiCall(`${API_BASE_URL}/notifications/${id}/unread/`, 'POST'),
};
const SupportAPI = { submit: (data) => apiCall(`${API_BASE_URL}/support-request/`, 'POST', data) };
const ReviewsAPI = {
list: () => apiCall(`${API_BASE_URL}/reviews/`),
submit: (ticketId, rating, comment) => apiCall(`${API_BASE_URL}/reviews/submit/`, 'POST', { ticket_id: ticketId, rating, comment: comment || '' }),
};