51 lines
1.8 KiB
JavaScript
51 lines
1.8 KiB
JavaScript
import UserService from '../services/user.js';
|
|
import GroupService from '../services/group.js';
|
|
import log from '../utils/log.js';
|
|
import statuses from '../utils/status.js';
|
|
|
|
const TAG = "/middlewares/existance.js"
|
|
|
|
const usernameExists = async (req, res, next) => {
|
|
try {
|
|
let { username } = req.body;
|
|
|
|
let user = await UserService.getByUsername(username);
|
|
if (!user || user == statuses.not_found) return res.status(404).send("User not found");
|
|
next();
|
|
} catch (e) { return res.status(500).send(log.unknownError(`${TAG}/usernameExists: ${e}`)); }
|
|
};
|
|
|
|
const usernameDoesntExist = async (req, res, next) => {
|
|
try {
|
|
let { username } = req.body;
|
|
|
|
let user = await UserService.getByUsername(username);
|
|
if (user || user != statuses.not_found) return res.status(400).send("Such username already taken");
|
|
next();
|
|
} catch (e) { return res.status(500).send(log.unknownError(`${TAG}/usernameDoesntExist: ${e}`)); }
|
|
}
|
|
|
|
const groupExists = async (req, res, next) => {
|
|
try {
|
|
|
|
const { id } = req.params;
|
|
|
|
let group = await GroupService.getById(id);
|
|
|
|
if (!group || group == statuses.not_found) return res.status(404).send("Group not found");
|
|
next();
|
|
} catch (e) { return res.status(500).send(log.unknownError(`${TAG}/groupExists: ${e}`)) }
|
|
}
|
|
|
|
const groupDoesntExist = async (req, res, next) => {
|
|
try {
|
|
|
|
const { id } = req.params;
|
|
|
|
let group = await GroupService.getById(id);
|
|
|
|
if (group || group != statuses.not_found) return res.status(400).send("Such group already exists");
|
|
next();
|
|
} catch (e) { return res.status(500).send(log.unknownError(`${TAG}/groupDoesntExist: ${e}`)) }
|
|
}
|
|
export default { usernameExists, usernameDoesntExist, groupExists, groupDoesntExist } |