149 lines
4.8 KiB
JavaScript
149 lines
4.8 KiB
JavaScript
import { w as writable } from "./index2.js";
|
|
import { g as get_store_value } from "./utils.js";
|
|
const createSplitExpenseStore = () => {
|
|
const defaultState = {
|
|
expenses: [],
|
|
pendingInvites: []
|
|
};
|
|
const initialState = defaultState;
|
|
const { subscribe, set, update } = writable(initialState);
|
|
return {
|
|
subscribe,
|
|
/**
|
|
* Add a new expense
|
|
* @param {Object} expense - The expense object to add
|
|
*/
|
|
addExpense: (expense) => update((state) => {
|
|
const id = `exp_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`;
|
|
const newExpense = {
|
|
...expense,
|
|
id,
|
|
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
status: "pending"
|
|
};
|
|
return {
|
|
...state,
|
|
expenses: [...state.expenses, newExpense]
|
|
};
|
|
}),
|
|
/**
|
|
* Get a specific expense by ID
|
|
* @param {string} id - The expense ID
|
|
*/
|
|
getExpense: (id) => {
|
|
const state = get_store_value({ subscribe });
|
|
return state.expenses.find((expense) => expense.id === id) || null;
|
|
},
|
|
/**
|
|
* Update an existing expense
|
|
* @param {string} id - The expense ID
|
|
* @param {Object} updates - The fields to update
|
|
*/
|
|
updateExpense: (id, updates) => update((state) => {
|
|
const expenseIndex = state.expenses.findIndex((expense) => expense.id === id);
|
|
if (expenseIndex === -1) return state;
|
|
const updatedExpenses = [...state.expenses];
|
|
updatedExpenses[expenseIndex] = {
|
|
...updatedExpenses[expenseIndex],
|
|
...updates,
|
|
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
};
|
|
return {
|
|
...state,
|
|
expenses: updatedExpenses
|
|
};
|
|
}),
|
|
/**
|
|
* Mark an expense as completed
|
|
* @param {string} id - The expense ID
|
|
*/
|
|
completeExpense: (id) => update((state) => {
|
|
const expenseIndex = state.expenses.findIndex((expense) => expense.id === id);
|
|
if (expenseIndex === -1) return state;
|
|
const updatedExpenses = [...state.expenses];
|
|
updatedExpenses[expenseIndex] = {
|
|
...updatedExpenses[expenseIndex],
|
|
status: "completed",
|
|
completedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
};
|
|
return {
|
|
...state,
|
|
expenses: updatedExpenses
|
|
};
|
|
}),
|
|
/**
|
|
* Add a pending invite for a participant
|
|
* @param {Object} invite - The invite data
|
|
*/
|
|
addPendingInvite: (invite) => update((state) => {
|
|
return {
|
|
...state,
|
|
pendingInvites: [...state.pendingInvites, {
|
|
...invite,
|
|
id: `inv_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`,
|
|
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
}]
|
|
};
|
|
}),
|
|
/**
|
|
* Remove a pending invite once processed
|
|
* @param {string} id - The invite ID
|
|
*/
|
|
removePendingInvite: (id) => update((state) => {
|
|
return {
|
|
...state,
|
|
pendingInvites: state.pendingInvites.filter((invite) => invite.id !== id)
|
|
};
|
|
}),
|
|
/**
|
|
* Get all expenses for a user
|
|
* @param {string} userId - The user's public key
|
|
*/
|
|
getUserExpenses: (publicKey) => {
|
|
const state = get_store_value({ subscribe });
|
|
return state.expenses.filter((expense) => {
|
|
if (expense.creator === publicKey) return true;
|
|
return expense.participants.some((p) => p.address === publicKey);
|
|
});
|
|
},
|
|
/**
|
|
* Mark a participant as paid in an expense
|
|
* @param {string} expenseId - The expense ID
|
|
* @param {string} participantAddress - The participant address
|
|
*/
|
|
markParticipantPaid: (expenseId, participantAddress) => update((state) => {
|
|
const expenseIndex = state.expenses.findIndex((expense2) => expense2.id === expenseId);
|
|
if (expenseIndex === -1) return state;
|
|
const updatedExpenses = [...state.expenses];
|
|
const expense = updatedExpenses[expenseIndex];
|
|
const updatedParticipants = expense.participants.map((participant) => {
|
|
if (participant.address === participantAddress) {
|
|
return {
|
|
...participant,
|
|
paid: true,
|
|
paidAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
};
|
|
}
|
|
return participant;
|
|
});
|
|
const allPaid = updatedParticipants.every((p) => p.paid || p.address === expense.creator);
|
|
updatedExpenses[expenseIndex] = {
|
|
...expense,
|
|
participants: updatedParticipants,
|
|
status: allPaid ? "completed" : expense.status,
|
|
completedAt: allPaid ? (/* @__PURE__ */ new Date()).toISOString() : expense.completedAt
|
|
};
|
|
return {
|
|
...state,
|
|
expenses: updatedExpenses
|
|
};
|
|
}),
|
|
// Reset the store to default state
|
|
reset: () => set(defaultState)
|
|
};
|
|
};
|
|
const splitExpenseStore = createSplitExpenseStore();
|
|
export {
|
|
splitExpenseStore as s
|
|
};
|