2017-07-18 06:30:16 +00:00
|
|
|
// @flow
|
2017-12-26 13:02:26 +00:00
|
|
|
import { DataTypes, sequelize, Op } from '../sequelize';
|
2017-05-27 18:08:52 +00:00
|
|
|
import Collection from './Collection';
|
2017-12-26 13:02:26 +00:00
|
|
|
import User from './User';
|
2016-04-29 05:25:37 +00:00
|
|
|
|
2017-04-27 04:47:03 +00:00
|
|
|
const Team = sequelize.define(
|
|
|
|
'team',
|
|
|
|
{
|
|
|
|
id: {
|
|
|
|
type: DataTypes.UUID,
|
|
|
|
defaultValue: DataTypes.UUIDV4,
|
|
|
|
primaryKey: true,
|
2016-09-15 03:13:29 +00:00
|
|
|
},
|
2017-04-27 04:47:03 +00:00
|
|
|
name: DataTypes.STRING,
|
|
|
|
slackId: { type: DataTypes.STRING, allowNull: true },
|
|
|
|
slackData: DataTypes.JSONB,
|
2016-06-20 07:18:03 +00:00
|
|
|
},
|
2017-04-27 04:47:03 +00:00
|
|
|
{
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
unique: true,
|
|
|
|
fields: ['slackId'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
);
|
2016-04-29 05:25:37 +00:00
|
|
|
|
2017-07-12 07:28:18 +00:00
|
|
|
Team.associate = models => {
|
2017-07-18 06:30:16 +00:00
|
|
|
Team.hasMany(models.Collection, { as: 'collections' });
|
2017-07-12 07:28:18 +00:00
|
|
|
Team.hasMany(models.Document, { as: 'documents' });
|
|
|
|
Team.hasMany(models.User, { as: 'users' });
|
|
|
|
};
|
|
|
|
|
|
|
|
Team.prototype.createFirstCollection = async function(userId) {
|
2017-11-27 05:51:06 +00:00
|
|
|
return await Collection.create({
|
|
|
|
name: 'General',
|
2017-07-12 07:28:18 +00:00
|
|
|
description: 'Your first Collection',
|
|
|
|
type: 'atlas',
|
|
|
|
teamId: this.id,
|
|
|
|
creatorId: userId,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-12-26 13:02:26 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-29 05:25:37 +00:00
|
|
|
export default Team;
|