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

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

	try {
		// const data = await user.create(req.body, req.user?.organisations);
		// res.status(203).send(data);
		const requestor = new ServerUser(req.user, pool);
		if (requestor.name) {
			const newUser = new ServerUser(req.body);
			newUser.filter(requestor);
			newUser.client = pool;
			console.log("newUser", newUser.toJSON());
			const asSaved = await newUser.save();
			console.log("asSaved", asSaved);
			if (asSaved instanceof ServerUser) {
				res.status(200).send(asSaved.toJSON());
			}
		}
		next();
	} catch (err) {
		next(err);
	}

};
