Skip to content

Instantly share code, notes, and snippets.

@jgcmarins
Created April 2, 2019 04:22
Show Gist options
  • Save jgcmarins/f90729ecde9ed3d586e230d04efb16e6 to your computer and use it in GitHub Desktop.
Save jgcmarins/f90729ecde9ed3d586e230d04efb16e6 to your computer and use it in GitHub Desktop.
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