diff --git a/src/refreshSubscriptionCron.js b/src/refreshSubscriptionCron.js index b8c290a..df6d15a 100644 --- a/src/refreshSubscriptionCron.js +++ b/src/refreshSubscriptionCron.js @@ -4,14 +4,45 @@ const { GoogleClient } = require('./server/lib/GoogleClient'); const { checkAndRefreshAccessToken } = require('./server/lib/oauth'); const { Subscription } = require('./server/models/subscriptionModel'); const { User } = require('./server/models/userModel'); +const { errorLogger } = require('./server/lib/logger'); async function refreshSubscription() { const currentTime = new Date(); const expiredIn3Day = new Date(currentTime); expiredIn3Day.setDate(currentTime.getDate() + 3); - const subscriptions = await Subscription.findAll(); // TODO: add lastKey + let lastKey = null; + const lastKeyRecord = await Subscription.findByPk('SYSTEM_LAST_KEY'); + if (lastKeyRecord && lastKeyRecord.formId) { + lastKey = lastKeyRecord.formId; + } + const findAllQuery = { + limit: 100, + }; + if (lastKey) { + findAllQuery.lastKey = { + id: lastKey, + }; + } + const subscriptions = await Subscription.findAll(findAllQuery); + if (subscriptions.lastKey) { + if (!lastKeyRecord) { + await Subscription.create({ + id: 'SYSTEM_LAST_KEY', + formId: subscriptions.lastKey.id, + }); + } else { + lastKeyRecord.formId = subscriptions.lastKey.id; + await lastKeyRecord.save(); + } + } else if (lastKeyRecord) { + lastKeyRecord.formId = ''; + await lastKeyRecord.save(); + } const users = {}; for (const subscription of subscriptions) { + if (subscription.id === 'SYSTEM_LAST_KEY') { + continue; + } if (subscription.watchExpiredAt < currentTime) { continue; } @@ -46,7 +77,7 @@ async function refreshSubscription() { } } catch (e) { console.error('refreshing error subscription: ', subscription.id); - console.error(e && e.message); + errorLogger(e); } } }