initial work for organizations
Some checks failed
Build and Push Docker Image / build-and-push (push) Failing after 1m30s

This commit is contained in:
Dmitri 2026-05-04 23:30:41 +02:00
parent 4cd399f2a0
commit a846093fbf
Signed by: kanopo
GPG Key ID: 759ADD40E3132AC7
6 changed files with 100 additions and 0 deletions

View File

@ -0,0 +1,7 @@
create table organizations (
id uuid primary key default uuidv4(),
name varchar(255) not null,
slug varchar(255) not null unique, -- acme-corp-a7x9
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);

View File

@ -0,0 +1,13 @@
CREATE TYPE org_role AS ENUM ('owner', 'admin', 'member', 'viewer');
CREATE TABLE org_memberships (
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
org_id UUID NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
role org_role NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, org_id)
);

View File

@ -1,2 +1,3 @@
pub mod organization;
pub mod refresh_token;
pub mod user;

View File

@ -0,0 +1,34 @@
use sqlx::{
prelude::FromRow,
types::{
Uuid,
chrono::{DateTime, Utc},
},
};
#[derive(Debug, FromRow)]
pub struct Organization {
pub id: Uuid,
pub name: String,
pub slug: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
#[derive(sqlx::Type, Debug, Clone, PartialEq)]
#[sqlx(type_name = "org_role", rename_all = "lowercase")]
pub enum OrgRole {
Owner,
Admin,
Member,
Viewer,
}
#[derive(Debug, FromRow)]
pub struct OrgMember {
pub user_id: Uuid,
pub org_id: Uuid,
pub role: OrgRole,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}

View File

@ -1,2 +1,3 @@
pub mod organization_repository;
pub mod refresh_token_repository;
pub mod user_repository;

View File

@ -0,0 +1,44 @@
use sqlx::{Executor, Postgres};
use uuid::Uuid;
use crate::{db::model::organization::Organization, errors::AppError};
pub async fn create_organization<'e, E>(
executor: E,
name: String,
slug: String,
) -> Result<Organization, AppError>
where
E: Executor<'e, Database = Postgres>,
{
let org = sqlx::query_as!(
Organization,
"insert into organizations (name, slug) values ($1, $2) returning *",
name,
slug
)
.fetch_one(executor)
.await
.map_err(AppError::from)?;
Ok(org)
}
pub async fn get_organizations_by_id_list<'e, E>(
executor: E,
ids: &[Uuid],
) -> Result<Vec<Organization>, AppError>
where
E: Executor<'e, Database = Postgres>,
{
let org = sqlx::query_as!(
Organization,
"select * from organizations where id = any($1)",
ids
)
.fetch_all(executor)
.await
.map_err(AppError::from)?;
Ok(org)
}