Skip to content

Commit

Permalink
feat(user): setup user tables (#2803)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Sahkal Poddar <[email protected]>
Co-authored-by: Sahkal Poddar <[email protected]>
Co-authored-by: Sai Harsha Vardhan <[email protected]>
Co-authored-by: Venkatesh <[email protected]>
Co-authored-by: venkatesh.devendran <[email protected]>
Co-authored-by: Abhishek Marrivagu <[email protected]>
  • Loading branch information
8 people authored Nov 10, 2023
1 parent 966369b commit 20c4226
Show file tree
Hide file tree
Showing 19 changed files with 919 additions and 1 deletion.
22 changes: 22 additions & 0 deletions crates/diesel_models/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,25 @@ pub enum FraudCheckLastStep {
TransactionOrRecordRefund,
Fulfillment,
}

#[derive(
Clone,
Copy,
Debug,
Default,
Eq,
PartialEq,
serde::Serialize,
serde::Deserialize,
strum::Display,
strum::EnumString,
frunk::LabelledGeneric,
)]
#[router_derive::diesel_enum(storage_type = "text")]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
pub enum UserStatus {
Active,
#[default]
InvitationSent,
}
2 changes: 2 additions & 0 deletions crates/diesel_models/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub mod reverse_lookup;
pub mod routing_algorithm;
#[allow(unused_qualifications)]
pub mod schema;
pub mod user;
pub mod user_role;

use diesel_impl::{DieselArray, OptionalDieselArray};

Expand Down
2 changes: 2 additions & 0 deletions crates/diesel_models/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ pub mod process_tracker;
pub mod refund;
pub mod reverse_lookup;
pub mod routing_algorithm;
pub mod user;
pub mod user_role;
62 changes: 62 additions & 0 deletions crates/diesel_models/src/query/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use diesel::{associations::HasTable, ExpressionMethods};
use error_stack::report;
use router_env::tracing::{self, instrument};

use crate::{
errors::{self},
query::generics,
schema::users::dsl,
user::*,
PgPooledConn, StorageResult,
};

impl UserNew {
#[instrument(skip(conn))]
pub async fn insert(self, conn: &PgPooledConn) -> StorageResult<User> {
generics::generic_insert(conn, self).await
}
}

impl User {
pub async fn find_by_user_email(conn: &PgPooledConn, user_email: &str) -> StorageResult<Self> {
generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
conn,
dsl::email.eq(user_email.to_owned()),
)
.await
}

pub async fn find_by_user_id(conn: &PgPooledConn, user_id: &str) -> StorageResult<Self> {
generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
conn,
dsl::user_id.eq(user_id.to_owned()),
)
.await
}

pub async fn update_by_user_id(
conn: &PgPooledConn,
user_id: &str,
user: UserUpdate,
) -> StorageResult<Self> {
generics::generic_update_with_results::<<Self as HasTable>::Table, _, _, _>(
conn,
dsl::user_id.eq(user_id.to_owned()),
UserUpdateInternal::from(user),
)
.await?
.first()
.cloned()
.ok_or_else(|| {
report!(errors::DatabaseError::NotFound).attach_printable("Error while updating user")
})
}

pub async fn delete_by_user_id(conn: &PgPooledConn, user_id: &str) -> StorageResult<bool> {
generics::generic_delete::<<Self as HasTable>::Table, _>(
conn,
dsl::user_id.eq(user_id.to_owned()),
)
.await
}
}
58 changes: 58 additions & 0 deletions crates/diesel_models/src/query/user_role.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use diesel::{associations::HasTable, BoolExpressionMethods, ExpressionMethods};
use router_env::tracing::{self, instrument};

use crate::{query::generics, schema::user_roles::dsl, user_role::*, PgPooledConn, StorageResult};

impl UserRoleNew {
#[instrument(skip(conn))]
pub async fn insert(self, conn: &PgPooledConn) -> StorageResult<UserRole> {
generics::generic_insert(conn, self).await
}
}

impl UserRole {
pub async fn find_by_user_id(conn: &PgPooledConn, user_id: String) -> StorageResult<Self> {
generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
conn,
dsl::user_id.eq(user_id),
)
.await
}

pub async fn update_by_user_id_merchant_id(
conn: &PgPooledConn,
user_id: String,
merchant_id: String,
update: UserRoleUpdate,
) -> StorageResult<Self> {
generics::generic_update_with_unique_predicate_get_result::<
<Self as HasTable>::Table,
_,
_,
_,
>(
conn,
dsl::user_id
.eq(user_id)
.and(dsl::merchant_id.eq(merchant_id)),
UserRoleUpdateInternal::from(update),
)
.await
}

pub async fn delete_by_user_id(conn: &PgPooledConn, user_id: String) -> StorageResult<bool> {
generics::generic_delete::<<Self as HasTable>::Table, _>(conn, dsl::user_id.eq(user_id))
.await
}

pub async fn list_by_user_id(conn: &PgPooledConn, user_id: String) -> StorageResult<Vec<Self>> {
generics::generic_filter::<<Self as HasTable>::Table, _, _, _>(
conn,
dsl::user_id.eq(user_id),
None,
None,
Some(dsl::created_at.asc()),
)
.await
}
}
47 changes: 47 additions & 0 deletions crates/diesel_models/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,51 @@ diesel::table! {
}
}

diesel::table! {
use diesel::sql_types::*;
use crate::enums::diesel_exports::*;

user_roles (id) {
id -> Int4,
#[max_length = 64]
user_id -> Varchar,
#[max_length = 64]
merchant_id -> Varchar,
#[max_length = 64]
role_id -> Varchar,
#[max_length = 64]
org_id -> Varchar,
#[max_length = 64]
status -> Varchar,
#[max_length = 64]
created_by -> Varchar,
#[max_length = 64]
last_modified_by -> Varchar,
created_at -> Timestamp,
last_modified_at -> Timestamp,
}
}

diesel::table! {
use diesel::sql_types::*;
use crate::enums::diesel_exports::*;

users (id) {
id -> Int4,
#[max_length = 64]
user_id -> Varchar,
#[max_length = 255]
email -> Varchar,
#[max_length = 255]
name -> Varchar,
#[max_length = 255]
password -> Varchar,
is_verified -> Bool,
created_at -> Timestamp,
last_modified_at -> Timestamp,
}
}

diesel::allow_tables_to_appear_in_same_query!(
address,
api_keys,
Expand Down Expand Up @@ -929,4 +974,6 @@ diesel::allow_tables_to_appear_in_same_query!(
refund,
reverse_lookup,
routing_algorithm,
user_roles,
users,
);
76 changes: 76 additions & 0 deletions crates/diesel_models/src/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use common_utils::pii;
use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
use masking::Secret;
use time::PrimitiveDateTime;

use crate::schema::users;

#[derive(Clone, Debug, Identifiable, Queryable)]
#[diesel(table_name = users)]
pub struct User {
pub id: i32,
pub user_id: String,
pub email: pii::Email,
pub name: Secret<String>,
pub password: Secret<String>,
pub is_verified: bool,
pub created_at: PrimitiveDateTime,
pub last_modified_at: PrimitiveDateTime,
}

#[derive(
router_derive::Setter, Clone, Debug, Default, Insertable, router_derive::DebugAsDisplay,
)]
#[diesel(table_name = users)]
pub struct UserNew {
pub user_id: String,
pub email: pii::Email,
pub name: Secret<String>,
pub password: Secret<String>,
pub is_verified: bool,
pub created_at: Option<PrimitiveDateTime>,
pub last_modified_at: Option<PrimitiveDateTime>,
}

#[derive(Clone, Debug, AsChangeset, router_derive::DebugAsDisplay)]
#[diesel(table_name = users)]
pub struct UserUpdateInternal {
name: Option<String>,
password: Option<Secret<String>>,
is_verified: Option<bool>,
last_modified_at: PrimitiveDateTime,
}

#[derive(Debug)]
pub enum UserUpdate {
VerifyUser,
AccountUpdate {
name: Option<String>,
password: Option<Secret<String>>,
is_verified: Option<bool>,
},
}

impl From<UserUpdate> for UserUpdateInternal {
fn from(user_update: UserUpdate) -> Self {
let last_modified_at = common_utils::date_time::now();
match user_update {
UserUpdate::VerifyUser => Self {
name: None,
password: None,
is_verified: Some(true),
last_modified_at,
},
UserUpdate::AccountUpdate {
name,
password,
is_verified,
} => Self {
name,
password,
is_verified,
last_modified_at,
},
}
}
}
79 changes: 79 additions & 0 deletions crates/diesel_models/src/user_role.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
use time::PrimitiveDateTime;

use crate::{enums, schema::user_roles};

#[derive(Clone, Debug, Identifiable, Queryable)]
#[diesel(table_name = user_roles)]
pub struct UserRole {
pub id: i32,
pub user_id: String,
pub merchant_id: String,
pub role_id: String,
pub org_id: String,
pub status: enums::UserStatus,
pub created_by: String,
pub last_modified_by: String,
pub created_at: PrimitiveDateTime,
pub last_modified_at: PrimitiveDateTime,
}

#[derive(router_derive::Setter, Clone, Debug, Insertable, router_derive::DebugAsDisplay)]
#[diesel(table_name = user_roles)]
pub struct UserRoleNew {
pub user_id: String,
pub merchant_id: String,
pub role_id: String,
pub org_id: String,
pub status: enums::UserStatus,
pub created_by: String,
pub last_modified_by: String,
pub created_at: PrimitiveDateTime,
pub last_modified_at: PrimitiveDateTime,
}

#[derive(Clone, Debug, AsChangeset, router_derive::DebugAsDisplay)]
#[diesel(table_name = user_roles)]
pub struct UserRoleUpdateInternal {
role_id: Option<String>,
status: Option<enums::UserStatus>,
last_modified_by: Option<String>,
last_modified_at: PrimitiveDateTime,
}

pub enum UserRoleUpdate {
UpdateStatus {
status: enums::UserStatus,
modified_by: String,
},
UpdateRole {
role_id: String,
modified_by: String,
},
}

impl From<UserRoleUpdate> for UserRoleUpdateInternal {
fn from(value: UserRoleUpdate) -> Self {
let last_modified_at = common_utils::date_time::now();
match value {
UserRoleUpdate::UpdateRole {
role_id,
modified_by,
} => Self {
role_id: Some(role_id),
last_modified_by: Some(modified_by),
status: None,
last_modified_at,
},
UserRoleUpdate::UpdateStatus {
status,
modified_by,
} => Self {
status: Some(status),
last_modified_at,
last_modified_by: Some(modified_by),
role_id: None,
},
}
}
}
4 changes: 4 additions & 0 deletions crates/router/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub mod payouts;
pub mod refund;
pub mod reverse_lookup;
pub mod routing_algorithm;
pub mod user;
pub mod user_role;

use data_models::payments::{
payment_attempt::PaymentAttemptInterface, payment_intent::PaymentIntentInterface,
Expand Down Expand Up @@ -80,6 +82,8 @@ pub trait StorageInterface:
+ organization::OrganizationInterface
+ routing_algorithm::RoutingAlgorithmInterface
+ gsm::GsmInterface
+ user::UserInterface
+ user_role::UserRoleInterface
+ 'static
{
fn get_scheduler_db(&self) -> Box<dyn scheduler::SchedulerInterface>;
Expand Down
Loading

0 comments on commit 20c4226

Please sign in to comment.