74 lines
2.0 KiB
Plaintext
74 lines
2.0 KiB
Plaintext
// calendars.rhai - Functions for handling calendars
|
|
|
|
import "utils" as utils;
|
|
import "../models/calendar_model" as calendar_model;
|
|
|
|
// Get all calendars
|
|
fn get_calendars() {
|
|
return calendar_model::get_sample_calendars();
|
|
}
|
|
|
|
// Get a specific calendar by ID
|
|
fn get_calendar(calendar_id) {
|
|
let calendars = get_all_calendars();
|
|
return utils::find_by_id(calendars, calendar_id);
|
|
}
|
|
|
|
// Get calendars owned by a specific user
|
|
fn get_calendars_by_owner(user_id) {
|
|
let calendars = get_all_calendars();
|
|
return utils::filter_by_property(calendars, "owner_id", user_id);
|
|
}
|
|
|
|
// Get calendars shared with a specific user
|
|
fn get_calendars_shared_with(user_id) {
|
|
let calendars = get_all_calendars();
|
|
let shared_calendars = [];
|
|
|
|
for calendar in calendars {
|
|
if utils::array_contains(calendar.shared_with, user_id) {
|
|
shared_calendars.push(calendar);
|
|
}
|
|
}
|
|
|
|
return shared_calendars;
|
|
}
|
|
|
|
// Get all calendars accessible by a user (owned + shared)
|
|
fn get_accessible_calendars(user_id) {
|
|
let owned = get_calendars_by_owner(user_id);
|
|
let shared_calendars = get_calendars_shared_with(user_id);
|
|
|
|
// Combine the two arrays
|
|
for calendar in shared_calendars {
|
|
owned.push(calendar);
|
|
}
|
|
|
|
return owned;
|
|
}
|
|
|
|
// Create a new calendar
|
|
fn create_calendar(name, owner_id, description, color, shared_with, visibility) {
|
|
// Generate a simple ID (in a real app, we would use a proper ID generation method)
|
|
let id = "cal" + (get_all_calendars().len() + 1).to_string();
|
|
|
|
return calendar_model::create_calendar(
|
|
id, name, owner_id, description, color, shared_with, visibility
|
|
);
|
|
}
|
|
|
|
// Format calendar details for display
|
|
fn format_calendar(calendar) {
|
|
if utils::is_empty(calendar) {
|
|
return "Calendar not found";
|
|
}
|
|
|
|
let shared_with_count = calendar.shared_with.len();
|
|
|
|
return `${calendar.name} (${calendar.color})
|
|
Description: ${calendar.description}
|
|
Owner: ${calendar.owner_id}
|
|
Visibility: ${calendar.visibility}
|
|
Shared with: ${shared_with_count} users`;
|
|
}
|