File size: 1,784 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const { encryptString } = require('../../shared/encryption');

exports.up = async (r, conn) => {
  const encryptOldSlackImportData = async () => {
    const records = await r
      .table('slackImports')
      .run(conn)
      .then(cursor => cursor.toArray());

    const recordPromises = records.map(async record => {
      const teamId = encryptString(record.teamId);
      const teamName = encryptString(record.teamName);
      const token = encryptString(record.token);

      return await r
        .table('slackImports')
        .get(record.id)
        .update({
          teamId,
          teamName,
          token,
        })
        .run(conn);
    });

    return await Promise.all([...recordPromises]);
  };

  const encryptNewSlackImportData = async () => {
    const records = await r
      .table('communitySettings')
      .filter(row => row.hasFields('slackSettings'))
      .run(conn)
      .then(cursor => cursor.toArray());

    const recordPromises = records.map(async record => {
      const teamId = encryptString(record.slackSettings.teamId);
      const teamName = encryptString(record.slackSettings.teamName);
      const token = encryptString(record.slackSettings.token);
      const scope = encryptString(record.slackSettings.scope);

      return await r
        .table('communitySettings')
        .get(record.id)
        .update({
          slackSettings: {
            teamId,
            teamName,
            token,
            scope,
          },
        })
        .run(conn);
    });

    return await Promise.all([...recordPromises]);
  };

  return await Promise.all([
    encryptOldSlackImportData(),
    encryptNewSlackImportData(),
  ]).catch(err => console.error(err));
};

exports.down = function(r, conn) {
  return Promise.resolve();
};