-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from nightly-labs/team-invites
Team invites
- Loading branch information
Showing
53 changed files
with
1,635 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
CREATE TABLE team_invites( | ||
invite_id BIGSERIAL PRIMARY KEY, | ||
team_id TEXT NOT NULL REFERENCES team(team_id), | ||
user_email TEXT NOT NULL, | ||
created_at TIMESTAMPTZ NOT NULL, | ||
accepted_at TIMESTAMPTZ, | ||
cancelled_at TIMESTAMPTZ | ||
); | ||
|
||
CREATE INDEX team_invites_user_email_idx ON team_invites(user_email); | ||
CREATE INDEX team_invites_team_id_idx ON team_invites(team_id); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod select; | ||
pub mod table_struct; | ||
pub mod update; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use super::table_struct::TeamInvite; | ||
use crate::db::Db; | ||
use crate::structs::db_error::DbError; | ||
use crate::tables::team_invites::table_struct::TEAM_INVITES_TABLE_NAME; | ||
|
||
impl Db { | ||
pub async fn get_invites_by_team_id( | ||
&self, | ||
team_id: &String, | ||
active_invites: bool, | ||
) -> Result<Vec<TeamInvite>, DbError> { | ||
let additional_filter = if active_invites { | ||
"AND ti.accepted_at IS NULL AND ti.cancelled_at IS NULL" | ||
} else { | ||
"" | ||
}; | ||
|
||
let query = format!( | ||
"SELECT ti.invite_id, ti.team_id, ti.user_email, ti.created_at, | ||
ti.accepted_at, ti.cancelled_at, t.team_name, gu.email AS admin_email | ||
FROM {TEAM_INVITES_TABLE_NAME} ti | ||
INNER JOIN team t ON ti.team_id = t.team_id | ||
INNER JOIN grafana_users gu ON t.team_admin_id = gu.user_id | ||
WHERE ti.team_id = $1 {additional_filter} | ||
ORDER BY ti.created_at DESC", | ||
); | ||
|
||
let typed_query = sqlx::query_as::<_, TeamInvite>(&query).bind(team_id); | ||
|
||
return typed_query | ||
.fetch_all(&self.connection_pool) | ||
.await | ||
.map_err(|e| e.into()); | ||
} | ||
|
||
pub async fn get_invites_by_user_email( | ||
&self, | ||
user_email: &String, | ||
active_invites: bool, | ||
) -> Result<Vec<TeamInvite>, DbError> { | ||
let additional_filter = if active_invites { | ||
"AND ti.accepted_at IS NULL AND ti.cancelled_at IS NULL" | ||
} else { | ||
"" | ||
}; | ||
|
||
let query = format!( | ||
"SELECT ti.invite_id, ti.team_id, ti.user_email, ti.created_at, \ | ||
ti.accepted_at, ti.cancelled_at, t.team_name, gu.email AS admin_email \ | ||
FROM {TEAM_INVITES_TABLE_NAME} ti \ | ||
INNER JOIN team t ON ti.team_id = t.team_id \ | ||
INNER JOIN grafana_users gu ON t.team_admin_id = gu.user_id \ | ||
WHERE ti.user_email = $1 {additional_filter} \ | ||
ORDER BY ti.created_at DESC", | ||
TEAM_INVITES_TABLE_NAME = TEAM_INVITES_TABLE_NAME | ||
); | ||
|
||
let typed_query = sqlx::query_as::<_, TeamInvite>(&query).bind(user_email); | ||
|
||
return typed_query | ||
.fetch_all(&self.connection_pool) | ||
.await | ||
.map_err(|e| e.into()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use sqlx::{ | ||
postgres::PgRow, | ||
types::chrono::{DateTime, Utc}, | ||
FromRow, Row, | ||
}; | ||
|
||
pub const TEAM_INVITES_TABLE_NAME: &str = "team_invites"; | ||
pub const TEAM_INVITES_KEYS: &str = | ||
"invite_id, team_id, user_email, created_at, accepted_at, cancelled_at"; | ||
|
||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub struct TeamInvite { | ||
pub invite_id: i64, | ||
pub team_id: String, | ||
pub user_email: String, | ||
pub created_at: DateTime<Utc>, | ||
pub accepted_at: Option<DateTime<Utc>>, | ||
pub cancelled_at: Option<DateTime<Utc>>, | ||
// Not present in the table, queried from the team table | ||
pub team_name: String, | ||
pub admin_email: String, | ||
} | ||
|
||
impl FromRow<'_, PgRow> for TeamInvite { | ||
fn from_row(row: &sqlx::postgres::PgRow) -> std::result::Result<Self, sqlx::Error> { | ||
Ok(TeamInvite { | ||
invite_id: row.get("invite_id"), | ||
team_id: row.get("team_id"), | ||
user_email: row.get("user_email"), | ||
created_at: row.get("created_at"), | ||
accepted_at: row.get("accepted_at"), | ||
cancelled_at: row.get("cancelled_at"), | ||
team_name: row.get("team_name"), | ||
admin_email: row.get("admin_email"), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use super::table_struct::{TEAM_INVITES_KEYS, TEAM_INVITES_TABLE_NAME}; | ||
use crate::db::Db; | ||
use crate::structs::db_error::DbError; | ||
use crate::tables::utils::get_current_datetime; | ||
use sqlx::query; | ||
|
||
impl Db { | ||
pub async fn create_new_team_invite( | ||
&self, | ||
team_id: &String, | ||
user_email: &String, | ||
) -> Result<(), DbError> { | ||
let query_body = format!( | ||
"INSERT INTO {TEAM_INVITES_TABLE_NAME} ({TEAM_INVITES_KEYS}) VALUES (DEFAULT, $1, $2, $3, NULL, NULL)" | ||
); | ||
|
||
let query_result = query(&query_body) | ||
.bind(&team_id) | ||
.bind(&user_email) | ||
.bind(&get_current_datetime()) | ||
.execute(&self.connection_pool) | ||
.await; | ||
|
||
match query_result { | ||
Ok(_) => Ok(()), | ||
Err(e) => Err(e).map_err(|e| e.into()), | ||
} | ||
} | ||
|
||
pub async fn accept_team_invite( | ||
&self, | ||
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>, | ||
team_id: &String, | ||
user_email: &String, | ||
) -> Result<(), DbError> { | ||
let query_body = format!( | ||
"UPDATE {TEAM_INVITES_TABLE_NAME} SET accepted_at = $1 WHERE team_id = $2 AND user_email = $3 AND accepted_at IS NULL AND cancelled_at IS NULL" | ||
); | ||
|
||
let query_result = query(&query_body) | ||
.bind(&get_current_datetime()) | ||
.bind(&team_id) | ||
.bind(&user_email) | ||
.execute(&mut **tx) | ||
.await; | ||
|
||
match query_result { | ||
Ok(_) => Ok(()), | ||
Err(e) => Err(e).map_err(|e| e.into()), | ||
} | ||
} | ||
|
||
pub async fn cancel_team_invite( | ||
&self, | ||
team_id: &String, | ||
user_email: &String, | ||
) -> Result<(), DbError> { | ||
let query_body = format!( | ||
"UPDATE {TEAM_INVITES_TABLE_NAME} SET cancelled_at = $1 WHERE team_id = $2 AND user_email = $3 AND accepted_at IS NULL AND cancelled_at IS NULL" | ||
); | ||
|
||
let query_result = query(&query_body) | ||
.bind(&get_current_datetime()) | ||
.bind(&team_id) | ||
.bind(&user_email) | ||
.execute(&self.connection_pool) | ||
.await; | ||
|
||
match query_result { | ||
Ok(_) => Ok(()), | ||
Err(e) => Err(e).map_err(|e| e.into()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
|
||
export type CloudApiErrors = "TeamDoesNotExist" | "UserDoesNotExist" | "CloudFeatureDisabled" | "InsufficientPermissions" | "TeamHasNoRegisteredApps" | "DatabaseError" | "MaximumUsersPerTeamReached" | "UserAlreadyBelongsToTheTeam" | "IncorrectPassword" | "AccessTokenFailure" | "RefreshTokenFailure" | "AppAlreadyExists" | "MaximumAppsPerTeamReached" | "TeamAlreadyExists" | "PersonalTeamAlreadyExists" | "EmailAlreadyExists" | "InternalServerError" | "UserDoesNotBelongsToTheTeam" | "InvalidName" | "UnauthorizedOriginError" | "AppDoesNotExist"; | ||
export type CloudApiErrors = "TeamDoesNotExist" | "UserDoesNotExist" | "CloudFeatureDisabled" | "InsufficientPermissions" | "TeamHasNoRegisteredApps" | "DatabaseError" | "MaximumUsersPerTeamReached" | "UserAlreadyBelongsToTheTeam" | "IncorrectPassword" | "AccessTokenFailure" | "RefreshTokenFailure" | "AppAlreadyExists" | "MaximumAppsPerTeamReached" | "TeamAlreadyExists" | "PersonalTeamAlreadyExists" | "EmailAlreadyExists" | "InternalServerError" | "UserDoesNotBelongsToTheTeam" | "InvalidName" | "UnauthorizedOriginError" | "AppDoesNotExist" | "UserAlreadyInvitedToTheTeam" | "MaximumInvitesPerTeamReached" | "InviteNotFound" | "ActionForbiddenForPersonalTeam" | "InviteDoesNotExist"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
|
||
export interface HttpAcceptTeamInviteRequest { teamId: string, } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
|
||
export type HttpAcceptTeamInviteResponse = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
|
||
export interface HttpCancelTeamUserInviteRequest { teamId: string, userEmail: string, } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
|
||
export type HttpCancelTeamUserInviteResponse = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
|
||
export type HttpCloudEndpoint = "/register_new_app" | "/register_with_password" | "/login_with_password" | "/register_new_team" | "/add_user_to_team" | "/remove_user_from_team" | "/get_user_joined_teams" | "/events"; | ||
export type HttpCloudEndpoint = "/register_new_app" | "/register_with_password" | "/login_with_password" | "/login_with_google" | "/register_new_team" | "/remove_user_from_team" | "/get_user_joined_teams" | "/events" | "/invite_user_to_team" | "/accept_team_invite" | "/get_team_user_invites" | "/get_user_team_invites" | "/cancel_team_invite"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
|
||
export interface HttpGetTeamUserInvitesRequest { teamId: string, } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
import type { TeamInvite } from "./TeamInvite"; | ||
|
||
export interface HttpGetTeamUserInvitesResponse { teamInvites: Array<TeamInvite>, } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
import type { TeamInvite } from "./TeamInvite"; | ||
|
||
export interface HttpGetUserTeamInvitesResponse { teamInvites: Array<TeamInvite>, } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
|
||
export interface HttpInviteUserToTeamRequest { teamId: string, userEmail: string, } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
|
||
export type HttpInviteUserToTeamResponse = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
|
||
export interface HttpLoginResponse { user_id: string, auth_token: string, refresh_token: string, } | ||
export interface HttpLoginResponse { userId: string, authToken: string, refreshToken: string, } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
|
||
export interface HttpLoginWithGoogleRequest { oauthToken: string, email: string, enforceIp: boolean, } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
|
||
export interface HttpRegisterNewAppResponse { app_id: string, } | ||
export interface HttpRegisterNewAppResponse { appId: string, } |
Oops, something went wrong.