Skip to content

Commit

Permalink
misc: refresh subscription with limitation
Browse files Browse the repository at this point in the history
  • Loading branch information
embbnux committed Jul 19, 2024
1 parent 047bdc0 commit 23b5975
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/refreshSubscriptionCron.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -46,7 +77,7 @@ async function refreshSubscription() {
}
} catch (e) {
console.error('refreshing error subscription: ', subscription.id);
console.error(e && e.message);
errorLogger(e);
}
}
}
Expand Down

0 comments on commit 23b5975

Please sign in to comment.