|
| 1 | +// Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and contributors |
| 2 | +// For license information, please see license.txt |
| 3 | + |
| 4 | +function time_to_seconds(time_str) { |
| 5 | + // Convert time string of format HH:MM:SS into seconds. |
| 6 | + let seq = time_str.split(':'); |
| 7 | + seq = seq.map((n) => parseInt(n)); |
| 8 | + return (seq[0]*60*60) + (seq[1]*60) + seq[2]; |
| 9 | +} |
| 10 | + |
| 11 | +function number_sort(array, ascending=true) { |
| 12 | + let array_copy = [...array]; |
| 13 | + if (ascending) { |
| 14 | + array_copy.sort((a, b) => a-b); // ascending order |
| 15 | + } else { |
| 16 | + array_copy.sort((a, b) => b-a); // descending order |
| 17 | + } |
| 18 | + return array_copy; |
| 19 | +} |
| 20 | + |
| 21 | +function groupby(items, key) { |
| 22 | + // Group the list of items using the given key. |
| 23 | + const obj = {}; |
| 24 | + items.forEach((item) => { |
| 25 | + if (item[key] in obj) { |
| 26 | + obj[item[key]].push(item); |
| 27 | + } else { |
| 28 | + obj[item[key]] = [item]; |
| 29 | + } |
| 30 | + }); |
| 31 | + return obj; |
| 32 | +} |
| 33 | + |
| 34 | +function check_timeslot_overlap(ts1, ts2) { |
| 35 | + /// Timeslot is a an array of length 2 ex: [from_time, to_time] |
| 36 | + /// time in timeslot is an integer represents number of seconds. |
| 37 | + if ((ts1[0] < ts2[0] && ts1[1] <= ts2[0]) || (ts1[0] >= ts2[1] && ts1[1] > ts2[1])) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + return true; |
| 41 | +} |
| 42 | + |
| 43 | +function validate_call_schedule(schedule) { |
| 44 | + validate_call_schedule_timeslot(schedule); |
| 45 | + validate_call_schedule_overlaps(schedule); |
| 46 | +} |
| 47 | + |
| 48 | +function validate_call_schedule_timeslot(schedule) { |
| 49 | + // Make sure that to time slot is ahead of from time slot. |
| 50 | + let errors = []; |
| 51 | + |
| 52 | + for (let row in schedule) { |
| 53 | + let record = schedule[row]; |
| 54 | + let from_time_in_secs = time_to_seconds(record.from_time); |
| 55 | + let to_time_in_secs = time_to_seconds(record.to_time); |
| 56 | + if (from_time_in_secs >= to_time_in_secs) { |
| 57 | + errors.push(__('Call Schedule Row {0}: To time slot should always be ahead of From time slot.', [row])); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (errors.length > 0) { |
| 62 | + frappe.throw(errors.join("<br/>")); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +function is_call_schedule_overlapped(day_schedule) { |
| 67 | + // Check if any time slots are overlapped in a day schedule. |
| 68 | + let timeslots = []; |
| 69 | + day_schedule.forEach((record)=> { |
| 70 | + timeslots.push([time_to_seconds(record.from_time), time_to_seconds(record.to_time)]); |
| 71 | + }); |
| 72 | + |
| 73 | + if (timeslots.length < 2) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + timeslots = number_sort(timeslots); |
| 78 | + |
| 79 | + // Sorted timeslots will be in ascending order if not overlapped. |
| 80 | + for (let i=1; i < timeslots.length; i++) { |
| 81 | + if (check_timeslot_overlap(timeslots[i-1], timeslots[i])) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + } |
| 85 | + return false; |
| 86 | +} |
| 87 | + |
| 88 | +function validate_call_schedule_overlaps(schedule) { |
| 89 | + let group_by_day = groupby(schedule, 'day_of_week'); |
| 90 | + for (const [day, day_schedule] of Object.entries(group_by_day)) { |
| 91 | + if (is_call_schedule_overlapped(day_schedule)) { |
| 92 | + frappe.throw(__('Please fix overlapping time slots for {0}', [day])); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +frappe.ui.form.on('Incoming Call Settings', { |
| 98 | + validate(frm) { |
| 99 | + validate_call_schedule(frm.doc.call_handling_schedule); |
| 100 | + } |
| 101 | +}); |
| 102 | + |
0 commit comments