Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/GradeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,18 @@ export const getCountriesDefaultGradeContext = (): { [x: string]: GradeContexts
return countries
}

export const validDisciplines = ['trad', 'sport', 'bouldering', 'deepwatersolo', 'alpine', 'snow', 'ice', 'mixed', 'aid', 'tr']
export const validDisciplines: Array<keyof DisciplineType> = [
'trad',
'sport',
'bouldering',
'deepwatersolo',
'alpine',
'snow',
'ice',
'mixed',
'aid',
'tr'
]

/**
* Perform runtime validation of climb discipline object
Expand Down
15 changes: 15 additions & 0 deletions src/model/AreaDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { getClimbModel } from '../db/ClimbSchema.js'
import { ClimbGQLQueryType } from '../db/ClimbTypes.js'
import { logger } from '../logger.js'
import { validDisciplines } from '../GradeUtils.js'

export default class AreaDataSource extends MongoDataSource<AreaType> {
areaModel = getAreaModel()
Expand Down Expand Up @@ -152,6 +153,20 @@ export default class AreaDataSource extends MongoDataSource<AreaType> {
const rs = await this.climbModel
.aggregate([
{ $match: { _id: uuid } },
// Stage to ensure all discipline fields exist and are booleans. This data may not exist
// for chronology reasons and is an easier fix than doing migrations.
{
$addFields: {
...validDisciplines.reduce((prev, curr) => {
prev[`type.${curr}`] = { $ifNull: [`$type.${curr}`, false] }
return prev
}, {}
),
// This ensures 'type' is an object, so the above $ifNull
// operations work correctly on their fields.
disciplines: { $ifNull: ['$type', {}] }
}
},
{
$lookup: {
from: 'areas', // other collection name
Expand Down
9 changes: 8 additions & 1 deletion src/model/__tests__/MutableClimbDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import MutableAreaDataSource from '../MutableAreaDataSource.js'
import { createIndexes, getAreaModel, getClimbModel } from '../../db/index.js'
import { logger } from '../../logger.js'
import { ClimbChangeInputType, ClimbType } from '../../db/ClimbTypes.js'
import { sanitizeDisciplines } from '../../GradeUtils.js'
import { sanitizeDisciplines, validDisciplines } from '../../GradeUtils.js'
import streamListener from '../../db/edit/streamListener.js'
import ChangeLogDataSource from '../ChangeLogDataSource.js'
import inMemoryDB from '../../utils/inMemoryDB.js'
import assert from 'assert'

export const newSportClimb1: ClimbChangeInputType = {
name: 'Cool route 1',
Expand Down Expand Up @@ -187,6 +188,12 @@ describe('Climb CRUD', () => {
for (const [i, climbIn] of newClimbsToAdd.entries()) {
const climbOut = await climbs.findOneClimbByMUUID(muid.from(newIDs[i]))

assert(climbOut !== null)
for (const key of validDisciplines) {
expect(climbOut.type[key]).not.toBeNull()
expect(climbOut.type[key]).not.toBeUndefined()
}

// Validate new climb
expect(climbOut).toMatchObject({
name: climbIn.name,
Expand Down
Loading