diff --git a/apps/web/src/services/cost-explorer/components/BudgetCreateStep2.vue b/apps/web/src/services/cost-explorer/components/BudgetCreateStep2.vue index f3eb7d7784..6a319af621 100644 --- a/apps/web/src/services/cost-explorer/components/BudgetCreateStep2.vue +++ b/apps/web/src/services/cost-explorer/components/BudgetCreateStep2.vue @@ -69,6 +69,11 @@ const state = reactive({ existingBudgetUsageList: [] as BudgetUsageModel[], }); + +const isCycleEnabled = computed(() => budgetCreatePageState.startMonth.length > 0 + && budgetCreatePageState.endMonth.length > 0 + && state.existingBudgetUsageList.length === 0); + const emit = defineEmits<{(e: 'click-next'): void}>(); const isDateInRange = (index: number) => { @@ -139,10 +144,24 @@ const fetchBudgetUsage = async (params: BudgetUsageListParameters) => { watch([() => budgetCreatePageState.startMonth, () => budgetCreatePageState.endMonth], async () => { if (budgetCreatePageState.startMonth.length > 0 && budgetCreatePageState.endMonth.length > 0) { await fetchBudgetUsage({ - project_id: budgetCreatePageState.project, - service_account_id: budgetCreatePageState.scope.serviceAccount, query: { filter: [ + budgetCreatePageState.scope.serviceAccount + ? { + k: 'service_account_id', + v: budgetCreatePageState.scope.serviceAccount, + o: 'eq', + } + : { + k: 'service_account_id', + v: [null, ''], + o: 'in', + }, + { + k: 'project_id', + v: budgetCreatePageState.project, + o: 'eq', + }, { k: 'date', v: dayjs.utc(budgetCreatePageState.startMonth[0]).format('YYYY-MM'), @@ -176,6 +195,25 @@ watch(() => [budgetCreatePageState.startMonth, budgetCreatePageState.endMonth], } }, { immediate: true }); +watch(() => budgetCreatePageState.selectedMonthlyBudgetAllocation, (nv, ov) => { + if (nv !== ov) { + if (nv === 'applySameAmount') { + budgetCreatePageStore.setInitialAmount(undefined); + budgetCreatePageStore.setMonthlyGrowthRate(undefined); + budgetCreatePageState.budgetEachDate = Array(12).fill(''); + } else if (nv === 'increaseBySpecificPercentage') { + budgetCreatePageStore.setBudgetAppliedSameAmount(undefined); + budgetCreatePageState.budgetEachDate = Array(12).fill(''); + } else if (nv === 'enterManually') { + budgetCreatePageStore.setBudgetAppliedSameAmount(undefined); + budgetCreatePageStore.setInitialAmount(undefined); + budgetCreatePageStore.setMonthlyGrowthRate(undefined); + } + + budgetCreatePageStore.setPlannedLimits([]); + } +}, { immediate: true }); + watch(() => [ budgetCreatePageState.selectedMonthlyBudgetAllocation, budgetCreatePageState.budgetAppliedSameAmount, @@ -185,53 +223,59 @@ watch(() => [ budgetCreatePageState.startMonth, budgetCreatePageState.endMonth, ], () => { + const planned_limits: any[] = []; + if (budgetCreatePageState.startMonth.length === 0 || budgetCreatePageState.endMonth.length === 0) { + budgetCreatePageStore.setPlannedLimits([]); return; } - const planned_limits: any = []; const startDate = dayjs.utc(budgetCreatePageState.startMonth[0]); const endDate = dayjs.utc(budgetCreatePageState.endMonth[0]); let currentDate = startDate; + const dateDiff = endDate.diff(startDate, 'month') + 1; + if (budgetCreatePageState.selectedMonthlyBudgetAllocation === 'applySameAmount') { - while (currentDate.isSameOrBefore(endDate, 'month') && budgetCreatePageState.budgetAppliedSameAmount - && budgetCreatePageState.budgetAppliedSameAmount > 0) { - planned_limits.push({ - date: currentDate.format('YYYY-MM'), - limit: budgetCreatePageState.budgetAppliedSameAmount, - }); - currentDate = currentDate.add(1, 'month'); + if (isValidPositiveNumber(budgetCreatePageState.budgetAppliedSameAmount)) { + while (currentDate.isSameOrBefore(endDate, 'month')) { + planned_limits.push({ + date: currentDate.format('YYYY-MM'), + limit: Number(budgetCreatePageState.budgetAppliedSameAmount), + }); + currentDate = currentDate.add(1, 'month'); + } } - } else if (budgetCreatePageState.selectedMonthlyBudgetAllocation === 'increaseBySpecificPercentage' - && budgetCreatePageState.initialAmount && budgetCreatePageState.monthlyGrowthRate - ) { - let currentAmount = budgetCreatePageState.initialAmount; - while (currentDate.isSameOrBefore(endDate, 'month')) { - planned_limits.push({ - date: currentDate.format('YYYY-MM'), - limit: Math.round(currentAmount), - }); - currentAmount *= (1 + ((budgetCreatePageState.monthlyGrowthRate || 0) / 100)); - currentDate = currentDate.add(1, 'month'); + } else if (budgetCreatePageState.selectedMonthlyBudgetAllocation === 'increaseBySpecificPercentage') { + if (isValidPositiveNumber(budgetCreatePageState.initialAmount) && isValidPositiveNumber(budgetCreatePageState.monthlyGrowthRate)) { + let currentAmount = budgetCreatePageState.initialAmount; + while (currentDate.isSameOrBefore(endDate, 'month')) { + planned_limits.push({ + date: currentDate.format('YYYY-MM'), + limit: Math.round(currentAmount), + }); + currentAmount *= (1 + (budgetCreatePageState.monthlyGrowthRate / 100)); + currentDate = currentDate.add(1, 'month'); + } } - } else if (budgetCreatePageState.selectedMonthlyBudgetAllocation === 'enterManually' - && budgetCreatePageState.budgetEachDate.length === Number(endDate.month() - startDate.month() + 1)) { - const monthIndices = Array.from({ length: 12 }, (_, i) => i); - monthIndices.forEach((i) => { - if (currentDate.isSameOrBefore(endDate, 'month')) { + } else if (budgetCreatePageState.selectedMonthlyBudgetAllocation === 'enterManually') { + if (budgetCreatePageState.budgetEachDate.length === dateDiff) { + for (let i = 0; i < dateDiff; i++) { + const value = budgetCreatePageState.budgetEachDate[i]; + if (!isValidPositiveNumber(value)) { + budgetCreatePageStore.setPlannedLimits([]); + return; + } planned_limits.push({ date: currentDate.format('YYYY-MM'), - limit: Number(budgetCreatePageState.budgetEachDate[i]), + limit: Number(value), }); currentDate = currentDate.add(1, 'month'); } - }); + } } - if (planned_limits.length > 0) { - budgetCreatePageStore.setPlannedLimits(planned_limits); - } + budgetCreatePageStore.setPlannedLimits(planned_limits); }, { deep: true }); watch(() => [ @@ -248,6 +292,10 @@ watch(() => [ budgetCreatePageState.startMonth, budgetCreatePageState.endMonth, ], () => { + if (!budgetCreatePageState.time_unit) { + state.isContinueAble = false; + return; + } if (state.existingBudgetUsageList.length > 0) { state.isContinueAble = false; return; @@ -304,10 +352,6 @@ watch(() => [ watch(() => budgetCreatePageState.selectedMonthlyBudgetAllocation, (nv, ov) => { if (nv !== ov) { budgetCreatePageStore.setPlannedLimits([]); - budgetCreatePageStore.setBudgetAppliedSameAmount(undefined); - budgetCreatePageStore.setInitialAmount(undefined); - budgetCreatePageStore.setMonthlyGrowthRate(undefined); - budgetCreatePageState.budgetEachDate = []; } }, { immediate: true }); @@ -351,16 +395,15 @@ watch(() => budgetCreatePageState.limit, () => { } }, { immediate: true }); -watch(() => state.existingBudgetUsageList, () => { - if (state.existingBudgetUsageList.length > 0) { - budgetCreatePageState.time_unit = ''; - budgetCreatePageState.limit = undefined; - budgetCreatePageState.budgetAppliedSameAmount = undefined; - budgetCreatePageState.initialAmount = undefined; - budgetCreatePageState.monthlyGrowthRate = undefined; - budgetCreatePageState.budgetEachDate = []; +watch(() => budgetCreatePageState.startMonth[0], (newVal, oldVal) => { + if (newVal !== oldVal && budgetCreatePageState.currentStep === 2) { + const isValidationReturn = budgetCreatePageState.time_unit !== '' || budgetCreatePageState.endMonth.length > 0; + if (!isValidationReturn) { + budgetCreatePageState.endMonth = []; + budgetCreatePageState.time_unit = ''; + } } -}, { deep: true, immediate: true }); +}, { immediate: true });