Skip to content

feat:return ancestors and pathTokens on climbs in area queries #469

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions src/model/AreaDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ export default class AreaDataSource extends MongoDataSource<AreaType> {
},
{
$set: {
'climbs.gradeContext': '$gradeContext' // manually set area's grade context to climb
'climbs.gradeContext': '$gradeContext', // manually set area's grade context to climb
'climbs.ancestors': '$ancestors', // copy ancestors from area to climbs
'climbs.pathTokens': '$pathTokens' // copy pathTokens from area to climbs
}
}
])
Expand Down Expand Up @@ -184,8 +186,52 @@ export default class AreaDataSource extends MongoDataSource<AreaType> {
*/
async findDescendantsByPath (path: string, isLeaf: boolean = false): Promise<AreaType[]> {
const regex = new RegExp(`^${path}`)
const data = this.collection.find({ ancestors: regex, 'metadata.leaf': isLeaf })
return await data.toArray()

const pipeline = [
{
$match: {
ancestors: regex,
'metadata.leaf': isLeaf
}
},
{
$lookup: {
from: 'climbs', // Collection name for climbs
localField: 'climbs',
foreignField: '_id',
as: 'climbs'
}
},
{
$unwind: {
path: '$climbs',
preserveNullAndEmptyArrays: true // Ensure areas without climbs are still included
}
},
{
$set: {
'climbs.ancestors': '$ancestors', // Copy ancestors from area to climbs
'climbs.pathTokens': '$pathTokens' // Copy pathTokens from area to climbs
}
},
{
$group: {
_id: '$_id',
area: { $first: '$$ROOT' }, // Group back into areas
climbs: { $push: '$climbs' }
}
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: ['$area', { climbs: '$climbs' }]
}
}
}
]

const data = await this.collection.aggregate(pipeline).toArray()
return data as AreaType[] // Explicitly cast the result to AreaType[]
}

/**
Expand Down
Loading