Skip to content

Commit e916d33

Browse files
Merge pull request #119 from daostack/firebase/bugs/107-not-revoking-user-canceled-subscriptions
#107 - Fix subscription revoke
2 parents 2312e3a + ab113d9 commit e916d33

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

packages/firebase/functions/src/subscriptions/business/revokeMemberships.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ const db = firestore();
1212
* Revokes all membership that are expired, but not yet revoked
1313
*/
1414
export const revokeMemberships = async (): Promise<void> => {
15-
logger.info(`Beginning membership revoking for ${new Date().getDate()}`);
15+
logger.info(`Beginning membership revoking for ${ new Date().getDate() }`);
1616

1717
// Only get the subscription cancelled by user, because the subscriptions
1818
// canceled by payment failure should already be revoked
1919
const subscriptions = await db.collection(Collections.Subscriptions)
20-
.where('dueDate', '<=', new Date().setHours(23, 59, 59, 999))
2120
.where('status', '==', CancellationReason.CanceledByUser)
2221
.where('revoked', '==', false)
2322
.get() as firestore.QuerySnapshot<ISubscriptionEntity>;
@@ -31,24 +30,31 @@ export const revokeMemberships = async (): Promise<void> => {
3130
logger.warn(
3231
`
3332
Trying to revoke subscription with status
34-
(${subscription.status}) from the cron
33+
(${ subscription.status }) from the cron
3534
`
3635
);
3736
} else {
38-
// eslint-disable-next-line no-loop-func
39-
promiseArr.push((async () => {
40-
// Add try/catch so that if one revoke fails
41-
// the others won't be canceled because of it
42-
try {
43-
logger.info(`Revoking membership for subscription with id ${subscription.id}`);
44-
45-
await revokeMembership(subscription);
46-
47-
logger.info(`Revoked membership ${subscription.id}`);
48-
} catch (e) {
49-
logger.warn('Error occurred while trying to revoke subscription', e);
50-
}
51-
})());
37+
// If the subscription is pass it's due date: revoke it, If is not leave it be
38+
if (subscription.dueDate.toDate() < new Date()) {
39+
// eslint-disable-next-line no-loop-func
40+
promiseArr.push((async () => {
41+
// Add try/catch so that if one revoke fails
42+
// the others won't be canceled because of it
43+
try {
44+
logger.info(`Revoking membership for subscription with id ${ subscription.id }`);
45+
46+
await revokeMembership(subscription);
47+
48+
logger.info(`Revoked membership ${ subscription.id }`);
49+
} catch (e) {
50+
logger.warn('Error occurred while trying to revoke subscription', e);
51+
}
52+
})());
53+
} else {
54+
logger.debug('Skipping revoke for user canceled subscription', {
55+
subscription
56+
});
57+
}
5258
}
5359
}
5460

0 commit comments

Comments
 (0)