fix: unsubscribe users if milestone gets deleted

This commit is contained in:
zkldi
2022-04-17 04:59:07 +01:00
parent 8c61c33172
commit a27ac55ccf
2 changed files with 18 additions and 6 deletions
+16 -5
View File
@@ -243,10 +243,10 @@ export async function SubscribeToMilestone(
return { milestoneSub, goals: result.goals, goalResults: result.goalResults };
}
export async function UnsubscribeFromMilestone(userID: integer, milestone: MilestoneDocument) {
export async function UnsubscribeFromMilestone(userID: integer, milestoneID: string) {
await db["milestone-subs"].remove({
userID,
milestoneID: milestone.milestoneID,
milestoneID,
});
}
@@ -264,8 +264,19 @@ export async function UpdateMilestoneSubscriptions(milestoneID: string) {
const subscriptions = await db["milestone-subs"].find({ milestoneID });
const milestone = await GetMilestoneForIDGuaranteed(milestoneID);
const goals = await GetGoalsInMilestone(milestone);
const maybeMilestone = await db.milestones.findOne({ milestoneID });
if (!maybeMilestone) {
logger.info(
`Milestone ${milestoneID} has been deleted. Unsubscribing ${subscriptions.length} users.`
);
return Promise.all(
subscriptions.map((e) => UnsubscribeFromMilestone(e.userID, e.milestoneID))
);
}
const goals = await GetGoalsInMilestone(maybeMilestone);
const goalSubscriptionPromises = [];
@@ -287,7 +298,7 @@ export async function UpdateMilestoneSubscriptions(milestoneID: string) {
if (newStuff !== 0) {
logger.info(
`Updating subscriptions for '${milestone.name}' resulted in ${newStuff} updates.`
`Updating subscriptions for '${maybeMilestone.name}' resulted in ${newStuff} updates.`
);
}
+2 -1
View File
@@ -291,7 +291,8 @@ const syncInstructions: SyncInstructions[] = [
milestones.filter((e) => IsSupported(e.game)),
collection,
"milestoneID",
logger
logger,
true
);
if (r.thingsChanged) {