Skip to content

Commit

Permalink
refactor: relax boulder-only crag check (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
vnugent committed Apr 30, 2024
1 parent 9a0a096 commit 468f6bf
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 34 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
"standard.treatErrorsAsWarnings": true,
"javascript.format.enable": false,
"javascript.format.semicolons": "remove",
"typescript.format.enable": false
"typescript.format.enable": false,
"prettier.enable": false,
"editor.defaultFormatter": "standard.vscode-standard"
}
25 changes: 1 addition & 24 deletions src/model/MutableClimbDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,6 @@ export default class MutableClimbDataSource extends ClimbDataSource {
parent.metadata.leaf = true
}

// is there at least 1 boulder problem in the input?
const hasBouldering = userInput.some(entry => entry.disciplines?.bouldering ?? false)

// input has at least 1 boulder problem AND area is not a bouldering area
if (hasBouldering && !(parent.metadata?.isBoulder ?? false)) {
if (parent.climbs.length === 0) {
// if an area is empty, we're allowed to turn to into a bouldering area
parent.metadata.isBoulder = true
} else {
throw new UserInputError('Adding boulder problems to a route-only area is not allowed')
}
}

// It's ok to have empty disciplines obj in the input in case
// we just want to update other fields.
// However, if disciplines is non-empty, is there 1 non-boulder problem in the input?
const hasARouteClimb = userInput.some(({ disciplines }) =>
disciplines != null && Object.keys(disciplines).length > 0 && !(disciplines?.bouldering ?? false))

if (hasARouteClimb && (parent.metadata?.isBoulder ?? false)) {
throw new UserInputError('Adding route climbs to a bouldering area is not allowed')
}

const cragGradeScales = gradeContextToGradeScales[parent.gradeContext]
if (cragGradeScales == null) {
throw new Error(`Area ${parent.area_name} (${parent.metadata.area_id.toUUID().toString()}) has invalid grade context: '${parent.gradeContext}'`)
Expand Down Expand Up @@ -219,7 +196,7 @@ export default class MutableClimbDataSource extends ClimbDataSource {
}
}))

const rs = await (await this.climbModel.bulkWrite(bulk, { session })).toJSON()
const rs = (await this.climbModel.bulkWrite(bulk, { session })).toJSON()

if (rs.ok === 1) {
const idList: MUUID[] = []
Expand Down
11 changes: 2 additions & 9 deletions src/model/__tests__/MutableClimbDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,8 @@ describe('Climb CRUD', () => {
climbs.addOrUpdateClimbs(testUser, newDestination.metadata.area_id, [newBoulderProblem1])
).rejects.toThrowError(/You can only add climbs to a crag/)

// Route-only area should not accept new boulder problems
await expect(
climbs.addOrUpdateClimbs(testUser, routesArea.metadata.area_id, [newBoulderProblem1])
).rejects.toThrowError(/Adding boulder problems to a route-only area/)
// Route-only area should accept new boulder problems
await climbs.addOrUpdateClimbs(testUser, routesArea.metadata.area_id, [newBoulderProblem1])
})

it('can add new boulder problems', async () => {
Expand All @@ -227,11 +225,6 @@ describe('Climb CRUD', () => {

if (newClimb == null) fail('Expecting new boulder problem to be added, but didn\'t find one')
expect(newClimb.name).toBe(newBoulderProblem1.name)

// Adding a boulder problem into an empty area will set isBoulder flag
const updatedArea = await areas.findOneAreaByUUID(boulderingArea.metadata.area_id)
if (updatedArea == null) fail('Expect area to be non-null')
expect(updatedArea.metadata.isBoulder).toBeTruthy()
})

it('can delete new boulder problems', async () => {
Expand Down

0 comments on commit 468f6bf

Please sign in to comment.