Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

サブスクリプションの更新の処理条件の調整 #6

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 18 additions & 22 deletions packages/backend/src/server/StripeWebhookServerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,8 @@ export class StripeWebhookServerService {
return;
}

if (subscription.cancel_at_period_end) {
return; // キャンセルされた場合は期限切れのタイミングでcustomer.subscription.deletedイベントが発生するので、ここでは何もしない
}

if (!user.subscriptionPlanId) { // サブスクリプションプランが新規に設定された場合
if (subscription.status === 'active') {
if (subscription.status === 'active') {
if (!user.subscriptionPlanId) { // サブスクリプションプランが新規に設定された場合
const roleIds = (await this.subscriptionPlansRepository.find()).map(x => x.roleId);
await this.roleService.getUserRoles(user.id).then(async (roles) => {
for (const role of roles) {
Expand All @@ -153,29 +149,29 @@ export class StripeWebhookServerService {
await this.roleService.assign(user.id, subscriptionPlan.roleId);
}
});
}
} else if (subscriptionPlan.id !== user.subscriptionPlanId) { // サブスクリプションプランが変更された場合
const oldSubscriptionPlan = await this.subscriptionPlansRepository.findOneByOrFail({ id: user.subscriptionPlanId ?? undefined });
await this.roleService.getUserRoles(user.id).then(async (roles) => {
// 旧サブスクリプションプランのロールが割り当てられている場合、ロールを解除する
if (roles.some((role) => role.id === oldSubscriptionPlan.roleId)) {
await this.roleService.unassign(user.id, oldSubscriptionPlan.roleId);
}

// 新しいサブスクリプションプランのロールが割り当てられていない場合、ロールを割り当てる
if (!roles.some((role) => role.id === subscriptionPlan.roleId)) {
await this.roleService.assign(user.id, subscriptionPlan.roleId);
}
});
} else if (previousData && previousData.status) { // サブスクリプションステータスが変更された場合
if (subscription.status === 'active') {
} else if (subscriptionPlan.id !== user.subscriptionPlanId) { // サブスクリプションプランが変更された場合
const oldSubscriptionPlan = await this.subscriptionPlansRepository.findOneByOrFail({ id: user.subscriptionPlanId ?? undefined });
await this.roleService.getUserRoles(user.id).then(async (roles) => {
// 旧サブスクリプションプランのロールが割り当てられている場合、ロールを解除する
if (roles.some((role) => role.id === oldSubscriptionPlan.roleId)) {
await this.roleService.unassign(user.id, oldSubscriptionPlan.roleId);
}

// 新しいサブスクリプションプランのロールが割り当てられていない場合、ロールを割り当てる
if (!roles.some((role) => role.id === subscriptionPlan.roleId)) {
await this.roleService.assign(user.id, subscriptionPlan.roleId);
}
});
} else if (previousData && previousData.status) { // サブスクリプションステータスが変更された場合
await this.roleService.getUserRoles(user.id).then(async (roles) => {
// ユーザーにロールが割り当てられていない場合、ロールを割り当てる
if (!roles.some((role) => role.id === subscriptionPlan.roleId)) {
await this.roleService.assign(user.id, subscriptionPlan.roleId);
}
});
}
} else if (subscription.cancel_at_period_end) {
return; // キャンセルされた場合は期限切れのタイミングでcustomer.subscription.deletedイベントが発生するので、ここでは何もしない
}

// ユーザーのサブスクリプションステータスとサブスクリプションプランを更新する
Expand Down
Loading