[8bcd64c] | 1 | $(document).ready(function() {
|
---|
| 2 | var business = {};
|
---|
[53765dd] | 3 | var serviceTypeList = {};
|
---|
[8bcd64c] | 4 |
|
---|
[53765dd] | 5 | getServiceTypes().then(function (serviceTypes) {
|
---|
| 6 | serviceTypeList = serviceTypes;
|
---|
| 7 | getBusinessInfo(serviceTypes).then(function (data) {
|
---|
| 8 | business = data;
|
---|
| 9 | });
|
---|
[1413ee2] | 10 | });
|
---|
[8bcd64c] | 11 |
|
---|
[53765dd] | 12 |
|
---|
| 13 |
|
---|
[77205be] | 14 | getAppointments();
|
---|
[8bcd64c] | 15 |
|
---|
| 16 | $("#add_service").click(function () {
|
---|
| 17 | var input_service = $("#input_service").val();
|
---|
| 18 | // clear the input
|
---|
| 19 | $("#input_service").val('');
|
---|
| 20 |
|
---|
| 21 | $("#predefined_services_admin_panel").append(
|
---|
| 22 | '<div class="form-outline mb-4">' +
|
---|
| 23 | ' <div class="row">' +
|
---|
| 24 | ' <div class="col-md-6">\n' +
|
---|
| 25 | ' <input class="form-check-input" type="checkbox" checked value="' + -1 + '" id="' + -1 + '">\n' +
|
---|
| 26 | ' <label class="form-check-label" for="' + -1 + '">\n' +
|
---|
| 27 | input_service +
|
---|
| 28 | ' </label>\n' +
|
---|
| 29 | ' </div>' +
|
---|
| 30 | ' <div class="form-outline col-md-2 d-grid">' +
|
---|
| 31 | ' <input type="text" id="' + -1 + input_service.replace(/\s/g, "") + "duration" + '" class="form-control" placeholder="time" />' +
|
---|
| 32 | ' </div>' +
|
---|
| 33 | ' <div class="form-outline col-md-2 d-grid">' +
|
---|
| 34 | ' <input type="text" id="' + -1 + input_service.replace(/\s/g, "") + "price"+ '" class="form-control" placeholder="price" />' +
|
---|
| 35 | ' </div>' +
|
---|
| 36 | ' </div>' +
|
---|
[f29cd58] | 37 | ' <div class="row" style="margin-top:10px;">' +
|
---|
| 38 | ' <div class="form-outline col-md-10 d-grid">' +
|
---|
| 39 | ' <input type="text" id="' + -1 + input_service.replace(/\s/g, "") + "description" + '" class="form-control" placeholder="description" />' +
|
---|
| 40 | ' </div>' +
|
---|
| 41 | ' </div>' +
|
---|
[8bcd64c] | 42 | '</div>');
|
---|
| 43 | event.preventDefault();
|
---|
| 44 | });
|
---|
[950fa0d] | 45 |
|
---|
| 46 | $("#update_services_button").click(function () {
|
---|
| 47 | businesses = [];
|
---|
| 48 | servicesObj = [];
|
---|
| 49 | $.each($('#predefined_services_admin_panel input:checked').siblings(), function (index, label) {
|
---|
| 50 |
|
---|
| 51 | let service = {};
|
---|
| 52 | var id = $(label).prop('for');
|
---|
| 53 | var text = $(label).text();
|
---|
| 54 | var time = $($($(label).parent()).siblings()[0]).children()[0].value;
|
---|
| 55 | var price = $($($(label).parent()).siblings()[1]).children()[0].value;
|
---|
[f29cd58] | 56 | var description = $($($($($($(label).parent())).parent()).siblings()[0]).children()[0]).children()[0].value;
|
---|
[950fa0d] | 57 |
|
---|
[1413ee2] | 58 | var serviceType = {};
|
---|
[950fa0d] | 59 | if (parseInt(id) != -1) {
|
---|
| 60 | service['id'] = parseInt(id);
|
---|
| 61 | serviceType['id'] = business['services'].find(obj => obj.id === parseInt(id))['serviceType']['id'];
|
---|
| 62 | } else {
|
---|
| 63 |
|
---|
| 64 | }
|
---|
| 65 | serviceType['name'] = text.trim();
|
---|
| 66 | service['serviceType'] = serviceType;
|
---|
| 67 | service['duration'] = parseInt(time);
|
---|
| 68 | service['price'] = parseInt(price);
|
---|
[f29cd58] | 69 | service['description'] = description;
|
---|
[950fa0d] | 70 | servicesObj.push(service);
|
---|
| 71 | });
|
---|
| 72 |
|
---|
[1413ee2] | 73 | var servicesForDelete=[];
|
---|
| 74 | $.each($('#predefined_services_admin_panel input:not(:checked)').siblings(), function (index, label) {
|
---|
| 75 | let id = $(label).prop('for');
|
---|
| 76 | let foundService = business['services'].find(obj => obj.id === parseInt(id));
|
---|
| 77 | if (foundService !== undefined && foundService['serviceStatus'] === 'ACTIVE') {
|
---|
| 78 | let service1 = {};
|
---|
| 79 | service1['id'] = id;
|
---|
| 80 | servicesForDelete.push(service1);
|
---|
[950fa0d] | 81 | }
|
---|
| 82 | });
|
---|
[1413ee2] | 83 |
|
---|
| 84 |
|
---|
| 85 | updateServices(servicesObj).then(function (response) {
|
---|
[53765dd] | 86 | getBusinessInfo(serviceTypeList).then(function (data) {
|
---|
[1413ee2] | 87 | business = data;
|
---|
| 88 | });
|
---|
| 89 | });
|
---|
| 90 |
|
---|
| 91 | if (servicesForDelete.length > 0) {
|
---|
| 92 | deleteServices(servicesForDelete).then(function (response) {
|
---|
[53765dd] | 93 | getBusinessInfo(serviceTypeList).then(function (data) {
|
---|
[1413ee2] | 94 | business = data;
|
---|
| 95 | });
|
---|
| 96 | });
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[950fa0d] | 99 | event.preventDefault();
|
---|
| 100 | });
|
---|
| 101 |
|
---|
| 102 | $("#update_owner_button").click(function() {
|
---|
| 103 | businesses = [];
|
---|
[1413ee2] | 104 | owner={};
|
---|
| 105 | owner['firstName'] = $('#firstName').val();
|
---|
| 106 | owner['lastName'] = $('#lastName').val();
|
---|
| 107 | owner['email'] = $('#email').val();
|
---|
| 108 | owner['phoneNumber'] = $('#phoneNumber').val();
|
---|
| 109 | owner['username'] = $('#username').val();
|
---|
| 110 |
|
---|
| 111 | business['owner'] = owner;
|
---|
[950fa0d] | 112 |
|
---|
| 113 | businesses.push(business);
|
---|
| 114 |
|
---|
[1413ee2] | 115 | updateBusinessInfo(businesses).then(function() {
|
---|
[53765dd] | 116 | getBusinessInfo(serviceTypeList).then(function (found) {
|
---|
[1413ee2] | 117 | business = found;
|
---|
| 118 | });
|
---|
[950fa0d] | 119 | });
|
---|
| 120 | });
|
---|
| 121 | event.preventDefault();
|
---|
[77205be] | 122 | });
|
---|
| 123 |
|
---|
[1413ee2] | 124 | function updateBusinessInfo(businessList) {
|
---|
| 125 | return $.ajax({
|
---|
| 126 | url: "http://localhost:8080/api/business",
|
---|
| 127 | type:"PATCH",
|
---|
| 128 | data: JSON.stringify(businessList),
|
---|
| 129 | contentType:"application/json; charset=utf-8",
|
---|
| 130 | dataType: 'text',
|
---|
| 131 | success: function(succ){
|
---|
| 132 | alert( "Updates applied successfully" );
|
---|
| 133 | },
|
---|
| 134 | error: function(err) {
|
---|
| 135 | alert(err);
|
---|
| 136 | }
|
---|
| 137 | });
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | function updateServices(serviceList) {
|
---|
| 141 | return $.ajax({
|
---|
| 142 | url: "http://localhost:8080/api/service",
|
---|
| 143 | type:"PATCH",
|
---|
| 144 | data: JSON.stringify(serviceList),
|
---|
| 145 | contentType:"application/json; charset=utf-8",
|
---|
| 146 | dataType: 'text',
|
---|
| 147 | success: function(succ){
|
---|
| 148 | alert( "Updates applied successfully" );
|
---|
| 149 | },
|
---|
| 150 | error: function(err) {
|
---|
| 151 | alert(err);
|
---|
| 152 | }
|
---|
| 153 | });
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | function deleteServices(services) {
|
---|
| 157 | return $.ajax({
|
---|
| 158 | url: "http://localhost:8080/api/service/delete",
|
---|
| 159 | type:"POST",
|
---|
| 160 | data: JSON.stringify(services),
|
---|
| 161 | contentType:"application/json; charset=utf-8",
|
---|
| 162 | dataType: 'text',
|
---|
| 163 | success: function(succ){
|
---|
| 164 | console.log("services deleted successfully");
|
---|
| 165 | },
|
---|
| 166 | error: function(err) {
|
---|
| 167 | console.error(err);
|
---|
| 168 | }
|
---|
| 169 | });
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[77205be] | 172 | function cancelAppointment(appointmentId) {
|
---|
| 173 | if (confirm("Are you sure you want to cancel the appointment?")) {
|
---|
| 174 | $.ajax({
|
---|
| 175 | url: "http://localhost:8080/api/appointment/" + appointmentId,
|
---|
| 176 | type:"DELETE"
|
---|
| 177 | }).success(function (data) {
|
---|
| 178 | alert("Appointment successfully canceled.")
|
---|
| 179 | getAppointments();
|
---|
| 180 | }).error(function (error) {
|
---|
| 181 | alert("Something went wrong.");
|
---|
| 182 | });
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | function getAppointments() {
|
---|
| 187 | $.ajax({
|
---|
| 188 | url: "http://localhost:8080/api/appointment/future/me"
|
---|
| 189 | }).then(function (data) {
|
---|
| 190 | var $el = $("#bookings-table-body");
|
---|
| 191 | $el.empty();
|
---|
| 192 | $.each(data, function (index, obj) {
|
---|
| 193 | var element =
|
---|
| 194 | '<tr>' +
|
---|
| 195 | ' <th scope="row">' + (parseInt(index) + 1) + '</th>' +
|
---|
| 196 | ' <td>' + obj['fullName'] + '</td>' +
|
---|
| 197 | ' <td>' + obj['email'] + '</td>' +
|
---|
| 198 | ' <td>' + obj['phoneNumber'] + '</td>' +
|
---|
| 199 | ' <td>' + obj['timePeriod'] + '</td>' +
|
---|
| 200 | ' <td>' + obj['serviceName'] + '</td>';
|
---|
| 201 |
|
---|
| 202 | switch (obj['status']) {
|
---|
| 203 | case 'NEW':
|
---|
| 204 | element += ' <td><button type="button" class="btn btn-danger" onclick="cancelAppointment(' + obj['appointmentId'] + ')">Cancel appointment</button></td>';
|
---|
| 205 | break;
|
---|
| 206 | case 'CANCELLED_BY_CUSTOMER':
|
---|
| 207 | element += ' <td><button type="button" class="btn btn-secondary" disabled>Cancelled by customer</button></td>';
|
---|
| 208 | break;
|
---|
| 209 | case 'CANCELLED_BY_BUSINESS_OWNER':
|
---|
| 210 | element += ' <td><button type="button" class="btn btn-secondary" disabled>Cancelled by business owner</button></td>';
|
---|
| 211 | break;
|
---|
| 212 | }
|
---|
| 213 | element+='</tr>';
|
---|
| 214 |
|
---|
| 215 | $el.append(element);
|
---|
| 216 | });
|
---|
| 217 | });
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[53765dd] | 220 | function getServiceTypes() {
|
---|
| 221 | return $.ajax({
|
---|
| 222 | url: "http://localhost:8080/api/nomenclature/serviceTypes/me"
|
---|
| 223 | }).then(function (serviceTypes) {
|
---|
| 224 | return serviceTypes;
|
---|
| 225 | });
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | function getBusinessInfo(serviceTypeList) {
|
---|
[1413ee2] | 229 | return $.ajax({
|
---|
[77205be] | 230 | url: "http://localhost:8080/api/business/me"
|
---|
[1413ee2] | 231 | }).then(function (business) {
|
---|
[77205be] | 232 | var $header = $("#header");
|
---|
| 233 |
|
---|
| 234 | // header
|
---|
[1413ee2] | 235 | $header.text("Welcome back " + business["owner"]['firstName'] + " " + business['owner']['lastName']);
|
---|
[77205be] | 236 |
|
---|
| 237 | // business info
|
---|
| 238 | if(business['businessStatus'] == 'NEW') {
|
---|
| 239 | $('#new_business_warning').removeAttr("hidden");
|
---|
| 240 | }
|
---|
| 241 | $('#business_status').val(business['businessStatus']);
|
---|
| 242 | $('#business_type').val(business['businessType']['text']);
|
---|
| 243 |
|
---|
| 244 | // owner info
|
---|
| 245 | $('#firstName').val(business['owner']['firstName']);
|
---|
| 246 | $('#lastName').val(business['owner']['lastName']);
|
---|
| 247 | $('#phoneNumber').val(business['owner']['phoneNumber']);
|
---|
| 248 | $('#email').val(business['owner']['email']);
|
---|
| 249 | $('#username').val(business['owner']['username']);
|
---|
| 250 |
|
---|
| 251 | // services info
|
---|
| 252 | var $el = $("#predefined_services_admin_panel");
|
---|
| 253 | $el.empty();
|
---|
[53765dd] | 254 | const existingServiceTypeIds = business['services'].map(service => service['serviceType']['id']);
|
---|
| 255 | const missingAddedServiceTypes = serviceTypeList.filter(serviceType => existingServiceTypeIds.indexOf(serviceType['id']) === -1);
|
---|
| 256 | //console.log(missingAddedServiceTypes);
|
---|
[1413ee2] | 257 |
|
---|
[77205be] | 258 | $.each(business['services'], function (index, obj) {
|
---|
[1413ee2] | 259 |
|
---|
[f29cd58] | 260 | var element =
|
---|
| 261 | '<div class=\"form-outline mb-4\">' +
|
---|
[77205be] | 262 | ' <div class="row">' +
|
---|
| 263 | ' <div class="col-md-6">\n' +
|
---|
[1413ee2] | 264 | ' <input class="form-check-input" type="checkbox" ';
|
---|
| 265 |
|
---|
| 266 | if (obj['serviceStatus'] === 'ACTIVE') {
|
---|
| 267 | element += 'checked';
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | element += ' value=\"' + obj.id + '\" id=\"' + obj.id + '\">\n' +
|
---|
[77205be] | 271 | ' <label class="form-check-label" for=\"' + obj.id + '\">\n' + obj['serviceType'].name + '</label>\n' +
|
---|
| 272 | ' </div>' +
|
---|
| 273 | ' <div class=\"form-outline col-md-2 d-grid\">' +
|
---|
| 274 | ' <input type=\"text\" id=\"' + obj.id + obj['serviceType'].name.replace(/\s/g, "") + "duration" + '\" class=\"form-control\" placeholder="time" value=\"' + obj.duration + '" />' +
|
---|
| 275 | ' </div>' +
|
---|
| 276 | ' <div class=\"form-outline col-md-2 d-grid\">' +
|
---|
| 277 | ' <input type=\"text\" id=\"' + obj.id + obj['serviceType'].name.replace(/\s/g, "") + "price" + '\" class=\"form-control\" placeholder="price" value=\"' + obj.price + '" />' +
|
---|
| 278 | ' </div>' +
|
---|
| 279 | ' </div>' +
|
---|
[f29cd58] | 280 | ' <div class="row" style="margin-top:10px;">' +
|
---|
| 281 | ' <div class="form-outline col-md-10 d-grid">' +
|
---|
| 282 | ' <input type="text" id="' + obj.id + obj['serviceType'].name.replace(/\s/g, "") + "description" + '" class="form-control" placeholder="description" value=\"' + obj.description + '" />' +
|
---|
| 283 | ' </div>' +
|
---|
| 284 | ' </div>' +
|
---|
[1413ee2] | 285 | '</div>';
|
---|
| 286 |
|
---|
| 287 | $el.append(element);
|
---|
[77205be] | 288 | });
|
---|
[1413ee2] | 289 | return business;
|
---|
[77205be] | 290 | });
|
---|
[f29cd58] | 291 | }
|
---|