Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

fix(server): subscription delete race condition (#11729)

forehalobfb94acc4241
diff --git a/packages/backend/server/src/plugins/payment/manager/user.ts b/packages/backend/server/src/plugins/payment/manager/user.ts
index d7a2a713f8..19b37611b6 100644
--- a/packages/backend/server/src/plugins/payment/manager/user.ts
+++ b/packages/backend/server/src/plugins/payment/manager/user.ts
@@ -256,17 +256,19 @@ export class UserSubscriptionManager extends SubscriptionManager {
stripeSubscription,
}: KnownStripeSubscription) {
this.assertUserIdExists(userId);
- this.event.emit('user.subscription.canceled', {
- userId,
- plan: lookupKey.plan,
- recurring: lookupKey.recurring,
- });
-
- await this.db.subscription.deleteMany({
+ const result = await this.db.subscription.deleteMany({
where: {
stripeSubscriptionId: stripeSubscription.id,
},
});
+
+ if (result.count > 0) {
+ this.event.emit('user.subscription.canceled', {
+ userId,
+ plan: lookupKey.plan,
+ recurring: lookupKey.recurring,
+ });
+ }
}
async cancelSubscription(subscription: Subscription) {
diff --git a/packages/backend/server/src/plugins/payment/manager/workspace.ts b/packages/backend/server/src/plugins/payment/manager/workspace.ts
index f46fa2f839..244cb3b0b7 100644
--- a/packages/backend/server/src/plugins/payment/manager/workspace.ts
+++ b/packages/backend/server/src/plugins/payment/manager/workspace.ts
@@ -187,15 +187,17 @@ export class WorkspaceSubscriptionManager extends SubscriptionManager {
);
}
- this.event.emit('workspace.subscription.canceled', {
- workspaceId,
- plan: lookupKey.plan,
- recurring: lookupKey.recurring,
- });
-
- await this.db.subscription.deleteMany({
+ const result = await this.db.subscription.deleteMany({
where: { stripeSubscriptionId: stripeSubscription.id },
});
+
+ if (result.count > 0) {
+ this.event.emit('workspace.subscription.canceled', {
+ workspaceId,
+ plan: lookupKey.plan,
+ recurring: lookupKey.recurring,
+ });
+ }
}
getSubscription(identity: z.infer<typeof WorkspaceSubscriptionIdentity>) {