fix: isolate local data by signed-in user
This commit is contained in:
parent
e155f09fe1
commit
f7756d8936
|
|
@ -5,6 +5,7 @@ import { db } from "../data/db";
|
||||||
import { localDate } from "../data/entries";
|
import { localDate } from "../data/entries";
|
||||||
import { createId } from "../data/ids";
|
import { createId } from "../data/ids";
|
||||||
import { useAuthStore } from "./auth";
|
import { useAuthStore } from "./auth";
|
||||||
|
import { useLedgerStore } from "./ledgers";
|
||||||
|
|
||||||
let syncPromise: Promise<void> | null = null;
|
let syncPromise: Promise<void> | null = null;
|
||||||
let syncTriggersStarted = false;
|
let syncTriggersStarted = false;
|
||||||
|
|
@ -45,6 +46,7 @@ function scheduleConversionPoll(entryIds: string[], resetAttempts = true) {
|
||||||
export const useEntryStore = defineStore("entries", {
|
export const useEntryStore = defineStore("entries", {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
entries: [] as LedgerEntry[],
|
entries: [] as LedgerEntry[],
|
||||||
|
loadedForUserId: "",
|
||||||
pendingEntryIds: [] as string[],
|
pendingEntryIds: [] as string[],
|
||||||
pendingEntries: [] as Array<{ id: string; ledgerIds: string[] }>,
|
pendingEntries: [] as Array<{ id: string; ledgerIds: string[] }>,
|
||||||
syncing: false,
|
syncing: false,
|
||||||
|
|
@ -53,15 +55,38 @@ export const useEntryStore = defineStore("entries", {
|
||||||
}),
|
}),
|
||||||
actions: {
|
actions: {
|
||||||
async loadEntries() {
|
async loadEntries() {
|
||||||
|
const auth = useAuthStore();
|
||||||
|
if (this.loadedForUserId !== (auth.user?.id ?? "")) {
|
||||||
|
this.entries = [];
|
||||||
|
this.pendingEntryIds = [];
|
||||||
|
this.pendingEntries = [];
|
||||||
|
this.loadedForUserId = auth.user?.id ?? "";
|
||||||
|
}
|
||||||
|
await useLedgerStore().loadLedgers();
|
||||||
await this.refreshLocalEntries();
|
await this.refreshLocalEntries();
|
||||||
this.startSyncTriggers();
|
this.startSyncTriggers();
|
||||||
void this.syncEntries();
|
void this.syncEntries();
|
||||||
},
|
},
|
||||||
async refreshLocalEntries() {
|
async refreshLocalEntries() {
|
||||||
|
const auth = useAuthStore();
|
||||||
|
if (!auth.user) {
|
||||||
|
this.entries = [];
|
||||||
|
this.pendingEntryIds = [];
|
||||||
|
this.pendingEntries = [];
|
||||||
|
this.loadedForUserId = "";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const allowedLedgerIds = new Set(useLedgerStore().ledgers.map((ledger) => ledger.id));
|
||||||
|
const canAccessEntry = (entry: LedgerEntry) =>
|
||||||
|
entry.ownerId === auth.user!.id || entry.ledgerIds.some((ledgerId) => allowedLedgerIds.has(ledgerId));
|
||||||
this.entries = (await db.entries.orderBy("occurredAt").reverse().toArray()).filter(
|
this.entries = (await db.entries.orderBy("occurredAt").reverse().toArray()).filter(
|
||||||
(entry) => entry.deletedAt === null,
|
(entry) => entry.deletedAt === null && canAccessEntry(entry),
|
||||||
|
);
|
||||||
|
const pendingOperations = (await db.syncOperations.filter((operation) => operation.syncedAt === null).toArray())
|
||||||
|
.filter((operation) =>
|
||||||
|
(operation.userId ?? operation.payload.updatedBy) === auth.user!.id
|
||||||
|
|| operation.ledgerIds.some((ledgerId) => allowedLedgerIds.has(ledgerId)),
|
||||||
);
|
);
|
||||||
const pendingOperations = await db.syncOperations.filter((operation) => operation.syncedAt === null).toArray();
|
|
||||||
this.pendingEntryIds = [...new Set(pendingOperations.map((operation) => operation.entityId))];
|
this.pendingEntryIds = [...new Set(pendingOperations.map((operation) => operation.entityId))];
|
||||||
this.pendingEntries = [...new Map(
|
this.pendingEntries = [...new Map(
|
||||||
pendingOperations.map((operation) => [operation.entityId, { id: operation.entityId, ledgerIds: operation.ledgerIds }]),
|
pendingOperations.map((operation) => [operation.entityId, { id: operation.entityId, ledgerIds: operation.ledgerIds }]),
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ export const useLedgerStore = defineStore("ledgers", {
|
||||||
.filter((entry) => entry.ownerId !== auth.user!.id && entry.ledgerIds.some((ledgerId) => !allowedIds.has(ledgerId)))
|
.filter((entry) => entry.ownerId !== auth.user!.id && entry.ledgerIds.some((ledgerId) => !allowedIds.has(ledgerId)))
|
||||||
.map((entry) => ({ ...entry, ledgerIds: entry.ledgerIds.filter((ledgerId) => allowedIds.has(ledgerId)) }));
|
.map((entry) => ({ ...entry, ledgerIds: entry.ledgerIds.filter((ledgerId) => allowedIds.has(ledgerId)) }));
|
||||||
const inaccessibleOperations = await db.syncOperations
|
const inaccessibleOperations = await db.syncOperations
|
||||||
.filter((item) => !item.ledgerIds.some((ledgerId) => allowedIds.has(ledgerId)))
|
.filter((item) => (item.userId ?? item.payload.updatedBy) !== auth.user!.id)
|
||||||
.primaryKeys();
|
.primaryKeys();
|
||||||
await db.transaction("rw", db.ledgers, db.entries, db.syncOperations, async () => {
|
await db.transaction("rw", db.ledgers, db.entries, db.syncOperations, async () => {
|
||||||
await db.ledgers.clear();
|
await db.ledgers.clear();
|
||||||
|
|
@ -89,6 +89,8 @@ export const useLedgerStore = defineStore("ledgers", {
|
||||||
},
|
},
|
||||||
async reloadLedgers() {
|
async reloadLedgers() {
|
||||||
this.loaded = false;
|
this.loaded = false;
|
||||||
|
this.ledgers = [];
|
||||||
|
this.currentLedgerId = "";
|
||||||
this.currentMembers = [];
|
this.currentMembers = [];
|
||||||
await this.loadLedgers();
|
await this.loadLedgers();
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue