From 37e589f2f2746afbc5d959032176080dfa68afb8 Mon Sep 17 00:00:00 2001 From: john-tco <135222889+john-tco@users.noreply.github.com> Date: Wed, 6 Mar 2024 14:59:40 +0000 Subject: [PATCH] add validLastUpdated field (#230) * initial * amend tests * rm --- ...GetGrantAdvertPublishingInformationResponseDTO.java | 4 ++++ .../gap/adminbackend/entities/GrantAdvert.java | 10 ++++++++++ .../gap/adminbackend/services/GrantAdvertService.java | 6 ++++-- .../db/migration/V1_96__add_valid_last_updated.sql | 1 + .../controllers/GrantAdvertControllerTest.java | 4 ++++ 5 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/db/migration/V1_96__add_valid_last_updated.sql diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/dtos/grantadvert/GetGrantAdvertPublishingInformationResponseDTO.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/dtos/grantadvert/GetGrantAdvertPublishingInformationResponseDTO.java index 696fafe0..77e9fd5e 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/dtos/grantadvert/GetGrantAdvertPublishingInformationResponseDTO.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/dtos/grantadvert/GetGrantAdvertPublishingInformationResponseDTO.java @@ -36,4 +36,8 @@ public class GetGrantAdvertPublishingInformationResponseDTO { private Instant lastUpdated; + private Instant created; + + private boolean validLastUpdated; + } diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/entities/GrantAdvert.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/entities/GrantAdvert.java index 3dac5dae..6dc9d1be 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/entities/GrantAdvert.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/entities/GrantAdvert.java @@ -91,6 +91,16 @@ public class GrantAdvert extends BaseEntity { @Column(name = "response", columnDefinition = "json") private GrantAdvertResponse response; + /** + * The "lastUpdated" field has historically not being set correctly (it's not being updated with the advert). + * A recent PR has fixed this, see: https://github.com/cabinetoffice/gap-find-admin-backend/pull/216 + * Advert rows created prior to the PR above have an invalid lastUpdated. + * Going forward, this field (validLastUpdated) needs to populate with "true" when adverts are created or updated. + * This field is used by the front-end to conditionally render the lastUpdated field (only when it's valid). + */ + @Column(name="valid_last_updated", columnDefinition = "boolean default false") + private boolean validLastUpdated; + @Override public boolean equals(Object o) { if (this == o) diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertService.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertService.java index 49c9512d..b9708680 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertService.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertService.java @@ -78,7 +78,7 @@ public class GrantAdvertService { private final FeatureFlagsConfigurationProperties featureFlagsProperties; - public GrantAdvert save(GrantAdvert advert) { + public GrantAdvert save(GrantAdvert advert) { final Authentication auth = SecurityContextHolder.getContext().getAuthentication(); Optional.ofNullable(auth) .ifPresentOrElse(authentication -> { @@ -92,6 +92,8 @@ public GrantAdvert save(GrantAdvert advert) { advert.getScheme().setLastUpdated(updatedAt); advert.getScheme().setLastUpdatedBy(adminSession.getGrantAdminId()); + + advert.setValidLastUpdated(true); }, () -> log.warn("Admin session was null. Update must have been performed by a lambda.")); return grantAdvertRepository.save(advert); @@ -106,7 +108,7 @@ public GrantAdvert create(Integer grantSchemeId, Integer grantAdminId, String na if (!doesAdvertExist) { final GrantAdvert grantAdvert = GrantAdvert.builder().grantAdvertName(name).scheme(scheme) .createdBy(grantAdmin).created(Instant.now()).lastUpdatedBy(grantAdmin).lastUpdated(Instant.now()) - .status(GrantAdvertStatus.DRAFT).version(version).build(); + .status(GrantAdvertStatus.DRAFT).version(version).validLastUpdated(true).build(); return save(grantAdvert); } final GrantAdvert existingAdvert = grantAdvertRepository.findBySchemeId(grantSchemeId).get(); diff --git a/src/main/resources/db/migration/V1_96__add_valid_last_updated.sql b/src/main/resources/db/migration/V1_96__add_valid_last_updated.sql new file mode 100644 index 00000000..daea2b12 --- /dev/null +++ b/src/main/resources/db/migration/V1_96__add_valid_last_updated.sql @@ -0,0 +1 @@ +ALTER TABLE grant_advert ADD valid_last_updated boolean default false \ No newline at end of file diff --git a/src/test/java/gov/cabinetoffice/gap/adminbackend/controllers/GrantAdvertControllerTest.java b/src/test/java/gov/cabinetoffice/gap/adminbackend/controllers/GrantAdvertControllerTest.java index e04e4dd9..b8762de5 100644 --- a/src/test/java/gov/cabinetoffice/gap/adminbackend/controllers/GrantAdvertControllerTest.java +++ b/src/test/java/gov/cabinetoffice/gap/adminbackend/controllers/GrantAdvertControllerTest.java @@ -456,6 +456,8 @@ class getAdvertPublishInformation { private final Instant lastPublishedDate = dateTimeInput; + private final Instant createdDate = Instant.parse("2021-01-01T00:00:00.00Z"); + private final Instant unpublishedDate = dateTimeInput; @Test @@ -466,6 +468,8 @@ void getAdvertPublishingInformation_GrantAdvertPublishingInformationReturned() t .contentfulSlug(contentfulSlug).unpublishedDate(unpublishedDate) .firstPublishedDate(firstPublishedDate).lastPublishedDate(lastPublishedDate) .lastUpdatedByEmail("an-email") + .created(createdDate) + .validLastUpdated(true) .lastUpdated(lastPublishedDate) .closingDate(closingDate).openingDate(openingDate).build();