Skip to content

Commit e493b89

Browse files
DTR-2498: Implements SortService with sortByMainObjectLastUpdateDate method
1 parent 55d194b commit e493b89

File tree

6 files changed

+187
-109
lines changed

6 files changed

+187
-109
lines changed

app/models/FullReturn.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ case class Vendor(
202202
postcode: Option[String] = None,
203203
isRepresentedByAgent: Option[String] = None,
204204
vendorResourceRef: Option[String] = None,
205-
nextVendorID: Option[String] = None
205+
nextVendorID: Option[String] = None,
206+
lastUpdateDate: Option[String] = None
206207
)
207208

208209

@@ -258,7 +259,8 @@ case class Land(
258259
titleNumber: Option[String] = None,
259260
landResourceRef: Option[String] = None,
260261
nextLandID: Option[String] = None,
261-
DARPostcode: Option[String] = None
262+
DARPostcode: Option[String] = None,
263+
lastUpdateDate: Option[String] = None
262264
)
263265

264266
object Land {

app/utils/LandPaginationHelper.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import uk.gov.hmrc.govukfrontend.views.viewmodels.summarylist.*
2525

2626
import javax.inject.Inject
2727

28-
class LandPaginationHelper @Inject(landService: LandService){
28+
class LandPaginationHelper @Inject(sortService: SortService, landService: LandService){
2929
private val ROWS_ON_PAGE = 15
3030

3131
def getPaginationInfoText[A](paginationIndex: Int, itemList: Seq[A])
@@ -52,8 +52,9 @@ class LandPaginationHelper @Inject(landService: LandService){
5252

5353
def generateLandSummary(paginationIndex: Int, lands: Seq[Land], userAnswers: UserAnswers)
5454
(implicit messages: Messages): Option[SummaryList] = {
55-
56-
val sortedLands: Seq[Land] = lands.filter(_.landID.isDefined).sortBy(l => !landService.isMainLand(userAnswers, l.landID.get))
55+
56+
val mainLandId = landService.getMainLand(userAnswers).flatMap(_.landID)
57+
val sortedLands: Seq[Land] = sortService.sortByMainObjectLastUpdateDate[Land](lands, mainLandId)(_.lastUpdateDate, _.landID)
5758
val paged: Seq[Seq[Land]] = sortedLands.grouped(ROWS_ON_PAGE).toSeq
5859
val currentPage: Option[Seq[Land]] = paged.lift(paginationIndex - 1)
5960

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,27 @@
1616

1717
package utils
1818

19-
import scala.annotation.tailrec
19+
import java.time.LocalDateTime
20+
import java.time.format.DateTimeFormatter
2021

21-
class LinkedListSortService {
22+
class SortService {
2223

23-
def sorter[ObjectType](
24-
list: Seq[ObjectType],
25-
mainObject: ObjectType
26-
)(
27-
getId: ObjectType => String,
28-
getNextId: ObjectType => Option[String]
29-
): List[ObjectType] = {
24+
private val formatter =
25+
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
3026

31-
val byId: Map[String, ObjectType] = list.map(x => getId(x) -> x).toMap
27+
def sortByMainObjectLastUpdateDate[ObjectType](
28+
list: Seq[ObjectType],
29+
mainObjectId: Option[String]
30+
)(
31+
getLastUpdateDate: ObjectType => Option[String],
32+
getObjectId: ObjectType => Option[String]
33+
): Seq[ObjectType] = {
3234

33-
@tailrec
34-
def traverse(current: ObjectType, acc: List[ObjectType]): List[ObjectType] = {
35-
getNextId(current) match {
36-
case Some(next) => traverse(byId(next), current :: acc)
37-
case None => (current :: acc).reverse
38-
}
35+
list.sortBy { obj =>
36+
(
37+
getObjectId(obj) != mainObjectId,
38+
getLastUpdateDate(obj).map(LocalDateTime.parse(_, formatter))
39+
)
3940
}
40-
41-
traverse(mainObject, Nil)
4241
}
4342
}

test/utils/LandPaginationHelperSpec.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ import services.land.LandService
2626
class LandPaginationHelperSpec extends SpecBase {
2727
private implicit val messages: Messages = stubMessages()
2828

29-
val service = new LandService()
30-
val helper = new LandPaginationHelper(service)
29+
val sortService = new SortService()
30+
val landService = new LandService()
31+
val helper = new LandPaginationHelper(sortService, landService)
3132

3233
private def createLand(landId: String, address: Option[String] = None, landResourceRef: Option[String] = None): Land = {
3334
Land(

test/utils/LinkedListSortServiceSpec.scala

Lines changed: 0 additions & 84 deletions
This file was deleted.

test/utils/SortServiceSpec.scala

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* Copyright 2026 HM Revenue & Customs
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package utils
18+
19+
import base.SpecBase
20+
import models.Land
21+
22+
class SortServiceSpec extends SpecBase {
23+
24+
val service = new SortService()
25+
26+
private def createLand(landId: Option[String], lastUpdatedDate: Option[String]): Land = {
27+
Land(
28+
landID = landId,
29+
returnID = Some("RET123456789"),
30+
propertyType = Some("01"),
31+
interestCreatedTransferred = Some("FG"),
32+
houseNumber = Some("123"),
33+
address1 = Some("Baker Street"),
34+
address2 = Some("Marylebone"),
35+
address3 = Some("London"),
36+
address4 = None,
37+
postcode = Some("NW1 6XE"),
38+
landArea = Some("250.5"),
39+
areaUnit = Some("SQMETRE"),
40+
localAuthorityNumber = Some("5900"),
41+
mineralRights = Some("NO"),
42+
NLPGUPRN = Some("10012345678"),
43+
willSendPlanByPost = Some("NO"),
44+
titleNumber = Some("TGL12456"),
45+
landResourceRef = Some("LDN-REF-001"),
46+
nextLandID = Some("LND002"),
47+
DARPostcode = Some("NW1 6XE"),
48+
lastUpdateDate = lastUpdatedDate
49+
)
50+
}
51+
52+
private def mainLandID: String = "LND001"
53+
54+
"SortService" - {
55+
".sortByLastUpdatedDate" - {
56+
"when the object is Land" - {
57+
"must sort a list with one item" in {
58+
59+
val land1 = createLand(landId = Some("LND001"), lastUpdatedDate = Some("2025-11-09 19:53:34"))
60+
val seqOfLands = Seq(land1)
61+
62+
val sortedLandList = Seq(land1)
63+
64+
service.sortByMainObjectLastUpdateDate[Land](list = seqOfLands, Some(mainLandID))(_.lastUpdateDate, _.landID) mustBe sortedLandList
65+
}
66+
67+
"must return the list as is when there is one item and lastUpdateDate is missing" in {
68+
69+
val land1 = createLand(landId = Some("LND001"), lastUpdatedDate = None)
70+
val seqOfLands = Seq(land1)
71+
72+
val sortedLandList = Seq(land1)
73+
74+
service.sortByMainObjectLastUpdateDate[Land](list = seqOfLands, Some(mainLandID))(_.lastUpdateDate, _.landID) mustBe sortedLandList
75+
}
76+
77+
"must return the list as is when there is one item and mainLandID is missing" in {
78+
79+
val land1 = createLand(landId = Some("LND001"), lastUpdatedDate = Some("2025-11-09 19:53:34"))
80+
val seqOfLands = Seq(land1)
81+
82+
val sortedLandList = Seq(land1)
83+
84+
service.sortByMainObjectLastUpdateDate[Land](list = seqOfLands, mainObjectId = None)(_.lastUpdateDate, _.landID) mustBe sortedLandList
85+
}
86+
87+
"must return the list as is when there is one item and both lastUpdateDate and mainLandID are missing" in {
88+
89+
val land1 = createLand(landId = Some("LND001"), lastUpdatedDate = None)
90+
val seqOfLands = Seq(land1)
91+
92+
val sortedLandList = Seq(land1)
93+
94+
service.sortByMainObjectLastUpdateDate[Land](list = seqOfLands, mainObjectId = None)(_.lastUpdateDate, _.landID) mustBe sortedLandList
95+
}
96+
97+
"must sort a list with multiple items" in {
98+
99+
val land1 = createLand(landId = Some("LND001"), lastUpdatedDate = Some("2022-11-09 19:53:34"))
100+
val land2 = createLand(landId = Some("LND002"), lastUpdatedDate = Some("2023-11-09 19:53:34"))
101+
val land3 = createLand(landId = Some("LND003"), lastUpdatedDate = Some("2024-12-09 19:53:34"))
102+
val land4 = createLand(landId = Some("LND004"), lastUpdatedDate = Some("2025-11-09 19:53:34"))
103+
val land5 = createLand(landId = Some("LND005"), lastUpdatedDate = Some("2025-11-09 19:54:34"))
104+
105+
val seqOfLands = Seq(land4, land2, land1, land5, land3)
106+
107+
val sortedLandList = Seq(land1, land2, land3, land4, land5)
108+
109+
service.sortByMainObjectLastUpdateDate[Land](list = seqOfLands, Some(mainLandID))(_.lastUpdateDate, _.landID) mustBe sortedLandList
110+
}
111+
112+
"must sort a list with multiple items when some lastUpdateDate are missing" in {
113+
114+
val land1 = createLand(landId = Some("LND001"), lastUpdatedDate = Some("2025-11-09 19:53:34"))
115+
val land2 = createLand(landId = Some("LND002"), lastUpdatedDate = None)
116+
val land3 = createLand(landId = Some("LND003"), lastUpdatedDate = None)
117+
val land4 = createLand(landId = Some("LND004"), lastUpdatedDate = Some("2024-11-09 19:53:34"))
118+
val land5 = createLand(landId = Some("LND005"), lastUpdatedDate = Some("2024-11-09 19:56:34"))
119+
120+
val seqOfLands = Seq(land4, land2, land1, land5, land3)
121+
122+
val sortedLandList = Seq(land1, land2, land3, land4, land5)
123+
124+
service.sortByMainObjectLastUpdateDate[Land](list = seqOfLands, Some(mainLandID))(_.lastUpdateDate, _.landID) mustBe sortedLandList
125+
}
126+
127+
"must sort a list with multiple items when mainLandID is missing" in {
128+
129+
val land1 = createLand(landId = Some("LND001"), lastUpdatedDate = Some("2022-11-09 19:53:34"))
130+
val land2 = createLand(landId = Some("LND002"), lastUpdatedDate = Some("2023-11-09 19:53:34"))
131+
val land3 = createLand(landId = Some("LND003"), lastUpdatedDate = Some("2024-12-09 19:53:34"))
132+
val land4 = createLand(landId = Some("LND004"), lastUpdatedDate = Some("2025-11-09 19:53:34"))
133+
val land5 = createLand(landId = Some("LND005"), lastUpdatedDate = Some("2025-11-09 19:54:34"))
134+
135+
val seqOfLands = Seq(land4, land2, land1, land5, land3)
136+
137+
val sortedLandList = Seq(land1, land2, land3, land4, land5)
138+
139+
service.sortByMainObjectLastUpdateDate[Land](list = seqOfLands, mainObjectId = None)(_.lastUpdateDate, _.landID) mustBe sortedLandList
140+
}
141+
142+
"must return the list with multiple items as is when all lastUpdateDate and mainLandID are missing" in {
143+
144+
val land1 = createLand(landId = Some("LND001"), lastUpdatedDate = None)
145+
val land2 = createLand(landId = Some("LND002"), lastUpdatedDate = None)
146+
val land3 = createLand(landId = Some("LND003"), lastUpdatedDate = None)
147+
val land4 = createLand(landId = Some("LND004"), lastUpdatedDate = None)
148+
val land5 = createLand(landId = Some("LND005"), lastUpdatedDate = None)
149+
150+
val seqOfLands = Seq(land4, land2, land1, land5, land3)
151+
152+
val sortedLandList = Seq(land4, land2, land1, land5, land3)
153+
154+
service.sortByMainObjectLastUpdateDate[Land](list = seqOfLands, mainObjectId = None)(_.lastUpdateDate, _.landID) mustBe sortedLandList
155+
}
156+
}
157+
}
158+
}
159+
}

0 commit comments

Comments
 (0)