Skip to content

Commit

Permalink
Merge pull request #2250 from ministryofjustice/feature/aps-1264-send…
Browse files Browse the repository at this point in the history
…-placement-request-booking-email-to-pr-creator

APS-1264 - send placement request booking email to placement request creator
  • Loading branch information
davidatkinsuk authored Sep 9, 2024
2 parents 76a1aa4 + e8c8031 commit 232af8a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ class Cas1BookingEmailService(
booking,
)

if (applicationSubmittedByUser.email != null) {
emailNotifier.sendEmail(
recipientEmailAddress = applicationSubmittedByUser.email!!,
templateId = notifyConfig.templates.bookingMade,
personalisation = emailPersonalisation,
application = application,
)
}
val applicants = setOfNotNull(
applicationSubmittedByUser.email,
booking.placementRequest?.placementApplication?.createdByUser?.email,
)

emailNotifier.sendEmails(
recipientEmailAddresses = applicants,
templateId = notifyConfig.templates.bookingMade,
personalisation = emailPersonalisation,
application = application,
)

if (booking.premises.emailAddress != null) {
emailNotifier.sendEmail(
Expand Down Expand Up @@ -92,14 +95,11 @@ class Cas1BookingEmailService(
}

fun buildCommonPersonalisation(application: ApplicationEntity, booking: BookingEntity): Map<String, Any> {
val applicationSubmittedByUser = application.createdByUser

val lengthOfStayDays = booking.arrivalDate.getDaysUntilInclusive(booking.departureDate).size
val lengthOfStayWeeks = lengthOfStayDays.toDouble() / DAYS_IN_WEEK
val lengthOfStayWeeksWholeNumber = (lengthOfStayDays.toDouble() % DAYS_IN_WEEK) == 0.0

return mapOf(
"name" to applicationSubmittedByUser.name,
"apName" to booking.premises.name,
"applicationUrl" to applicationUrlTemplate.resolve("id", application.id.toString()),
"applicationTimelineUrl" to applicationTimelineUrlTemplate.resolve("applicationId", application.id.toString()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class PlacementApplicationEntityFactory : Factory<PlacementApplicationEntity> {

fun withDefaults() = apply {
this.createdByUser = { UserEntityFactory().withDefaultProbationRegion().produce() }
this.application = { ApprovedPremisesApplicationEntityFactory().withDefaults().produce() }
}

fun withId(id: UUID) = apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.ApprovedPremises
import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.ApprovedPremisesEntityFactory
import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.BookingEntityFactory
import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.Cas1ApplicationUserDetailsEntityFactory
import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.PlacementApplicationEntityFactory
import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.PlacementRequestEntityFactory
import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.ProbationRegionEntityFactory
import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.UserEntityFactory
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApAreaEntity
Expand All @@ -23,6 +25,7 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.unit.service.cas1.Cas1Bo
import uk.gov.justice.digital.hmpps.approvedpremisesapi.unit.service.cas1.Cas1BookingEmailServiceTest.TestConstants.AP_AREA_EMAIL
import uk.gov.justice.digital.hmpps.approvedpremisesapi.unit.service.cas1.Cas1BookingEmailServiceTest.TestConstants.CASE_MANAGER_EMAIL
import uk.gov.justice.digital.hmpps.approvedpremisesapi.unit.service.cas1.Cas1BookingEmailServiceTest.TestConstants.CRN
import uk.gov.justice.digital.hmpps.approvedpremisesapi.unit.service.cas1.Cas1BookingEmailServiceTest.TestConstants.PLACEMENT_APPLICATION_CREATOR_EMAIL
import uk.gov.justice.digital.hmpps.approvedpremisesapi.unit.service.cas1.Cas1BookingEmailServiceTest.TestConstants.PREMISES_EMAIL
import uk.gov.justice.digital.hmpps.approvedpremisesapi.unit.service.cas1.Cas1BookingEmailServiceTest.TestConstants.PREMISES_NAME
import uk.gov.justice.digital.hmpps.approvedpremisesapi.unit.service.cas1.Cas1BookingEmailServiceTest.TestConstants.REGION_NAME
Expand All @@ -36,6 +39,7 @@ class Cas1BookingEmailServiceTest {
private object TestConstants {
const val AP_AREA_EMAIL = "[email protected]"
const val APPLICANT_EMAIL = "[email protected]"
const val PLACEMENT_APPLICATION_CREATOR_EMAIL = "[email protected]"
const val CRN = "CRN123"
const val PREMISES_EMAIL = "[email protected]"
const val PREMISES_NAME = "The Premises Name"
Expand Down Expand Up @@ -96,7 +100,6 @@ class Cas1BookingEmailServiceTest {
PREMISES_EMAIL,
notifyConfig.templates.bookingMadePremises,
mapOf(
"name" to applicant.name,
"apName" to PREMISES_NAME,
"applicationUrl" to "http://frontend/applications/${application.id}",
"bookingUrl" to "http://frontend/premises/${premises.id}/bookings/${booking.id}",
Expand All @@ -112,7 +115,7 @@ class Cas1BookingEmailServiceTest {

@SuppressWarnings("CyclomaticComplexMethod")
@Test
fun `bookingMade sends email to applicant and premises email addresses when defined, when length of stay whole number of weeks`() {
fun `bookingMade sends email to applicant, premises email addresses when defined, when length of stay whole number of weeks`() {
val applicant = UserEntityFactory()
.withUnitTestControlProbationRegion()
.withEmail(APPLICANT_EMAIL)
Expand All @@ -130,7 +133,6 @@ class Cas1BookingEmailServiceTest {
mockEmailNotificationService.assertEmailRequestCount(2)

val personalisation = mapOf(
"name" to applicant.name,
"apName" to PREMISES_NAME,
"applicationUrl" to "http://frontend/applications/${application.id}",
"bookingUrl" to "http://frontend/premises/${premises.id}/bookings/${booking.id}",
Expand Down Expand Up @@ -193,6 +195,73 @@ class Cas1BookingEmailServiceTest {
application,
)
}

@SuppressWarnings("CyclomaticComplexMethod")
@Test
fun `bookingMade sends email to applicant, placement application creator and premises email addresses for request for placements`() {
val applicant = UserEntityFactory()
.withUnitTestControlProbationRegion()
.withEmail(APPLICANT_EMAIL)
.produce()

val (application, booking) = createApplicationAndBooking(
applicant,
premises,
arrivalDate = LocalDate.of(2023, 2, 1),
departureDate = LocalDate.of(2023, 2, 14),
)

booking.placementRequest = PlacementRequestEntityFactory()
.withDefaults()
.withPlacementApplication(
PlacementApplicationEntityFactory()
.withDefaults()
.withCreatedByUser(
UserEntityFactory()
.withUnitTestControlProbationRegion()
.withEmail(PLACEMENT_APPLICATION_CREATOR_EMAIL)
.produce(),
)
.produce(),
)
.produce()

service.bookingMade(application, booking)

mockEmailNotificationService.assertEmailRequestCount(3)

val personalisation = mapOf(
"apName" to PREMISES_NAME,
"applicationUrl" to "http://frontend/applications/${application.id}",
"bookingUrl" to "http://frontend/premises/${premises.id}/bookings/${booking.id}",
"crn" to CRN,
"startDate" to "2023-02-01",
"endDate" to "2023-02-14",
"lengthStay" to 2,
"lengthStayUnit" to "weeks",
)

mockEmailNotificationService.assertEmailRequested(
APPLICANT_EMAIL,
notifyConfig.templates.bookingMade,
personalisation,
application,
)

mockEmailNotificationService.assertEmailRequested(
PLACEMENT_APPLICATION_CREATOR_EMAIL,
notifyConfig.templates.bookingMade,
personalisation,
application,
)

mockEmailNotificationService.assertEmailRequested(
PREMISES_EMAIL,
notifyConfig.templates.bookingMadePremises,
personalisation,
application,
)
}
}

@Nested
Expand Down

0 comments on commit 232af8a

Please sign in to comment.