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

ringcentral / google-forms-notification-add-in / 9791525289

04 Jul 2024 09:03AM UTC coverage: 95.112% (-0.1%) from 95.238%
9791525289

push

github

web-flow
misc: filter data in logs (#67)

* misc: filter data in logs

* misc: filter code

248 of 274 branches covered (90.51%)

Branch coverage included in aggregate %.

43 of 49 new or added lines in 9 files covered. (87.76%)

686 of 708 relevant lines covered (96.89%)

14.94 hits per line

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

96.05
/src/server/handlers/notificationHandler.js
1
const { User } = require('../models/userModel');
4✔
2
const { sendAdaptiveCardMessage } = require('../lib/messageHelper');
4✔
3
const { checkAndRefreshAccessToken } = require('../lib/oauth');
4✔
4
const { formatGoogleFormResponseIntoCard } = require('../lib/formatGoogleFormResponse');
4✔
5
const { GoogleClient } = require('../lib/GoogleClient');
4✔
6
const { errorLogger } = require('../lib/logger');
4✔
7

8
async function onReceiveNotification(subscription, messageTime) {
9
  const userId = subscription.userId;
14✔
10
  const user = await User.findByPk(userId);
14✔
11
  if (!user || !user.accessToken) {
14✔
12
    return;
2✔
13
  }
14
  try {
12✔
15
    await checkAndRefreshAccessToken(user);
12✔
16
  } catch (e) {
17
    if (e.response && e.response.status === 401) {
2✔
18
      user.accessToken = '';
1✔
19
      await user.save();
1✔
20
      return;
1✔
21
    }
22
    throw e;
1✔
23
  }
24
  const googleClient = new GoogleClient({ token: user.accessToken });
10✔
25
  const formId = subscription.formId;
10✔
26
  let form;
27
  let responses;
28
  try {
10✔
29
    form = await googleClient.getForm(formId);
10✔
30
    // console.log(JSON.stringify(form, null, 2));
31
    responses = await googleClient.getFormResponses(formId, subscription.messageReceivedAt);
8✔
32
    // console.log(JSON.stringify(responses, null, 2));
33
  } catch (e) {
34
    if (e.response && e.response.status === 401) {
2✔
35
      user.accessToken = '';
1✔
36
      await user.save();
1✔
37
      return;
1✔
38
    }
39
    throw e;
1✔
40
  }
41

42
  const messageCards = responses.map((response) => formatGoogleFormResponseIntoCard(form, response));
8✔
43
  await Promise.all(messageCards.map(async messageCard => {
8✔
44
    if (subscription.rcWebhookList && subscription.rcWebhookList.length > 0) {
8✔
45
      await Promise.all(subscription.rcWebhookList.map(async rcWebhook => {
7✔
46
        try {
10✔
47
          const response = await sendAdaptiveCardMessage(rcWebhook.uri, messageCard);
10✔
48
          if (response.data.error && response.data.error.indexOf('Webhook not found') >= -1) {
10✔
49
            subscription.rcWebhookList = [{
3✔
50
              ...rcWebhook,
51
              active: false,
52
            }].concat(subscription.rcWebhookList.filter((webhook) => webhook.id !== rcWebhook.id));
6✔
53
          }
54
        } catch (e) {
55
          console.error('Error sending message to RC Webhook:');
×
NEW
56
          errorLogger(e);
×
57
        }
58
      }));
59
    } else if (subscription.rcWebhookUri) {
1!
60
      await sendAdaptiveCardMessage(
1✔
61
        subscription.rcWebhookUri,
62
        messageCard
63
      );
64
    }
65
  }));
66
  if (
8✔
67
    subscription.rcWebhookList &&
22✔
68
    subscription.rcWebhookList.length > 0 &&
69
    subscription.rcWebhookList.filter(rcWebhook => rcWebhook.active).length === 0
10✔
70
  ) {
71
    // webhook is deleted
72
    await googleClient.deleteWatch(subscription.formId, subscription.id);
1✔
73
    await subscription.destroy();
1✔
74
    user.subscriptions = user.subscriptions.filter(sub => sub.id !== subscription.id);
3✔
75
    await user.save();
1✔
76
  } else {
77
    subscription.messageReceivedAt = new Date(messageTime);
7✔
78
    await subscription.save();
7✔
79
  }
80
}
81

82
exports.onReceiveNotification = onReceiveNotification;
4✔
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

© 2025 Coveralls, Inc