Admin endpoints

This commit is contained in:
Jori Lallo
2017-12-26 15:02:26 +02:00
parent a74e90fc09
commit 26d0d815a2
14 changed files with 366 additions and 38 deletions

View File

@@ -1,6 +1,7 @@
// @flow
import { DataTypes, sequelize } from '../sequelize';
import { DataTypes, sequelize, Op } from '../sequelize';
import Collection from './Collection';
import User from './User';
const Team = sequelize.define(
'team',
@@ -41,4 +42,26 @@ Team.prototype.createFirstCollection = async function(userId) {
return atlas;
};
Team.prototype.addAdmin = async function(user: User) {
return await user.update({ isAdmin: true });
};
Team.prototype.removeAdmin = async function(user: User) {
const res = await User.findAndCountAll({
where: {
teamId: user.teamId,
isAdmin: true,
id: {
[Op.ne]: user.id,
},
},
limit: 1,
});
if (res.count >= 1) {
return await user.update({ isAdmin: false });
} else {
throw new Error('At least one admin is required');
}
};
export default Team;