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

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

19 Jul 2024 03:12AM UTC coverage: 93.429% (-1.5%) from 94.902%
10001960285

push

github

embbnux
misc: refresh subscription with limitation

260 of 296 branches covered (87.84%)

Branch coverage included in aggregate %.

11 of 20 new or added lines in 1 file covered. (55.0%)

721 of 754 relevant lines covered (95.62%)

15.43 hits per line

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

80.0
/src/refreshSubscriptionCron.js
1
require('dotenv').config();
1✔
2

3
const { GoogleClient } = require('./server/lib/GoogleClient');
1✔
4
const { checkAndRefreshAccessToken } = require('./server/lib/oauth');
1✔
5
const { Subscription } = require('./server/models/subscriptionModel');
1✔
6
const { User } = require('./server/models/userModel');
1✔
7
const { errorLogger } = require('./server/lib/logger');
1✔
8

9
async function refreshSubscription() {
10
  const currentTime = new Date();
8✔
11
  const expiredIn3Day = new Date(currentTime);
8✔
12
  expiredIn3Day.setDate(currentTime.getDate() + 3);
8✔
13
  let lastKey = null;
8✔
14
  const lastKeyRecord = await Subscription.findByPk('SYSTEM_LAST_KEY');
8✔
15
  if (lastKeyRecord && lastKeyRecord.formId) {
8!
NEW
16
    lastKey = lastKeyRecord.formId;
×
17
  }
18
  const findAllQuery = {
8✔
19
    limit: 100,
20
  };
21
  if (lastKey) {
8!
NEW
22
    findAllQuery.lastKey = {
×
23
      id: lastKey,
24
    };
25
  }
26
  const subscriptions = await Subscription.findAll(findAllQuery);
8✔
27
  if (subscriptions.lastKey) {
8!
NEW
28
    if (!lastKeyRecord) {
×
NEW
29
      await Subscription.create({
×
30
        id: 'SYSTEM_LAST_KEY',
31
        formId: subscriptions.lastKey.id,
32
      });
33
    } else {
NEW
34
      lastKeyRecord.formId = subscriptions.lastKey.id;
×
NEW
35
      await lastKeyRecord.save();
×
36
    }
37
  } else if (lastKeyRecord) {
8!
NEW
38
    lastKeyRecord.formId = '';
×
NEW
39
    await lastKeyRecord.save();
×
40
  }
41
  const users = {};
8✔
42
  for (const subscription of subscriptions) {
8✔
43
    if (subscription.id === 'SYSTEM_LAST_KEY') {
9!
NEW
44
      continue;
×
45
    }
46
    if (subscription.watchExpiredAt < currentTime) {
9✔
47
      continue;
1✔
48
    }
49
    if (subscription.watchExpiredAt > expiredIn3Day) {
8✔
50
      continue;
1✔
51
    }
52
    try {
7✔
53
      console.log('refreshing subscription:', subscription.id);
7✔
54
      if (!users[subscription.userId]) {
7✔
55
        users[subscription.userId] = await User.findByPk(subscription.userId);
6✔
56
      }
57
      const user = users[subscription.userId];
7✔
58
      if (!user || !user.accessToken) {
7✔
59
        return;
1✔
60
      }
61
      try {
6✔
62
        await checkAndRefreshAccessToken(user);
6✔
63
        const googleClient = new GoogleClient({ token: user.accessToken });
4✔
64
        const { expireTime } = await googleClient.renewWatch(subscription.formId, subscription.id);
4✔
65
        subscription.watchExpiredAt = new Date(expireTime);
4✔
66
        await subscription.save();
4✔
67
        console.log(`refreshing subscription ${subscription.id} successfully`);
4✔
68
      } catch (e) {
69
        if (e.response && e.response.status === 401) {
2✔
70
          user.accessToken = '';
1✔
71
          user.name = '';
1✔
72
          await user.save();
1✔
73
          console.log('refreshing subscription failed: access token expired: ', user.id);
1✔
74
          return;
1✔
75
        }
76
        throw e;
1✔
77
      }
78
    } catch (e) {
79
      console.error('refreshing error subscription: ', subscription.id);
1✔
80
      errorLogger(e);
1✔
81
    }
82
  }
83
}
84

85
exports.refresh = refreshSubscription;
1✔
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