Skip to content

Commit

Permalink
rework handling of essential pitch fields
Browse files Browse the repository at this point in the history
  • Loading branch information
kl4us committed Aug 6, 2023
1 parent 45cb1c9 commit 174d5a4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
11 changes: 8 additions & 3 deletions src/db/ClimbTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export type ClimbGQLQueryType = ClimbType & {
* Clinbs have a number of fields that may be expected to appear within their documents.
*/
export type ClimbType = IClimbProps & {
pitches?: IPitch
pitches?: IPitch[]
metadata: IClimbMetadata
content?: IClimbContent
}

/* Models a single pitch of a multi-pitch route */
export interface IPitch {
_id: MUUID
parent_id: MUUID
parent_id: string
number: number
grades?: Partial<Record<GradeScalesTypes, string>>
type?: DisciplineType
Expand Down Expand Up @@ -175,6 +175,8 @@ export interface IPitchInput {
description?: string;
}



export interface ClimbChangeInputType {
id?: string
name?: string
Expand All @@ -194,6 +196,9 @@ export interface ClimbChangeInputType {
}
}

// Includes all properties of IPitchInput except for id and parent_id
export type UpdatablePitchInput = Omit<IPitchInput, 'id' | 'parent_id'>;

type UpdatableClimbFieldsType = {
fa: ClimbType['fa'],
name: ClimbType['name'],
Expand All @@ -203,7 +208,7 @@ type UpdatableClimbFieldsType = {
content: ClimbType['content'],
length: ClimbType['length'],
boltsCount: ClimbType['boltsCount'],
pitches: IPitchInput[],
pitches: UpdatablePitchInput[],
}
/**
* Minimum required fields when adding a new climb or boulder problem
Expand Down
8 changes: 4 additions & 4 deletions src/model/MutableClimbDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ export default class MutableClimbDataSource extends ClimbDataSource {
const pitches = userInput[i].pitches

const newPitchesWithIDs = pitches != null
? pitches.map(pitch => ({
? pitches.map((pitch): IPitch => ({
...pitch,
_id: muid.from(pitch.id ?? muid.v4()),
parent_id: muid.from(pitch.parent_id ?? newClimbIds[i])
parent_id: muid.from(pitch.parent_id ?? newClimbIds[i]).toString(),
number: pitch.number ?? 0 // revert to 0 if number cannot be retrieved
}))
: null;
: null;


const { description, location, protection, name, fa, length, boltsCount } = userInput[i]

// Make sure we don't update content = {}
Expand Down
16 changes: 8 additions & 8 deletions src/model/__tests__/MutableClimbDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,10 @@ describe('Climb CRUD', () => {
}

// Store original pitch IDs and parent IDs
const originalPitch1ID = original.pitches[0]._id.toUUID().toString();
const originalPitch1ParentID = original.pitches[0].parent_id.toUUID().toString();
const originalPitch2ID = original.pitches[1]._id.toUUID().toString();
const originalPitch2ParentID = original.pitches[1].parent_id.toUUID().toString();
const originalPitch1ID = original.pitches[0]._id.toUUID().toString()
const originalPitch1ParentID = original.pitches[0].parent_id
const originalPitch2ID = original.pitches[1]._id.toUUID().toString()
const originalPitch2ParentID = original.pitches[1].parent_id

const updatedPitch1 = {
_id: originalPitch1ID,
Expand Down Expand Up @@ -608,19 +608,19 @@ describe('Climb CRUD', () => {

// Check if 'actual' is not null before accessing its properties
if (actual) {
/* // Validate that the climb is updated correctly
// Validate that the climb is updated correctly
expect(actual).toMatchObject({
name: actual.name, // Assuming the name did not change
type: actual.type, // Assuming the type did not change
pitches: changes[0].pitches // The pitches should now match the updated pitches
}); */
});

// Check that the pitches.id and pitches.parent_id are identical to the original values
if (actual.pitches) {
expect(actual.pitches[0]._id.toUUID().toString()).toEqual(originalPitch1ID);
expect(actual.pitches[0].parent_id.toUUID().toString()).toEqual(originalPitch1ParentID);
expect(actual.pitches[0].parent_id).toEqual(originalPitch1ParentID);
expect(actual.pitches[1]._id.toUUID().toString()).toEqual(originalPitch2ID);
expect(actual.pitches[1].parent_id.toUUID().toString()).toEqual(originalPitch2ParentID);
expect(actual.pitches[1].parent_id).toEqual(originalPitch2ParentID);
}

// Check that the createdBy and updatedBy fields are not undefined before accessing their properties
Expand Down

0 comments on commit 174d5a4

Please sign in to comment.