All files / src/stores pricing.ts

100% Statements 19/19
60% Branches 3/5
100% Functions 4/4
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                                            4x 12x 12x 12x     8x 8x 8x 8x 7x   1x 1x   8x             5x 5x 5x 3x   5x     12x    
import { defineStore } from 'pinia'
import { ref } from 'vue'
import api from '@/services/api'
 
export interface PricingEntry {
  id: string
  description: string
  unit: string
  defaultValue: number
  value: number
  isOverridden: boolean
  updatedAt: string | null
  updatedBy: string | null
}
 
/**
 * Admin-editable credit cost of each metered operation (DynamoDB storage/
 * throughput, S3 storage, Lambda compute, API Gateway calls — see
 * flows-api's docs/architecture/USAGE_METERING.md). Every metering lambda
 * picks up a saved change within its own cache TTL (up to an hour),
 * without a redeploy.
 */
export const usePricingStore = defineStore('pricing', () => {
  const pricing = ref<PricingEntry[]>([])
  const loading = ref(false)
  const error = ref<string | null>(null)
 
  async function fetchPricing() {
    loading.value = true
    error.value = null
    try {
      const response = await api.get('/v1/platform-admin/pricing')
      pricing.value = response.data
    } catch (err: any) {
      error.value = err.response?.data?.error || err.message || 'Failed to load pricing'
      throw err
    } finally {
      loading.value = false
    }
  }
 
  // `value: null` clears the override and reverts to the built-in default —
  // see PUT /v1/platform-admin/pricing/{id}.
  async function updatePricing(id: string, value: number | null): Promise<PricingEntry> {
    const response = await api.put(`/v1/platform-admin/pricing/${id}`, { value })
    const index = pricing.value.findIndex((entry) => entry.id === id)
    if (index !== -1) {
      pricing.value[index] = response.data
    }
    return response.data
  }
 
  return { pricing, loading, error, fetchPricing, updatePricing }
})