Bug: chore(users): change entity_type column of roles to non-optional



Make entity-type in roles non-optional
diff --git a/crates/diesel_models/src/role.rs b/crates/diesel_models/src/role.rs
index 3fb64e645d6..8199bd3979c 100644
--- a/crates/diesel_models/src/role.rs
+++ b/crates/diesel_models/src/role.rs
@@ -18,7 +18,7 @@ pub struct Role {
     pub created_by: String,
     pub last_modified_at: PrimitiveDateTime,
     pub last_modified_by: String,
-    pub entity_type: Option<enums::EntityType>,
+    pub entity_type: enums::EntityType,
 }
 
 #[derive(router_derive::Setter, Clone, Debug, Insertable, router_derive::DebugAsDisplay)]
@@ -35,7 +35,7 @@ pub struct RoleNew {
     pub created_by: String,
     pub last_modified_at: PrimitiveDateTime,
     pub last_modified_by: String,
-    pub entity_type: Option<enums::EntityType>,
+    pub entity_type: enums::EntityType,
 }
 
 #[derive(Clone, Debug, AsChangeset, router_derive::DebugAsDisplay)]
diff --git a/crates/diesel_models/src/schema.rs b/crates/diesel_models/src/schema.rs
index 8d2dfc47b62..e2ab676b2d3 100644
--- a/crates/diesel_models/src/schema.rs
+++ b/crates/diesel_models/src/schema.rs
@@ -1235,7 +1235,7 @@ diesel::table! {
         #[max_length = 64]
         last_modified_by -> Varchar,
         #[max_length = 64]
-        entity_type -> Nullable<Varchar>,
+        entity_type -> Varchar,
     }
 }
 
diff --git a/crates/diesel_models/src/schema_v2.rs b/crates/diesel_models/src/schema_v2.rs
index b5e895cde79..5651bf95dd9 100644
--- a/crates/diesel_models/src/schema_v2.rs
+++ b/crates/diesel_models/src/schema_v2.rs
@@ -1181,7 +1181,7 @@ diesel::table! {
         #[max_length = 64]
         last_modified_by -> Varchar,
         #[max_length = 64]
-        entity_type -> Nullable<Varchar>,
+        entity_type -> Varchar,
     }
 }
 
diff --git a/crates/router/src/core/user_role/role.rs b/crates/router/src/core/user_role/role.rs
index b5b5cab421f..e43f23674e9 100644
--- a/crates/router/src/core/user_role/role.rs
+++ b/crates/router/src/core/user_role/role.rs
@@ -64,7 +64,7 @@ pub async fn create_role(
             org_id: user_from_token.org_id,
             groups: req.groups,
             scope: req.role_scope,
-            entity_type: Some(EntityType::Merchant),
+            entity_type: EntityType::Merchant,
             created_by: user_from_token.user_id.clone(),
             last_modified_by: user_from_token.user_id,
             created_at: now,
diff --git a/crates/router/src/db/role.rs b/crates/router/src/db/role.rs
index ce009d38a9e..d13508356e5 100644
--- a/crates/router/src/db/role.rs
+++ b/crates/router/src/db/role.rs
@@ -354,7 +354,7 @@ impl RoleInterface for MockDb {
                     None => true,
                 };
 
-                matches_merchant && role.org_id == *org_id && role.entity_type == entity_type
+                matches_merchant && role.org_id == *org_id && Some(role.entity_type) == entity_type
             })
             .take(limit_usize)
             .cloned()
diff --git a/crates/router/src/services/authorization/roles.rs b/crates/router/src/services/authorization/roles.rs
index 63d547bfa67..bf66eb92466 100644
--- a/crates/router/src/services/authorization/roles.rs
+++ b/crates/router/src/services/authorization/roles.rs
@@ -119,7 +119,7 @@ impl From<diesel_models::role::Role> for RoleInfo {
             role_name: role.role_name,
             groups: role.groups.into_iter().map(Into::into).collect(),
             scope: role.scope,
-            entity_type: role.entity_type.unwrap_or(EntityType::Merchant),
+            entity_type: role.entity_type,
             is_invitable: true,
             is_deletable: true,
             is_updatable: true,
diff --git a/migrations/2024-10-24-123318_update-entity-type-column-in-roles/down.sql b/migrations/2024-10-24-123318_update-entity-type-column-in-roles/down.sql
new file mode 100644
index 00000000000..60dfa892e60
--- /dev/null
+++ b/migrations/2024-10-24-123318_update-entity-type-column-in-roles/down.sql
@@ -0,0 +1,4 @@
+-- This file should undo anything in `up.sql`
+ALTER TABLE roles ALTER COLUMN entity_type DROP DEFAULT;
+
+ALTER TABLE roles ALTER COLUMN entity_type DROP NOT NULL;
\ No newline at end of file
diff --git a/migrations/2024-10-24-123318_update-entity-type-column-in-roles/up.sql b/migrations/2024-10-24-123318_update-entity-type-column-in-roles/up.sql
new file mode 100644
index 00000000000..564a026184e
--- /dev/null
+++ b/migrations/2024-10-24-123318_update-entity-type-column-in-roles/up.sql
@@ -0,0 +1,6 @@
+-- Your SQL goes here
+UPDATE roles SET entity_type = 'merchant' WHERE entity_type IS NULL;
+
+ALTER TABLE roles ALTER COLUMN entity_type SET DEFAULT 'merchant';
+
+ALTER TABLE roles ALTER COLUMN entity_type SET NOT NULL;
\ No newline at end of file

## Type of Change
<!-- Put an `x` in the boxes that apply -->

- [ ] Bugfix
- [ ] New feature
- [X] Enhancement
- [ ] Refactoring
- [ ] Dependency updates
- [ ] Documentation
- [ ] CI/CD

## Description
<!-- Describe your changes in detail -->
Earlier entity-type column in roles table was optional , after this change the column will be non-optional with default value as Merchant

### Additional Changes

- [ ] This PR modifies the API contract
- [X] This PR modifies the database schema
- [ ] This PR modifies application configuration/environment variables

<!--
Provide links to the files with corresponding changes.

Following are the paths where you can find config files:
1. `config`
2. `crates/router/src/configs`
3. `loadtest/config`
-->


## Motivation and Context
<!--
Why is this change required? What problem does it solve?
If it fixes an open issue, please link to the issue here.

If you don't have an issue, we'd recommend starting with one first so the PR
can focus on the implementation (unless it is an obvious bug or documentation fix
that will have little conversation).
-->


## How did you test it?
<!--
Did you write an integration/unit/API test to verify the code changes?
Or did you test this change manually (provide relevant screenshots)?
-->
- Request : 
```
curl 'http://localhost:8080/user/role' \
  -H 'authorization: Bearer JWT \
  --data-raw '{"role_scope":"merchant","groups":["operations_manage","connectors_view","connectors_manage"],"role_name":"merch-scope-merch-role"}'

```

Response :
```
{
    "role_id": "role_qrQ05Q9pu7czBlDEWnGE",
    "groups": [
        "operations_manage",
        "connectors_view",
        "connectors_manage"
    ],
    "role_name": "merch-scope-merch-role",
    "role_scope": "merchant"
}
```

Db should have an entry in roles table with role-name as merch-scope-merch-role and entity-type as merchant. All the existing flows should work as before

## Checklist
<!-- Put an `x` in the boxes that apply -->

- [X] I formatted the code `cargo +nightly fmt --all`
- [X] I addressed lints thrown by `cargo clippy`
- [ ] I reviewed the submitted code
- [ ] I added unit tests for my changes where possible
