Created
April 2, 2019 04:22
-
-
Save jgcmarins/f90729ecde9ed3d586e230d04efb16e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mongoose from 'mongoose'; | |
import UserModel from './UserModel'; | |
export default async (parentId, leafId) => { | |
const searchResult = await UserModel.aggregate([ | |
{ $match: { _id: parentId.toString() } }, | |
{ | |
$graphLookup: { | |
from: 'User', | |
startWith: '$manager', | |
connectFromField: 'manager', | |
connectToField: '_id', | |
as: 'connection', | |
}, | |
}, | |
]); | |
if (!searchResult || !Array.isArray(searchResult) || searchResult.length < 1) { | |
return false; | |
} | |
return !!searchResult[0].connection.find(doc => doc._id.equals(leafId.toString())); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment