This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/server/models/Team.js

67 lines
1.4 KiB
JavaScript
Raw Normal View History

// @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,
},
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
Team.associate = models => {
Team.hasMany(models.Collection, { as: 'collections' });
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',
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;