@@ -110,12 +110,23 @@ export default class MutableClimbDataSource extends ClimbDataSource {
110110 // when adding new climbs, ensure they are added in expected sort order, such that:
111111 // * multiple climbs keep the order they're input in.
112112 // * all newly added climbs come after existing climbs.
113- let newClimbLeftRightIndex = await this . climbModel
113+ let newClimbLeftRightIndex = ( await this . climbModel
114114 . find ( { _id : { $in : parent . climbs } } )
115115 . select ( 'metadata.left_right_index' )
116116 . sort ( { 'metadata.left_right_index' : - 1 } )
117- . limit ( 1 ) [ 0 ]
117+ . limit ( 1 ) ) [ 0 ]
118118 ?. metadata . left_right_index ?? 1
119+ function resolveLeftRightIndex ( i : number ) : { left_right_index : number } | null {
120+ // user input is always prioritized
121+ if ( userInput [ i ] . leftRightIndex !== undefined ) {
122+ return { left_right_index : userInput [ i ] . leftRightIndex ! }
123+ }
124+ // otherwise, auto-order new climbs
125+ if ( ! idList [ i ] . existed ) {
126+ return { left_right_index : newClimbLeftRightIndex ++ }
127+ }
128+ return null
129+ }
119130
120131 for ( let i = 0 ; i < userInput . length ; i ++ ) {
121132 // when adding new climbs we require name and disciplines
@@ -196,9 +207,7 @@ export default class MutableClimbDataSource extends ClimbDataSource {
196207 metadata : {
197208 areaRef : parent . metadata . area_id ,
198209 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 ++ } ,
201- ...userInput [ i ] ?. leftRightIndex != null && { left_right_index : userInput [ i ] . leftRightIndex }
210+ ...resolveLeftRightIndex ( i )
202211 } ,
203212 ...! idList [ i ] . existed && { createdBy : experimentalUserId ?? userId } ,
204213 ...idList [ i ] . existed && { updatedBy : userId } ,
0 commit comments