- Added `to_json` method to serialize Event to JSON string. - Added `from_json` method to deserialize Event from JSON string. - This allows for easier data exchange and persistence.
353 lines
10 KiB
Rust
353 lines
10 KiB
Rust
use chrono::{DateTime, Utc};
|
|
use heromodels_core::BaseModelData;
|
|
use heromodels_derive::model;
|
|
use rhai::{CustomType, TypeBuilder};
|
|
use rhai_autobind_macros::rhai_model_export;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
/// Represents the status of an attendee for an event
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
pub enum AttendanceStatus {
|
|
Accepted = 0,
|
|
Declined = 1,
|
|
Tentative = 2,
|
|
NoResponse = 3,
|
|
}
|
|
|
|
/// Represents the status of an event
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
pub enum EventStatus {
|
|
Draft = 0,
|
|
Published = 1,
|
|
Cancelled = 2,
|
|
}
|
|
|
|
/// Represents an attendee of an event
|
|
#[model]
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
pub struct Attendee {
|
|
/// Base model data
|
|
pub base_data: BaseModelData,
|
|
/// ID of the user attending
|
|
pub contact_id: u32,
|
|
/// Attendance status of the user for the event
|
|
pub status: AttendanceStatus,
|
|
}
|
|
|
|
impl Attendee {
|
|
/// Creates a new attendee with auto-generated ID
|
|
pub fn new(contact_id: u32) -> Self {
|
|
Self {
|
|
base_data: BaseModelData::new(), // ID will be auto-generated by OurDB
|
|
contact_id,
|
|
status: AttendanceStatus::NoResponse,
|
|
}
|
|
}
|
|
|
|
/// Creates a new attendee with optional ID (use None for auto-generated ID)
|
|
pub fn new_with_id(id: Option<u32>, contact_id: u32) -> Self {
|
|
let mut base_data = BaseModelData::new();
|
|
if let Some(id) = id {
|
|
base_data.update_id(id);
|
|
}
|
|
|
|
Self {
|
|
base_data,
|
|
contact_id,
|
|
status: AttendanceStatus::NoResponse,
|
|
}
|
|
}
|
|
|
|
pub fn status(mut self, status: AttendanceStatus) -> Self {
|
|
self.status = status;
|
|
self
|
|
}
|
|
}
|
|
|
|
/// Represents an event in a calendar
|
|
#[model]
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)]
|
|
pub struct Event {
|
|
/// Base model data
|
|
pub base_data: BaseModelData,
|
|
/// Title of the event
|
|
pub title: String,
|
|
/// Optional description of the event
|
|
pub description: Option<String>,
|
|
/// Start time of the event
|
|
pub start_time: DateTime<Utc>,
|
|
/// End time of the event
|
|
pub end_time: DateTime<Utc>,
|
|
/// List of attendee IDs for the event
|
|
pub attendees: Vec<u32>,
|
|
/// Optional location of the event
|
|
pub location: Option<String>,
|
|
/// Color for the event (hex color code)
|
|
pub color: Option<String>,
|
|
/// Whether this is an all-day event
|
|
pub all_day: bool,
|
|
/// ID of the user who created the event
|
|
pub created_by: Option<u32>,
|
|
/// Status of the event
|
|
pub status: EventStatus,
|
|
/// Whether this is a recurring event
|
|
pub is_recurring: bool,
|
|
/// Optional timezone for display purposes
|
|
pub timezone: Option<String>,
|
|
/// Optional category/tag for the event
|
|
pub category: Option<String>,
|
|
/// Optional reminder settings (minutes before event)
|
|
pub reminder_minutes: Option<i32>,
|
|
}
|
|
|
|
impl Event {
|
|
/// Converts the event to a JSON string
|
|
pub fn to_json(&self) -> Result<String, serde_json::Error> {
|
|
serde_json::to_string(self)
|
|
}
|
|
|
|
/// Creates an event from a JSON string
|
|
pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
|
|
serde_json::from_str(json)
|
|
}
|
|
|
|
/// Creates a new event with auto-generated ID
|
|
pub fn new(title: impl ToString, start_time: DateTime<Utc>, end_time: DateTime<Utc>) -> Self {
|
|
Self {
|
|
base_data: BaseModelData::new(), // ID will be auto-generated by OurDB
|
|
title: title.to_string(),
|
|
description: None,
|
|
start_time,
|
|
end_time,
|
|
attendees: Vec::new(),
|
|
location: None,
|
|
color: Some("#4285F4".to_string()), // Default blue color
|
|
all_day: false,
|
|
created_by: None,
|
|
status: EventStatus::Published,
|
|
is_recurring: false,
|
|
timezone: None,
|
|
category: None,
|
|
reminder_minutes: None,
|
|
}
|
|
}
|
|
|
|
/// Creates a new event with optional ID (use None for auto-generated ID)
|
|
pub fn new_with_id(
|
|
id: Option<u32>,
|
|
title: impl ToString,
|
|
start_time: DateTime<Utc>,
|
|
end_time: DateTime<Utc>,
|
|
) -> Self {
|
|
let mut base_data = BaseModelData::new();
|
|
if let Some(id) = id {
|
|
base_data.update_id(id);
|
|
}
|
|
|
|
Self {
|
|
base_data,
|
|
title: title.to_string(),
|
|
description: None,
|
|
start_time,
|
|
end_time,
|
|
attendees: Vec::new(),
|
|
location: None,
|
|
color: Some("#4285F4".to_string()), // Default blue color
|
|
all_day: false,
|
|
created_by: None,
|
|
status: EventStatus::Published,
|
|
is_recurring: false,
|
|
timezone: None,
|
|
category: None,
|
|
reminder_minutes: None,
|
|
}
|
|
}
|
|
|
|
/// Sets the title for the event
|
|
pub fn title(mut self, title: impl ToString) -> Self {
|
|
self.title = title.to_string();
|
|
self
|
|
}
|
|
|
|
/// Sets the description for the event
|
|
pub fn description(mut self, description: impl ToString) -> Self {
|
|
self.description = Some(description.to_string());
|
|
self
|
|
}
|
|
|
|
/// Sets the location for the event
|
|
pub fn location(mut self, location: impl ToString) -> Self {
|
|
self.location = Some(location.to_string());
|
|
self
|
|
}
|
|
|
|
/// Sets the color for the event
|
|
pub fn color(mut self, color: impl ToString) -> Self {
|
|
self.color = Some(color.to_string());
|
|
self
|
|
}
|
|
|
|
/// Sets whether this is an all-day event
|
|
pub fn all_day(mut self, all_day: bool) -> Self {
|
|
self.all_day = all_day;
|
|
self
|
|
}
|
|
|
|
/// Sets the creator of the event
|
|
pub fn created_by(mut self, user_id: u32) -> Self {
|
|
self.created_by = Some(user_id);
|
|
self
|
|
}
|
|
|
|
/// Sets the status of the event
|
|
pub fn status(mut self, status: EventStatus) -> Self {
|
|
self.status = status;
|
|
self
|
|
}
|
|
|
|
/// Sets whether this is a recurring event
|
|
pub fn is_recurring(mut self, is_recurring: bool) -> Self {
|
|
self.is_recurring = is_recurring;
|
|
self
|
|
}
|
|
|
|
/// Sets the timezone for the event
|
|
pub fn timezone(mut self, timezone: impl ToString) -> Self {
|
|
self.timezone = Some(timezone.to_string());
|
|
self
|
|
}
|
|
|
|
/// Sets the category for the event
|
|
pub fn category(mut self, category: impl ToString) -> Self {
|
|
self.category = Some(category.to_string());
|
|
self
|
|
}
|
|
|
|
/// Sets reminder minutes before the event
|
|
pub fn reminder_minutes(mut self, minutes: i32) -> Self {
|
|
self.reminder_minutes = Some(minutes);
|
|
self
|
|
}
|
|
|
|
/// Adds an attendee ID to the event
|
|
pub fn add_attendee(mut self, attendee_id: u32) -> Self {
|
|
// Prevent duplicate attendees by ID
|
|
if !self.attendees.iter().any(|&a_id| a_id == attendee_id) {
|
|
self.attendees.push(attendee_id);
|
|
}
|
|
self
|
|
}
|
|
|
|
/// Removes an attendee from the event by attendee ID
|
|
pub fn remove_attendee(mut self, attendee_id: u32) -> Self {
|
|
self.attendees.retain(|&a_id| a_id != attendee_id);
|
|
self
|
|
}
|
|
|
|
/// Reschedules the event to new start and end times
|
|
pub fn reschedule(
|
|
mut self,
|
|
new_start_time: DateTime<Utc>,
|
|
new_end_time: DateTime<Utc>,
|
|
) -> Self {
|
|
// Basic validation: end_time should be after start_time
|
|
if new_end_time > new_start_time {
|
|
self.start_time = new_start_time;
|
|
self.end_time = new_end_time;
|
|
}
|
|
// Optionally, add error handling or return a Result type
|
|
self
|
|
}
|
|
}
|
|
|
|
/// Represents a calendar with events
|
|
#[rhai_model_export(db_type = "std::sync::Arc<crate::db::hero::OurDB>")]
|
|
#[model]
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)]
|
|
pub struct Calendar {
|
|
/// Base model data
|
|
pub base_data: BaseModelData,
|
|
/// Name of the calendar
|
|
pub name: String,
|
|
/// Optional description of the calendar
|
|
pub description: Option<String>,
|
|
/// List of event IDs in the calendar
|
|
pub events: Vec<i64>,
|
|
/// ID of the user who owns this calendar
|
|
pub owner_id: Option<u32>,
|
|
/// Whether this calendar is public
|
|
pub is_public: bool,
|
|
/// Color theme for the calendar (hex color code)
|
|
pub color: Option<String>,
|
|
}
|
|
|
|
impl Calendar {
|
|
/// Creates a new calendar with auto-generated ID
|
|
///
|
|
/// # Arguments
|
|
/// * `id` - Optional ID for the calendar (use None for auto-generated ID)
|
|
/// * `name` - Name of the calendar
|
|
pub fn new(id: Option<u32>, name: impl ToString) -> Self {
|
|
let mut base_data = BaseModelData::new();
|
|
if let Some(id) = id {
|
|
base_data.update_id(id);
|
|
}
|
|
|
|
Self {
|
|
base_data,
|
|
name: name.to_string(),
|
|
description: None,
|
|
events: Vec::new(),
|
|
owner_id: None,
|
|
is_public: false,
|
|
color: Some("#4285F4".to_string()), // Default blue color
|
|
}
|
|
}
|
|
|
|
/// Sets the name for the calendar
|
|
pub fn name(mut self, name: impl ToString) -> Self {
|
|
self.name = name.to_string();
|
|
self
|
|
}
|
|
|
|
/// Sets the description for the calendar
|
|
pub fn description(mut self, description: impl ToString) -> Self {
|
|
self.description = Some(description.to_string());
|
|
self
|
|
}
|
|
|
|
/// Sets the owner of the calendar
|
|
pub fn owner_id(mut self, user_id: u32) -> Self {
|
|
self.owner_id = Some(user_id);
|
|
self
|
|
}
|
|
|
|
/// Sets whether the calendar is public
|
|
pub fn is_public(mut self, is_public: bool) -> Self {
|
|
self.is_public = is_public;
|
|
self
|
|
}
|
|
|
|
/// Sets the color for the calendar
|
|
pub fn color(mut self, color: impl ToString) -> Self {
|
|
self.color = Some(color.to_string());
|
|
self
|
|
}
|
|
|
|
/// Adds an event to the calendar
|
|
pub fn add_event(mut self, event_id: i64) -> Self {
|
|
// Prevent duplicate events by id
|
|
if !self.events.iter().any(|e_id| *e_id == event_id) {
|
|
self.events.push(event_id);
|
|
}
|
|
self
|
|
}
|
|
|
|
/// Removes an event from the calendar by its ID
|
|
pub fn remove_event(mut self, event_id_to_remove: i64) -> Self {
|
|
self.events
|
|
.retain(|&event_id_in_vec| event_id_in_vec != event_id_to_remove);
|
|
self
|
|
}
|
|
}
|