db/heromodels/src/models/calendar/calendar.rs
2025-06-25 20:46:15 +03:00

359 lines
11 KiB
Rust

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, Default)]
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,
}
impl AttendanceStatus {
/// Convert a string to an AttendanceStatus
pub fn from_string(s: &str) -> Result<Self, String> {
match s {
"Accepted" => Ok(AttendanceStatus::Accepted),
"Declined" => Ok(AttendanceStatus::Declined),
"Tentative" => Ok(AttendanceStatus::Tentative),
"NoResponse" => Ok(AttendanceStatus::NoResponse),
_ => Err(format!("Invalid attendance status: '{}'", s)),
}
}
/// Convert an AttendanceStatus to a string
pub fn to_string(&self) -> String {
match self {
AttendanceStatus::Accepted => "Accepted".to_string(),
AttendanceStatus::Declined => "Declined".to_string(),
AttendanceStatus::Tentative => "Tentative".to_string(),
AttendanceStatus::NoResponse => "NoResponse".to_string(),
}
}
}
/// Represents an attendee of an event
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType, Default)]
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
#[index]
pub title: String,
/// Optional description of the event
pub description: Option<String>,
/// Start time of the event (Unix timestamp)
pub start_time: i64,
/// End time of the event (Unix timestamp)
pub end_time: i64,
/// List of attendees for the event
pub attendees: Vec<Attendee>,
/// 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)
}
}
impl Event {
/// Creates a new event
pub fn new() -> Self {
let now = chrono::Utc::now().timestamp();
Self {
base_data: BaseModelData::new(),
title: String::new(),
description: None,
start_time: now,
end_time: now + 3600, // Add 1 hour in seconds
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 user_id
pub fn remove_attendee(mut self, contact_id: u32) -> Self {
self.attendees.retain(|a| a.contact_id != contact_id);
self
}
/// Updates the status of an existing attendee
pub fn update_attendee_status(mut self, contact_id: u32, status: AttendanceStatus) -> Self {
if let Some(attendee) = self
.attendees
.iter_mut()
.find(|a| a.contact_id == contact_id)
{
attendee.status = status;
}
self
}
/// Reschedules the event to new start and end times
pub fn reschedule(mut self, new_start_time: i64, new_end_time: i64) -> 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
// Temporarily removed rhai_model_export macro to fix compilation issues
// #[rhai_model_export(
// db_type = "std::sync::Arc<crate::db::hero::OurDB>",
// )]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[model]
pub struct Calendar {
/// Base model data
pub base_data: BaseModelData,
/// Name of the calendar
#[index]
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
}
}