File size: 1,187 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// @flow
import { isAuthedResolver, isAdmin } from '../../utils/permissions';
import UserError from '../../utils/UserError';
import type { GraphQLContext } from '../../';
import { banUser } from 'shared/db/queries/user';

type BanUserInput = {
  input: {
    userId: string,
    reason: string,
  },
};

export default isAuthedResolver(
  async (_: any, args: BanUserInput, ctx: GraphQLContext) => {
    const {
      input: { userId, reason },
    } = args;
    const { loaders, user: currentUser } = ctx;

    if (!isAdmin(currentUser.id)) {
      return new UserError('You don’t have permission to do that.');
    }

    if (currentUser.id === userId) {
      return new UserError('You cannot ban yourself.');
    }

    const reportedUser = await loaders.user.load(userId);

    if (!reportedUser) {
      return new UserError(`User with ID ${userId} does not exist.`);
    }

    if (reportedUser.bannedAt) {
      return new UserError('This user has already been banned');
    }

    return banUser({
      userId,
      reason,
      currentUserId: currentUser.id,
    })
      .then(() => {
        return true;
      })
      .catch(err => new UserError(err.message));
  }
);