2017-07-18 06:30:16 +00:00
|
|
|
// @flow
|
2018-06-01 22:00:48 +00:00
|
|
|
import uuid from 'uuid';
|
2017-12-26 13:02:26 +00:00
|
|
|
import { DataTypes, sequelize, Op } from '../sequelize';
|
2018-06-01 22:00:48 +00:00
|
|
|
import { publicS3Endpoint, uploadToS3FromUrl } from '../utils/s3';
|
2018-11-03 01:50:13 +00:00
|
|
|
import { RESERVED_SUBDOMAINS } from '../domains';
|
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
|
|
|
|
2018-11-03 01:50:13 +00:00
|
|
|
const Team = sequelize.define('team', {
|
|
|
|
id: {
|
|
|
|
type: DataTypes.UUID,
|
|
|
|
defaultValue: DataTypes.UUIDV4,
|
|
|
|
primaryKey: true,
|
|
|
|
},
|
|
|
|
name: DataTypes.STRING,
|
|
|
|
subdomain: {
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: true,
|
|
|
|
validate: {
|
|
|
|
isLowercase: true,
|
|
|
|
isAlphanumeric: true,
|
|
|
|
len: [4, 32],
|
|
|
|
notIn: [RESERVED_SUBDOMAINS],
|
2016-09-15 03:13:29 +00:00
|
|
|
},
|
2018-11-03 01:50:13 +00:00
|
|
|
unique: true,
|
2016-06-20 07:18:03 +00:00
|
|
|
},
|
2018-11-03 01:50:13 +00:00
|
|
|
slackId: { type: DataTypes.STRING, allowNull: true },
|
|
|
|
googleId: { type: DataTypes.STRING, allowNull: true },
|
|
|
|
avatarUrl: { type: DataTypes.STRING, allowNull: true },
|
|
|
|
sharing: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true },
|
|
|
|
slackData: DataTypes.JSONB,
|
|
|
|
});
|
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' });
|
|
|
|
};
|
|
|
|
|
2018-06-01 22:00:48 +00:00
|
|
|
const uploadAvatar = async model => {
|
|
|
|
const endpoint = publicS3Endpoint();
|
|
|
|
|
|
|
|
if (model.avatarUrl && !model.avatarUrl.startsWith(endpoint)) {
|
2018-08-19 23:06:39 +00:00
|
|
|
try {
|
|
|
|
const newUrl = await uploadToS3FromUrl(
|
|
|
|
model.avatarUrl,
|
|
|
|
`avatars/${model.id}/${uuid.v4()}`
|
|
|
|
);
|
|
|
|
if (newUrl) model.avatarUrl = newUrl;
|
|
|
|
} catch (err) {
|
|
|
|
// we can try again next time
|
|
|
|
console.error(err);
|
|
|
|
}
|
2018-06-01 22:00:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-12 07:28:18 +00:00
|
|
|
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) {
|
2018-02-18 09:14:51 +00:00
|
|
|
return user.update({ isAdmin: true });
|
2017-12-26 13:02:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Team.prototype.removeAdmin = async function(user: User) {
|
|
|
|
const res = await User.findAndCountAll({
|
|
|
|
where: {
|
2018-02-18 09:14:51 +00:00
|
|
|
teamId: this.id,
|
2017-12-26 13:02:26 +00:00
|
|
|
isAdmin: true,
|
|
|
|
id: {
|
2018-05-05 23:16:08 +00:00
|
|
|
// $FlowFixMe
|
2017-12-26 13:02:26 +00:00
|
|
|
[Op.ne]: user.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
limit: 1,
|
|
|
|
});
|
|
|
|
if (res.count >= 1) {
|
2018-02-18 09:14:51 +00:00
|
|
|
return user.update({ isAdmin: false });
|
2017-12-26 13:02:26 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('At least one admin is required');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-04 23:38:51 +00:00
|
|
|
Team.prototype.suspendUser = async function(user: User, admin: User) {
|
|
|
|
if (user.id === admin.id)
|
|
|
|
throw new Error('Unable to suspend the current user');
|
|
|
|
return user.update({
|
|
|
|
suspendedById: admin.id,
|
|
|
|
suspendedAt: new Date(),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Team.prototype.activateUser = async function(user: User, admin: User) {
|
|
|
|
return user.update({
|
|
|
|
suspendedById: null,
|
|
|
|
suspendedAt: null,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-06-01 22:00:48 +00:00
|
|
|
Team.beforeSave(uploadAvatar);
|
|
|
|
|
2016-04-29 05:25:37 +00:00
|
|
|
export default Team;
|