* Get workspace with members in a single query
(teamId: string, workspaceSlug: string)
| 273 | * Get workspace with members in a single query |
| 274 | */ |
| 275 | async getWorkspaceWithMembers(teamId: string, workspaceSlug: string): Promise<any> { |
| 276 | this.trackQuery('workspaces', 'aggregateWithMembers'); |
| 277 | |
| 278 | const db = this.client.db(databaseName); |
| 279 | const workspaces = db.collection<WorkspaceDbSchema>('workspaces'); |
| 280 | |
| 281 | const result = await workspaces.aggregate([ |
| 282 | { |
| 283 | $match: { |
| 284 | team: new ObjectId(teamId), |
| 285 | 'meta.slug': workspaceSlug |
| 286 | } |
| 287 | }, |
| 288 | { |
| 289 | $lookup: { |
| 290 | from: 'workspace_users', |
| 291 | localField: '_id', |
| 292 | foreignField: 'workspace', |
| 293 | as: 'members' |
| 294 | } |
| 295 | }, |
| 296 | { |
| 297 | $lookup: { |
| 298 | from: 'users', |
| 299 | let: { memberUserIds: '$members.user' }, |
| 300 | pipeline: [ |
| 301 | { |
| 302 | $match: { |
| 303 | $expr: { $in: ['$_id', '$$memberUserIds'] } |
| 304 | } |
| 305 | }, |
| 306 | { |
| 307 | $project: { |
| 308 | _id: 1, |
| 309 | name: 1, |
| 310 | email: 1, |
| 311 | image: 1 |
| 312 | } |
| 313 | } |
| 314 | ], |
| 315 | as: 'memberUsers' |
| 316 | } |
| 317 | }, |
| 318 | { |
| 319 | $addFields: { |
| 320 | members: { |
| 321 | $map: { |
| 322 | input: '$members', |
| 323 | as: 'member', |
| 324 | in: { |
| 325 | $mergeObjects: [ |
| 326 | '$$member', |
| 327 | { |
| 328 | user: { |
| 329 | $arrayElemAt: [ |
| 330 | { |
| 331 | $filter: { |
| 332 | input: '$memberUsers', |
nothing calls this directly
no test coverage detected