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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use crate::{
shared::entity::{Entity, ID},
Meta, Metadata,
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(tag = "variant", content = "id")]
pub enum TimePlan {
Calendar(ID),
Schedule(ID),
Empty,
}
#[derive(Clone, Debug, Serialize)]
pub struct ServiceResource {
pub id: ID,
pub user_id: ID,
pub availibility: TimePlan,
pub busy: Vec<ID>,
pub buffer: i64,
pub closest_booking_time: i64,
pub furthest_booking_time: Option<i64>,
}
impl ServiceResource {
pub fn new(user_id: ID, availibility: TimePlan, busy: Vec<ID>) -> Self {
Self {
id: Default::default(),
user_id,
availibility,
busy,
buffer: 0,
closest_booking_time: 0,
furthest_booking_time: None,
}
}
pub fn set_availibility(&mut self, availibility: TimePlan) {
self.availibility = availibility;
}
pub fn set_busy(&mut self, busy: Vec<ID>) {
self.busy = busy;
}
pub fn set_buffer(&mut self, buffer: i64) -> bool {
let min_buffer = 0;
let max_buffer = 60 * 12;
if buffer < min_buffer || buffer > max_buffer {
return false;
}
self.buffer = buffer;
true
}
pub fn get_calendar_ids(&self) -> Vec<ID> {
let mut calendar_ids = self.busy.clone();
if let TimePlan::Calendar(id) = &self.availibility {
calendar_ids.push(id.clone());
}
calendar_ids
}
pub fn get_schedule_id(&self) -> Option<ID> {
match &self.availibility {
TimePlan::Schedule(id) => Some(id.clone()),
_ => None,
}
}
pub fn contains_calendar(&self, calendar_id: &ID) -> bool {
match &self.availibility {
TimePlan::Calendar(id) if id == calendar_id => {
return true;
}
_ => (),
}
self.busy.contains(calendar_id)
}
pub fn remove_calendar(&mut self, calendar_id: &ID) {
match &self.availibility {
TimePlan::Calendar(id) if id == calendar_id => {
self.availibility = TimePlan::Empty;
}
_ => (),
}
self.busy.retain(|cal_id| cal_id != calendar_id);
}
pub fn contains_schedule(&self, schedule_id: &ID) -> bool {
matches!(&self.availibility, TimePlan::Schedule(id) if id == schedule_id)
}
pub fn remove_schedule(&mut self, schedule_id: &ID) {
match &self.availibility {
TimePlan::Schedule(id) if id == schedule_id => {
self.availibility = TimePlan::Empty;
}
_ => (),
}
}
}
#[derive(Clone, Debug)]
pub struct Service {
pub id: ID,
pub account_id: ID,
pub users: Vec<ServiceResource>,
pub metadata: Metadata,
}
impl Entity for Service {
fn id(&self) -> &ID {
&self.id
}
}
impl Meta for Service {
fn metadata(&self) -> &Metadata {
&self.metadata
}
fn account_id(&self) -> &ID {
&self.account_id
}
}
impl Service {
pub fn new(account_id: ID) -> Self {
Self {
id: Default::default(),
account_id,
users: Default::default(),
metadata: Default::default(),
}
}
pub fn add_user(&mut self, user: ServiceResource) {
self.users.push(user);
}
pub fn remove_user(&mut self, user_id: &ID) -> Option<ServiceResource> {
for (pos, user) in self.users.iter().enumerate() {
if user.user_id == *user_id {
return Some(self.users.remove(pos));
}
}
None
}
pub fn find_user(&self, user_id: &ID) -> Option<&ServiceResource> {
self.users.iter().find(|u| u.user_id == *user_id)
}
pub fn find_user_mut(&mut self, user_id: &ID) -> Option<&mut ServiceResource> {
self.users.iter_mut().find(|u| u.user_id == *user_id)
}
}