initial work for organizations
Some checks failed
Build and Push Docker Image / build-and-push (push) Failing after 1m30s
Some checks failed
Build and Push Docker Image / build-and-push (push) Failing after 1m30s
This commit is contained in:
parent
4cd399f2a0
commit
a846093fbf
7
migrations/0003_create_organizations_table.sql
Normal file
7
migrations/0003_create_organizations_table.sql
Normal 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()
|
||||||
|
);
|
||||||
13
migrations/0004_create_org_membership_table.sql
Normal file
13
migrations/0004_create_org_membership_table.sql
Normal 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)
|
||||||
|
);
|
||||||
@ -1,2 +1,3 @@
|
|||||||
|
pub mod organization;
|
||||||
pub mod refresh_token;
|
pub mod refresh_token;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
|||||||
34
src/db/model/organization.rs
Normal file
34
src/db/model/organization.rs
Normal 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>,
|
||||||
|
}
|
||||||
@ -1,2 +1,3 @@
|
|||||||
|
pub mod organization_repository;
|
||||||
pub mod refresh_token_repository;
|
pub mod refresh_token_repository;
|
||||||
pub mod user_repository;
|
pub mod user_repository;
|
||||||
|
|||||||
44
src/db/repository/organization_repository.rs
Normal file
44
src/db/repository/organization_repository.rs
Normal 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)
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user