From 81d34dfa86e862440803567041bc7b48a702afc9 Mon Sep 17 00:00:00 2001 From: Prospector <6166773+Prospector@users.noreply.github.com> Date: Sun, 16 Feb 2025 18:58:09 -0800 Subject: [PATCH] Add stock checking to server upgrades (#3270) * Add stock checking to server upgrades * Fix route --- .../src/pages/settings/billing/index.vue | 34 +++++++++++++++++++ .../src/components/billing/PurchaseModal.vue | 19 +++++++---- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/apps/frontend/src/pages/settings/billing/index.vue b/apps/frontend/src/pages/settings/billing/index.vue index 912885f39..29e93f7c3 100644 --- a/apps/frontend/src/pages/settings/billing/index.vue +++ b/apps/frontend/src/pages/settings/billing/index.vue @@ -382,6 +382,7 @@ text: err.message ?? (err.data ? err.data.description : err), }) " + :fetch-capacity-statuses="fetchCapacityStatuses" :customer="customer" :payment-methods="paymentMethods" :return-url="`${config.public.siteUrl}/servers/manage`" @@ -566,6 +567,7 @@ definePageMeta({ middleware: "auth", }); +const app = useNuxtApp(); const auth = await useAuth(); const baseId = useId(); @@ -990,6 +992,38 @@ const showPyroUpgradeModal = async (subscription) => { pyroPurchaseModal.value.show(); }; +async function fetchCapacityStatuses(serverId, product) { + if (product) { + try { + return { + custom: await usePyroFetch(`servers/${serverId}/upgrade-stock`, { + method: "POST", + body: { + cpu: product.metadata.cpu, + memory_mb: product.metadata.ram, + swap_mb: product.metadata.swap, + storage_mb: product.metadata.storage, + }, + }), + }; + } catch (error) { + console.error("Error checking server capacities:", error); + app.$notify({ + group: "main", + title: "Error checking server capacities", + text: error, + type: "error", + }); + return { + custom: { available: 0 }, + small: { available: 0 }, + medium: { available: 0 }, + large: { available: 0 }, + }; + } + } +} + const resubscribePyro = async (subscriptionId) => { try { await useBaseFetch(`billing/subscription/${subscriptionId}`, { diff --git a/packages/ui/src/components/billing/PurchaseModal.vue b/packages/ui/src/components/billing/PurchaseModal.vue index bc18c7d3b..9f986ddc3 100644 --- a/packages/ui/src/components/billing/PurchaseModal.vue +++ b/packages/ui/src/components/billing/PurchaseModal.vue @@ -127,7 +127,7 @@
@@ -737,13 +737,20 @@ const updateCustomServerStock = async () => { updateCustomServerStockTimeout = setTimeout(async () => { if (props.fetchCapacityStatuses) { - const capacityStatus = await props.fetchCapacityStatuses(mutatedProduct.value) - if (capacityStatus.custom?.available === 0) { - customOutOfStock.value = true + if (props.existingSubscription) { + if (mutatedProduct.value) { + const capacityStatus = await props.fetchCapacityStatuses( + props.existingSubscription.metadata.id, + mutatedProduct.value, + ) + customOutOfStock.value = capacityStatus.custom?.available === 0 + console.log(capacityStatus) + } } else { - customOutOfStock.value = false + const capacityStatus = await props.fetchCapacityStatuses(mutatedProduct.value) + customOutOfStock.value = capacityStatus.custom?.available === 0 } - } else if (!props.existingServer) { + } else { console.error('No fetchCapacityStatuses function provided.') customOutOfStock.value = true }