From b3c8fa0b472e5cb47404a1412ca2de429958816a Mon Sep 17 00:00:00 2001 From: Nitin Prakash Date: Thu, 22 Aug 2024 14:01:49 +0530 Subject: [PATCH] core types --- Makefile | 2 +- azure-email/Azure/Email.hs | 76 +++++++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index ff41316..8d188a2 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -SRC=$(shell find azure-auth/ azure-key-vault/ azure-blob-storage/ -type f -name '*.hs') +SRC=$(shell find azure-auth/ azure-key-vault/ azure-blob-storage/ azure-email/ -type f -name '*.hs') .PHONY: format format: $(SRC) diff --git a/azure-email/Azure/Email.hs b/azure-email/Azure/Email.hs index 4bc78fa..abf5092 100644 --- a/azure-email/Azure/Email.hs +++ b/azure-email/Azure/Email.hs @@ -1,2 +1,74 @@ -module Azure.Email - ( ) where +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE DerivingVia #-} + +module Azure.Email () where + +import Data.Aeson (ToJSON (..), object, (.=)) +import Data.Text (Text) +import GHC.Generics (Generic) + +data EmailAddress = EmailAddress + { eaEmail :: !Text + , eaDisplayName :: !Text + } + deriving stock (Eq, Show, Generic) + +instance ToJSON EmailAddress where + toJSON EmailAddress{..} = + object + [ "address" .= eaEmail + , "displayName" .= eaDisplayName + ] + +-- | Fields to represent @cc@, @bcc@ and @to@ in an email +data EmailRecipients = EmailRecipients + { ccRecipients :: ![EmailAddress] + , bccRecipients :: ![EmailAddress] + , toRecipients :: ![EmailAddress] + } + deriving stock (Generic) + +instance ToJSON EmailRecipients where + toJSON EmailRecipients{..} = + object + [ "to" .= toRecipients + , "cc" .= ccRecipients + , "bcc" .= bccRecipients + ] + +-- | Azure email requires both HTML and plain text format email content +data EmailContent = EmailContent + { ecHtml :: !Text + -- ^ Html version of the email message. + , ecPlainText :: !Text + -- ^ Plain text version of the email message. + , ecSubject :: !Text + -- ^ Subject of the email message. + } + deriving stock (Eq, Show, Generic) + +instance ToJSON EmailContent where + toJSON EmailContent{..} = + object + [ "plainText" .= ecPlainText + , "html" .= ecHtml + , "subject" .= ecSubject + ] + +{- | Source: +https://learn.microsoft.com/en-us/rest/api/communication/dataplane/email/send?view=rest-communication-dataplane-2023-03-31&tabs=HTTP +-} +data AzureEmailRequest = AzureEmailRequest + { aerContent :: !EmailContent + , aerRecipients :: !EmailRecipients + , aerSenderAddress :: !Text -- TODO: This should probably be it's own newtype + } + deriving stock (Generic) + +instance ToJSON AzureEmailRequest where + toJSON AzureEmailRequest{..} = + object + [ "content" .= aerContent + , "recipients" .= aerRecipients + , "senderAddress" .= aerSenderAddress + ]