Skip to content

Commit

Permalink
Fix discord emails being used when getting subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
synzen committed Jan 2, 2025
1 parent 2d79d4b commit 91910ec
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ export class PaddleWebhooksService {
.select("discordUserId")
.lean();

const discordEmail = foundUser?.email;
const discordEmail = foundUser?.discordUserId;

if (!discordEmail) {
throw new Error(
`Could not find user with discord ID ${event.data.custom_data.userId} when updating subscription for customer ${event.data.customer_id}`
`Could not find user with user ID ${event.data.custom_data.userId} when updating subscription for customer ${event.data.customer_id}`
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export class SupporterSubscriptionsController {

const { id } =
await this.supporterSubscriptionsService.getUpdatePaymentMethodTransaction(
{ email }
{ discordUserId }
);

return {
Expand Down Expand Up @@ -264,7 +264,7 @@ export class SupporterSubscriptionsController {

const preview =
await this.supporterSubscriptionsService.previewSubscriptionChange({
email,
discordUserId,
items: [
{
priceId: priceId,
Expand Down Expand Up @@ -297,7 +297,7 @@ export class SupporterSubscriptionsController {
}

await this.supporterSubscriptionsService.changeSubscription({
email,
discordUserId,
items: [
{
priceId: priceId,
Expand All @@ -324,7 +324,7 @@ export class SupporterSubscriptionsController {
}

await this.supporterSubscriptionsService.cancelSubscription({
email,
discordUserId,
});
}

Expand All @@ -345,7 +345,7 @@ export class SupporterSubscriptionsController {
}

await this.supporterSubscriptionsService.resumeSubscription({
email,
discordUserId,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,14 @@ export class SupporterSubscriptionsService {
}

async previewSubscriptionChange({
email,
discordUserId,
items,
}: {
email: string;
discordUserId: string;
items: Array<{ priceId: string; quantity: number }>;
}) {
const { subscription } =
await this.supportersService.getSupporterSubscription({ email });
await this.supportersService.getSupporterSubscription({ discordUserId });

const existingSubscriptionId = subscription?.id;

Expand Down Expand Up @@ -267,14 +267,14 @@ export class SupporterSubscriptionsService {
}

async changeSubscription({
email,
discordUserId,
items,
}: {
email: string;
discordUserId: string;
items: Array<{ priceId: string; quantity: number }>;
}) {
const { subscription, discordUserId } =
await this.supportersService.getSupporterSubscription({ email });
const { subscription } =
await this.supportersService.getSupporterSubscription({ discordUserId });

const existingSubscriptionId = subscription?.id;

Expand Down Expand Up @@ -302,7 +302,7 @@ export class SupporterSubscriptionsService {
const currentUpdatedAt = subscription.updatedAt.getTime();

await this.pollForSubscriptionChange({
email,
discordUserId,
check: (sub) => {
const latestUpdatedAt = sub.subscription?.updatedAt;

Expand All @@ -319,9 +319,9 @@ export class SupporterSubscriptionsService {
}
}

async cancelSubscription({ email }: { email: string }) {
async cancelSubscription({ discordUserId }: { discordUserId: string }) {
const { subscription } =
await this.supportersService.getSupporterSubscription({ email });
await this.supportersService.getSupporterSubscription({ discordUserId });

const existingSubscriptionId = subscription?.id;

Expand All @@ -342,7 +342,7 @@ export class SupporterSubscriptionsService {
);

await this.pollForSubscriptionChange({
email,
discordUserId,
check: (sub) => {
const cancellationDate = sub.subscription?.cancellationDate;

Expand All @@ -351,9 +351,9 @@ export class SupporterSubscriptionsService {
});
}

async resumeSubscription({ email }: { email: string }) {
async resumeSubscription({ discordUserId }: { discordUserId: string }) {
const { subscription } =
await this.supportersService.getSupporterSubscription({ email });
await this.supportersService.getSupporterSubscription({ discordUserId });

const existingSubscriptionId = subscription?.id;

Expand All @@ -374,7 +374,7 @@ export class SupporterSubscriptionsService {
);

await this.pollForSubscriptionChange({
email,
discordUserId,
check: (sub) => {
const cancellationDate = sub.subscription?.cancellationDate;

Expand All @@ -383,9 +383,13 @@ export class SupporterSubscriptionsService {
});
}

async getUpdatePaymentMethodTransaction({ email }: { email: string }) {
async getUpdatePaymentMethodTransaction({
discordUserId,
}: {
discordUserId: string;
}) {
const { subscription } =
await this.supportersService.getSupporterSubscription({ email });
await this.supportersService.getSupporterSubscription({ discordUserId });

const existingSubscriptionId = subscription?.id;

Expand All @@ -406,10 +410,10 @@ export class SupporterSubscriptionsService {
}

async pollForSubscriptionChange({
email,
discordUserId,
check,
}: {
email: string;
discordUserId: string;
check: (
sub: Awaited<ReturnType<SupportersService["getSupporterSubscription"]>>
) => boolean;
Expand All @@ -420,7 +424,9 @@ export class SupporterSubscriptionsService {

while (true) {
const subscription =
await this.supportersService.getSupporterSubscription({ email });
await this.supportersService.getSupporterSubscription({
discordUserId,
});

if (check(subscription)) {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,17 +303,17 @@ export class SupportersService {
}

async getSupporterSubscription({
email,
billingEmail,
discordUserId,
}:
| { email: string; discordUserId?: string }
| { discordUserId: string; email?: string }) {
| { billingEmail: string; discordUserId?: string }
| { billingEmail?: string; discordUserId: string }) {
let supporter: Supporter | null = null;

if (email) {
if (billingEmail) {
supporter = await this.supporterModel
.findOne({
"paddleCustomer.email": email,
"paddleCustomer.email": billingEmail,
})
.lean();
} else {
Expand Down
2 changes: 1 addition & 1 deletion services/backend-api/src/features/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class UsersService {

const { subscription, customer } =
await this.supportersService.getSupporterSubscription({
email: user.email,
discordUserId: user.discordUserId,
});

let creditAvailableBalanceFormatted = "0";
Expand Down

0 comments on commit 91910ec

Please sign in to comment.