@@ -13,12 +13,17 @@ module Azure.Types
13
13
, EmailRecipients (.. )
14
14
, EmailContent (.. )
15
15
, EmailAttachment (.. )
16
+
17
+ -- * Smart constructors
18
+ , newAzureEmailRequest
16
19
) where
17
20
18
21
import Data.Aeson (FromJSON (.. ), ToJSON (.. ), object , withObject , withText , (.:) , (.=) )
19
22
import Data.Aeson.Types (parseFail )
20
23
import Data.Text (Text )
21
24
25
+ import qualified Data.Text as Text
26
+
22
27
{- | Each email is represented as an object with @displayName@
23
28
and an associated @address@.
24
29
@@ -37,6 +42,14 @@ instance ToJSON EmailAddress where
37
42
, " displayName" .= eaDisplayName
38
43
]
39
44
45
+ {- | Why text type instead of represting it as @EmailAddress@?
46
+
47
+ Well, Azure API dictates that sender address should only be the email
48
+ instead of a combination of email and display name (EmailAddress in our case).
49
+ Therefore, we fallback to use text as a type alias for this one case.
50
+ -}
51
+ type SenderEmailAddress = Text
52
+
40
53
-- | Fields to represent @cc@, @bcc@ and @to@ in an email
41
54
data EmailRecipients = EmailRecipients
42
55
{ ccRecipients :: ! [EmailAddress ]
@@ -97,7 +110,7 @@ Source: https://learn.microsoft.com/en-us/rest/api/communication/dataplane/email
97
110
data AzureEmailRequest = AzureEmailRequest
98
111
{ aerContent :: ! EmailContent
99
112
, aerRecipients :: ! EmailRecipients
100
- , aerSenderAddress :: ! Text -- TODO: This should probably be it's own newtype
113
+ , aerSenderAddress :: ! SenderEmailAddress
101
114
, aerReplyTo :: ! [EmailAddress ] -- TODO: Should this be NonEmpty instead?
102
115
, aerAttachments :: ! [EmailAttachment ]
103
116
, aerUserEngagementTrackingDisabled :: ! Bool
@@ -115,6 +128,24 @@ instance ToJSON AzureEmailRequest where
115
128
, " userEngagementTrackingDisabled" .= aerUserEngagementTrackingDisabled
116
129
]
117
130
131
+ {- | Smart constructor to build a send email request.
132
+
133
+ There are few default settings that the caller needs to be aware of:
134
+ 1. @replyTo@ for recipient is the sender's email address. In case there needs to be multiple
135
+ email addresses in @replyTo@ field, it is advised to build a custom request based on the
136
+ exposed data types instead.
137
+ 2. Attachements are not included, yet.
138
+ 3. Enagagement tracking is disabled.
139
+ -}
140
+ newAzureEmailRequest ::
141
+ SenderEmailAddress ->
142
+ EmailRecipients ->
143
+ EmailContent ->
144
+ AzureEmailRequest
145
+ newAzureEmailRequest senderAddress recipients content =
146
+ let senderEmailAddress = EmailAddress senderAddress Text. empty
147
+ in AzureEmailRequest content recipients senderAddress [senderEmailAddress] [] True
148
+
118
149
{- | Possible states once a send email action is triggered.
119
150
Source: https://learn.microsoft.com/en-us/rest/api/communication/dataplane/email/send?view=rest-communication-dataplane-2023-03-31&tabs=HTTP#emailsendstatus
120
151
-}
0 commit comments