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