mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-27 06:13:06 +00:00
fix(ops): logout always clears persisted outbox and mirror
Cold reload left IndexedDB/OPFS intact when executor/store singletons were null. Wipe adapters directly, clear both mirror backends, and always revoke the session even if storage wipe fails.
This commit is contained in:
parent
1b7caf7cf5
commit
2caa82b10a
7 changed files with 102 additions and 32 deletions
|
|
@ -130,6 +130,9 @@ const handleUpdatedAtConflict = (
|
||||||
throw new NonRetriableError(message)
|
throw new NonRetriableError(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DOMAIN_OUTBOX_DB = 'teqo-ops-domain'
|
||||||
|
const DOMAIN_OUTBOX_STORE = 'outbox'
|
||||||
|
|
||||||
const createDomainOfflineExecutor = (): OfflineExecutor =>
|
const createDomainOfflineExecutor = (): OfflineExecutor =>
|
||||||
startOfflineExecutor({
|
startOfflineExecutor({
|
||||||
collections: {
|
collections: {
|
||||||
|
|
@ -142,7 +145,7 @@ const createDomainOfflineExecutor = (): OfflineExecutor =>
|
||||||
demands: demandsCollection,
|
demands: demandsCollection,
|
||||||
activities: activitiesCollection,
|
activities: activitiesCollection,
|
||||||
},
|
},
|
||||||
storage: new IndexedDBAdapter('teqo-ops-domain', 'outbox'),
|
storage: new IndexedDBAdapter(DOMAIN_OUTBOX_DB, DOMAIN_OUTBOX_STORE),
|
||||||
beforeRetry: collapseAllDomainOutbox,
|
beforeRetry: collapseAllDomainOutbox,
|
||||||
mutationFns: {
|
mutationFns: {
|
||||||
[LEADERSHIP_UPDATE_FN]: async ({ transaction }) => {
|
[LEADERSHIP_UPDATE_FN]: async ({ transaction }) => {
|
||||||
|
|
@ -636,17 +639,19 @@ const wipeDomainOutboxCollections = (): void => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Logout wipe — outbox storage + in-memory rows (OH11/OH13). */
|
/** Logout wipe — persisted IndexedDB even when the executor singleton was never booted (cold reload). */
|
||||||
export const clearOpsDomainOutboxForLogout = async (): Promise<void> => {
|
export const clearOpsDomainOutboxForLogout = async (): Promise<void> => {
|
||||||
if (executorSingleton) {
|
|
||||||
try {
|
try {
|
||||||
|
if (executorSingleton) {
|
||||||
await executorSingleton.clearOutbox()
|
await executorSingleton.clearOutbox()
|
||||||
executorSingleton.dispose()
|
executorSingleton.dispose()
|
||||||
|
} else {
|
||||||
|
await new IndexedDBAdapter(DOMAIN_OUTBOX_DB, DOMAIN_OUTBOX_STORE).clear()
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// Best effort — private mode / torn-down storage.
|
// Best effort — private mode / torn-down storage.
|
||||||
}
|
}
|
||||||
executorSingleton = null
|
executorSingleton = null
|
||||||
initPromise = null
|
initPromise = null
|
||||||
}
|
|
||||||
wipeDomainOutboxCollections()
|
wipeDomainOutboxCollections()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,9 @@ const opsEstimateOutboxCollection = createCollection(
|
||||||
let executorSingleton: OfflineExecutor | null = null
|
let executorSingleton: OfflineExecutor | null = null
|
||||||
let initPromise: Promise<OfflineExecutor> | null = null
|
let initPromise: Promise<OfflineExecutor> | null = null
|
||||||
|
|
||||||
|
const ESTIMATE_OUTBOX_DB = 'teqo-ops-estimate'
|
||||||
|
const ESTIMATE_OUTBOX_STORE = 'outbox'
|
||||||
|
|
||||||
const createEstimateOfflineExecutor = (): OfflineExecutor =>
|
const createEstimateOfflineExecutor = (): OfflineExecutor =>
|
||||||
startOfflineExecutor({
|
startOfflineExecutor({
|
||||||
collections: {
|
collections: {
|
||||||
|
|
@ -54,7 +57,7 @@ const createEstimateOfflineExecutor = (): OfflineExecutor =>
|
||||||
// Mirror rows touched in onMutate must be registered for persistence.
|
// Mirror rows touched in onMutate must be registered for persistence.
|
||||||
votePledges: votePledgesCollection,
|
votePledges: votePledgesCollection,
|
||||||
},
|
},
|
||||||
storage: new IndexedDBAdapter('teqo-ops-estimate', 'outbox'),
|
storage: new IndexedDBAdapter(ESTIMATE_OUTBOX_DB, ESTIMATE_OUTBOX_STORE),
|
||||||
beforeRetry: collapseEstimateOutboxByPledge,
|
beforeRetry: collapseEstimateOutboxByPledge,
|
||||||
mutationFns: {
|
mutationFns: {
|
||||||
[ESTIMATE_MUTATION_FN]: async ({ transaction }) => {
|
[ESTIMATE_MUTATION_FN]: async ({ transaction }) => {
|
||||||
|
|
@ -188,18 +191,20 @@ export const collectOpsEstimateOutboxKeys = (): Set<OpsOutboxKey> => {
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Logout wipe — outbox storage + in-memory rows (OH11). */
|
/** Logout wipe — persisted IndexedDB even when the executor singleton was never booted (cold reload). */
|
||||||
export const clearOpsEstimateOutboxForLogout = async (): Promise<void> => {
|
export const clearOpsEstimateOutboxForLogout = async (): Promise<void> => {
|
||||||
if (executorSingleton) {
|
|
||||||
try {
|
try {
|
||||||
|
if (executorSingleton) {
|
||||||
await executorSingleton.clearOutbox()
|
await executorSingleton.clearOutbox()
|
||||||
executorSingleton.dispose()
|
executorSingleton.dispose()
|
||||||
|
} else {
|
||||||
|
await new IndexedDBAdapter(ESTIMATE_OUTBOX_DB, ESTIMATE_OUTBOX_STORE).clear()
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// Best effort — private mode / torn-down storage.
|
// Best effort — private mode / torn-down storage.
|
||||||
}
|
}
|
||||||
executorSingleton = null
|
executorSingleton = null
|
||||||
initPromise = null
|
initPromise = null
|
||||||
}
|
|
||||||
for (const row of opsEstimateOutboxCollection.toArray) {
|
for (const row of opsEstimateOutboxCollection.toArray) {
|
||||||
if (opsEstimateOutboxCollection.has(row.pledgeId)) {
|
if (opsEstimateOutboxCollection.has(row.pledgeId)) {
|
||||||
opsEstimateOutboxCollection.delete(row.pledgeId)
|
opsEstimateOutboxCollection.delete(row.pledgeId)
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import {
|
||||||
stateDeputiesCollection,
|
stateDeputiesCollection,
|
||||||
} from '@/components/campaign/opsSync/opsMirrorCollections'
|
} from '@/components/campaign/opsSync/opsMirrorCollections'
|
||||||
import {
|
import {
|
||||||
|
clearAllOpsMirrorPersistence,
|
||||||
openOpsMirrorStore,
|
openOpsMirrorStore,
|
||||||
type OpsMirrorPersistenceMode,
|
type OpsMirrorPersistenceMode,
|
||||||
type OpsMirrorStore,
|
type OpsMirrorStore,
|
||||||
|
|
@ -117,12 +118,11 @@ export const getOpsMirrorLastSyncedAt = (): string | null => lastSyncedAtMirror
|
||||||
export const clearOpsMirrorPersistenceForLogout = async (): Promise<void> => {
|
export const clearOpsMirrorPersistenceForLogout = async (): Promise<void> => {
|
||||||
wipeOpsMirrorCollections()
|
wipeOpsMirrorCollections()
|
||||||
bootPromise = null
|
bootPromise = null
|
||||||
const store = storeSingleton
|
|
||||||
storeSingleton = null
|
storeSingleton = null
|
||||||
persistenceMode = null
|
persistenceMode = null
|
||||||
if (store) {
|
// Clear both durable backends — prior sessions may have used either, and the
|
||||||
await store.clear()
|
// singleton is null after a cold reload even when IndexedDB/OPFS still hold data.
|
||||||
}
|
await clearAllOpsMirrorPersistence()
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Test/DI seam — reset module singletons between unit cases. */
|
/** Test/DI seam — reset module singletons between unit cases. */
|
||||||
|
|
|
||||||
|
|
@ -156,6 +156,14 @@ const createOpfsOpsMirrorStore = (): OpsMirrorStore => {
|
||||||
* Boot-time feature detect: try OPFS, fall back to IndexedDB on any failure
|
* Boot-time feature detect: try OPFS, fall back to IndexedDB on any failure
|
||||||
* (private mode, missing API, write probe fail). Never throws.
|
* (private mode, missing API, write probe fail). Never throws.
|
||||||
*/
|
*/
|
||||||
|
/**
|
||||||
|
* Logout wipe for both durable backends. Clears OPFS and IndexedDB even when
|
||||||
|
* the in-memory store singleton was never booted (cold reload after enqueue).
|
||||||
|
*/
|
||||||
|
export const clearAllOpsMirrorPersistence = async (): Promise<void> => {
|
||||||
|
await Promise.allSettled([createOpfsOpsMirrorStore().clear(), createIdbOpsMirrorStore().clear()])
|
||||||
|
}
|
||||||
|
|
||||||
export const openOpsMirrorStore = async (options?: {
|
export const openOpsMirrorStore = async (options?: {
|
||||||
forceMode?: OpsMirrorPersistenceMode
|
forceMode?: OpsMirrorPersistenceMode
|
||||||
}): Promise<OpsMirrorStore> => {
|
}): Promise<OpsMirrorStore> => {
|
||||||
|
|
|
||||||
|
|
@ -148,6 +148,9 @@ const handleUpdatedAtConflict = (
|
||||||
throw new NonRetriableError(message)
|
throw new NonRetriableError(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const MUNICIPALITY_OUTBOX_DB = 'teqo-ops-municipality'
|
||||||
|
const MUNICIPALITY_OUTBOX_STORE = 'outbox'
|
||||||
|
|
||||||
const createMunicipalityOfflineExecutor = (): OfflineExecutor =>
|
const createMunicipalityOfflineExecutor = (): OfflineExecutor =>
|
||||||
startOfflineExecutor({
|
startOfflineExecutor({
|
||||||
collections: {
|
collections: {
|
||||||
|
|
@ -159,7 +162,7 @@ const createMunicipalityOfflineExecutor = (): OfflineExecutor =>
|
||||||
// Mirror rows touched in onMutate must be registered for persistence.
|
// Mirror rows touched in onMutate must be registered for persistence.
|
||||||
votePledges: votePledgesCollection,
|
votePledges: votePledgesCollection,
|
||||||
},
|
},
|
||||||
storage: new IndexedDBAdapter('teqo-ops-municipality', 'outbox'),
|
storage: new IndexedDBAdapter(MUNICIPALITY_OUTBOX_DB, MUNICIPALITY_OUTBOX_STORE),
|
||||||
beforeRetry: collapseAllMunicipalityOutbox,
|
beforeRetry: collapseAllMunicipalityOutbox,
|
||||||
mutationFns: {
|
mutationFns: {
|
||||||
[DECLARE_MUTATION_FN]: async ({ transaction }) => {
|
[DECLARE_MUTATION_FN]: async ({ transaction }) => {
|
||||||
|
|
@ -695,17 +698,19 @@ const wipeMunicipalityOutboxCollections = (): void => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Logout wipe — outbox storage + in-memory rows (OH11). */
|
/** Logout wipe — persisted IndexedDB even when the executor singleton was never booted (cold reload). */
|
||||||
export const clearOpsMunicipalityOutboxForLogout = async (): Promise<void> => {
|
export const clearOpsMunicipalityOutboxForLogout = async (): Promise<void> => {
|
||||||
if (executorSingleton) {
|
|
||||||
try {
|
try {
|
||||||
|
if (executorSingleton) {
|
||||||
await executorSingleton.clearOutbox()
|
await executorSingleton.clearOutbox()
|
||||||
executorSingleton.dispose()
|
executorSingleton.dispose()
|
||||||
|
} else {
|
||||||
|
await new IndexedDBAdapter(MUNICIPALITY_OUTBOX_DB, MUNICIPALITY_OUTBOX_STORE).clear()
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// Best effort — private mode / torn-down storage.
|
// Best effort — private mode / torn-down storage.
|
||||||
}
|
}
|
||||||
executorSingleton = null
|
executorSingleton = null
|
||||||
initPromise = null
|
initPromise = null
|
||||||
}
|
|
||||||
wipeMunicipalityOutboxCollections()
|
wipeMunicipalityOutboxCollections()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -80,11 +80,14 @@ export const CampaignSidebar = ({ user }: { user: CampaignSidebarUser }) => {
|
||||||
clearRecentVisits()
|
clearRecentVisits()
|
||||||
clearLastActedMunicipality()
|
clearLastActedMunicipality()
|
||||||
clearMunicipalitySavedFilters()
|
clearMunicipalitySavedFilters()
|
||||||
|
try {
|
||||||
await clearCampaignOpsStorage()
|
await clearCampaignOpsStorage()
|
||||||
await clearCampaignPwaCaches()
|
await clearCampaignPwaCaches()
|
||||||
// logoutCampaign redirects — no need to reset the pending flag on success.
|
} finally {
|
||||||
|
// logoutCampaign redirects — always revoke the session even if storage wipe fails.
|
||||||
await logoutCampaign()
|
await logoutCampaign()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Sidebar collapsible="offcanvas" className="print:hidden">
|
<Sidebar collapsible="offcanvas" className="print:hidden">
|
||||||
|
|
|
||||||
44
tests/unit/opsLogoutWipe.unit.spec.ts
Normal file
44
tests/unit/opsLogoutWipe.unit.spec.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
// @vitest-environment jsdom
|
||||||
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||||
|
|
||||||
|
const adapterClear = vi.fn(async () => undefined)
|
||||||
|
|
||||||
|
vi.mock('@tanstack/offline-transactions', async () => {
|
||||||
|
const actual = await vi.importActual<typeof import('@tanstack/offline-transactions')>(
|
||||||
|
'@tanstack/offline-transactions',
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
IndexedDBAdapter: class {
|
||||||
|
clear = adapterClear
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('campaignOps logout wipe (Pass 5 P0)', () => {
|
||||||
|
afterEach(() => {
|
||||||
|
vi.resetModules()
|
||||||
|
adapterClear.mockClear()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('clears estimate/municipality/domain IndexedDB when executor singletons are null', async () => {
|
||||||
|
const { clearOpsEstimateOutboxForLogout } =
|
||||||
|
await import('@/components/campaign/opsSync/opsEstimateOutbox')
|
||||||
|
const { clearOpsMunicipalityOutboxForLogout } =
|
||||||
|
await import('@/components/campaign/opsSync/opsMunicipalityOutbox')
|
||||||
|
const { clearOpsDomainOutboxForLogout } =
|
||||||
|
await import('@/components/campaign/opsSync/opsDomainOutbox')
|
||||||
|
|
||||||
|
await clearOpsEstimateOutboxForLogout()
|
||||||
|
await clearOpsMunicipalityOutboxForLogout()
|
||||||
|
await clearOpsDomainOutboxForLogout()
|
||||||
|
|
||||||
|
expect(adapterClear).toHaveBeenCalledTimes(3)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('clearAllOpsMirrorPersistence settles without throwing when backends are empty', async () => {
|
||||||
|
const { clearAllOpsMirrorPersistence } =
|
||||||
|
await import('@/components/campaign/opsSync/opsMirrorPersistence')
|
||||||
|
await expect(clearAllOpsMirrorPersistence()).resolves.toBeUndefined()
|
||||||
|
})
|
||||||
|
})
|
||||||
Loading…
Reference in a new issue