Fakultas Ilmu Komputer UI

utils.tsx 3.71 KB
Newer Older
Sean Zeliq Urian's avatar
Sean Zeliq Urian committed
1
import { LogDetail } from 'scenes/ActivityLog/types/types';
2
3
4
5
6
7
8
9
10
11
12

const generateLogMessage = async (log: any, services: any) => {
  let message = '';

  switch (log.model_name) {
    case 'Account': {
      const accountResponse = await services.main.getAccount(log.object_id);
      switch (log.action_type) {
        case 'Create': {
          if (accountResponse.status === 200) {
            let username = accountResponse.data.username;
Sean Zeliq Urian's avatar
Sean Zeliq Urian committed
13
            let accountType = accountResponse.data.is_admin ? 'admin' : 'kader';
14
15
16
17
18
19
20
21
22
23
24
25
26
            message =
              'Menambahkan akun ' +
              accountType +
              ' dengan username ' +
              username;
          } else {
            message = 'Menambahkan akun yang sudah dihapus';
          }
          break;
        }
        case 'Edit': {
          if (accountResponse.status === 200) {
            let username = accountResponse.data.username;
Sean Zeliq Urian's avatar
Sean Zeliq Urian committed
27
            let accountType = accountResponse.data.is_admin ? 'admin' : 'kader';
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
69
70
71
72
73
74
75
            message =
              'Memperbarui akun ' +
              accountType +
              ' dengan username ' +
              username;
          } else {
            message = 'Memperbarui akun yang sudah dihapus';
          }
          break;
        }
        case 'Delete': {
          message = 'Menghapus akun';
          break;
        }
      }
      break;
    }
    case 'Case Subject': {
      const caseSubjectResponse = await services.main.getCaseSubject(
        log.object_id
      );
      message =
        'Menambahkan subjek kasus baru atas nama ' +
        caseSubjectResponse.data.name;
      break;
    }
    case 'Investigation Case': {
      const investigationCaseResponse = await services.main.getInvestigationCase(
        log.object_id
      );
      message =
        'Menambahkan kasus positif atas nama ' +
        investigationCaseResponse.data.case_subject.name;
      break;
    }
    case 'Monitoring Case': {
      const monitoringCaseResponse = await services.main.getMonitoringCase(
        log.object_id
      );
      message =
        'Menambahkan objek pemantauan baru atas nama ' +
        monitoringCaseResponse.data.investigation_case.case_subject.name;
      break;
    }
  }
  return message;
};

Sean Zeliq Urian's avatar
Sean Zeliq Urian committed
76
77
78
79
const generateLogDetail = async (
  log: any,
  services: any
): Promise<LogDetail> => {
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  let logDetail: LogDetail = {
    activity: log.model_name,
    authorRole: log.author.is_admin ? 'admin' : 'kader',
    authorName: log.author.username,
    object: '',
  };

  switch (log.model_name) {
    case 'Account': {
      logDetail.activity = `${log.action_type} ${logDetail.activity}`;
      const accountResponse = await services.main.getAccount(log.object_id);
      if (accountResponse.status === 200) {
        let username = accountResponse.data.username;
        let accountType = accountResponse.data.is_admin ? 'admin' : 'kader';
        logDetail.object = `${username} as ${accountType}`;
      } else {
        logDetail.object = 'deleted';
      }
      break;
    }
    case 'Case Subject': {
      const caseSubjectResponse = await services.main.getCaseSubject(
        log.object_id
      );
      logDetail.object = caseSubjectResponse.data.name;
      break;
    }
    case 'Investigation Case': {
      const investigationCaseResponse = await services.main.getInvestigationCase(
        log.object_id
      );
      logDetail.object = investigationCaseResponse.data.case_subject.name;
      break;
    }
    case 'Monitoring Case': {
      const monitoringCaseResponse = await services.main.getMonitoringCase(
        log.object_id
      );
      logDetail.object =
        monitoringCaseResponse.data.investigation_case.case_subject.name;
      break;
    }
  }

  return logDetail;
};

Sean Zeliq Urian's avatar
Sean Zeliq Urian committed
127
export { generateLogDetail, generateLogMessage };