app/composables/useFixtureIO.ts

136 lines
4 KiB
TypeScript

import { useMembersStore } from '~/stores/members'
import { usePoliciesStore } from '~/stores/policies'
import { useStreamsStore } from '~/stores/streams'
import { useBudgetStore } from '~/stores/budget'
import { useScenariosStore } from '~/stores/scenarios'
import { useCashStore } from '~/stores/cash'
import { useSessionStore } from '~/stores/session'
export type AppSnapshot = {
members: any[]
policies: Record<string, any>
streams: any[]
budget: Record<string, any>
scenarios: Record<string, any>
cash: Record<string, any>
session: Record<string, any>
}
export function useFixtureIO() {
const exportAll = (): AppSnapshot => {
const members = useMembersStore()
const policies = usePoliciesStore()
const streams = useStreamsStore()
const budget = useBudgetStore()
const scenarios = useScenariosStore()
const cash = useCashStore()
const session = useSessionStore()
return {
members: members.members,
policies: {
equalHourlyWage: policies.equalHourlyWage,
payrollOncostPct: policies.payrollOncostPct,
savingsTargetMonths: policies.savingsTargetMonths,
minCashCushionAmount: policies.minCashCushionAmount,
operatingMode: policies.operatingMode,
payPolicy: policies.payPolicy,
deferredCapHoursPerQtr: policies.deferredCapHoursPerQtr,
deferredSunsetMonths: policies.deferredSunsetMonths,
surplusOrder: policies.surplusOrder,
paymentPriority: policies.paymentPriority,
volunteerScope: policies.volunteerScope
},
streams: streams.streams,
budget: {
budgetLines: budget.budgetLines,
overheadCosts: budget.overheadCosts,
productionCosts: budget.productionCosts,
currentPeriod: budget.currentPeriod
},
scenarios: {
sliders: scenarios.sliders,
activeScenario: scenarios.activeScenario
},
cash: {
cashEvents: cash.cashEvents,
paymentQueue: cash.paymentQueue,
currentCash: cash.currentCash,
currentSavings: cash.currentSavings
},
session: {
checklist: session.checklist,
draftAllocations: session.draftAllocations,
rationale: session.rationale,
currentSession: session.currentSession,
savedRecords: session.savedRecords
}
}
}
const importAll = (snapshot: AppSnapshot) => {
const members = useMembersStore()
const policies = usePoliciesStore()
const streams = useStreamsStore()
const budget = useBudgetStore()
const scenarios = useScenariosStore()
const cash = useCashStore()
const session = useSessionStore()
try {
// Import members
if (snapshot.members && Array.isArray(snapshot.members)) {
members.$patch({ members: snapshot.members })
}
// Import policies
if (snapshot.policies) {
policies.$patch(snapshot.policies)
}
// Import streams
if (snapshot.streams && Array.isArray(snapshot.streams)) {
streams.$patch({ streams: snapshot.streams })
}
// Import budget
if (snapshot.budget) {
budget.$patch(snapshot.budget)
}
// Import scenarios
if (snapshot.scenarios) {
scenarios.$patch(snapshot.scenarios)
}
// Import cash
if (snapshot.cash) {
cash.updateCurrentBalances(
snapshot.cash.currentCash || 0,
snapshot.cash.currentSavings || 0
)
// Handle cash events and payment queue if present
if (snapshot.cash.cashEvents) {
cash.$patch({ cashEvents: snapshot.cash.cashEvents })
}
if (snapshot.cash.paymentQueue) {
cash.$patch({ paymentQueue: snapshot.cash.paymentQueue })
}
}
// Import session
if (snapshot.session) {
session.$patch(snapshot.session)
}
console.log('Successfully imported data snapshot')
} catch (error) {
console.error('Failed to import snapshot:', error)
throw error
}
}
return { exportAll, importAll }
}