1 | export function deleteAppointment(term,type){
|
---|
2 | if (typeof type !== 'undefined'){
|
---|
3 | fetch(`/api/requests/listRequests?term=${term}`)
|
---|
4 | .then(response => response.json())
|
---|
5 | .then(userIds=>{
|
---|
6 | console.log(userIds);
|
---|
7 | })
|
---|
8 | .catch(error => {
|
---|
9 | console.error('Error fetching user IDs:', error);
|
---|
10 | });
|
---|
11 | }
|
---|
12 |
|
---|
13 |
|
---|
14 | fetch(`/api/appointments/deleteAppointment?term=${term}`, {
|
---|
15 | method: 'DELETE',
|
---|
16 | headers: {
|
---|
17 | 'Content-Type': 'application/json'
|
---|
18 | }
|
---|
19 | })
|
---|
20 | .then(response => {
|
---|
21 | if (!response.ok) {
|
---|
22 | throw new Error('Failed to delete the appointment');
|
---|
23 | }
|
---|
24 | else{
|
---|
25 | location.reload();
|
---|
26 | }
|
---|
27 | })
|
---|
28 | .catch(error => {
|
---|
29 | console.error('Error:', error);
|
---|
30 | });
|
---|
31 | }
|
---|
32 | export function confirmCarriedOut(term,userInput){
|
---|
33 | removeAppointment(term,"carriedOut")
|
---|
34 | .then(async resultString => {
|
---|
35 | let additionalInfo = resultString.split("&")[0];
|
---|
36 | let userId = resultString.split("&")[1];
|
---|
37 | const updateResponse = await fetch(`/api/users/carriedOut`, {
|
---|
38 | method: 'PUT',
|
---|
39 | headers: {
|
---|
40 | 'Content-Type': 'application/json',
|
---|
41 | },
|
---|
42 | body: JSON.stringify({
|
---|
43 | userId: userId,
|
---|
44 | term: term,
|
---|
45 | additionalInfo: additionalInfo,
|
---|
46 | status: "carried_out",
|
---|
47 | note: userInput
|
---|
48 | }),
|
---|
49 | });
|
---|
50 |
|
---|
51 | if (updateResponse.ok) {
|
---|
52 | location.reload();
|
---|
53 | console.log(`User updated successfully in carried_out`);
|
---|
54 | } else {
|
---|
55 | console.error(`Failed to update user. Status: ${updateResponse.status} - ${updateResponse.statusText}`);
|
---|
56 | }
|
---|
57 | })
|
---|
58 | .catch(error => {
|
---|
59 | console.error("Error:", error);
|
---|
60 | });
|
---|
61 |
|
---|
62 |
|
---|
63 | }
|
---|
64 | export async function getUsersByTermExcept(term, excludedUsername) {
|
---|
65 | try {
|
---|
66 | const response = await fetch(`api/requests/users-by-term?term=${term}&excludedUsername=${excludedUsername}`);
|
---|
67 | if (!response.ok) {
|
---|
68 | throw new Error(`HTTP error! Status: ${response.status}`);
|
---|
69 | }
|
---|
70 |
|
---|
71 | const users = await response.json();
|
---|
72 | console.log('Users:', users);
|
---|
73 | for (const userId of users.ids) {
|
---|
74 | await removeRequestAndUpdateUser(term, userId,"rejected");
|
---|
75 | }
|
---|
76 |
|
---|
77 | } catch (error) {
|
---|
78 | console.error('Error fetching users:', error);
|
---|
79 | }
|
---|
80 | }
|
---|
81 | export async function removeRequestAndUpdateUser(term,userId,status){
|
---|
82 | try {
|
---|
83 | const removeResponse = await fetch(`/api/requests/removeRequest?term=${term}&userId=${userId}`, {
|
---|
84 | method: 'GET',
|
---|
85 | });
|
---|
86 |
|
---|
87 | if (removeResponse.ok) {
|
---|
88 | const additionalInfo = await removeResponse.text();
|
---|
89 | console.log("Removed appointment datetime:", additionalInfo);
|
---|
90 | if(status!=="carriedOut"){
|
---|
91 | const updateResponse = await fetch(`/api/users/addTerm`, {
|
---|
92 | method: 'PUT',
|
---|
93 | headers: {
|
---|
94 | 'Content-Type': 'application/json',
|
---|
95 | },
|
---|
96 | body: JSON.stringify({
|
---|
97 | userId: userId,
|
---|
98 | term: term,
|
---|
99 | additionalInfo: additionalInfo,
|
---|
100 | status: status
|
---|
101 | }),
|
---|
102 | });
|
---|
103 |
|
---|
104 | if (updateResponse.ok) {
|
---|
105 | console.log(`User updated successfully.`);
|
---|
106 | } else {
|
---|
107 | console.error(`Failed to update user. Status: ${updateResponse.status} - ${updateResponse.statusText}`);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | else{
|
---|
111 | return additionalInfo;
|
---|
112 | }
|
---|
113 |
|
---|
114 | } else {
|
---|
115 | console.error(`Failed to remove request. Status: ${removeResponse.status} - ${removeResponse.statusText}`);
|
---|
116 | }
|
---|
117 | } catch (error) {
|
---|
118 | console.error("Error:", error);
|
---|
119 | }
|
---|
120 |
|
---|
121 | }
|
---|
122 | export async function removeAppointment(term,extra) {
|
---|
123 | let temp;
|
---|
124 | try {
|
---|
125 | const response = await fetch(`/api/appointments/removeUserFromAppointment?term=${term}`, {
|
---|
126 | method: 'PUT',
|
---|
127 | });
|
---|
128 |
|
---|
129 | if (response.ok) {
|
---|
130 | const userId = await response.text();
|
---|
131 | if(extra!=="carriedOut"){
|
---|
132 | temp=extra;
|
---|
133 | await removeRequestAndUpdateUser(term,userId,temp);
|
---|
134 | }
|
---|
135 | else{
|
---|
136 | temp=extra;
|
---|
137 | let importantAdditional=await removeRequestAndUpdateUser(term,userId,temp);
|
---|
138 | deleteAppointment(term,extra);
|
---|
139 | return importantAdditional+"&"+userId;
|
---|
140 |
|
---|
141 | }
|
---|
142 | location.reload();
|
---|
143 |
|
---|
144 | } else {
|
---|
145 | console.error("Failed to get userId");
|
---|
146 | }
|
---|
147 | } catch (error) {
|
---|
148 | console.error("Error:", error);
|
---|
149 | }
|
---|
150 | }
|
---|
151 | export async function makeReservation(dateTime,approvedUser){
|
---|
152 | try {
|
---|
153 | const response = await fetch(`/api/appointments/addAppointments?username=${approvedUser}&dateTime=${dateTime}`);
|
---|
154 | if (response.ok) {
|
---|
155 | console.log('Appointment added successfully.');
|
---|
156 | location.reload();
|
---|
157 | } else {
|
---|
158 | const text = await response.text();
|
---|
159 | throw new Error(`HTTP error! Status: ${response.status}. Response: ${text}`);
|
---|
160 | }
|
---|
161 | } catch (error) {
|
---|
162 | console.error('Error adding appointment:', error);
|
---|
163 | }
|
---|
164 | getUsersByTermExcept(dateTime, approvedUser).then(r => console.log("success"));
|
---|
165 | }
|
---|
166 | export function displayDiv(dateTime,username){
|
---|
167 | if (typeof username !== 'undefined'){
|
---|
168 | makeReservation(dateTime, username);
|
---|
169 | }
|
---|
170 | else{
|
---|
171 | let temp=document.getElementById("confirm-approval");
|
---|
172 | temp.addEventListener('click',()=>{
|
---|
173 | const approvedUser = document.getElementById("username-approval").value;
|
---|
174 | makeReservation(dateTime,approvedUser)
|
---|
175 | });
|
---|
176 | }
|
---|
177 | } |
---|