Skip to content

Commit

Permalink
moving publish action into a lambda (#296)
Browse files Browse the repository at this point in the history
* replacing synchronous calls to publish adverts into opensearch with a queue

* updating code to match lambda + cleanup

* updating queue action

* updating queue action (again)

* adding to the queue after saving
  • Loading branch information
GavCookCO authored Apr 25, 2024
1 parent 2708ae1 commit 1b2b9f3
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 281 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Configuration
@ConfigurationProperties(prefix = "open-search")
public class OpenSearchConfig {
private String url;
private String domain;
private String username;
private String password;
@Configuration("openSearchSqsProperties")
@ConfigurationProperties(prefix = "open-search-sqs")
public class OpenSearchSqsProperties {
private String queueUrl;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package gov.cabinetoffice.gap.adminbackend.dtos;

public record SendAdvertToSqsDto(String contentfulEntryId, String type) {
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package gov.cabinetoffice.gap.adminbackend.services;

import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.model.SendMessageRequest;
import com.contentful.java.cda.CDAArray;
import com.contentful.java.cda.CDAClient;
import com.contentful.java.cda.CDAEntry;
import com.contentful.java.cda.QueryOperation;
import com.contentful.java.cma.CMAClient;
import com.contentful.java.cma.model.CMAEntry;
import com.contentful.java.cma.model.rich.CMARichDocument;
import com.fasterxml.jackson.databind.ObjectMapper;
import gov.cabinetoffice.gap.adminbackend.config.ContentfulConfigProperties;
import gov.cabinetoffice.gap.adminbackend.config.FeatureFlagsConfigurationProperties;
import gov.cabinetoffice.gap.adminbackend.config.OpenSearchSqsProperties;
import gov.cabinetoffice.gap.adminbackend.dtos.SendAdvertToSqsDto;
import gov.cabinetoffice.gap.adminbackend.dtos.grantadvert.GetGrantAdvertPageResponseDTO;
import gov.cabinetoffice.gap.adminbackend.dtos.grantadvert.GetGrantAdvertPublishingInformationResponseDTO;
import gov.cabinetoffice.gap.adminbackend.dtos.grantadvert.GetGrantAdvertStatusResponseDTO;
Expand Down Expand Up @@ -62,14 +67,15 @@ public class GrantAdvertService {
private final CMAClient contentfulManagementClient;
private final CDAClient contentfulDeliveryClient;
private final UserService userService;
private final OpenSearchService openSearchService;
private final WebClient.Builder webClientBuilder;

private final WebClient webClient;
private final AmazonSQS amazonSqs;
private final ObjectMapper mapper;
private final Clock clock;
private final ContentfulConfigProperties contentfulProperties;
private final FeatureFlagsConfigurationProperties featureFlagsProperties;

private final OpenSearchSqsProperties openSearchSqsProperties;

public GrantAdvert save(GrantAdvert advert) {
final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Optional.ofNullable(auth)
Expand Down Expand Up @@ -314,33 +320,50 @@ public GrantAdvert publishAdvert(UUID advertId) {
}

contentfulAdvert = contentfulManagementClient.entries().fetchOne(contentfulAdvert.getId());
final boolean isPublished = Boolean.TRUE.equals(contentfulAdvert.isPublished());
advert.setStatus(GrantAdvertStatus.PUBLISHED);
advert.setContentfulSlug(contentfulAdvert.getField("label", CONTENTFUL_LOCALE));
advert.setContentfulEntryId(contentfulAdvert.getId());

if (Boolean.FALSE.equals(contentfulAdvert.isPublished())) {
final CMAEntry publishedAdvert = contentfulManagementClient.entries().publish(contentfulAdvert);
openSearchService.indexEntry(publishedAdvert);
if (!isPublished) {
contentfulManagementClient.entries().publish(contentfulAdvert);
}

updateGrantAdvertApplicationDates(advert);
return save(advert);
final GrantAdvert savedAdvert = save(advert);
sendMessageToQueue(new SendAdvertToSqsDto(contentfulAdvert.getId(), "ADD"));

return savedAdvert;
}

public void unpublishAdvert(UUID advertId) {
final GrantAdvert advert = this.getAdvertById(advertId);
final CMAEntry contentfulAdvert = contentfulManagementClient.entries().fetchOne(advert.getContentfulEntryId());
final boolean isPublished = Boolean.TRUE.equals(contentfulAdvert.isPublished());

if (Boolean.TRUE.equals(contentfulAdvert.isPublished())) {
final CMAEntry unpublishedAd = contentfulManagementClient.entries().unPublish(contentfulAdvert);
openSearchService.removeIndexEntry(unpublishedAd);
if (isPublished) {
contentfulManagementClient.entries().unPublish(contentfulAdvert);
}

advert.setStatus(GrantAdvertStatus.DRAFT);
advert.setContentfulSlug(null);
advert.setUnpublishedDate(Instant.now());

save(advert);
sendMessageToQueue(new SendAdvertToSqsDto(contentfulAdvert.getId(), "REMOVE"));
}

public void sendMessageToQueue(final SendAdvertToSqsDto advertDto) {
final UUID messageId = UUID.randomUUID();
final String messageBody = mapper.valueToTree(advertDto).toString();
final SendMessageRequest messageRequest = new SendMessageRequest()
.withQueueUrl(openSearchSqsProperties.getQueueUrl())
.withMessageGroupId(messageId.toString())
.withMessageBody(messageBody)
.withMessageDeduplicationId(messageId.toString());

amazonSqs.sendMessage(messageRequest);
log.info("Message sent to queue for advert with contentful ID {}", advertDto.contentfulEntryId());
}

private CMAEntry createAdvertInContentful(final GrantAdvert grantAdvert) {
Expand Down Expand Up @@ -438,7 +461,8 @@ private void createRichTextQuestionsInContentful(final GrantAdvert advert, final
}

final Instant now = Instant.now();
webClient.patch()
webClientBuilder.build()
.patch()
.uri(contentfulUrl)
.headers(h -> {
h.set("Authorization", String.format("Bearer %s", contentfulProperties.getAccessToken()));
Expand Down

This file was deleted.

5 changes: 1 addition & 4 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,4 @@ aws.kms.stage=stage

spring.jpa.properties.hibernate.order_by.default_null_ordering=last

open-search.url=https://search-url
open-search.domain=domain
open-search.username=username
open-search.password=password
open-search-sqs.queueUrl=an-sqs-url
Loading

0 comments on commit 1b2b9f3

Please sign in to comment.