diff --git a/src/graphql/area/AreaMutations.ts b/src/graphql/area/AreaMutations.ts index 9fa514ef..6c62de5a 100644 --- a/src/graphql/area/AreaMutations.ts +++ b/src/graphql/area/AreaMutations.ts @@ -29,7 +29,7 @@ const AreaMutations = { addArea: async (_, { input }, { dataSources, user }: ContextWithAuth): Promise => { const { areas } = dataSources - const { name, parentUuid, countryCode, experimentalAuthor } = input + const { name, parentUuid, countryCode, experimentalAuthor, isLeaf, isBoulder } = input // permission middleware shouldn't send undefined uuid if (user?.uuid == null) throw new Error('Missing user uuid') @@ -38,7 +38,9 @@ const AreaMutations = { user.uuid, name, parentUuid == null ? null : muuid.from(parentUuid), countryCode, - experimentalAuthor + experimentalAuthor, + isLeaf, + isBoulder ) }, diff --git a/src/graphql/schema/AreaEdit.gql b/src/graphql/schema/AreaEdit.gql index 6d0c0bb2..3db833d1 100644 --- a/src/graphql/schema/AreaEdit.gql +++ b/src/graphql/schema/AreaEdit.gql @@ -39,6 +39,8 @@ input AreaInput { parentUuid: ID countryCode: String isDestination: Boolean + isLeaf: Boolean + isBoulder: Boolean experimentalAuthor: ExperimentalAuthorType } diff --git a/src/model/MutableAreaDataSource.ts b/src/model/MutableAreaDataSource.ts index 4ea428bf..bdd67b08 100644 --- a/src/model/MutableAreaDataSource.ts +++ b/src/model/MutableAreaDataSource.ts @@ -107,7 +107,13 @@ export default class MutableAreaDataSource extends AreaDataSource { * @param parentUuid * @param countryCode */ - async addArea (user: MUUID, areaName: string, parentUuid: MUUID | null, countryCode?: string, experimentalAuthor?: ExperimentalAuthorType): Promise { + async addArea (user: MUUID, + areaName: string, + parentUuid: MUUID | null, + countryCode?: string, + experimentalAuthor?: ExperimentalAuthorType, + isLeaf?: boolean, + isBoulder?: boolean): Promise { if (parentUuid == null && countryCode == null) { throw new Error('Adding area failed. Must provide parent Id or country code') } @@ -127,14 +133,14 @@ export default class MutableAreaDataSource extends AreaDataSource { // see https://jira.mongodb.org/browse/NODE-2014 await session.withTransaction( async (session) => { - ret = await this._addArea(session, user, areaName, uuid, experimentalAuthor) + ret = await this._addArea(session, user, areaName, uuid, experimentalAuthor, isLeaf, isBoulder) return ret }) // @ts-expect-error return ret } - async _addArea (session, user: MUUID, areaName: string, parentUuid: MUUID, experimentalAuthor?: ExperimentalAuthorType): Promise { + async _addArea (session, user: MUUID, areaName: string, parentUuid: MUUID, experimentalAuthor?: ExperimentalAuthorType, isLeaf?: boolean, isBoulder?: boolean): Promise { const parentFilter = { 'metadata.area_id': parentUuid } const parent = await this.areaModel.findOne(parentFilter).session(session).orFail(new UserInputError('Expecting 1 parent, found none.')) @@ -170,6 +176,18 @@ export default class MutableAreaDataSource extends AreaDataSource { const parentPathTokens = parent.pathTokens const parentGradeContext = parent.gradeContext const newArea = newAreaHelper(areaName, parentAncestors, parentPathTokens, parentGradeContext) + if (isLeaf != null) { + newArea.metadata.leaf = isLeaf + } + if (isBoulder != null) { + if (isBoulder) { + // a boulder is also a leaf area + newArea.metadata.leaf = true + newArea.metadata.isBoulder = true + } else { + newArea.metadata.isBoulder = false + } + } newArea.metadata.lnglat = parent.metadata.lnglat newArea.createdBy = experimentaAuthorId ?? user newArea._change = produce(newChangeMeta, draft => { diff --git a/src/model/__tests__/updateAreas.ts b/src/model/__tests__/updateAreas.ts index 6da6b3b6..693edc68 100644 --- a/src/model/__tests__/updateAreas.ts +++ b/src/model/__tests__/updateAreas.ts @@ -109,6 +109,16 @@ describe('Areas', () => { expect(countryInDb.children[0]).toEqual(area?._id) }) + it('should set crag/boulder attribute when adding new areas', async () => { + let parent = await areas.addArea(testUser, 'Boulder A', null, 'can', undefined, false, true) + expect(parent.metadata.isBoulder).toBe(true) + expect(parent.metadata.leaf).toBe(true) + + parent = await areas.addArea(testUser, 'Sport A', null, 'can', undefined, true, undefined) + expect(parent.metadata.isBoulder).toBe(false) + expect(parent.metadata.leaf).toBe(true) + }) + it('should update multiple fields', async () => { await areas.addCountry('au') const a1 = await areas.addArea(testUser, 'One', null, 'au')