diff --git a/src/main/kotlin/uk/gov/communities/prsdb/webapp/constants/InvitationLifetimeConstants.kt b/src/main/kotlin/uk/gov/communities/prsdb/webapp/constants/InvitationLifetimeConstants.kt index c6a0469de7..a0be34b51d 100644 --- a/src/main/kotlin/uk/gov/communities/prsdb/webapp/constants/InvitationLifetimeConstants.kt +++ b/src/main/kotlin/uk/gov/communities/prsdb/webapp/constants/InvitationLifetimeConstants.kt @@ -1,4 +1,4 @@ package uk.gov.communities.prsdb.webapp.constants const val LOCAL_COUNCIL_INVITATION_LIFETIME_IN_HOURS: Int = 48 -const val JOINT_LANDLORD_INVITATION_LIFETIME_IN_HOURS: Int = 672 // 28 days +const val JOINT_LANDLORD_INVITATION_LIFETIME_IN_DAYS = 28 diff --git a/src/main/kotlin/uk/gov/communities/prsdb/webapp/database/entity/JointLandlordInvitation.kt b/src/main/kotlin/uk/gov/communities/prsdb/webapp/database/entity/JointLandlordInvitation.kt index 3c9376ce79..a8004502d9 100644 --- a/src/main/kotlin/uk/gov/communities/prsdb/webapp/database/entity/JointLandlordInvitation.kt +++ b/src/main/kotlin/uk/gov/communities/prsdb/webapp/database/entity/JointLandlordInvitation.kt @@ -7,6 +7,12 @@ import jakarta.persistence.GenerationType import jakarta.persistence.Id import jakarta.persistence.JoinColumn import jakarta.persistence.ManyToOne +import kotlinx.datetime.DatePeriod +import kotlinx.datetime.LocalDate +import kotlinx.datetime.plus +import kotlinx.datetime.toKotlinInstant +import uk.gov.communities.prsdb.webapp.constants.JOINT_LANDLORD_INVITATION_LIFETIME_IN_DAYS +import uk.gov.communities.prsdb.webapp.helpers.DateTimeHelper import java.util.UUID @Entity @@ -45,6 +51,15 @@ class JointLandlordInvitation( this.invitingLandlord = invitingLandlord } + val expiresOnDate: LocalDate + get() = + DateTimeHelper + .getDateInUK(createdDate.toKotlinInstant()) + .plus(DatePeriod(days = JOINT_LANDLORD_INVITATION_LIFETIME_IN_DAYS)) + + val isExpired: Boolean + get() = DateTimeHelper().getCurrentDateInUK() > expiresOnDate + constructor( id: Long, token: UUID, diff --git a/src/test/kotlin/uk/gov/communities/prsdb/webapp/database/entity/JointLandlordInvitationTests.kt b/src/test/kotlin/uk/gov/communities/prsdb/webapp/database/entity/JointLandlordInvitationTests.kt new file mode 100644 index 0000000000..bc15372639 --- /dev/null +++ b/src/test/kotlin/uk/gov/communities/prsdb/webapp/database/entity/JointLandlordInvitationTests.kt @@ -0,0 +1,35 @@ +package uk.gov.communities.prsdb.webapp.database.entity + +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import uk.gov.communities.prsdb.webapp.constants.JOINT_LANDLORD_INVITATION_LIFETIME_IN_DAYS +import uk.gov.communities.prsdb.webapp.testHelpers.mockObjects.MockJointLandlordData +import java.time.Instant +import java.time.temporal.ChronoUnit + +class JointLandlordInvitationTests { + @Test + fun `isExpired returns false when the current day is earlier than the expiry date`() { + val createdDate = Instant.now().minus((JOINT_LANDLORD_INVITATION_LIFETIME_IN_DAYS - 1).toLong(), ChronoUnit.DAYS) + val invitation = MockJointLandlordData.createJointLandlordInvitation(createdDate = createdDate) + + assertFalse(invitation.isExpired) + } + + @Test + fun `isExpired returns false when the current day equals the expiry date`() { + val createdDate = Instant.now().minus(JOINT_LANDLORD_INVITATION_LIFETIME_IN_DAYS.toLong(), ChronoUnit.DAYS) + val invitation = MockJointLandlordData.createJointLandlordInvitation(createdDate = createdDate) + + assertFalse(invitation.isExpired) + } + + @Test + fun `isExpired returns true when the current day is later than the expiry date`() { + val createdDate = Instant.now().minus((JOINT_LANDLORD_INVITATION_LIFETIME_IN_DAYS + 1).toLong(), ChronoUnit.DAYS) + val invitation = MockJointLandlordData.createJointLandlordInvitation(createdDate = createdDate) + + assertTrue(invitation.isExpired) + } +}