Skip to main content

sofie_tenancy/
lib.rs

1//! Organizations as owners: an organization area's principal is the organization, so its
2//! records are the organization's, while the account acting in it is read from the session.
3//! Members hold a role each and join by an invitation mailed to their address; with
4//! `billing`, members and pending invitations are held to the seats the plan allows.
5
6#[cfg(feature = "account-data")]
7mod account_data;
8mod acting;
9mod invitation;
10mod members;
11mod organization;
12mod organizations;
13
14use sofie::db::Migrations;
15
16#[cfg(feature = "account-data")]
17pub use account_data::OrganizationMemberships;
18pub use acting::Acting;
19pub use invitation::Invitation;
20pub use members::{ROUTES as ORGANIZATION_ROUTES, routes as organization_routes};
21pub use organization::{
22    MemberJoined, MemberLeft, MemberRoleChanged, Organization, OrganizationArea,
23    OrganizationDeleted, OrganizationId, OrganizationMember, OrganizationRole,
24};
25pub use organizations::{ROUTES as MEMBER_ROUTES, routes as member_routes};
26
27const MODULE_NAME: &str = "sofie_tenancy";
28
29/// Its routes are mounted in the app's own areas (`member_routes`, `organization_routes`).
30pub const MODULE: sofie::http::Module = sofie::http::Module {
31    migrations: Some(migrations()),
32    #[cfg(feature = "billing")]
33    jobs: worker,
34    ..sofie::http::Module::new(MODULE_NAME)
35};
36
37#[cfg(feature = "billing")]
38#[must_use]
39pub fn worker(worker: sofie::worker::Worker) -> sofie::worker::Worker {
40    worker.on::<sofie::billing::SubscriptionChanged, _>(
41        "sofie_tenancy.cancel_deleted_plan",
42        organization::cancel_deleted_plan,
43    )
44}
45
46#[must_use]
47pub const fn migrations() -> Migrations {
48    Migrations::new(
49        MODULE_NAME,
50        sofie::db::embed!(
51            "migrations",
52            ["0001_organizations.sql", "0002_organization_deleted.sql"]
53        ),
54    )
55    .sqlite(sofie::db::embed!(
56        "migrations/sqlite",
57        ["0001_organizations.sql", "0002_organization_deleted.sql"]
58    ))
59    .aggregates(AGGREGATES)
60}
61
62const AGGREGATES: &[sofie::db::AggregateTable] = &[
63    sofie::db::AggregateTable::of::<Organization>(),
64    sofie::db::AggregateTable::of::<OrganizationMember>(),
65    sofie::db::AggregateTable::of::<Invitation>(),
66];