
const ServerUser = require('../../../lib/db/user/User');
const { pool } = require('../../../lib/db/connection');

module.exports = async function (req, res, next) {

	try {
		const requestor = new ServerUser(req.user, pool);
		console.log("REQUESTOR", requestor.toJSON());
		if (requestor.name) {
			const allUsers = await ServerUser.fromSQL(); // Get all users
			const listableUsers = requestor.editablePeers(allUsers);
			res.status(200).send(listableUsers.map(u => u.toJSON()));
		}

		// const userOrgs = organisations.extract(req.user?.organisations ?? {}, [ "write", "edit" ]);
		// const users = await user.list(userOrgs.includes("*") ? null : userOrgs); // null: list all
		// console.log("user", JSON.stringify(req.user, null, 4));
		// console.log("userOrgs", userOrgs);
		// console.log("users", users);
		// res.status(200).send(users);
		next();
	} catch (err) {
		next(err);
	}

};
