-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from guardian/email_changes
Email changes
- Loading branch information
Showing
32 changed files
with
924 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...t-api/src/main/scala/com/gu/newproduct/api/addsubscription/email/ContributionFields.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.gu.newproduct.api.addsubscription.email | ||
|
||
import java.time.LocalDate | ||
import java.time.format.DateTimeFormatter | ||
|
||
import com.gu.i18n.Currency | ||
import com.gu.newproduct.api.addsubscription.AmountMinorUnits | ||
import com.gu.newproduct.api.addsubscription.zuora.GetBillToContact.Contact | ||
import com.gu.newproduct.api.addsubscription.zuora.GetPaymentMethod.DirectDebit | ||
import play.api.libs.json.Json | ||
|
||
case class ContributionFields( | ||
EmailAddress: String, | ||
created: String, | ||
amount: String, | ||
currency: String, | ||
edition: String, | ||
name: String, | ||
product: String, | ||
`account name`: Option[String] = None, | ||
`account number`: Option[String] = None, | ||
`sort code`: Option[String] = None, | ||
`Mandate ID`: Option[String] = None, | ||
`first payment date`: Option[String] = None, | ||
`payment method`: Option[String] = None | ||
) | ||
|
||
object ContributionFields { | ||
implicit val writes = Json.writes[ContributionFields] | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...ew-product-api/src/main/scala/com/gu/newproduct/api/addsubscription/email/ETPayload.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.gu.newproduct.api.addsubscription.email | ||
|
||
import play.api.libs.json.{Json, Writes} | ||
|
||
case class CContactAttributes[A](SubscriberAttributes: A) | ||
|
||
case class CTo[A](Address: String, SubscriberKey: String, ContactAttributes: CContactAttributes[A]) | ||
|
||
case class ETPayload[A](To: CTo[A], DataExtensionName: String) | ||
|
||
object CContactAttributes { | ||
implicit def writes[A: Writes] = Json.writes[CContactAttributes[A]] | ||
} | ||
|
||
object CTo { | ||
implicit def writes[A: Writes] = Json.writes[CTo[A]] | ||
} | ||
|
||
object ETPayload { | ||
|
||
implicit def writes[A: Writes] = Json.writes[ETPayload[A]] | ||
|
||
def apply[A](email: String, fields: A): ETPayload[A] = | ||
ETPayload( | ||
To = CTo(email, email, CContactAttributes(fields)), | ||
DataExtensionName = "regular-contribution-thank-you" | ||
) | ||
} |
15 changes: 15 additions & 0 deletions
15
...ew-product-api/src/main/scala/com/gu/newproduct/api/addsubscription/email/EtSqsSend.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.gu.newproduct.api.addsubscription.email | ||
|
||
import com.gu.effects.sqs.AwsSQSSend.Payload | ||
import com.gu.util.Logging | ||
import play.api.libs.json.{Json, Writes} | ||
|
||
import scala.concurrent.Future | ||
|
||
object EtSqsSend extends Logging { | ||
|
||
def apply[FIELDS: Writes](sqsSend: Payload => Future[Unit]) = { etPayload: ETPayload[FIELDS] => | ||
val payloadString = Json.prettyPrint(Json.toJson(etPayload)) | ||
sqsSend(Payload(payloadString)) | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
...pi/src/main/scala/com/gu/newproduct/api/addsubscription/email/SendConfirmationEmail.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.gu.newproduct.api.addsubscription.email | ||
|
||
import java.time.LocalDate | ||
import java.time.format.DateTimeFormatter | ||
|
||
import com.gu.i18n.Currency | ||
import com.gu.newproduct.api.addsubscription.zuora.GetBillToContact.Contact | ||
import com.gu.newproduct.api.addsubscription.zuora.GetPaymentMethod.{DirectDebit, PaymentMethod} | ||
import com.gu.newproduct.api.addsubscription.{AmountMinorUnits, ZuoraAccountId} | ||
import com.gu.util.Logging | ||
import com.gu.util.apigateway.ApiGatewayResponse | ||
import com.gu.util.reader.Types.ApiGatewayOp.{ContinueProcessing, ReturnWithResponse} | ||
import com.gu.util.resthttp.Types.ClientFailableOp | ||
import com.gu.util.reader.AsyncTypes._ | ||
import com.gu.newproduct.api.addsubscription.TypeConvert._ | ||
|
||
import scala.concurrent.Future | ||
|
||
object SendConfirmationEmail extends Logging { | ||
|
||
case class ContributionsEmailData( | ||
accountId: ZuoraAccountId, | ||
currency: Currency, | ||
paymentMethod: PaymentMethod, | ||
amountMinorUnits: AmountMinorUnits, | ||
firstPaymentDate: LocalDate | ||
) | ||
|
||
def apply( | ||
etSqsSend: ETPayload[ContributionFields] => Future[Unit], | ||
getCurrentDate: () => LocalDate, | ||
getBillTo: ZuoraAccountId => ClientFailableOp[Contact] | ||
)(data: ContributionsEmailData) = { | ||
|
||
val response = for { | ||
billTo <- getBillTo(data.accountId).toAsyncApiGatewayOp("getting billTo contact from Zuora") | ||
maybeContributionFields = toContributionFields(getCurrentDate(), billTo, data) | ||
etPayload <- toPayload(maybeContributionFields) | ||
sendMessageResult <- etSqsSend(etPayload).toAsyncApiGatewayOp("sending sqs message") | ||
} yield sendMessageResult | ||
|
||
response.replace(ContinueProcessing(())) | ||
|
||
} | ||
|
||
def toPayload(maybeContributionFields: Option[ContributionFields]): AsyncApiGatewayOp[ETPayload[ContributionFields]] = | ||
maybeContributionFields.map { fields => | ||
val payload = ETPayload(fields.EmailAddress, fields) | ||
ContinueProcessing(payload).toAsync | ||
}.getOrElse { | ||
logger.info("Not enough data in zuora account to send contribution thank you email, skipping") | ||
ReturnWithResponse(ApiGatewayResponse.successfulExecution).toAsync | ||
} | ||
|
||
def hyphenate(s: String) = s"${s.substring(0, 2)}-${s.substring(2, 4)}-${s.substring(4, 6)}" | ||
def formatAmount(amount: AmountMinorUnits) = (amount.value / BigDecimal(100)).bigDecimal.stripTrailingZeros.toPlainString | ||
val firstPaymentDateFormat = DateTimeFormatter.ofPattern("EEEE, d MMMM yyyy") | ||
|
||
def toContributionFields(currentDate: LocalDate, billTo: Contact, data: ContributionsEmailData): Option[ContributionFields] = { | ||
|
||
val maybeDirectDebit = data.paymentMethod match { | ||
case d: DirectDebit => Some(d) | ||
case _ => None | ||
} | ||
billTo.email.map { email => | ||
ContributionFields( | ||
EmailAddress = email.value, | ||
created = currentDate.toString, | ||
amount = formatAmount(data.amountMinorUnits), | ||
currency = data.currency.glyph, | ||
edition = billTo.country.map(_.alpha2).getOrElse(""), | ||
name = s"${billTo.firstName.value} ${billTo.lastName.value}", | ||
product = "monthly-contribution", | ||
`account name` = maybeDirectDebit.map(_.accountName.value), | ||
`account number` = maybeDirectDebit.map(_.accountNumberMask.value), | ||
`sort code` = maybeDirectDebit.map(x => hyphenate(x.sortCode.value)), | ||
`Mandate ID` = maybeDirectDebit.map(_.mandateId.value), | ||
`first payment date` = maybeDirectDebit.map { _ => | ||
data.firstPaymentDate.format(firstPaymentDateFormat) | ||
}, | ||
`payment method` = maybeDirectDebit.map(_ => "Direct Debit") | ||
) | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.