Skip to content

Commit 571d026

Browse files
committed
Feat: prevent downgrading an org when a coupon/credit is applied
1 parent 34ca5d4 commit 571d026

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/routes/(console)/apply-credit/+page.svelte

+16-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
WizardSecondaryContent,
1313
WizardSecondaryFooter
1414
} from '$lib/layout';
15-
import { type PaymentList } from '$lib/sdk/billing';
15+
import { type PaymentList, type Plan } from '$lib/sdk/billing';
1616
import { app } from '$lib/stores/app';
1717
import { isOrganization } from '$lib/stores/billing.js';
1818
import { addNotification } from '$lib/stores/notifications';
@@ -72,6 +72,12 @@
7272
let couponData = data?.couponData;
7373
let campaign = data?.campaign;
7474
let billingPlan = BillingPlan.PRO;
75+
let currentPlan: Plan;
76+
77+
function isUpgrade() {
78+
const newPlan = $page.data.plansInfo.get(billingPlan);
79+
return currentPlan && newPlan && currentPlan.order < newPlan.order;
80+
}
7581
7682
onMount(async () => {
7783
await loadPaymentMethods();
@@ -132,7 +138,7 @@
132138
}
133139
134140
// Upgrade existing org
135-
else if (selectedOrg?.billingPlan !== billingPlan) {
141+
else if (selectedOrg?.billingPlan !== billingPlan && isUpgrade()) {
136142
org = await sdk.forConsole.billing.updatePlan(
137143
selectedOrg.$id,
138144
billingPlan,
@@ -216,6 +222,14 @@
216222
selectedOrg?.billingPlan === BillingPlan.SCALE
217223
? BillingPlan.SCALE
218224
: (campaign?.plan ?? BillingPlan.PRO);
225+
226+
$: {
227+
if (selectedOrgId) {
228+
(async () => {
229+
currentPlan = await sdk.forConsole.billing.getOrganizationPlan(selectedOrgId);
230+
})();
231+
}
232+
}
219233
</script>
220234

221235
<svelte:head>

0 commit comments

Comments
 (0)