Skip to content

Commit

Permalink
chore(notifications): some settings entity boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
bodymindarts committed Jan 11, 2024
1 parent 00aaf24 commit f631835
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 26 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE account_notification_settings (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
galoy_account_id VARCHAR UNIQUE NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE account_notification_settings_events (
id UUID REFERENCES account_notification_settings(id) NOT NULL,
sequence INT NOT NULL,
event_type VARCHAR NOT NULL,
event JSONB NOT NULL,
recorded_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE(id, sequence)
);
9 changes: 9 additions & 0 deletions core/notifications/src/account_notification_settings/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum AccountNotificationSettingsError {
#[error("AccountNotificationSettingsError - Sqlx: {0}")]
Sqlx(#[from] sqlx::Error),
#[error("AccountNotificationSettingsError - EntityError: {0}")]
EntityError(#[from] es_entity::EntityError),
}
6 changes: 4 additions & 2 deletions core/notifications/src/account_notification_settings/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod entity;
// mod value;
pub mod error;
mod repo;

pub use entity::*;
// pub use entity::*;
pub use repo::*;
39 changes: 39 additions & 0 deletions core/notifications/src/account_notification_settings/repo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use sqlx::PgPool;

use es_entity::*;

use super::{entity::*, error::*};
use crate::primitives::*;

#[derive(Debug, Clone)]
pub struct AccountNotificationSettingsRepo {
pool: PgPool,
}

impl AccountNotificationSettingsRepo {
pub fn new(pool: &PgPool) -> Self {
Self { pool: pool.clone() }
}

pub async fn find_for_account_id(
&self,
account_id: GaloyAccountId,
) -> Result<Option<AccountNotificationSettings>, AccountNotificationSettingsError> {
let rows = sqlx::query_as!(
GenericEvent,
r#"SELECT a.id, e.sequence, e.event
FROM account_notification_settings a
JOIN account_notification_settings_events e ON a.id = e.id
WHERE a.galoy_account_id = $1
ORDER BY e.sequence"#,
account_id.as_ref(),
)
.fetch_all(&self.pool)
.await?;
let res = EntityEvents::load_first::<AccountNotificationSettings>(rows);
if matches!(res, Err(EntityError::NoEntityEventsPresent)) {
return Ok(None);
}
Ok(Some(res?))
}
}
7 changes: 6 additions & 1 deletion core/notifications/src/app/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use thiserror::Error;

use crate::account_notification_settings::error::*;

#[derive(Error, Debug)]
pub enum ApplicationError {}
pub enum ApplicationError {
#[error("{0}")]
AccountNotificationSettingsError(#[from] AccountNotificationSettingsError),
}
16 changes: 12 additions & 4 deletions core/notifications/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,29 @@ pub use error::*;
#[derive(Clone)]
pub struct NotificationsApp {
_config: AppConfig,
settings: AccountNotificationSettingsRepo,
_pool: Pool<Postgres>,
}

impl NotificationsApp {
pub fn new(pool: Pool<Postgres>, config: AppConfig) -> Self {
let settings = AccountNotificationSettingsRepo::new(&pool);
Self {
_config: config,
_pool: pool,
settings,
}
}

pub async fn notification_settings_for_account_id(
pub async fn disable_channel_on_account(
&self,
_galoy_account_id: GaloyAccountId,
) -> Result<AccountNotificationSettings, ApplicationError> {
unimplemented!()
account_id: GaloyAccountId,
_channel: NotificationChannel,
) -> Result<(), ApplicationError> {
let _account_setting = self.settings.find_for_account_id(account_id).await?;
// settings.disable_channel(channel)
// repo.persist(settings)
// Ok(settings)
Ok(())
}
}
10 changes: 6 additions & 4 deletions core/notifications/src/graphql/schema.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_graphql::*;

use super::types::*;
use crate::app::NotificationsApp;
use crate::{app::NotificationsApp, primitives::*};

pub struct AuthSubject {
pub id: String,
Expand Down Expand Up @@ -35,7 +35,7 @@ pub struct AccountUpdateNotificationSettingsPayloadAlt {

#[derive(InputObject)]
struct AccountDisableNotificationChannelInputAlt {
channel: NotificationChannelAlt,
channel: NotificationChannel,
}

pub struct Mutation;
Expand All @@ -45,13 +45,15 @@ impl Mutation {
async fn account_disable_notification_channel_alt(
&self,
ctx: &Context<'_>,
_input: AccountDisableNotificationChannelInputAlt,
input: AccountDisableNotificationChannelInputAlt,
) -> async_graphql::Result<AccountUpdateNotificationSettingsPayloadAlt> {
let _app = ctx.data_unchecked::<NotificationsApp>();
let subject = ctx.data::<AuthSubject>()?;
if subject.read_only {
return Err("Permission denied".into());
}
let app = ctx.data_unchecked::<NotificationsApp>();
app.disable_channel_on_account(GaloyAccountId::from(subject.id.clone()), input.channel)
.await?;
Ok(AccountUpdateNotificationSettingsPayloadAlt {
notification_settings: NotificationSettingsAlt {
push: NotificationChannelSettingsAlt {
Expand Down
14 changes: 3 additions & 11 deletions core/notifications/src/graphql/types.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
use async_graphql::*;

use crate::primitives::*;

#[derive(SimpleObject)]
pub(super) struct NotificationSettingsAlt {
pub push: NotificationChannelSettingsAlt,
}

#[derive(Enum, Copy, Clone, Eq, PartialEq)]
pub(super) enum NotificationCategoryAlt {
Circles,
}

#[derive(SimpleObject)]
pub(super) struct NotificationChannelSettingsAlt {
pub enabled: bool,
pub disabled_categories: Vec<NotificationCategoryAlt>,
}

#[derive(Enum, Copy, Clone, Eq, PartialEq)]
pub(super) enum NotificationChannelAlt {
Push,
pub disabled_categories: Vec<NotificationCategory>,
}
23 changes: 23 additions & 0 deletions core/notifications/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,28 @@ use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GaloyAccountId(String);
impl From<String> for GaloyAccountId {
fn from(s: String) -> Self {
Self(s)
}
}

impl AsRef<str> for GaloyAccountId {
fn as_ref(&self) -> &str {
&self.0
}
}

es_entity::entity_id! { AccountNotificationSettingsId }

#[derive(async_graphql::Enum, Copy, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[graphql(name = "NotificationChannelAlt")]
pub enum NotificationChannel {
Push,
}

#[derive(async_graphql::Enum, Copy, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[graphql(name = "NotificationCategoryAlt")]
pub enum NotificationCategory {
Circles,
}
4 changes: 2 additions & 2 deletions lib/es-entity-rs/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use thiserror::Error;

#[derive(Error, Debug)]
pub enum EntityError {
#[error("EntityError - NoEvents")]
NoEvents,
#[error("EntityError - NoEntityEventsPresent")]
NoEntityEventsPresent,
#[error("EntityError - UninitializedFieldError: {0}")]
UninitializedFieldError(#[from] derive_builder::UninitializedFieldError),
#[error("EntityError - LoadEvent: {0}")]
Expand Down
4 changes: 2 additions & 2 deletions lib/es-entity-rs/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ where
if let Some(current) = current {
E::try_from(current)
} else {
Err(EntityError::NoEvents)
Err(EntityError::NoEntityEventsPresent)
}
}

Expand Down Expand Up @@ -162,7 +162,7 @@ mod tests {
fn load_zero_events() {
let generic_events = vec![];
let res = EntityEvents::load_first::<DummyEntity>(generic_events);
assert!(matches!(res, Err(EntityError::NoEvents)));
assert!(matches!(res, Err(EntityError::NoEntityEventsPresent)));
}

#[test]
Expand Down

0 comments on commit f631835

Please sign in to comment.