|
1 | 1 | use std::ops::Deref; |
2 | 2 |
|
3 | 3 | use callisto::{ |
4 | | - bot_keys, bots, |
5 | | - sea_orm_active_enums::{BotStatusEnum, PermissionScopeEnum}, |
| 4 | + bot_installations, bot_keys, bots, |
| 5 | + sea_orm_active_enums::{ |
| 6 | + BotStatusEnum, InstallationBotStatusEnum, InstallationTargetTypeEnum, PermissionScopeEnum, |
| 7 | + }, |
6 | 8 | }; |
7 | 9 | use common::errors::MegaError; |
8 | 10 | use rsa::{ |
9 | 11 | RsaPrivateKey, |
10 | 12 | pkcs8::{EncodePrivateKey, EncodePublicKey}, |
11 | 13 | rand_core::OsRng, |
12 | 14 | }; |
13 | | -use sea_orm::{ActiveModelTrait, IntoActiveModel}; |
| 15 | +use sea_orm::{ |
| 16 | + ActiveModelTrait, ActiveValue::Set, ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter, |
| 17 | +}; |
14 | 18 |
|
15 | 19 | use crate::storage::base_storage::{BaseStorage, StorageConnector}; |
16 | 20 |
|
@@ -73,6 +77,101 @@ impl BotsStorage { |
73 | 77 | Ok((bot, private_pem)) |
74 | 78 | } |
75 | 79 |
|
| 80 | + pub async fn get_installed_bot_by_id( |
| 81 | + &self, |
| 82 | + bot_id: i64, |
| 83 | + ) -> Result<Vec<bot_installations::Model>, MegaError> { |
| 84 | + let installations = bot_installations::Entity::find() |
| 85 | + .filter(bot_installations::Column::BotId.eq(bot_id)) |
| 86 | + .all(self.get_connection()) |
| 87 | + .await?; |
| 88 | + |
| 89 | + Ok(installations) |
| 90 | + } |
| 91 | + |
| 92 | + /// Install a bot to a target (organization or repository) |
| 93 | + pub async fn install_bot( |
| 94 | + &self, |
| 95 | + bot_id: i64, |
| 96 | + target_type: InstallationTargetTypeEnum, |
| 97 | + target_id: i64, |
| 98 | + installed_by: i64, |
| 99 | + ) -> Result<bot_installations::Model, MegaError> { |
| 100 | + // Check if already installed |
| 101 | + if (bot_installations::Entity::find() |
| 102 | + .filter(bot_installations::Column::BotId.eq(bot_id)) |
| 103 | + .filter(bot_installations::Column::TargetType.eq(target_type.clone())) |
| 104 | + .filter(bot_installations::Column::TargetId.eq(target_id)) |
| 105 | + .one(self.get_connection()) |
| 106 | + .await?) |
| 107 | + .is_some() |
| 108 | + { |
| 109 | + return Err(MegaError::Other( |
| 110 | + "Bot already installed on this target".into(), |
| 111 | + )); |
| 112 | + } |
| 113 | + |
| 114 | + // Insert installation record |
| 115 | + let model = bot_installations::Model::new( |
| 116 | + bot_id, |
| 117 | + target_type, |
| 118 | + target_id, |
| 119 | + InstallationBotStatusEnum::Enabled, |
| 120 | + installed_by, |
| 121 | + ); |
| 122 | + |
| 123 | + let active_model = model.into_active_model(); |
| 124 | + |
| 125 | + let res = active_model.insert(self.get_connection()).await?; |
| 126 | + Ok(res) |
| 127 | + } |
| 128 | + |
| 129 | + /// Uninstall a bot from a target |
| 130 | + pub async fn uninstall_bot( |
| 131 | + &self, |
| 132 | + bot_id: i64, |
| 133 | + target_type: InstallationTargetTypeEnum, |
| 134 | + target_id: i64, |
| 135 | + ) -> Result<(), MegaError> { |
| 136 | + let installation = bot_installations::Entity::find() |
| 137 | + .filter(bot_installations::Column::BotId.eq(bot_id)) |
| 138 | + .filter(bot_installations::Column::TargetType.eq(target_type.clone())) |
| 139 | + .filter(bot_installations::Column::TargetId.eq(target_id)) |
| 140 | + .one(self.get_connection()) |
| 141 | + .await? |
| 142 | + .ok_or_else(|| MegaError::Other("Bot installation not found".into()))? |
| 143 | + .into_active_model(); |
| 144 | + |
| 145 | + installation |
| 146 | + .into_active_model() |
| 147 | + .delete(self.get_connection()) |
| 148 | + .await?; |
| 149 | + |
| 150 | + Ok(()) |
| 151 | + } |
| 152 | + |
| 153 | + /// Change the status of an installed bot (Enabled / Disabled) |
| 154 | + pub async fn change_installed_bot_status( |
| 155 | + &self, |
| 156 | + bot_id: i64, |
| 157 | + target_type: InstallationTargetTypeEnum, |
| 158 | + target_id: i64, |
| 159 | + status: InstallationBotStatusEnum, |
| 160 | + ) -> Result<bot_installations::Model, MegaError> { |
| 161 | + let mut installation = bot_installations::Entity::find() |
| 162 | + .filter(bot_installations::Column::BotId.eq(bot_id)) |
| 163 | + .filter(bot_installations::Column::TargetType.eq(target_type.clone())) |
| 164 | + .filter(bot_installations::Column::TargetId.eq(target_id)) |
| 165 | + .one(self.get_connection()) |
| 166 | + .await? |
| 167 | + .ok_or_else(|| MegaError::Other("Bot installation not found".into()))? |
| 168 | + .into_active_model(); |
| 169 | + |
| 170 | + installation.status = Set(status); |
| 171 | + let res = installation.update(self.get_connection()).await?; |
| 172 | + Ok(res) |
| 173 | + } |
| 174 | + |
76 | 175 | pub async fn new_bot_model( |
77 | 176 | &self, |
78 | 177 | name: &str, |
|
0 commit comments