fix: project selection for archive fix

This commit is contained in:
Harsh Mahajan committed 2025-08-30 01:29:57 +05:30
1 parent ceed8f2c5e
commit 1228ee2a0f
2 files changed
+27 -50

No files matched your search

@@ -8,12 +8,8 @@
import { formatNumberWithCommas } from '$lib/helpers/numbers';
import { Modal } from '$lib/components';
import { currentPlan } from '$lib/stores/organization';
import { Alert } from '@appwrite.io/pink-svelte';
import { sdk } from '$lib/stores/sdk';
import { addNotification } from '$lib/stores/notifications';
import { invalidate } from '$app/navigation';
import { Dependencies } from '$lib/constants';
import { toLocaleDate, toLocaleDateTime } from '$lib/helpers/date';
import { billingProjectsLimitDate } from '$lib/stores/billing';
import type { Organization } from '$lib/stores/organization';
@@ -21,7 +17,6 @@
// Props
let {
organization,
projects = [],
members = [],
storageUsage = 0
@@ -44,12 +39,8 @@
storage: getServiceLimit('storage', BillingPlan.FREE)
});
//fallback to free limit when undefined
let allowedProjectsToKeep = $derived(
$currentPlan?.projects && $currentPlan.projects > 0
? $currentPlan.projects
: freePlanLimits.projects
);
// When preparing to downgrade to Free, enforce Free plan limit locally (2)
let allowedProjectsToKeep = $derived(freePlanLimits.projects);
let currentUsage = $derived({
projects: projects?.length || 0,
@@ -115,37 +106,18 @@
return selectedProjects.filter((id) => projects.some((p) => p.$id === id));
}
async function updateSelected() {
function updateSelected() {
error = null;
if (!organization?.$id) {
error = 'Missing organization ID.';
return;
}
const filteredSelection = selectedProjects.filter((id) =>
projects.some((p) => p.$id === id)
);
if (filteredSelection.length !== allowedProjectsToKeep) {
error = `You must select exactly ${allowedProjectsToKeep} projects to keep.`;
return;
}
try {
await sdk.forConsole.billing.updateSelectedProjects(
organization.$id,
filteredSelection
);
showSelectProject = false;
invalidate(Dependencies.ORGANIZATION);
addNotification({
type: 'success',
message: `Projects updated for archiving`
});
} catch (e) {
error = e.message;
}
// Keep selection locally; parent flow will apply after plan change
showSelectProject = false;
addNotification({ type: 'success', message: `Projects selected for archiving` });
}
</script>
@@ -123,11 +123,11 @@
async function handleSubmit() {
if (isDowngrade) {
if (selectedPlan === BillingPlan.FREE && usageLimitsComponent?.validateOrAlert) {
// If target plan has a non-zero project limit, ensure selection made
const targetProjectsLimit = $plansInfo?.get(selectedPlan)?.projects ?? 0;
if (targetProjectsLimit > 0 && usageLimitsComponent?.validateOrAlert) {
const ok = usageLimitsComponent.validateOrAlert();
if (!ok) {
return;
}
if (!ok) return;
}
await downgrade();
} else if (isUpgrade) {
@@ -165,18 +165,7 @@
async function downgrade() {
try {
// If downgrading to FREE and we have project limits, select projects first
if (selectedPlan === BillingPlan.FREE && usageLimitsComponent) {
const selected = usageLimitsComponent.getSelectedProjects();
if (selected.length > 0) {
// Send selected projects BEFORE downgrading
await sdk.forConsole.billing.updateSelectedProjects(
data.organization.$id,
selected
);
}
}
// 1) ppdate the plan first
await sdk.forConsole.billing.updatePlan(
data.organization.$id,
selectedPlan,
@@ -184,6 +173,22 @@
null
);
// 2) If the target plan has a project limit, apply selected projects now
const targetProjectsLimit = $plansInfo?.get(selectedPlan)?.projects ?? 0;
if (targetProjectsLimit > 0 && usageLimitsComponent) {
const selected = usageLimitsComponent.getSelectedProjects();
if (selected?.length) {
try {
await sdk.forConsole.billing.updateSelectedProjects(
data.organization.$id,
selected
);
} catch (projectError) {
console.warn('Project selection failed after plan update:', projectError);
}
}
}
trackDowngradeFeedback();
await invalidate(Dependencies.ORGANIZATION);