From 0590b154968d322878d2ddc4681cdc1411991118 Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Wed, 26 Jul 2023 17:50:49 +0300 Subject: [PATCH] Update on "[Events] Add project notification settings" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Events] Add project notification settings ghstack-source-id: 2b4044a63b5cc394bfe66af8706764450703d904 Pull Request resolved: https://github.com/devtari-io/cronback/pull/18 This PR adds a new model for project notifications. The semantics of the model is explained in `notifications.proto`. 1. Adds a the model to the db model in metadata_svc and exposed it in the project store. 2. Expose setters/getters to notification settings in metadata svc. 3. Expose APIs to manipulate the notification settings. I opted into having a single endpoint for updating the notification setting json for convenience. Later, we can also add this to the CLI. Test plan: ``` ~/repos/cronback/cronback ❯❯❯ cargo cli --localhost --secret-token adminkey admin projects create ✘ 130 Project 'prj_026601H699SE7VY0YEFHRXXE75117B' was created successfully. ~/repos/cronback/cronback ❯❯❯ http -b --auth adminkey --auth-type bearer POST http://localhost:8888/v1/admin/projects/prj_026601H699SE7VY0YEFHRXXE75117B/notification_settings examples/notifications.json ~/repos/cronback/cronback ❯❯❯ http -b --auth adminkey --auth-type bearer GET http://localhost:8888/v1/admin/projects/prj_026601H699SE7VY0YEFHRXXE75117B/notification_settings { "channels": { "email": { "address": "testgmail.com", "type": "email", "verified": false } }, "subscriptions": [ { "channel_names": [ "email" ], "event": { "type": "on_run_failure" } } ] } ``` [ghstack-poisoned] --- .../src/metadata/metadata_store.rs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/cronback-services/src/metadata/metadata_store.rs b/cronback-services/src/metadata/metadata_store.rs index eacc929..5d55392 100644 --- a/cronback-services/src/metadata/metadata_store.rs +++ b/cronback-services/src/metadata/metadata_store.rs @@ -89,7 +89,16 @@ impl MetadataStore { #[cfg(test)] mod tests { + use std::collections::HashMap; + use super::*; + use crate::metadata::db_model::notifications::{ + EmailNotification, + NotificationChannel, + NotificationEvent, + NotificationSubscription, + OnRunFailure, + }; use crate::metadata::migrate_up; fn build_project(status: ProjectStatus) -> Project { @@ -153,6 +162,30 @@ mod tests { Err(DatabaseError::DB(sea_orm::DbErr::RecordNotUpdated)) )); + // Test notification setters / getters + { + let email = EmailNotification { + address: "test@gmail.com".to_string(), + verified: true, + }; + let mut channels = HashMap::new(); + channels + .insert("email".to_string(), NotificationChannel::Email(email)); + let setting = NotificationSettings { + channels, + subscriptions: vec![NotificationSubscription { + channel_names: vec!["email".to_string()], + event: NotificationEvent::OnRunFailure(OnRunFailure {}), + }], + }; + store + .set_notification_settings(&project2.id, setting.clone()) + .await?; + + let found = store.get_notification_settings(&project2.id).await?; + assert_eq!(found, Some(setting)); + } + Ok(()) } }