Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breadcrumbs issue #213 fix #314

Merged
merged 9 commits into from
Jul 4, 2023
49 changes: 47 additions & 2 deletions src/model/MutableAreaDataSource.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { geometry, Point } from '@turf/helpers'
import muuid, { MUUID } from 'uuid-mongodb'
import { v5 as uuidv5, NIL } from 'uuid'
import mongoose, { ClientSession } from 'mongoose'
import mongoose, { ClientSession, Types } from 'mongoose'
import { produce } from 'immer'
import { UserInputError } from 'apollo-server'
import isoCountries from 'i18n-iso-countries'
Expand Down Expand Up @@ -301,7 +301,22 @@ export default class MutableAreaDataSource extends AreaDataSource {
throw new Error('Area update error. Reason: Updating leaf or boulder status of an area with subareas is not allowed.')
}

if (areaName != null) area.set({ area_name: sanitizeStrict(areaName) })
if (areaName != null) {
const sanitizedName = sanitizeStrict(areaName)
area.set({ area_name: sanitizedName })

// change our pathTokens
const newPath = [...area.pathTokens]
newPath.pop()
newPath.push(sanitizedName)
area.set({ pathTokens: newPath })

// iterate over the DB id's in the child list and update pathTokens
for (const childId of area.children) {
vnugent marked this conversation as resolved.
Show resolved Hide resolved
await this.updatePathTokens(childId, sanitizedName)
vnugent marked this conversation as resolved.
Show resolved Hide resolved
}
}

if (shortCode != null) area.set({ shortCode: shortCode.toUpperCase() })
if (isDestination != null) area.set({ 'metadata.isDestination': isDestination })
if (isLeaf != null) area.set({ 'metadata.leaf': isLeaf })
Expand Down Expand Up @@ -358,6 +373,36 @@ export default class MutableAreaDataSource extends AreaDataSource {
return ret
}

async updatePathTokens (childId: Types.ObjectId, newAreaName: string, index: number = 2): Promise<void> {
// const session = await this.areaModel.startSession()
const filter = { _id: childId }
// const area = await this.areaModel.findOne(filter).session(session)
const area = await this.areaModel.findOne(filter)

if (area == null) {
throw new Error('pathTokens update error. Reason: child area not found.')
}

if (area.pathTokens.length > 0) {
// copy old tokens
const newPath = [...area.pathTokens]
// update the correct index in path tokens -> (length - depth)
newPath[newPath.length - index] = newAreaName
// set tokens in the object and save the object to the DB
area.set({ pathTokens: newPath })
area.save(function (err, result) {
if (err != null) {
throw new Error('pathTokens update error. Reason: save operation failed.')
}
})

// iterate over the DB id's in the child list
for (const childId of area.children) {
await this.updatePathTokens(childId, newAreaName, index + 1)
}
}
}

/**
*
* @param user user id
Expand Down
Loading