Skip to content

Commit bef6534

Browse files
authored
Merge pull request #470 from OpenBeta/bug/#468
Fix for mising types that the schema promises
2 parents f07a8a5 + 43fc503 commit bef6534

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/GradeUtils.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,18 @@ export const getCountriesDefaultGradeContext = (): { [x: string]: GradeContexts
238238
return countries
239239
}
240240

241-
export const validDisciplines = ['trad', 'sport', 'bouldering', 'deepwatersolo', 'alpine', 'snow', 'ice', 'mixed', 'aid', 'tr']
241+
export const validDisciplines: Array<keyof DisciplineType> = [
242+
'trad',
243+
'sport',
244+
'bouldering',
245+
'deepwatersolo',
246+
'alpine',
247+
'snow',
248+
'ice',
249+
'mixed',
250+
'aid',
251+
'tr'
252+
]
242253

243254
/**
244255
* Perform runtime validation of climb discipline object

src/model/AreaDataSource.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
import { getClimbModel } from '../db/ClimbSchema.js'
2121
import { ClimbGQLQueryType } from '../db/ClimbTypes.js'
2222
import { logger } from '../logger.js'
23+
import { validDisciplines } from '../GradeUtils.js'
2324

2425
export default class AreaDataSource extends MongoDataSource<AreaType> {
2526
areaModel = getAreaModel()
@@ -152,6 +153,20 @@ export default class AreaDataSource extends MongoDataSource<AreaType> {
152153
const rs = await this.climbModel
153154
.aggregate([
154155
{ $match: { _id: uuid } },
156+
// Stage to ensure all discipline fields exist and are booleans. This data may not exist
157+
// for chronology reasons and is an easier fix than doing migrations.
158+
{
159+
$addFields: {
160+
...validDisciplines.reduce((prev, curr) => {
161+
prev[`type.${curr}`] = { $ifNull: [`$type.${curr}`, false] }
162+
return prev
163+
}, {}
164+
),
165+
// This ensures 'type' is an object, so the above $ifNull
166+
// operations work correctly on their fields.
167+
disciplines: { $ifNull: ['$type', {}] }
168+
}
169+
},
155170
{
156171
$lookup: {
157172
from: 'areas', // other collection name

src/model/__tests__/MutableClimbDataSource.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import MutableAreaDataSource from '../MutableAreaDataSource.js'
77
import { createIndexes, getAreaModel, getClimbModel } from '../../db/index.js'
88
import { logger } from '../../logger.js'
99
import { ClimbChangeInputType, ClimbType } from '../../db/ClimbTypes.js'
10-
import { sanitizeDisciplines } from '../../GradeUtils.js'
10+
import { sanitizeDisciplines, validDisciplines } from '../../GradeUtils.js'
1111
import streamListener from '../../db/edit/streamListener.js'
1212
import ChangeLogDataSource from '../ChangeLogDataSource.js'
1313
import inMemoryDB from '../../utils/inMemoryDB.js'
14+
import assert from 'assert'
1415

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

191+
assert(climbOut !== null)
192+
for (const key of validDisciplines) {
193+
expect(climbOut.type[key]).not.toBeNull()
194+
expect(climbOut.type[key]).not.toBeUndefined()
195+
}
196+
190197
// Validate new climb
191198
expect(climbOut).toMatchObject({
192199
name: climbIn.name,

0 commit comments

Comments
 (0)