• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

errsole / errsole.js / #34

19 Jul 2024 07:00PM UTC coverage: 94.853% (+0.8%) from 94.058%
#34

push

venki91
build: release a minor version

236 of 266 branches covered (88.72%)

Branch coverage included in aggregate %.

538 of 550 relevant lines covered (97.82%)

2.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.77
/lib/main/server/controllers/appController.js
1
const Jsonapi = require('../utils/jsonapiUtil');
2✔
2
const NPMUpdates = require('../utils/npmUpdates');
2✔
3
const { getStorageConnection } = require('../storageConnection');
2✔
4
const packageJson = require('../../../../package.json');
2✔
5
const helpers = require('../utils/helpers');
2✔
6
const Alerts = require('../utils/alerts');
2✔
7

8
exports.checkUpdates = async (req, res) => {
2✔
9
  try {
4✔
10
    const errsoleLatestVersion = await NPMUpdates.fetchLatestVersion('errsole');
4✔
11
    const storageConnection = getStorageConnection();
3✔
12
    const storageLatestVersion = await NPMUpdates.fetchLatestVersion(
2✔
13
      storageConnection.name
14
    );
15
    const data = {
1✔
16
      name: packageJson.name,
17
      version: packageJson.version,
18
      latest_version: errsoleLatestVersion,
19
      storage_name: storageConnection.name,
20
      storage_version: storageConnection.version,
21
      storage_latest_version: storageLatestVersion
22
    };
23
    res.send(Jsonapi.Serializer.serialize(Jsonapi.AppType, data));
1✔
24
  } catch (error) {
25
    console.error(error);
3✔
26
    res.status(500).send({
3✔
27
      errors: [
28
        {
29
          error: 'Internal Server Error',
30
          message: 'An unexpected error occurred'
31
        }
32
      ]
33
    });
34
  }
35
};
36

37
exports.getSlackDetails = async (req, res) => {
2✔
38
  try {
2✔
39
    const storageConnection = getStorageConnection();
2✔
40
    const data = await storageConnection.getConfig('slackIntegration');
2✔
41
    if (data && data.item) {
1!
42
      data.item.value = JSON.parse(data.item.value);
1✔
43
      delete data.item.value.url;
1✔
44
    }
45
    res.send(Jsonapi.Serializer.serialize(Jsonapi.AppType, data.item || {}));
1!
46
  } catch (error) {
47
    console.error(error);
1✔
48
    res.status(500).send({
1✔
49
      errors: [
50
        {
51
          error: 'Internal Server Error',
52
          message: 'An unexpected error occurred'
53
        }
54
      ]
55
    });
56
  }
57
};
58

59
exports.addSlackDetails = async (req, res) => {
2✔
60
  try {
5✔
61
    const { url } = helpers.extractAttributes(req.body);
5✔
62
    const slackUrl = await helpers.SlackUrl(url);
5✔
63
    if (!slackUrl) {
4✔
64
      const errorData = [
1✔
65
        {
66
          error: 'Conflict',
67
          message: 'You have sent a url which is not a slack url.'
68
        }
69
      ];
70
      return res.status(409).send({ errors: errorData });
1✔
71
    } else {
72
      const storageConnection = getStorageConnection();
3✔
73
      const data = await storageConnection.getConfig('slackIntegration');
3✔
74
      if (data && !data.item) {
2✔
75
        const details = {
1✔
76
          url,
77
          username: 'Errsole',
78
          icon_url: 'https://avatars.githubusercontent.com/u/84983840',
79
          status: true
80
        };
81
        const result = await storageConnection.setConfig(
1✔
82
          'slackIntegration',
83
          JSON.stringify(details)
84
        );
85
        if (result && result.item) {
1!
86
          result.item.value = JSON.parse(result.item.value);
1✔
87
          res.send(Jsonapi.Serializer.serialize(Jsonapi.AppType, result.item));
1✔
88
        } else {
89
          res.status(500).send({
×
90
            errors: [
91
              {
92
                error: 'Internal Server Error',
93
                message: 'An unexpected error occurred'
94
              }
95
            ]
96
          });
97
        }
98
      } else {
99
        const errorData = [
1✔
100
          {
101
            error: 'Conflict',
102
            message: 'You have already added a webhook url for slack.'
103
          }
104
        ];
105
        res.status(409).send({ errors: errorData });
1✔
106
      }
107
    }
108
  } catch (error) {
109
    console.error(error);
2✔
110
    res.status(500).send({
2✔
111
      errors: [
112
        {
113
          error: 'Internal Server Error',
114
          message: 'An unexpected error occurred'
115
        }
116
      ]
117
    });
118
  }
119
};
120

121
exports.updateSlackDetails = async (req, res) => {
2✔
122
  try {
6✔
123
    const { status } = helpers.extractAttributes(req.body);
6✔
124
    const storageConnection = getStorageConnection();
6✔
125
    const data = await storageConnection.getConfig('slackIntegration');
5✔
126
    if (data && data.item) {
4✔
127
      let parsedValue;
128
      try {
3✔
129
        parsedValue = JSON.parse(data.item.value);
3✔
130
        parsedValue.status = status;
2✔
131
      } catch (err) {
132
        console.error(err);
1✔
133
        res.status(500).send({
1✔
134
          errors: [
135
            {
136
              error: 'Internal Server Error',
137
              message: 'An unexpected error occurred'
138
            }
139
          ]
140
        });
141
      }
142
      data.item.value.status = status;
3✔
143
      const result = await storageConnection.setConfig(
3✔
144
        'slackIntegration',
145
        JSON.stringify(parsedValue)
146
      );
147
      if (result && result.item) {
1!
148
        result.item.value = JSON.parse(result.item.value);
1✔
149
        res.send(Jsonapi.Serializer.serialize(Jsonapi.AppType, result.item));
1✔
150
      } else {
151
        res.status(500).send({
×
152
          errors: [
153
            {
154
              error: 'Internal Server Error',
155
              message: 'An unexpected error occurred'
156
            }
157
          ]
158
        });
159
      }
160
    } else {
161
      res.status(500).send({
1✔
162
        errors: [
163
          {
164
            error: 'Internal Server Error',
165
            message: 'An unexpected error occurred'
166
          }
167
        ]
168
      });
169
    }
170
  } catch (error) {
171
    console.error(error);
4✔
172
    res.status(500).send({
4✔
173
      errors: [
174
        {
175
          error: 'Internal Server Error',
176
          message: 'An unexpected error occurred'
177
        }
178
      ]
179
    });
180
  }
181
};
182

183
exports.deleteSlackDetails = async (req, res) => {
2✔
184
  try {
4✔
185
    const storageConnection = getStorageConnection();
4✔
186
    const data = await storageConnection.deleteConfig('slackIntegration');
3✔
187
    if (data) {
2✔
188
      res.send(
1✔
189
        Jsonapi.Serializer.serialize(Jsonapi.AppType, {
190
          data: 'slack integration has been removed'
191
        })
192
      );
193
    } else {
194
      res.status(500).send({
1✔
195
        errors: [
196
          {
197
            error: 'Internal Server Error',
198
            message: 'An unexpected error occurred'
199
          }
200
        ]
201
      });
202
    }
203
  } catch (error) {
204
    console.error(error);
2✔
205
    res.status(500).send({
2✔
206
      errors: [
207
        {
208
          error: 'Internal Server Error',
209
          message: 'An unexpected error occurred'
210
        }
211
      ]
212
    });
213
  }
214
};
215

216
exports.getEmailDetails = async (req, res) => {
2✔
217
  try {
6✔
218
    const storageConnection = getStorageConnection();
6✔
219
    const data = await storageConnection.getConfig('emailIntegration');
6✔
220
    if (data && data.item) {
5✔
221
      data.item.value = JSON.parse(data.item.value);
4✔
222
      delete data.item.value.url;
3✔
223
    }
224
    res.send(Jsonapi.Serializer.serialize(Jsonapi.AppType, data.item || {}));
4✔
225
  } catch (error) {
226
    console.error(error);
2✔
227
    res.status(500).send({
2✔
228
      errors: [
229
        {
230
          error: 'Internal Server Error',
231
          message: 'An unexpected error occurred'
232
        }
233
      ]
234
    });
235
  }
236
};
237

238
exports.addEmailDetails = async (req, res) => {
2✔
239
  try {
4✔
240
    const { sender, host, port, username, password, receivers } =
241
      helpers.extractAttributes(req.body);
4✔
242
    const storageConnection = getStorageConnection();
4✔
243
    const details = {
3✔
244
      sender,
245
      host,
246
      port,
247
      username,
248
      password,
249
      receivers,
250
      status: true
251
    };
252
    const result = await storageConnection.setConfig(
3✔
253
      'emailIntegration',
254
      JSON.stringify(details)
255
    );
256
    if (result && result.item) {
2✔
257
      result.item.value = JSON.parse(result.item.value);
1✔
258
      await Alerts.clearEmailTransport();
1✔
259
      res.send(Jsonapi.Serializer.serialize(Jsonapi.AppType, result.item));
1✔
260
    } else {
261
      res.status(500).send({
1✔
262
        errors: [
263
          {
264
            error: 'Internal Server Error',
265
            message: 'An unexpected error occurred'
266
          }
267
        ]
268
      });
269
    }
270
  } catch (error) {
271
    console.error(error);
2✔
272
    res.status(500).send({
2✔
273
      errors: [
274
        {
275
          error: 'Internal Server Error',
276
          message: 'An unexpected error occurred'
277
        }
278
      ]
279
    });
280
  }
281
};
282

283
exports.updateEmailDetails = async (req, res) => {
2✔
284
  try {
5✔
285
    const { status } = helpers.extractAttributes(req.body);
5✔
286
    const storageConnection = getStorageConnection();
5✔
287
    const data = await storageConnection.getConfig('emailIntegration');
5✔
288
    if (data && data.item) {
4✔
289
      let parsedValue;
290
      try {
3✔
291
        parsedValue = JSON.parse(data.item.value);
3✔
292
        parsedValue.status = status;
2✔
293
      } catch (err) {
294
        console.error(err);
1✔
295
        res.status(500).send({
1✔
296
          errors: [
297
            {
298
              error: 'Internal Server Error',
299
              message: 'An unexpected error occurred'
300
            }
301
          ]
302
        });
303
      }
304
      data.item.value.status = status;
3✔
305
      const result = await storageConnection.setConfig(
3✔
306
        'emailIntegration',
307
        JSON.stringify(parsedValue)
308
      );
309
      if (result && result.item) {
3✔
310
        result.item.value = JSON.parse(result.item.value);
1✔
311
        await Alerts.clearEmailTransport();
1✔
312
        res.send(Jsonapi.Serializer.serialize(Jsonapi.AppType, result.item));
1✔
313
      } else {
314
        res.status(500).send({
2✔
315
          errors: [
316
            {
317
              error: 'Internal Server Error',
318
              message: 'An unexpected error occurred'
319
            }
320
          ]
321
        });
322
      }
323
    } else {
324
      res.status(500).send({
1✔
325
        errors: [
326
          {
327
            error: 'Internal Server Error',
328
            message: 'An unexpected error occurred'
329
          }
330
        ]
331
      });
332
    }
333
  } catch (error) {
334
    console.error(error);
1✔
335
    res.status(500).send({
1✔
336
      errors: [
337
        {
338
          error: 'Internal Server Error',
339
          message: 'An unexpected error occurred'
340
        }
341
      ]
342
    });
343
  }
344
};
345

346
exports.deleteEmailDetails = async (req, res) => {
2✔
347
  try {
3✔
348
    const { url } = helpers.extractAttributes(req.body);
3✔
349
    const storageConnection = getStorageConnection();
3✔
350
    const data = await storageConnection.deleteConfig('emailIntegration');
3✔
351
    if (data) {
2✔
352
      res.send(Jsonapi.Serializer.serialize(Jsonapi.AppType, { url }));
1✔
353
    } else {
354
      res.status(500).send({
1✔
355
        errors: [
356
          {
357
            error: 'Internal Server Error',
358
            message: 'An unexpected error occurred'
359
          }
360
        ]
361
      });
362
    }
363
  } catch (error) {
364
    console.error(error);
1✔
365
    res.status(500).send({
1✔
366
      errors: [
367
        {
368
          error: 'Internal Server Error',
369
          message: 'An unexpected error occurred'
370
        }
371
      ]
372
    });
373
  }
374
};
375

376
exports.testSlackNotification = async (req, res) => {
2✔
377
  try {
2✔
378
    const result = await Alerts.testSlackAlert('This is a test notification from the Errsole Logger.', 'Test Notification');
2✔
379
    res.send(Jsonapi.Serializer.serialize(Jsonapi.AppType, { success: result }));
1✔
380
  } catch (error) {
381
    console.error(error);
1✔
382
    res.status(500).send({
1✔
383
      errors: [
384
        {
385
          error: 'Internal Server Error',
386
          message: 'An unexpected error occurred'
387
        }
388
      ]
389
    });
390
  }
391
};
392

393
exports.testEmailNotification = async (req, res) => {
2✔
394
  try {
2✔
395
    const result = await Alerts.testEmailAlert('This is a test notification from the Errsole Logger. If you received this email, it means your SMTP settings are correctly configured in Errsole.', 'Test Notification');
2✔
396
    res.send(Jsonapi.Serializer.serialize(Jsonapi.AppType, { success: result }));
1✔
397
  } catch (error) {
398
    console.error(error);
1✔
399
    res.status(500).send({
1✔
400
      errors: [
401
        {
402
          error: 'Internal Server Error',
403
          message: 'An unexpected error occurred'
404
        }
405
      ]
406
    });
407
  }
408
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc