Skip to content

Commit

Permalink
feat: support specifying area type when adding new areas (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
vnugent committed Jul 24, 2023
1 parent 9b0b35b commit 1900669
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/graphql/area/AreaMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const AreaMutations = {

addArea: async (_, { input }, { dataSources, user }: ContextWithAuth): Promise<AreaType | null> => {
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')
Expand All @@ -38,7 +38,9 @@ const AreaMutations = {
user.uuid, name,
parentUuid == null ? null : muuid.from(parentUuid),
countryCode,
experimentalAuthor
experimentalAuthor,
isLeaf,
isBoulder
)
},

Expand Down
2 changes: 2 additions & 0 deletions src/graphql/schema/AreaEdit.gql
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ input AreaInput {
parentUuid: ID
countryCode: String
isDestination: Boolean
isLeaf: Boolean
isBoulder: Boolean
experimentalAuthor: ExperimentalAuthorType
}

Expand Down
24 changes: 21 additions & 3 deletions src/model/MutableAreaDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AreaType> {
async addArea (user: MUUID,
areaName: string,
parentUuid: MUUID | null,
countryCode?: string,
experimentalAuthor?: ExperimentalAuthorType,
isLeaf?: boolean,
isBoulder?: boolean): Promise<AreaType> {
if (parentUuid == null && countryCode == null) {
throw new Error('Adding area failed. Must provide parent Id or country code')
}
Expand All @@ -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<any> {
async _addArea (session, user: MUUID, areaName: string, parentUuid: MUUID, experimentalAuthor?: ExperimentalAuthorType, isLeaf?: boolean, isBoulder?: boolean): Promise<any> {
const parentFilter = { 'metadata.area_id': parentUuid }
const parent = await this.areaModel.findOne(parentFilter).session(session).orFail(new UserInputError('Expecting 1 parent, found none.'))

Expand Down Expand Up @@ -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 => {
Expand Down
10 changes: 10 additions & 0 deletions src/model/__tests__/updateAreas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 1900669

Please sign in to comment.