mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-21 20:16:26 +08:00
feat(server): early subscription for iap (#13826)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added a mutation to request/apply a subscription by transaction ID (client mutation and server operation), returning subscription details. * **Bug Fixes / Improvements** * More robust external subscription sync with safer conflict detection, optional short-lived confirmation, improved parsing and error logging. * **Chores** * Standardized time constants for clarity. * **Tests** * Updated subscription test data (expiration date) to reflect new lifecycle expectations. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -22,6 +22,7 @@ import {
|
||||
AccessDenied,
|
||||
AuthenticationRequired,
|
||||
FailedToCheckout,
|
||||
InvalidSubscriptionParameters,
|
||||
Throttle,
|
||||
WorkspaceIdRequiredToUpdateTeamSubscription,
|
||||
} from '../../base';
|
||||
@@ -543,6 +544,56 @@ export class UserSubscriptionResolver {
|
||||
});
|
||||
}
|
||||
|
||||
@Throttle('strict')
|
||||
@Mutation(() => [SubscriptionType], {
|
||||
description: 'Request to apply the subscription in advance',
|
||||
})
|
||||
async requestApplySubscription(
|
||||
@CurrentUser() user: CurrentUser,
|
||||
@Args('transactionId') transactionId: string
|
||||
): Promise<Subscription[]> {
|
||||
if (!user) {
|
||||
throw new AuthenticationRequired();
|
||||
}
|
||||
|
||||
let existsSubscription = await this.db.subscription.findFirst({
|
||||
where: { rcExternalRef: transactionId },
|
||||
});
|
||||
|
||||
// subscription with the transactionId already exists
|
||||
if (existsSubscription) {
|
||||
if (existsSubscription.targetId !== user.id) {
|
||||
throw new InvalidSubscriptionParameters();
|
||||
} else {
|
||||
this.normalizeSubscription(existsSubscription);
|
||||
return [existsSubscription];
|
||||
}
|
||||
}
|
||||
|
||||
let current: Subscription[] = [];
|
||||
|
||||
try {
|
||||
await this.rcHandler.syncAppUserWithExternalRef(user.id, transactionId);
|
||||
current = await this.db.subscription.findMany({
|
||||
where: {
|
||||
targetId: user.id,
|
||||
status: {
|
||||
in: [
|
||||
SubscriptionStatus.Active,
|
||||
SubscriptionStatus.Trialing,
|
||||
SubscriptionStatus.PastDue,
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
||||
// ignore errors
|
||||
} catch {}
|
||||
|
||||
current.forEach(subscription => this.normalizeSubscription(subscription));
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
@Throttle('strict')
|
||||
@Mutation(() => [SubscriptionType], {
|
||||
description: 'Refresh current user subscriptions and return latest.',
|
||||
|
||||
Reference in New Issue
Block a user