-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOps.hs
More file actions
84 lines (77 loc) · 3.47 KB
/
Ops.hs
File metadata and controls
84 lines (77 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module Share.Notifications.Ops
( listNotificationDeliveryMethods,
createWebhookDeliveryMethod,
updateWebhookDeliveryMethod,
deleteWebhookDeliveryMethod,
hydrateEvent,
)
where
import Share.IDs
import Share.Notifications.Queries qualified as NotifQ
import Share.Notifications.Types
import Share.Notifications.Webhooks.Secrets (WebhookConfig (..))
import Share.Notifications.Webhooks.Secrets qualified as WebhookSecrets
import Share.Postgres qualified as PG
import Share.Prelude
import Share.Utils.URI (URIParam (..))
import Share.Web.App (WebApp)
import Share.Web.Errors (respondError)
import Share.Web.UI.Links qualified as Links
listNotificationDeliveryMethods :: UserId -> Maybe NotificationSubscriptionId -> WebApp [NotificationDeliveryMethod]
listNotificationDeliveryMethods userId maySubscriptionId = do
(emailDeliveryMethods, webhookIds) <- PG.runTransaction do
emailDeliveryMethods <- NotifQ.listEmailDeliveryMethods userId maySubscriptionId
webhookIds <- NotifQ.listWebhooks userId maySubscriptionId
pure (emailDeliveryMethods, webhookIds)
webhookDeliveryMethods <- for webhookIds \webhookId -> do
WebhookSecrets.fetchWebhookConfig webhookId >>= \case
Left err -> respondError err
Right (WebhookConfig {uri = URIParam uri}) -> do
pure $ (NotificationWebhookConfig webhookId uri)
pure $ (EmailDeliveryMethod <$> emailDeliveryMethods) <> (WebhookDeliveryMethod <$> webhookDeliveryMethods)
createWebhookDeliveryMethod :: UserId -> URIParam -> Text -> WebApp NotificationWebhookId
createWebhookDeliveryMethod userId uriParam webhookName = do
-- Note that we can't be completely transactional between postgres and vault here.
webhookId <- PG.runTransaction do
NotifQ.createWebhookDeliveryMethod userId webhookName
let webhookConfig = WebhookConfig {uri = uriParam}
WebhookSecrets.putWebhookConfig webhookId webhookConfig
pure webhookId
updateWebhookDeliveryMethod :: UserId -> NotificationWebhookId -> URIParam -> WebApp ()
updateWebhookDeliveryMethod notificationUser webhookDeliveryMethodId url = do
isValid <- PG.runTransaction $ do
PG.queryExpect1Col
[PG.sql|
SELECT EXISTS(
SELECT FROM notification_webhooks nw
WHERE nw.id = #{webhookDeliveryMethodId}
AND nw.subscriber_user_id = #{notificationUser}
)
|]
when isValid $ do
-- Update the webhook config in Vault
WebhookSecrets.putWebhookConfig webhookDeliveryMethodId (WebhookConfig url) >>= \case
Left err -> respondError err
Right _ -> pure ()
deleteWebhookDeliveryMethod :: UserId -> NotificationWebhookId -> WebApp ()
deleteWebhookDeliveryMethod notificationUser webhookDeliveryMethodId = do
isValid <- PG.runTransaction $ do
PG.queryExpect1Col
[PG.sql|
SELECT EXISTS(
SELECT FROM notification_webhooks nw
WHERE nw.id = #{webhookDeliveryMethodId}
AND nw.subscriber_user_id = #{notificationUser}
)
|]
when isValid $ do
-- Delete the webhook config in Vault
WebhookSecrets.deleteWebhookConfig webhookDeliveryMethodId >>= \case
Left err -> respondError err
Right _ -> do
PG.runTransaction $ do
NotifQ.deleteWebhookDeliveryMethod notificationUser webhookDeliveryMethodId
hydrateEvent :: HydratedEventPayload -> PG.Transaction e HydratedEvent
hydrateEvent hydratedEventPayload = do
hydratedEventLink <- Links.notificationLink hydratedEventPayload
pure $ HydratedEvent {hydratedEventPayload, hydratedEventLink}