-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermissions.js
64 lines (54 loc) · 1.84 KB
/
permissions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import Sequelize from 'sequelize';
const createResolver = (resolver) => {
const baseResolver = resolver;
baseResolver.createResolver = (childResolver) => {
const newResolver = async (parent, args, context, info) => {
await resolver(parent, args, context, info);
return childResolver(parent, args, context, info);
};
return createResolver(newResolver);
};
return baseResolver;
};
// TODO: Continue trying to figure out why this returns false for logged in user when meQuery is run on ViewTeam page.
// requiresAuth
export default createResolver((parent, args, { user }) => {
if (!user || !user.id) {
throw new Error('Not authenticated');
}
});
// requiresTeamAccess
export const requiresTeamAccess = createResolver(async (parent, { channelId }, { user, models }) => {
if (!user || !user.id) {
throw new Error('Not authenticated, team access.');
}
// Check if part of the team
const channel = await models.Channel.findOne({ where: { id: channelId } });
const member = await models.Member.findOne({
where: { teamId: channel.teamId, user_id: user.id },
});
if (!member) {
throw new Error(`You have to be a member of the team to subscribe to it's messages.`);
}
});
// export const requiresTeamAccess = createResolver((parent, args, { user }) => {
// console.log('requiresTeamAccess');
// });
// directMessage subscription
export const directMessageSubscription = createResolver(
async (parent, { teamId, userId }, { user, models }) => {
if (!user || !user.id) {
throw new Error('Not authenticated, directmessages.');
}
const Op = Sequelize.Op;
const members = await models.Member.findAll({
where: {
teamId,
[Op.or]: [{ userId }, { userId: user.id }],
},
});
if (members.length < 1) {
throw new Error('Something went wrong');
}
},
);