Skip to content

Commit

Permalink
chore(notifications): address pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleSamtoshi committed Jan 12, 2024
1 parent 9a7ef74 commit cbe2faa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 65 deletions.
60 changes: 29 additions & 31 deletions core/notifications/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod error;

use sqlx::{Pool, Postgres};

use crate::{user_notification_settings::*, primitives::*};
use crate::{primitives::*, user_notification_settings::*};

pub use config::*;
pub use error::*;
Expand All @@ -29,24 +29,25 @@ impl NotificationsApp {
&self,
user_id: GaloyUserId,
) -> Result<UserNotificationSettings, ApplicationError> {
if let Some(settings) = self.settings.find_for_user_id(&user_id).await? {
Ok(settings)
} else {
Ok(UserNotificationSettings::new(user_id))
}
let user_settings = self
.settings
.find_for_user_id(&user_id)
.await?
.unwrap_or_else(|| UserNotificationSettings::new(user_id));

Ok(user_settings)
}

pub async fn disable_channel_on_user(
&self,
user_id: GaloyUserId,
channel: UserNotificationChannel,
) -> Result<UserNotificationSettings, ApplicationError> {
let mut user_settings =
if let Some(settings) = self.settings.find_for_user_id(&user_id).await? {
settings
} else {
UserNotificationSettings::new(user_id)
};
let mut user_settings = self
.settings
.find_for_user_id(&user_id)
.await?
.unwrap_or_else(|| UserNotificationSettings::new(user_id));
user_settings.disable_channel(channel);
self.settings.persist(&mut user_settings).await?;
Ok(user_settings)
Expand All @@ -57,13 +58,12 @@ impl NotificationsApp {
user_id: GaloyUserId,
channel: UserNotificationChannel,
) -> Result<UserNotificationSettings, ApplicationError> {
let mut user_settings =
if let Some(settings) = self.settings.find_for_user_id(&user_id).await? {
settings
} else {
UserNotificationSettings::new(user_id)
};

let mut user_settings = self
.settings
.find_for_user_id(&user_id)
.await?
.unwrap_or_else(|| UserNotificationSettings::new(user_id));

user_settings.enable_channel(channel);
self.settings.persist(&mut user_settings).await?;
Ok(user_settings)
Expand All @@ -75,12 +75,11 @@ impl NotificationsApp {
channel: UserNotificationChannel,
category: UserNotificationCategory,
) -> Result<UserNotificationSettings, ApplicationError> {
let mut user_settings =
if let Some(settings) = self.settings.find_for_user_id(&user_id).await? {
settings
} else {
UserNotificationSettings::new(user_id)
};
let mut user_settings = self
.settings
.find_for_user_id(&user_id)
.await?
.unwrap_or_else(|| UserNotificationSettings::new(user_id));
user_settings.disable_category(channel, category);
self.settings.persist(&mut user_settings).await?;
Ok(user_settings)
Expand All @@ -92,12 +91,11 @@ impl NotificationsApp {
channel: UserNotificationChannel,
category: UserNotificationCategory,
) -> Result<UserNotificationSettings, ApplicationError> {
let mut user_settings =
if let Some(settings) = self.settings.find_for_user_id(&user_id).await? {
settings
} else {
UserNotificationSettings::new(user_id)
};
let mut user_settings = self
.settings
.find_for_user_id(&user_id)
.await?
.unwrap_or_else(|| UserNotificationSettings::new(user_id));
user_settings.enable_category(channel, category);
self.settings.persist(&mut user_settings).await?;
Ok(user_settings)
Expand Down
6 changes: 4 additions & 2 deletions core/notifications/src/graphql/convert.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::types;
use crate::{user_notification_settings, primitives::*};
use crate::{primitives::*, user_notification_settings};

impl From<user_notification_settings::UserNotificationSettings> for types::UserNotificationSettings {
impl From<user_notification_settings::UserNotificationSettings>
for types::UserNotificationSettings
{
fn from(settings: user_notification_settings::UserNotificationSettings) -> Self {
Self {
push: types::UserNotificationChannelSettings {
Expand Down
1 change: 0 additions & 1 deletion core/notifications/src/graphql/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,4 @@ impl Mutation {
notification_settings: UserNotificationSettings::from(settings),
})
}

}
2 changes: 1 addition & 1 deletion core/notifications/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![cfg_attr(feature = "fail-on-warnings", deny(warnings))]
#![cfg_attr(feature = "fail-on-warnings", deny(clippy::all))]

mod user_notification_settings;
mod app;
mod data_import;
mod primitives;
mod user_notification_settings;

pub mod cli;
pub mod graphql;
55 changes: 25 additions & 30 deletions core/notifications/src/user_notification_settings/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ impl UserNotificationSettings {
let id = UserNotificationSettingsId::new();
Self::try_from(EntityEvents::init(
id,
[UserNotificationSettingsEvent::Initialized {
id,
galoy_user_id,
}],
[UserNotificationSettingsEvent::Initialized { id, galoy_user_id }],
))
.expect("Could not create default")
}
Expand All @@ -79,12 +76,8 @@ impl UserNotificationSettings {

pub fn is_channel_enabled(&self, channel: UserNotificationChannel) -> bool {
self.events.iter().fold(true, |acc, event| match event {
UserNotificationSettingsEvent::ChannelDisabled { channel: c } if c == &channel => {
false
}
UserNotificationSettingsEvent::ChannelEnabled { channel: c } if c == &channel => {
true
}
UserNotificationSettingsEvent::ChannelDisabled { channel: c } if c == &channel => false,
UserNotificationSettingsEvent::ChannelEnabled { channel: c } if c == &channel => true,
_ => acc,
})
}
Expand Down Expand Up @@ -141,16 +134,10 @@ impl UserNotificationSettings {
impl TryFrom<EntityEvents<UserNotificationSettingsEvent>> for UserNotificationSettings {
type Error = EntityError;

fn try_from(
events: EntityEvents<UserNotificationSettingsEvent>,
) -> Result<Self, Self::Error> {
fn try_from(events: EntityEvents<UserNotificationSettingsEvent>) -> Result<Self, Self::Error> {
let mut builder = UserNotificationSettingsBuilder::default();
for event in events.iter() {
if let UserNotificationSettingsEvent::Initialized {
id,
galoy_user_id,
} = event
{
if let UserNotificationSettingsEvent::Initialized { id, galoy_user_id } = event {
builder = builder.id(*id);
builder = builder.galoy_user_id(galoy_user_id.clone());
}
Expand Down Expand Up @@ -184,17 +171,15 @@ mod tests {
#[test]
fn can_disable_channel() {
let events = initial_events();
let mut settings =
UserNotificationSettings::try_from(events).expect("Could not hydrate");
let mut settings = UserNotificationSettings::try_from(events).expect("Could not hydrate");
settings.disable_channel(UserNotificationChannel::Push);
assert!(!settings.is_channel_enabled(UserNotificationChannel::Push));
}

#[test]
fn can_reenable_channel() {
let events = initial_events();
let mut settings =
UserNotificationSettings::try_from(events).expect("Could not hydrate");
let mut settings = UserNotificationSettings::try_from(events).expect("Could not hydrate");
settings.disable_channel(UserNotificationChannel::Push);
settings.enable_channel(UserNotificationChannel::Push);
assert!(settings.is_channel_enabled(UserNotificationChannel::Push));
Expand All @@ -213,9 +198,11 @@ mod tests {
#[test]
fn can_disable_categories() {
let events = initial_events();
let mut settings =
UserNotificationSettings::try_from(events).expect("Could not hydrate");
settings.disable_category(UserNotificationChannel::Push, UserNotificationCategory::Circles);
let mut settings = UserNotificationSettings::try_from(events).expect("Could not hydrate");
settings.disable_category(
UserNotificationChannel::Push,
UserNotificationCategory::Circles,
);
assert_eq!(
settings.disabled_categories_for(UserNotificationChannel::Push),
HashSet::from([UserNotificationCategory::Circles])
Expand All @@ -225,11 +212,19 @@ mod tests {
#[test]
fn can_enable_categories() {
let events = initial_events();
let mut settings =
UserNotificationSettings::try_from(events).expect("Could not hydrate");
settings.disable_category(UserNotificationChannel::Push, UserNotificationCategory::Circles);
settings.disable_category(UserNotificationChannel::Push, UserNotificationCategory::Payments);
settings.enable_category(UserNotificationChannel::Push, UserNotificationCategory::Circles);
let mut settings = UserNotificationSettings::try_from(events).expect("Could not hydrate");
settings.disable_category(
UserNotificationChannel::Push,
UserNotificationCategory::Circles,
);
settings.disable_category(
UserNotificationChannel::Push,
UserNotificationCategory::Payments,
);
settings.enable_category(
UserNotificationChannel::Push,
UserNotificationCategory::Circles,
);
assert_eq!(
settings.disabled_categories_for(UserNotificationChannel::Push),
HashSet::from([UserNotificationCategory::Payments])
Expand Down

0 comments on commit cbe2faa

Please sign in to comment.