Skip to content

Commit

Permalink
Merge pull request #142 from nightly-labs/more-endpoints-cloud
Browse files Browse the repository at this point in the history
More endpoints cloud
  • Loading branch information
Giems authored Mar 25, 2024
2 parents 7453f7f + 3512917 commit 93c1dbe
Show file tree
Hide file tree
Showing 31 changed files with 930 additions and 30 deletions.
46 changes: 31 additions & 15 deletions database/src/tables/team_invites/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,32 @@ 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;
use sqlx::query_as;

impl Db {
pub async fn get_invites_by_team_id(
&self,
team_id: &String,
active_invites: bool,
) -> Result<Vec<TeamInvite>, DbError> {
let query = if active_invites {
format!(
"SELECT * FROM {TEAM_INVITES_TABLE_NAME} WHERE team_id = $1 AND accepted_at IS NULL AND cancelled_at IS NULL"
)
let additional_filter = if active_invites {
"AND ti.accepted_at IS NULL AND ti.cancelled_at IS NULL"
} else {
format!("SELECT * FROM {TEAM_INVITES_TABLE_NAME} WHERE team_id = $1")
""
};
let typed_query = query_as::<_, TeamInvite>(&query);

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
.bind(&team_id)
.fetch_all(&self.connection_pool)
.await
.map_err(|e| e.into());
Expand All @@ -31,17 +38,26 @@ impl Db {
user_email: &String,
active_invites: bool,
) -> Result<Vec<TeamInvite>, DbError> {
let query = if active_invites {
format!(
"SELECT * FROM {TEAM_INVITES_TABLE_NAME} WHERE user_email = $1 AND accepted_at IS NULL AND cancelled_at IS NULL"
)
let additional_filter = if active_invites {
"AND ti.accepted_at IS NULL AND ti.cancelled_at IS NULL"
} else {
format!("SELECT * FROM {TEAM_INVITES_TABLE_NAME} WHERE user_email = $1")
""
};
let typed_query = query_as::<_, TeamInvite>(&query);

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
.bind(&user_email)
.fetch_all(&self.connection_pool)
.await
.map_err(|e| e.into());
Expand Down
5 changes: 5 additions & 0 deletions database/src/tables/team_invites/table_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ pub struct TeamInvite {
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 {
Expand All @@ -27,6 +30,8 @@ impl FromRow<'_, PgRow> for TeamInvite {
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"),
})
}
}
2 changes: 1 addition & 1 deletion server/bindings/CloudApiErrors.ts
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" | "UserAlreadyInvitedToTheTeam" | "MaximumInvitesPerTeamReached" | "InviteNotFound";
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";
3 changes: 3 additions & 0 deletions server/bindings/HttpCancelTeamUserInviteRequest.ts
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, }
3 changes: 3 additions & 0 deletions server/bindings/HttpCancelTeamUserInviteResponse.ts
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;
2 changes: 1 addition & 1 deletion server/bindings/HttpCloudEndpoint.ts
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" | "/login_with_google" | "/register_new_team" | "/remove_user_from_team" | "/get_user_joined_teams" | "/events" | "/invite_user_to_team" | "/accept_team_invite";
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";
3 changes: 3 additions & 0 deletions server/bindings/HttpGetTeamUserInvitesRequest.ts
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, }
4 changes: 4 additions & 0 deletions server/bindings/HttpGetTeamUserInvitesResponse.ts
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>, }
2 changes: 1 addition & 1 deletion server/bindings/HttpGetUserJoinedTeamsResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import type { AppInfo } from "./AppInfo";
import type { JoinedTeam } from "./JoinedTeam";
import type { UserPrivilege } from "./UserPrivilege";

export interface HttpGetUserJoinedTeamsResponse { teams: Record<string, JoinedTeam>, teams_apps: Record<string, Array<AppInfo>>, user_privileges: Record<string, Record<string, UserPrivilege>>, }
export interface HttpGetUserJoinedTeamsResponse { teams: Record<string, JoinedTeam>, teamsApps: Record<string, Array<AppInfo>>, userPrivileges: Record<string, Record<string, UserPrivilege>>, }
4 changes: 4 additions & 0 deletions server/bindings/HttpGetUserTeamInvitesResponse.ts
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>, }
2 changes: 1 addition & 1 deletion server/bindings/HttpLoginResponse.ts
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, }
2 changes: 1 addition & 1 deletion server/bindings/HttpRegisterNewAppResponse.ts
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, }
2 changes: 1 addition & 1 deletion server/bindings/HttpRegisterNewTeamResponse.ts
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 HttpRegisterNewTeamResponse { team_id: string, }
export interface HttpRegisterNewTeamResponse { teamId: string, }
2 changes: 1 addition & 1 deletion server/bindings/HttpRegisterWithPasswordResponse.ts
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 HttpRegisterWithPasswordResponse { user_id: string, }
export interface HttpRegisterWithPasswordResponse { userId: string, }
3 changes: 3 additions & 0 deletions server/bindings/TeamInvite.ts
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 TeamInvite { creatorEmail: string, teamName: string, userEmail: string, createdAt: string, }
Loading

0 comments on commit 93c1dbe

Please sign in to comment.