forked from OpenBeta/openbeta-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutableClimbDataSource.ts
More file actions
728 lines (599 loc) · 24 KB
/
MutableClimbDataSource.ts
File metadata and controls
728 lines (599 loc) · 24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
import muid from 'uuid-mongodb'
import { ChangeStream } from 'mongodb'
import MutableClimbDataSource from '../MutableClimbDataSource.js'
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 streamListener from '../../db/edit/streamListener.js'
import ChangeLogDataSource from '../ChangeLogDataSource.js'
import inMemoryDB from '../../utils/inMemoryDB.js'
export const newSportClimb1: ClimbChangeInputType = {
name: 'Cool route 1',
disciplines: {
sport: true
},
description: 'A good warm up problem',
location: 'Start from the left arete',
protection: '2 bolts',
boltsCount: 2
}
describe('Climb CRUD', () => {
let climbs: MutableClimbDataSource
let areas: MutableAreaDataSource
let stream: ChangeStream
const testUser = muid.v4()
const newClimbsToAdd: ClimbChangeInputType[] = [
{
name: 'Sport 1',
// Intentionally disable TS check to make sure input is sanitized
disciplines: {
sport: true
},
description: 'The best climb',
location: '5m left of the big tree',
protection: '5 quickdraws'
},
{
name: 'Deep water 1',
disciplines: {
deepwatersolo: true
}
},
{
name: 'Cool trad one',
disciplines: {
trad: true
}
},
{
name: 'Icy ice one',
disciplines: {
ice: true
}
},
{
name: 'Cool aid one',
disciplines: {
aid: true
}
}
]
const newSportClimb2: ClimbChangeInputType = {
name: 'Cool route 2',
disciplines: {
sport: true
},
description: 'A local testpiece'
}
const newAidRoute: ClimbChangeInputType = {
name: 'Gnarly Aid',
disciplines: {
aid: true
},
description: 'certain death',
grade: 'A0'
}
const newBoulderProblem1: ClimbChangeInputType = {
name: 'Cool boulder 1',
disciplines: {
bouldering: true
},
description: 'A good warm up problem',
location: 'Start from the left arete',
protection: '2 pads'
}
const newBoulderProblem2: ClimbChangeInputType = {
name: 'Cool boulder 2',
disciplines: {
bouldering: true
},
grade: '5c'
}
const newIceRoute: ClimbChangeInputType = {
name: 'Cool Ice line',
disciplines: {
ice: true
},
grade: 'WI8+'
}
// Define a sport climb with two individual pitches
const newClimbWithPitches: ClimbChangeInputType = {
name: 'Short Multi-Pitch',
disciplines: {
sport: true
},
grade: '7', // max grade of its child pitches
description: 'A challenging climb with two pitches',
location: '5m left of the big tree',
protection: '5 quickdraws',
pitches: [
{
pitchNumber: 1,
grades: { uiaa: '7' },
disciplines: { sport: true },
length: 30,
boltsCount: 5,
description: 'First pitch description'
},
{
pitchNumber: 2,
grades: { uiaa: '6+' },
disciplines: { sport: true },
length: 40,
boltsCount: 6,
description: 'Second pitch description'
}
]
}
beforeAll(async () => {
await inMemoryDB.connect()
stream = await streamListener()
try {
await getAreaModel().collection.drop()
await getClimbModel().collection.drop()
} catch (e) {
logger.info('Expected exception')
}
await createIndexes()
climbs = MutableClimbDataSource.getInstance()
areas = MutableAreaDataSource.getInstance()
await ChangeLogDataSource.getInstance()._testRemoveAll()
await areas.addCountry('fr')
})
afterAll(async () => {
try {
await stream.close()
await inMemoryDB.close()
} catch (e) {
console.log('closing mongoose', e)
}
})
it('can add new climbs', async () => {
await areas.addCountry('usa')
const newDestination = await areas.addArea(testUser, 'California', null, 'usa')
if (newDestination == null) fail('Expect new area to be created')
const routesArea = await areas.addArea(testUser, 'Sport & Trad', newDestination.metadata.area_id)
const newIDs = await climbs.addOrUpdateClimbs(
testUser,
routesArea.metadata.area_id,
newClimbsToAdd
)
expect(newIDs).toHaveLength(newClimbsToAdd.length)
// Validate all climbs were added, and in the order we expect
for (const [i, climbIn] of newClimbsToAdd.entries()) {
const climbOut = await climbs.findOneClimbByMUUID(muid.from(newIDs[i]))
// Validate new climb
expect(climbOut).toMatchObject({
name: climbIn.name,
type: sanitizeDisciplines(climbIn.disciplines),
metadata: {
left_right_index: i + 1
},
...climbIn.description === undefined
? {}
: {
content: {
description: climbIn.description,
location: climbIn.location,
protection: climbIn.protection
}
}
})
}
// California contains subareas. Should fail.
await expect(
climbs.addOrUpdateClimbs(testUser, newDestination.metadata.area_id, [newBoulderProblem1])
).rejects.toThrowError(/You can only add climbs to a crag/)
// Route-only area should accept new boulder problems
const [newBoulderID] = await climbs.addOrUpdateClimbs(testUser, routesArea.metadata.area_id, [newBoulderProblem1])
// Should come after existing climbs
expect(await climbs.findOneClimbByMUUID(muid.from(newBoulderID))).toMatchObject({
metadata: {
left_right_index: newClimbsToAdd.length + 1
}
})
})
it('can add new boulder problems', async () => {
await areas.addCountry('esp')
const newDestination = await areas.addArea(testUser, 'Valencia', null, 'esp')
if (newDestination == null) fail('Expect new area to be created')
const boulderingArea = await areas.addArea(testUser, 'Bouldering only', newDestination.metadata.area_id)
expect(boulderingArea.metadata.isBoulder).toBeFalsy()
const newIDs = await climbs.addOrUpdateClimbs(
testUser,
boulderingArea.metadata.area_id,
[newBoulderProblem1, newBoulderProblem2])
expect(newIDs).toHaveLength(2)
const newClimb = await climbs.findOneClimbByMUUID(muid.from(newIDs[0]))
if (newClimb == null) fail('Expecting new boulder problem to be added, but didn\'t find one')
expect(newClimb.name).toBe(newBoulderProblem1.name)
})
it('can delete new boulder problems', async () => {
const newBoulderingArea = await areas.addArea(testUser, 'Bouldering area 1', null, 'fr')
if (newBoulderingArea == null) fail('Expect new area to be created')
const newIDs = await climbs.addOrUpdateClimbs(
testUser,
newBoulderingArea.metadata.area_id,
[newBoulderProblem1, newBoulderProblem2])
expect(newIDs).toHaveLength(2)
// delete a random (non-existing) climb
const count0 = await climbs.deleteClimbs(
testUser,
newBoulderingArea.metadata.area_id,
[muid.v4()])
expect(count0).toEqual(0)
// try delete a correct climb and a non-existent one
const count1 = await climbs.deleteClimbs(
testUser,
newBoulderingArea.metadata.area_id,
[muid.from(newIDs[0]), muid.v4()])
// immediately delete a previously deleted climb. Should be a no op.
const count2 = await climbs.deleteClimbs(
testUser,
newBoulderingArea.metadata.area_id,
[muid.from(newIDs[0]), muid.v4()])
expect(count1).toEqual(1)
expect(count2).toEqual(0)
// A delay is needed here due to how TTL index works
// eslint-disable-next-line
await new Promise(res => setTimeout(res, 2000))
// make sure the right one is deleted
let rs = await climbs.findOneClimbByMUUID(muid.from(newIDs[0]))
expect(rs).toBeNull()
// expect one to remain
rs = await climbs.findOneClimbByMUUID(muid.from(newIDs[1]))
if (rs == null) fail('Expect climb 2 to exist')
expect(rs._id.toUUID().toString()).toEqual(newIDs[1])
const areaRs = await areas.findOneAreaByUUID(newBoulderingArea.metadata.area_id)
expect(areaRs.climbs).toHaveLength(1)
expect((areaRs.climbs[0] as ClimbType)._id.toUUID().toString()).toEqual(newIDs[1])
})
it('handles mixed grades and disciplines correctly', async () => {
await areas.addCountry('can')
const newBoulderingArea = await areas.addArea(testUser, 'Bouldering area 1', null, 'can')
if (newBoulderingArea == null) fail('Expect new area to be created')
const newIDs = await climbs.addOrUpdateClimbs(
testUser,
newBoulderingArea.metadata.area_id,
[{ ...newBoulderProblem1, grade: 'V3' }, // good grade
{ ...newBoulderProblem2, grade: '5.9' }]) // invalid grade (YDS grade for a boulder problem)
expect(newIDs).toHaveLength(2)
const climb1 = await climbs.findOneClimbByMUUID(muid.from(newIDs[0]))
expect(climb1?.grades).toEqual({ vscale: 'V3' })
const climb2 = await climbs.findOneClimbByMUUID(muid.from(newIDs[1]))
expect(climb2?.grades).toEqual(undefined)
})
it('handles Australian grade context correctly', async () => {
await areas.addCountry('aus')
{
// A roped climbing area
const newClimbingArea = await areas.addArea(testUser, 'Climbing area 1', null, 'aus')
if (newClimbingArea == null) fail('Expect new area to be created')
const newclimbs = [
{ ...newSportClimb1, grade: '17' }, // good sport grade
{ ...newSportClimb2, grade: '29/30', disciplines: { trad: true } }, // good trad and slash grade
{ ...newSportClimb2, grade: '5.9' }, // bad AU context grade
{ ...newIceRoute, grade: 'WI4+' }, // good WI AU context grade
{ ...newAidRoute, grade: 'A0' } // good aid grade
]
const newIDs = await climbs.addOrUpdateClimbs(
testUser,
newClimbingArea.metadata.area_id,
newclimbs
)
expect(newIDs).toHaveLength(newclimbs.length)
const climb1 = await climbs.findOneClimbByMUUID(muid.from(newIDs[0]))
expect(climb1?.grades).toEqual({ ewbank: '17' })
expect(climb1?.type.sport).toBe(true)
expect(newSportClimb1?.boltsCount).toEqual(2)
const climb2 = await climbs.findOneClimbByMUUID(muid.from(newIDs[1]))
expect(climb2?.grades).toEqual({ ewbank: '29/30' })
expect(climb2?.type.sport).toBe(false)
expect(climb2?.type.trad).toBe(true)
const climb3 = await climbs.findOneClimbByMUUID(muid.from(newIDs[2]))
expect(climb3?.grades).toEqual(undefined)
const climb4 = await climbs.findOneClimbByMUUID(muid.from(newIDs[3]))
expect(climb4?.grades).toEqual({ wi: 'WI4+' })
expect(climb4?.type.sport).toBe(false)
expect(climb4?.type.trad).toBe(false)
expect(climb4?.type.bouldering).toBe(false)
expect(climb4?.type.ice).toBe(true)
const climb5 = await climbs.findOneClimbByMUUID(muid.from(newIDs[4]))
expect(climb5?.grades).toEqual({ aid: 'A0' })
expect(climb5?.type.sport).toBe(false)
expect(climb5?.type.trad).toBe(false)
expect(climb5?.type.aid).toBe(true)
}
{
// A bouldering area
const newBoulderingArea = await areas.addArea(testUser, 'Bouldering area 1', null, 'aus')
if (newBoulderingArea == null) fail('Expect new area to be created')
const newIDs = await climbs.addOrUpdateClimbs(
testUser,
newBoulderingArea.metadata.area_id,
[{ ...newBoulderProblem1, grade: 'V3' }, // good grade
{ ...newBoulderProblem2, grade: '23' }, // bad boulder grade
{ ...newBoulderProblem2, grade: '7B' }]) // invalid grade (font grade for a AU context boulder problem)
expect(newIDs).toHaveLength(3)
const climb1 = await climbs.findOneClimbByMUUID(muid.from(newIDs[0]))
expect(climb1?.grades).toEqual({ vscale: 'V3' })
const climb2 = await climbs.findOneClimbByMUUID(muid.from(newIDs[1]))
expect(climb2?.grades).toEqual(undefined)
const climb3 = await climbs.findOneClimbByMUUID(muid.from(newIDs[2]))
expect(climb3?.grades).toEqual(undefined)
}
})
it('handles Brazilian grade context correctly', async () => {
await areas.addCountry('bra')
{
// A roped climbing area
const newClimbingArea = await areas.addArea(testUser, 'Climbing area in Brazil', null, 'bra')
if (newClimbingArea == null) fail('Expect new area to be created in Brazil')
const newclimbs = [
{ ...newSportClimb1, grade: 'VIsup' }, // good sport grade
{ ...newSportClimb2, grade: 'VIsup/VIIa', disciplines: { trad: true } }, // good trad and slash grade
{ ...newSportClimb2, grade: '5.9' }, // bad BRZ context grade
{ ...newIceRoute, grade: 'WI4+' }, // good WI BRZ context grade
{ ...newAidRoute, grade: 'A0' } // good aid grade
]
const newIDs = await climbs.addOrUpdateClimbs(
testUser,
newClimbingArea.metadata.area_id,
newclimbs
)
expect(newIDs).toHaveLength(newclimbs.length)
const climb1 = await climbs.findOneClimbByMUUID(muid.from(newIDs[0]))
expect(climb1?.grades).toEqual({ brazilian_crux: 'VIsup' })
expect(climb1?.type.sport).toBe(true)
expect(newSportClimb1?.boltsCount).toEqual(2)
const climb2 = await climbs.findOneClimbByMUUID(muid.from(newIDs[1]))
expect(climb2?.grades).toEqual({ brazilian_crux: 'VIsup/VIIa' })
expect(climb2?.type.sport).toBe(false)
expect(climb2?.type.trad).toBe(true)
const climb3 = await climbs.findOneClimbByMUUID(muid.from(newIDs[2]))
expect(climb3?.grades).toEqual(undefined)
const climb4 = await climbs.findOneClimbByMUUID(muid.from(newIDs[3]))
expect(climb4?.grades).toEqual({ wi: 'WI4+' })
expect(climb4?.type.sport).toBe(false)
expect(climb4?.type.trad).toBe(false)
expect(climb4?.type.bouldering).toBe(false)
expect(climb4?.type.ice).toBe(true)
const climb5 = await climbs.findOneClimbByMUUID(muid.from(newIDs[4]))
expect(climb5?.grades).toEqual({ aid: 'A0' })
expect(climb5?.type.sport).toBe(false)
expect(climb5?.type.trad).toBe(false)
expect(climb5?.type.aid).toBe(true)
}
{
// A bouldering area
const newBoulderingArea = await areas.addArea(testUser, 'Bouldering area 1', null, 'bra')
if (newBoulderingArea == null) fail('Expect new area to be created')
const newIDs = await climbs.addOrUpdateClimbs(
testUser,
newBoulderingArea.metadata.area_id,
[{ ...newBoulderProblem1, grade: 'V3' }, // good grade
{ ...newBoulderProblem2, grade: '23' }, // bad boulder grade
{ ...newBoulderProblem2, grade: '7B' }]) // invalid grade (font grade for a BRZ context boulder problem)
expect(newIDs).toHaveLength(3)
const climb1 = await climbs.findOneClimbByMUUID(muid.from(newIDs[0]))
expect(climb1?.grades).toEqual({ vscale: 'V3' })
const climb2 = await climbs.findOneClimbByMUUID(muid.from(newIDs[1]))
expect(climb2?.grades).toEqual(undefined)
const climb3 = await climbs.findOneClimbByMUUID(muid.from(newIDs[2]))
expect(climb3?.grades).toEqual(undefined)
}
})
it('handles UIAA grades correctly', async () => {
await areas.addCountry('deu') // Assuming Germany since UIAA is dominant grading system
// A roped climbing area
const newClimbingArea = await areas.addArea(testUser, 'Climbing area 1', null, 'deu')
if (newClimbingArea == null) fail('Expect new area to be created')
const newIDs = await climbs.addOrUpdateClimbs(
testUser,
newClimbingArea.metadata.area_id,
[{ ...newSportClimb1, grade: '6+' }, // good UIAA grade
{ ...newSportClimb2, grade: '7-' }, // good UIAA grade
{ ...newSportClimb2, grade: '5' }, // good UIAA grade
{ ...newSportClimb1, grade: 'V6' }]) // bad UIAA grade (V-scale used)
expect(newIDs).toHaveLength(4)
const climb1 = await climbs.findOneClimbByMUUID(muid.from(newIDs[0]))
expect(climb1?.grades).toEqual({ uiaa: '6+' })
const climb2 = await climbs.findOneClimbByMUUID(muid.from(newIDs[1]))
expect(climb2?.grades).toEqual({ uiaa: '7-' })
const climb3 = await climbs.findOneClimbByMUUID(muid.from(newIDs[2]))
expect(climb3?.grades).toEqual({ uiaa: '5' })
const climb4 = await climbs.findOneClimbByMUUID(muid.from(newIDs[3]))
expect(climb4?.grades).toEqual(undefined)
})
it('can update boulder problems', async () => {
const newDestination = await areas.addArea(testUser, 'Bouldering area A100', null, 'fr')
if (newDestination == null) fail('Expect new area to be created')
const newIDs = await climbs.addOrUpdateClimbs(
testUser,
newDestination.metadata.area_id,
[newBoulderProblem1, newBoulderProblem2])
const actual0 = await climbs.findOneClimbByMUUID(muid.from(newIDs[0]))
expect(actual0).toMatchObject({
name: newBoulderProblem1.name,
type: sanitizeDisciplines(newBoulderProblem1.disciplines)
})
expect(actual0?.createdBy?.toUUID().toString()).toEqual(testUser.toString())
expect(actual0?.updatedBy).toBeUndefined()
const changes: ClimbChangeInputType[] = [
{
id: newIDs[0],
name: 'new name A100',
grade: '6b',
disciplines: sanitizeDisciplines({ bouldering: true })
},
{
id: newIDs[1],
name: 'new name A200'
}
]
const otherUser = muid.v4()
const updated = await climbs.addOrUpdateClimbs(otherUser, newDestination.metadata.area_id, changes)
expect(updated).toHaveLength(2)
const actual1 = await climbs.findOneClimbByMUUID(muid.from(newIDs[0]))
expect(actual1).toMatchObject({
name: changes[0].name,
grades: {
font: changes[0].grade
},
// Make sure update doesn't touch other fields
type: sanitizeDisciplines(changes[0].disciplines),
content: {
description: newBoulderProblem1.description,
location: newBoulderProblem1.location,
protection: newBoulderProblem1.protection
}
})
expect(actual1?.createdBy?.toUUID().toString()).toEqual(testUser.toUUID().toString())
expect(actual1?.updatedBy?.toUUID().toString()).toEqual(otherUser.toUUID().toString())
})
it('can update climb length, boltsCount & fa', async () => {
const newDestination = await areas.addArea(testUser, 'Sport area Z100', null, 'fr')
if (newDestination == null) fail('Expect new area to be created')
const newIDs = await climbs.addOrUpdateClimbs(
testUser,
newDestination.metadata.area_id,
newClimbsToAdd
)
const change: ClimbChangeInputType = {
id: newIDs[0],
fa: 'First name Last name, 2023',
length: 20,
boltsCount: 5
}
await climbs.addOrUpdateClimbs(testUser,
newDestination.metadata.area_id,
[change])
const actual = await climbs.findOneClimbByMUUID(muid.from(newIDs[0]))
expect(actual?.fa).not.toBeNull()
expect(actual?.length).not.toBeNull()
expect(actual?.boltsCount).not.toBeNull()
expect(actual).toMatchObject({
fa: change.fa,
length: change.length,
boltsCount: change.boltsCount
})
})
it('can add multi-pitch climbs', async () => {
await areas.addCountry('aut')
const newDestination = await areas.addArea(testUser, 'Some Location with Multi-Pitch Climbs', null, 'aut')
if (newDestination == null) fail('Expect new area to be created')
const routesArea = await areas.addArea(testUser, 'Sport & Trad Multi-Pitches', newDestination.metadata.area_id)
// create new climb with individual pitches
const newIDs = await climbs.addOrUpdateClimbs(
testUser,
routesArea.metadata.area_id,
[newClimbWithPitches]
)
expect(newIDs).toHaveLength(1)
const climb = await climbs.findOneClimbByMUUID(muid.from(newIDs[0]))
// Validate new climb
expect(climb).toMatchObject({
name: newClimbWithPitches.name,
type: sanitizeDisciplines(newClimbWithPitches.disciplines),
content: {
description: newClimbWithPitches.description,
location: newClimbWithPitches.location,
protection: newClimbWithPitches.protection
},
pitches: newClimbWithPitches.pitches
})
// Validate each pitch
if (climb?.pitches != null) {
climb.pitches.forEach((pitch) => {
expect(pitch).toHaveProperty('_id')
expect(pitch).toHaveProperty('parentId')
expect(pitch).toHaveProperty('pitchNumber')
})
} else {
fail('Pitches are missing either of required attributes id, parentId, pitchNumber')
}
})
it('can update multi-pitch problems', async () => {
const newDestination = await areas.addArea(testUser, 'Some Multi-Pitch Area to be Updated', null, 'deu')
if (newDestination == null) fail('Expect new area to be created')
const newIDs = await climbs.addOrUpdateClimbs(
testUser,
newDestination.metadata.area_id,
[newClimbWithPitches]
)
// Fetch the original climb
const original = await climbs.findOneClimbByMUUID(muid.from(newIDs[0]))
// Check if 'original' is not null before accessing its properties
if ((original == null) || (original.pitches == null) || original.pitches.length < 2) {
fail('Original climb is null or does not have at least two pitches (as defined in the test case)')
return
}
// Store original pitch IDs and parent IDs
const originalPitch1ID = original.pitches[0]._id.toUUID().toString()
const originalPitch2ID = original.pitches[1]._id.toUUID().toString()
const originalPitch1ParentID = original.pitches[0].parentId.toUUID().toString()
const originalPitch2ParentID = original.pitches[1].parentId.toUUID().toString()
// Define updated pitch info
const updatedPitch1 = {
id: originalPitch1ID,
parentId: originalPitch1ParentID,
pitchNumber: 1,
grades: { ewbank: '19' },
disciplines: { sport: false, alpine: true },
length: 20,
boltsCount: 6,
description: 'Updated first pitch description'
}
const updatedPitch2 = {
id: originalPitch2ID,
parentId: originalPitch2ParentID,
pitchNumber: 2,
grades: { ewbank: '18' },
disciplines: { sport: false, alpine: true },
length: 25,
boltsCount: 5,
description: 'Updated second pitch description'
}
const changes: ClimbChangeInputType[] = [
{
id: newIDs[0],
pitches: [updatedPitch1, updatedPitch2]
}
]
// update climb
await climbs.addOrUpdateClimbs(testUser, newDestination.metadata.area_id, changes)
// Fetch the updated climb
const updatedClimb = await climbs.findOneClimbByMUUID(muid.from(newIDs[0]))
if (updatedClimb != null) {
// Check that the pitches.id and pitches.parentId are identical to the original values
if (updatedClimb.pitches != null) {
const assertPitch = (
pitch,
expectedPitch,
originalID,
originalParentID
): void => {
expect(pitch._id.toUUID().toString()).toEqual(originalID)
expect(pitch.parentId.toUUID().toString()).toEqual(originalParentID)
expect(pitch.pitchNumber).toEqual(expectedPitch.pitchNumber)
expect(pitch.grades).toEqual(expectedPitch.grades)
expect(pitch.type).toEqual(expectedPitch.type)
expect(pitch.length).toEqual(expectedPitch.length)
expect(pitch.boltsCount).toEqual(expectedPitch.boltsCount)
expect(pitch.description).toEqual(expectedPitch.description)
}
assertPitch(updatedClimb.pitches[0], updatedPitch1, originalPitch1ID, originalPitch1ParentID)
assertPitch(updatedClimb.pitches[1], updatedPitch2, originalPitch2ID, originalPitch2ParentID)
}
// Check that the createdBy and updatedBy fields are not undefined before accessing their properties
if ((updatedClimb.createdBy != null) && (updatedClimb.updatedBy != null)) {
expect(updatedClimb.createdBy.toUUID().toString()).toEqual(testUser.toString())
expect(updatedClimb.updatedBy.toUUID().toString()).toEqual(testUser.toString())
} else {
fail('createdBy or updatedBy is undefined')
}
}
})
})