Skip to content

Commit

Permalink
chore(notifications): enable / disable channels on settings
Browse files Browse the repository at this point in the history
  • Loading branch information
bodymindarts committed Jan 11, 2024
1 parent c2b3f9e commit 3b966b9
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 44 deletions.
69 changes: 66 additions & 3 deletions core/notifications/src/account_notification_settings/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub enum AccountNotificationSettingsEvent {
ChannelDisabled {
channel: NotificationChannel,
},
ChannelEnabled {
channel: NotificationChannel,
},
}

impl EntityEvent for AccountNotificationSettingsEvent {
Expand All @@ -37,13 +40,31 @@ impl EsEntity for AccountNotificationSettings {

impl AccountNotificationSettings {
pub fn disable_channel(&mut self, channel: NotificationChannel) {
// check if its already disabled -> NoOp
if !self.is_channel_enabled(channel) {
return;
}
self.events
.push(AccountNotificationSettingsEvent::ChannelDisabled { channel });
}

pub fn project<T: EsEntityProjection<AccountNotificationSettingsEvent>>(&self) -> T {
self.events.project()
pub fn enable_channel(&mut self, channel: NotificationChannel) {
if self.is_channel_enabled(channel) {
return;
}
self.events
.push(AccountNotificationSettingsEvent::ChannelEnabled { channel });
}

pub fn is_channel_enabled(&self, channel: NotificationChannel) -> bool {
self.events.iter().fold(true, |acc, event| match event {
AccountNotificationSettingsEvent::ChannelDisabled { channel: c } if c == &channel => {
false
}
AccountNotificationSettingsEvent::ChannelEnabled { channel: c } if c == &channel => {
true
}
_ => acc,
})
}
}

Expand All @@ -69,3 +90,45 @@ impl TryFrom<EntityEvents<AccountNotificationSettingsEvent>> for AccountNotifica
builder.events(events).build()
}
}

#[cfg(test)]
mod tests {
use super::*;

fn initial_events() -> EntityEvents<AccountNotificationSettingsEvent> {
let id = AccountNotificationSettingsId::new();
EntityEvents::init(
id,
[AccountNotificationSettingsEvent::Initialized {
id,
galoy_account_id: GaloyAccountId::from("galoy_id".to_string()),
}],
)
}

#[test]
fn channel_is_initially_enabled() {
let events = initial_events();
let settings = AccountNotificationSettings::try_from(events).expect("Could not hydrate");
assert!(settings.is_channel_enabled(NotificationChannel::Push));
}

#[test]
fn can_disable_channel() {
let events = initial_events();
let mut settings =
AccountNotificationSettings::try_from(events).expect("Could not hydrate");
settings.disable_channel(NotificationChannel::Push);
assert!(!settings.is_channel_enabled(NotificationChannel::Push));
}

#[test]
fn can_reenable_channel() {
let events = initial_events();
let mut settings =
AccountNotificationSettings::try_from(events).expect("Could not hydrate");
settings.disable_channel(NotificationChannel::Push);
settings.enable_channel(NotificationChannel::Push);
assert!(settings.is_channel_enabled(NotificationChannel::Push));
}
}
9 changes: 3 additions & 6 deletions core/notifications/src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod config;
mod error;

use es_entity::EsEntityProjection;
use sqlx::{Pool, Postgres};

use crate::{account_notification_settings::*, primitives::*};
Expand All @@ -26,13 +25,11 @@ impl NotificationsApp {
}
}

pub async fn disable_channel_on_account<
T: EsEntityProjection<AccountNotificationSettingsEvent>,
>(
pub async fn disable_channel_on_account(
&self,
account_id: GaloyAccountId,
channel: NotificationChannel,
) -> Result<T, ApplicationError> {
) -> Result<AccountNotificationSettings, ApplicationError> {
let mut account_settings =
if let Some(settings) = self.settings.find_for_account_id(account_id).await? {
settings
Expand All @@ -42,6 +39,6 @@ impl NotificationsApp {
};
account_settings.disable_channel(channel);
self.settings.persist(&mut account_settings).await?;
Ok(account_settings.project())
Ok(account_settings)
}
}
14 changes: 9 additions & 5 deletions core/notifications/src/graphql/convert.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use es_entity::EsEntityProjection;

use super::types::NotificationSettingsAlt;
use crate::account_notification_settings::*;
use crate::{account_notification_settings::*, primitives::*};

impl EsEntityProjection<AccountNotificationSettingsEvent> for NotificationSettingsAlt {
fn apply(&mut self, _: &AccountNotificationSettingsEvent) -> Self {
impl From<AccountNotificationSettings> for NotificationSettingsAlt {
fn from(settings: AccountNotificationSettings) -> Self {
// NotificationSettingsAlt {
// push: NotificationChannelSettingsAlt {
// enabled: settings.is_channel_enabled(NotificationChannel::Push),
// disabled_categories: settings.dissabled_categories_for(NotificationChannel::Push),

Check warning on line 9 in core/notifications/src/graphql/convert.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"dissabled" should be "disabled".
// },
// }
unimplemented!()
}
}
4 changes: 2 additions & 2 deletions core/notifications/src/graphql/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ impl Mutation {
return Err("Permission denied".into());
}
let app = ctx.data_unchecked::<NotificationsApp>();
let res: NotificationSettingsAlt = app
let settings = app
.disable_channel_on_account(GaloyAccountId::from(subject.id.clone()), input.channel)
.await?;
Ok(AccountUpdateNotificationSettingsPayloadAlt {
notification_settings: res,
notification_settings: NotificationSettingsAlt::from(settings),
})
}
}
11 changes: 0 additions & 11 deletions core/notifications/src/graphql/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@ pub(super) struct NotificationSettingsAlt {
pub push: NotificationChannelSettingsAlt,
}

impl Default for NotificationSettingsAlt {
fn default() -> Self {
Self {
push: NotificationChannelSettingsAlt {
enabled: true,
disabled_categories: vec![],
},
}
}
}

#[derive(SimpleObject)]
pub(super) struct NotificationChannelSettingsAlt {
pub enabled: bool,
Expand Down
17 changes: 0 additions & 17 deletions lib/es-entity-rs/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ pub trait EsEntity: TryFrom<EntityEvents<<Self as EsEntity>::Event>, Error = Ent
type Event: EntityEvent;
}

pub trait EsEntityProjection<E>
where
Self: Default,
{
fn apply(&mut self, event: &E) -> Self;
}

pub struct EntityEvents<T: EntityEvent> {
entity_id: <T as EntityEvent>::EntityId,
persisted_events: Vec<T>,
Expand All @@ -53,16 +46,6 @@ where
self.new_events.push(event);
}

pub fn project<P: EsEntityProjection<T>>(&self) -> P {
self.persisted_events
.iter()
.chain(self.new_events.iter())
.fold(P::default(), |mut acc, event| {
acc = acc.apply(event);
acc
})
}

pub async fn persist(
&mut self,
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
Expand Down

0 comments on commit 3b966b9

Please sign in to comment.