Skip to content

Commit

Permalink
Update News Letter Logic
Browse files Browse the repository at this point in the history
  • Loading branch information
GabeWilmoth committed Nov 4, 2024
1 parent 569d484 commit b0c2a2f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 47 deletions.
1 change: 0 additions & 1 deletion app/models/MayberryMiniTrucks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ case class ContactRequest(
isFailedFilter: Boolean = false
)

case class InventoryTemplateTopLevel(vehicles: List[InventoryDetailsForTemplate])

case class InventoryDetailsForTemplate(
photoUrl: String,
Expand Down
28 changes: 0 additions & 28 deletions app/services/EmailService.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package services

import com.fasterxml.jackson.databind.JsonNode
import com.sendgrid.{Method, Request, SendGrid}
import com.sendgrid.helpers.mail.Mail
import com.sendgrid.helpers.mail.objects.{Email, Personalization}
import com.typesafe.config.{Config, ConfigFactory}
import models.InventoryTemplateTopLevel
import shared.AppFunctions.objectMapper

import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}
Expand Down Expand Up @@ -49,31 +46,6 @@ class EmailService @Inject()()(implicit ec: ExecutionContext) {
}
}

def sendEmailWithStringData(to: String, templateId: String, dynamicData: InventoryTemplateTopLevel): Future[Unit] = Future {
val from = new Email("[email protected]")
val toEmail = new Email(to)
val mail = new Mail()
mail.setFrom(from)
mail.setTemplateId(templateId)

val personalization = new Personalization()
personalization.addTo(toEmail)

val jsonData: JsonNode = objectMapper.valueToTree(dynamicData)
personalization.addDynamicTemplateData("vehicles", jsonData)
mail.addPersonalization(personalization)

val request = new Request()
request.setMethod(Method.POST)
request.setEndpoint("mail/send")
request.setBody(mail.build())

val response = sendGridClient.api(request)
if (response.getStatusCode >= 400) {
throw new Exception(s"Failed to send email: ${response.getBody}")
}
}

def removeEmailFromGlobalUnsubscribe(email: String): Future[Unit] = Future {
val request = new Request()
request.setMethod(Method.DELETE)
Expand Down
39 changes: 21 additions & 18 deletions app/services/InventoryNewsLetterService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package services
import akka.actor.{ActorSystem, Cancellable}
import dao.{CosmosDb, CosmosQuery}
import dao.CosmosQuery.{getAllResults, getInStockInventoryAddedInLastWeek}
import models.{Inventory, InventoryDetailsForTemplate, InventoryTemplateTopLevel, Subscribers}
import models.{Inventory, Subscribers}
import shared.AppFunctions._

import java.time.format.DateTimeFormatter
Expand All @@ -27,26 +27,29 @@ class InventoryNewsLetterService @Inject()(actorSystem: ActorSystem,
val sevenDaysAgo = Instant.now().minus(7, ChronoUnit.DAYS)
val formattedDate = DateTimeFormatter.ISO_INSTANT.format(sevenDaysAgo)


for {
inventoryList <- cosmosDb.runQuery[Inventory](getInStockInventoryAddedInLastWeek(formattedDate), inventoryCollection)
_ = println(inventoryList)
inventoryTemplate = inventoryList.map(item => InventoryDetailsForTemplate(
item.imageLinks.head,
item.year,
item.make,
item.model,
formatPrice(item.price),
formatNumberWithCommas(item.mileage.toString),
item.engine,
item.transmission,
item.exteriorColor,
s"http://localhost:3000/inventory/${item.vin}",
))

inventoryMap = inventoryList.zipWithIndex.flatMap {
case (vehicle, index) => Map(
s"photoUrl$index" -> vehicle.imageLinks.head,
s"year$index" -> vehicle.year,
s"make$index" -> vehicle.make,
s"model$index" -> vehicle.model,
s"price$index" -> formatPrice(vehicle.price),
s"mileage$index" -> formatNumberWithCommas(vehicle.mileage.toString),
s"engine$index" -> vehicle.engine,
s"transmission$index" -> vehicle.transmission,
s"color$index" -> vehicle.exteriorColor,
s"itemURL$index" -> s"http://localhost:3000/inventory/${vehicle.vin}"
)
}.toMap
subscriberList <- cosmosDb.runQuery[Subscribers](getAllResults(), subscriberCollection)
_ = subscriberList.map(subscriber =>
emailService.sendEmailWithStringData(subscriber.email, "d-965b9c678e364190a1d8aef756faa080", InventoryTemplateTopLevel(inventoryTemplate))
if(inventoryList.length >= 6) {
emailService.sendEmail(subscriber.email, "d-965b9c678e364190a1d8aef756faa080", inventoryMap)
} else if (inventoryList.length >= 3) {
emailService.sendEmail(subscriber.email, "d-287fccd69f8e479c88b1234cb078b5f1", inventoryMap)
}
)
} yield ""
}
Expand All @@ -66,7 +69,7 @@ class InventoryNewsLetterService @Inject()(actorSystem: ActorSystem,

// Schedule the task to run weekly on Wednesdays at 9 AM EST
private val cancellable: Cancellable = actorSystem.scheduler.scheduleAtFixedRate(
10.millis, //TODO: Call calculateInitialDelay as this will ensure this runs every Wednesday
calculateInitialDelay(), //Change me to 10.millis to run quicker at startup
7.days
)(new Runnable {
override def run(): Unit = task()
Expand Down

0 comments on commit b0c2a2f

Please sign in to comment.