| 1 | use crate::constant::{MAX_EVENT_DESCRIPTION_LEN, MAX_EVENT_TITLE_LEN}; |
| 2 | use crate::domain::class_group::ClassGroupId; |
| 3 | use crate::domain::course::CourseId; |
| 4 | use crate::domain::monotonic_id::next_uuid; |
| 5 | use crate::domain::role::Role; |
| 6 | use crate::domain::timestamp::Timestamp; |
| 7 | use crate::domain::user::UserId; |
| 8 | use crate::error::{AppError, ValidationError}; |
| 9 | use crate::validate::{validate_optional, validate_required}; |
| 10 | |
| 11 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] |
| 12 | #[sqlx(transparent)] |
| 13 | pub struct EventId(uuid::Uuid); |
| 14 | |
| 15 | impl EventId { |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | pub fn generate() -> Self { |
| 21 | Self(next_uuid()) |
| 22 | } |
| 23 | |
| 24 | |
| 25 | |
| 26 | pub fn uuid(&self) -> uuid::Uuid { |
| 27 | self.0 |
| 28 | } |
| 29 | |
| 30 | |
| 31 | |
| 32 | pub fn from_key(key: &str) -> Self { |
| 33 | Self(uuid::Uuid::parse_str(key).unwrap_or(uuid::Uuid::nil())) |
| 34 | } |
| 35 | |
| 36 | pub fn key(&self) -> String { |
| 37 | self.0.to_string() |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] |
| 42 | #[sqlx(transparent)] |
| 43 | pub struct EventTitle(String); |
| 44 | |
| 45 | impl EventTitle { |
| 46 | pub fn try_new(value: &str) -> Result<Self, ValidationError> { |
| 47 | validate_required("title", value, MAX_EVENT_TITLE_LEN)?; |
| 48 | Ok(Self(value.to_string())) |
| 49 | } |
| 50 | |
| 51 | pub fn as_str(&self) -> &str { |
| 52 | &self.0 |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] |
| 57 | #[sqlx(transparent)] |
| 58 | pub struct EventDescription(String); |
| 59 | |
| 60 | impl EventDescription { |
| 61 | pub fn try_new(value: &str) -> Result<Self, ValidationError> { |
| 62 | validate_optional("description", value, MAX_EVENT_DESCRIPTION_LEN)?; |
| 63 | Ok(Self(value.to_string())) |
| 64 | } |
| 65 | |
| 66 | pub fn as_str(&self) -> &str { |
| 67 | &self.0 |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | #[derive(Debug, Clone, Copy, PartialEq, Eq, sqlx::Type)] |
| 85 | #[sqlx(type_name = "TEXT", rename_all = "lowercase")] |
| 86 | pub enum EventAudienceKind { |
| 87 | |
| 88 | School, |
| 89 | |
| 90 | Role, |
| 91 | |
| 92 | |
| 93 | Course, |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | Class, |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | Registration, |
| 107 | } |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | #[derive(Debug, Clone, sqlx::FromRow)] |
| 114 | pub struct Event { |
| 115 | pub(crate) id: EventId, |
| 116 | pub(crate) creator: UserId, |
| 117 | pub(crate) title: EventTitle, |
| 118 | pub(crate) description: EventDescription, |
| 119 | pub(crate) audience_kind: EventAudienceKind, |
| 120 | pub(crate) audience_role: Option<Role>, |
| 121 | pub(crate) audience_course: Option<CourseId>, |
| 122 | pub(crate) audience_class: Option<ClassGroupId>, |
| 123 | pub(crate) audience_capacity: Option<i64>, |
| 124 | pub(crate) starts_at: Option<Timestamp>, |
| 125 | pub(crate) ends_at: Option<Timestamp>, |
| 126 | } |
| 127 | |
| 128 | impl EventAudienceKind { |
| 129 | |
| 130 | |
| 131 | pub fn as_str(self) -> &'static str { |
| 132 | match self { |
| 133 | EventAudienceKind::School => "school", |
| 134 | EventAudienceKind::Role => "role", |
| 135 | |
| 136 | EventAudienceKind::Course => "course", |
| 137 | EventAudienceKind::Class => "class", |
| 138 | EventAudienceKind::Registration => "registration", |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 148 | pub struct EventAudience { |
| 149 | pub kind: EventAudienceKind, |
| 150 | pub role: Option<Role>, |
| 151 | pub course: Option<CourseId>, |
| 152 | pub class: Option<ClassGroupId>, |
| 153 | pub capacity: Option<i64>, |
| 154 | } |
| 155 | |
| 156 | impl EventAudience { |
| 157 | |
| 158 | pub fn of_row(event: &Event) -> Self { |
| 159 | Self { |
| 160 | kind: event.get_audience_kind(), |
| 161 | role: event.get_audience_role(), |
| 162 | course: event.get_audience_course().cloned(), |
| 163 | class: event.get_audience_class().cloned(), |
| 164 | capacity: event.get_audience_capacity(), |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | impl Event { |
| 170 | pub fn get_id(&self) -> &EventId { |
| 171 | &self.id |
| 172 | } |
| 173 | |
| 174 | pub fn get_creator(&self) -> &UserId { |
| 175 | &self.creator |
| 176 | } |
| 177 | |
| 178 | pub fn get_title(&self) -> &EventTitle { |
| 179 | &self.title |
| 180 | } |
| 181 | |
| 182 | pub fn get_description(&self) -> &EventDescription { |
| 183 | &self.description |
| 184 | } |
| 185 | |
| 186 | |
| 187 | |
| 188 | pub fn get_audience_kind(&self) -> EventAudienceKind { |
| 189 | self.audience_kind |
| 190 | } |
| 191 | |
| 192 | pub fn get_audience_role(&self) -> Option<Role> { |
| 193 | self.audience_role |
| 194 | } |
| 195 | |
| 196 | pub fn get_audience_course(&self) -> Option<&CourseId> { |
| 197 | self.audience_course.as_ref() |
| 198 | } |
| 199 | |
| 200 | pub fn get_audience_class(&self) -> Option<&ClassGroupId> { |
| 201 | self.audience_class.as_ref() |
| 202 | } |
| 203 | |
| 204 | pub fn get_audience_capacity(&self) -> Option<i64> { |
| 205 | self.audience_capacity |
| 206 | } |
| 207 | |
| 208 | pub fn get_starts_at(&self) -> Option<Timestamp> { |
| 209 | self.starts_at |
| 210 | } |
| 211 | |
| 212 | pub fn get_ends_at(&self) -> Option<Timestamp> { |
| 213 | self.ends_at |
| 214 | } |
| 215 | |
| 216 | pub fn is_creator(&self, user: &UserId) -> bool { |
| 217 | &self.creator == user |
| 218 | } |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | pub fn registration_capacity(&self) -> Result<Option<i64>, AppError> { |
| 226 | if self.audience_kind != EventAudienceKind::Registration { |
| 227 | return Err(AppError::Validation(ValidationError::Invalid { |
| 228 | field: "audience", |
| 229 | reason: "this event does not take registrations", |
| 230 | })); |
| 231 | } |
| 232 | |
| 233 | if let Some(closes_at) = self.starts_at.or(self.ends_at) |
| 234 | && Timestamp::now().as_millis() >= closes_at.as_millis() |
| 235 | { |
| 236 | return Err(AppError::Conflict( |
| 237 | "registration closed — the event has started or ended", |
| 238 | )); |
| 239 | } |
| 240 | Ok(self.audience_capacity) |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | #[cfg(test)] |
| 245 | mod tests { |
| 246 | use super::*; |
| 247 | |
| 248 | #[tokio::test] |
| 249 | async fn title_is_required() { |
| 250 | assert!(EventTitle::try_new("standup").is_ok()); |
| 251 | assert!(EventTitle::try_new("").is_err()); |
| 252 | assert!(EventTitle::try_new(" ").is_err()); |
| 253 | } |
| 254 | |
| 255 | #[tokio::test] |
| 256 | async fn description_is_optional() { |
| 257 | assert!(EventDescription::try_new("").is_ok()); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | #[test] |
| 264 | fn audience_kind_spellings_are_frozen() { |
| 265 | assert_eq!(EventAudienceKind::School.as_str(), "school"); |
| 266 | assert_eq!(EventAudienceKind::Role.as_str(), "role"); |
| 267 | assert_eq!(EventAudienceKind::Course.as_str(), "course"); |
| 268 | assert_eq!(EventAudienceKind::Class.as_str(), "class"); |
| 269 | assert_eq!(EventAudienceKind::Registration.as_str(), "registration"); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | fn event_with( |
| 274 | audience_kind: EventAudienceKind, |
| 275 | audience_capacity: Option<i64>, |
| 276 | starts_at: Option<Timestamp>, |
| 277 | ends_at: Option<Timestamp>, |
| 278 | ) -> Event { |
| 279 | Event { |
| 280 | id: EventId::generate(), |
| 281 | creator: UserId::from_key("0198f1a2-3b4c-7d5e-8f90-aa2b3c4d5e6f"), |
| 282 | title: EventTitle::try_new("signup").unwrap(), |
| 283 | description: EventDescription::try_new("").unwrap(), |
| 284 | audience_kind, |
| 285 | audience_role: None, |
| 286 | audience_course: None, |
| 287 | audience_class: None, |
| 288 | audience_capacity, |
| 289 | starts_at, |
| 290 | ends_at, |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | #[tokio::test] |
| 295 | async fn registration_capacity_gates_and_echoes_cap() { |
| 296 | let past = Some(Timestamp::from_millis(Timestamp::now().as_millis() - 1)); |
| 297 | |
| 298 | assert!(matches!( |
| 299 | event_with(EventAudienceKind::School, None, None, None).registration_capacity(), |
| 300 | Err(AppError::Validation(_)) |
| 301 | )); |
| 302 | assert_eq!( |
| 303 | event_with(EventAudienceKind::Registration, None, None, None) |
| 304 | .registration_capacity() |
| 305 | .unwrap(), |
| 306 | None |
| 307 | ); |
| 308 | assert_eq!( |
| 309 | event_with(EventAudienceKind::Registration, Some(30), None, None) |
| 310 | .registration_capacity() |
| 311 | .unwrap(), |
| 312 | Some(30) |
| 313 | ); |
| 314 | |
| 315 | |
| 316 | assert!(matches!( |
| 317 | event_with(EventAudienceKind::Registration, Some(30), past, None) |
| 318 | .registration_capacity(), |
| 319 | Err(AppError::Conflict(_)) |
| 320 | )); |
| 321 | assert!(matches!( |
| 322 | event_with(EventAudienceKind::Registration, None, None, past).registration_capacity(), |
| 323 | Err(AppError::Conflict(_)) |
| 324 | )); |
| 325 | } |
| 326 | } |