1- import { GraphQLError } from 'graphql'
21import { ApolloServerErrorCode } from '@apollo/server/errors'
2+ import { GraphQLError } from 'graphql'
33import { ClientSession } from 'mongoose'
44import muid , { MUUID } from 'uuid-mongodb'
55
@@ -10,12 +10,12 @@ import { ChangeRecordMetadataType } from '../db/ChangeLogType.js'
1010import { getClimbModel } from '../db/ClimbSchema.js'
1111import { ClimbChangeDocType , ClimbChangeInputType , ClimbEditOperationType , ClimbType , IPitch } from '../db/ClimbTypes.js'
1212import { aggregateCragStats } from '../db/utils/Aggregate.js'
13+ import { withTransaction } from '../utils/helpers.js'
1314import { sanitize , sanitizeStrict } from '../utils/sanitize.js'
1415import ChangeLogDataSource from './ChangeLogDataSource.js'
1516import ClimbDataSource from './ClimbDataSource.js'
1617import ExperimentalUserDataSource from './ExperimentalUserDataSource.js'
1718import MutableAreaDataSource from './MutableAreaDataSource.js'
18- import { withTransaction } from '../utils/helpers.js'
1919
2020export interface AddOrUpdateClimbsOptions {
2121 userId : MUUID
@@ -110,16 +110,17 @@ 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+ const maxExisting = ( 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 ]
118- ?. metadata . left_right_index ?? 1
117+ . limit ( 1 ) ) [ 0 ] ?. metadata . left_right_index
118+ let newClimbLeftRightIndex = ( maxExisting ?? 0 ) + 1
119+
119120 function resolveLeftRightIndex ( i : number ) : { left_right_index : number } | null {
120121 // user input is always prioritized
121122 if ( userInput [ i ] . leftRightIndex !== undefined ) {
122- return { left_right_index : userInput [ i ] . leftRightIndex ! }
123+ return { left_right_index : userInput [ i ] . leftRightIndex ?? 0 }
123124 }
124125 // otherwise, auto-order new climbs
125126 if ( ! idList [ i ] . existed ) {
0 commit comments