forked from danielstern/express-react-fullstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.js
More file actions
20 lines (17 loc) · 701 Bytes
/
utility.js
File metadata and controls
20 lines (17 loc) · 701 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { connectDB } from './connect-db'
export async function assembleUserState(user){
let db = await connectDB();
let tasks = await db.collection(`tasks`).find({owner:user.id}).toArray();
let comments = await db.collection(`comments`).find({task:{$in:tasks.map(task=>task.id)}}).toArray();
let users = [
await db.collection(`users`).findOne({id:user.id}),
...await db.collection(`users`).find({id:{$in:[...tasks,comments].map(x=>x.owner)}}).toArray()
];
return {
session:{authenticated:`AUTHENTICATED`,id:user.id},
groups:await db.collection(`groups`).find({owner:user.id}).toArray(),
tasks,
users,
comments
};
}