Skip to content

Commit 7de5817

Browse files
committed
MutableClimbDataSource#addOrUpdateClimbs should keep order of climbs
1 parent 205bf32 commit 7de5817

2 files changed

Lines changed: 36 additions & 13 deletions

File tree

src/model/MutableClimbDataSource.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ export default class MutableClimbDataSource extends ClimbDataSource {
107107

108108
const newDocs: ClimbChangeDocType[] = []
109109

110+
// when adding new climbs, ensure they are added in expected sort order, such that:
111+
// * multiple climbs keep the order they're input in.
112+
// * all newly added climbs come after existing climbs.
113+
let newClimbLeftRightIndex = await this.climbModel
114+
.find({ _id: { $in: parent.climbs } })
115+
.select('metadata.left_right_index')
116+
.sort({ 'metadata.left_right_index': -1 })
117+
.limit(1)[0]
118+
?.metadata.left_right_index ?? 1
119+
110120
for (let i = 0; i < userInput.length; i++) {
111121
// when adding new climbs we require name and disciplines
112122
if (!idList[i].existed && userInput[i].name == null) {
@@ -186,6 +196,8 @@ export default class MutableClimbDataSource extends ClimbDataSource {
186196
metadata: {
187197
areaRef: parent.metadata.area_id,
188198
lnglat: parent.metadata.lnglat,
199+
// waterfall left_right_index -- new climbs get auto incremented, but user input always overrides
200+
...!idList[i].existed && { left_right_index: newClimbLeftRightIndex++ },
189201
...userInput[i]?.leftRightIndex != null && { left_right_index: userInput[i].leftRightIndex }
190202
},
191203
...!idList[i].existed && { createdBy: experimentalUserId ?? userId },

src/model/__tests__/MutableClimbDataSource.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,22 +178,33 @@ describe('Climb CRUD', () => {
178178
const newIDs = await climbs.addOrUpdateClimbs(
179179
testUser,
180180
routesArea.metadata.area_id,
181-
newClimbsToAdd)
181+
newClimbsToAdd
182+
)
182183

183184
expect(newIDs).toHaveLength(newClimbsToAdd.length)
184185

185-
const climb0 = await climbs.findOneClimbByMUUID(muid.from(newIDs[0]))
186-
187-
// Validate new climb
188-
expect(climb0).toMatchObject({
189-
name: newClimbsToAdd[0].name,
190-
type: sanitizeDisciplines(newClimbsToAdd[0].disciplines),
191-
content: {
192-
description: newClimbsToAdd[0].description,
193-
location: newClimbsToAdd[0].location,
194-
protection: newClimbsToAdd[0].protection
195-
}
196-
})
186+
// Validate all climbs were added, and in the order we expect
187+
for (const [i, climbIn] of newClimbsToAdd.entries()) {
188+
const climbOut = await climbs.findOneClimbByMUUID(muid.from(newIDs[i]))
189+
190+
// Validate new climb
191+
expect(climbOut).toMatchObject({
192+
name: climbIn.name,
193+
type: sanitizeDisciplines(climbIn.disciplines),
194+
metadata: {
195+
left_right_index: i + 1
196+
},
197+
...climbIn.description === undefined
198+
? {}
199+
: {
200+
content: {
201+
description: climbIn.description,
202+
location: climbIn.location,
203+
protection: climbIn.protection
204+
}
205+
}
206+
})
207+
}
197208

198209
// California contains subareas. Should fail.
199210
await expect(

0 commit comments

Comments
 (0)