Hello from MCP server

List Files | Just Commands | Repo | Logs

← back |
import { defineStore } from "pinia";
import { getDb } from "@/dataAccess/getDb";

export const useOrganizationStore = defineStore("organization", {
  state: () => ({
    db: null as Awaited<ReturnType<typeof getDb>> | null,
    isLoading: false,
    // Default variables - will be replaced with actual org variables
    hourlyFee: 5.262,
    saDiscount: 1.5,
    serviceCallFee: 2.0,
    salesTax: 1.08,
  }),

  actions: {
    async initDb() {
      if (!this.db) {
        this.db = await getDb();
      }
    },

    async loadVariables() {
      this.isLoading = true;
      try {
        await this.initDb();
        
        if (this.db?.organizations) {
          const variables = await this.db.organizations.getCurrentVariables();
          
          // Completely replace state with loaded variables (preserving db and isLoading)
          this.$state = {
            ...variables,
            db: this.db,
            isLoading: false,
          };
        }
      } catch (error) {
        console.error("Failed to load organization variables:", error);
        // Keep defaults on error
        this.isLoading = false;
      }
    },

    // Clear local state (useful on logout)
    clear() {
      this.db = null;
      // Reset to defaults
      this.$state = {
        db: null,
        isLoading: false,
        hourlyFee: 5.262,
        saDiscount: 1.5,
        serviceCallFee: 2.0,
        salesTax: 1.08,
      };
    },
  },
});