Skip to content

Commit

Permalink
Ensure All Newsletters page is async (#26873)
Browse files Browse the repository at this point in the history
* refactor: make newsletters page async incl futures and swap DCR endpoint to rendering app

* keep email newsletters page using facia baseurl
  • Loading branch information
cemms1 authored Feb 5, 2024
1 parent 6411f92 commit af8c538
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 43 deletions.
75 changes: 34 additions & 41 deletions applications/app/controllers/SignupPageController.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package controllers

import common.{GuLogging, ImplicitControllerExecutionContext}
import conf.switches.Switches.{UseDcrNewslettersPage}
import conf.switches.Switches.UseDcrNewslettersPage
import model.{ApplicationContext, Cached, NoCache}
import model.Cached.RevalidatableResult
import pages.NewsletterHtmlPage
Expand All @@ -13,12 +13,12 @@ import services.newsletters.GroupedNewslettersResponse.GroupedNewslettersRespons
import services.newsletters.NewsletterSignupAgent
import services.newsletters.model.NewsletterResponseV2
import staticpages.StaticPages
import implicits.{HtmlFormat, JsonFormat}
import implicits.Requests.RichRequestHeader

import scala.concurrent.{Await, ExecutionContext, Future}
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration._
import model.dotcomrendering.DotcomNewslettersPageRenderingDataModel
import model.SimplePage
import model.CacheTime

class SignupPageController(
Expand All @@ -36,61 +36,48 @@ class SignupPageController(

private def localRenderNewslettersPage()(implicit
request: RequestHeader,
): Result = {
): Future[Result] = {
val groupedNewsletters: Either[String, GroupedNewslettersResponse] =
newsletterSignupAgent.getGroupedNewsletters()

groupedNewsletters match {
case Right(groupedNewsletters) =>
Cached(defaultCacheDuration)(
RevalidatableResult.Ok(
NewsletterHtmlPage.html(StaticPages.simpleNewslettersPage(request.path, groupedNewsletters)),
Future.successful(
Cached(defaultCacheDuration)(
RevalidatableResult.Ok(
NewsletterHtmlPage.html(StaticPages.simpleNewslettersPage(request.path, groupedNewsletters)),
),
),
)
case Left(e) =>
log.error(s"API call to get newsletters failed: $e")
NoCache(InternalServerError)
Future(NoCache(InternalServerError))
}
}

private def remoteRenderNewslettersPage()(implicit
request: RequestHeader,
): Result = {
): Future[Result] = {

val newsletters: Either[String, List[NewsletterResponseV2]] =
newsletterSignupAgent.getV2Newsletters()

newsletters match {
case Right(newsletters) =>
Await.result(
remoteRenderer.getEmailNewsletters(
ws = wsClient,
newsletters = newsletters,
page = StaticPages.dcrSimpleNewsletterPage(request.path),
),
3.seconds,
remoteRenderer.getEmailNewsletters(
ws = wsClient,
newsletters = newsletters,
page = StaticPages.dcrSimpleNewsletterPage(request.path),
)
case Left(e) =>
log.error(s"API call to get newsletters failed: $e")
NoCache(InternalServerError)
Future(NoCache(InternalServerError))
}
}

def renderNewslettersPage()(implicit
executionContext: ExecutionContext = this.executionContext,
): Action[AnyContent] =
csrfAddToken {
Action { implicit request =>
if (request.forceDCR || UseDcrNewslettersPage.isSwitchedOn) {
remoteRenderNewslettersPage()
} else {
localRenderNewslettersPage()
}
}
}

private def renderDCRNewslettersJson()(implicit
request: RequestHeader,
): Result = {
): Future[Result] = {
val newsletters: Either[String, List[NewsletterResponseV2]] =
newsletterSignupAgent.getV2Newsletters()

Expand All @@ -100,29 +87,35 @@ class SignupPageController(
val dataModel =
DotcomNewslettersPageRenderingDataModel.apply(page, newsletters, request)
val dataJson = DotcomNewslettersPageRenderingDataModel.toJson(dataModel)
common.renderJson(dataJson, page).as("application/json")
Future.successful(common.renderJson(dataJson, page).as("application/json"))
}
case Left(e) =>
log.error(s"API call to get newsletters failed: $e")
throw new RuntimeException()
}
}

private def renderNewslettersJson()(implicit
private def notFoundPage()(implicit
request: RequestHeader,
): Result = {
Cached(CacheTime.NotFound)(Cached.WithoutRevalidationResult(NotFound))
): Future[Result] = {
Future(Cached(CacheTime.NotFound)(Cached.WithoutRevalidationResult(NotFound)))
}

def renderNewslettersJson()(implicit
def renderNewsletters()(implicit
executionContext: ExecutionContext = this.executionContext,
): Action[AnyContent] =
csrfAddToken {
Action { implicit request =>
if (request.forceDCR || UseDcrNewslettersPage.isSwitchedOn) {
renderDCRNewslettersJson()
} else {
renderNewslettersJson()
Action.async { implicit request =>
val useDCR = request.forceDCR || UseDcrNewslettersPage.isSwitchedOn

request.getRequestFormat match {
case HtmlFormat if useDCR =>
remoteRenderNewslettersPage()
case HtmlFormat =>
localRenderNewslettersPage()
case JsonFormat if useDCR =>
renderDCRNewslettersJson()
case _ => notFoundPage()
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions applications/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ GET /_healthcheck
GET /sitemaps/news.xml controllers.SiteMapController.renderNewsSiteMap()
GET /sitemaps/video.xml controllers.SiteMapController.renderVideoSiteMap()

GET /email-newsletters.json controllers.SignupPageController.renderNewslettersJson()
GET /email-newsletters controllers.SignupPageController.renderNewslettersPage()
GET /email-newsletters.json controllers.SignupPageController.renderNewsletters()
GET /email-newsletters controllers.SignupPageController.renderNewsletters()

GET /survey/:formName/show controllers.SurveyPageController.renderFormStackSurvey(formName)
GET /survey/thankyou controllers.SurveyPageController.thankYou()
Expand Down

0 comments on commit af8c538

Please sign in to comment.