diff --git a/docs/hooks.md b/docs/hooks.md index de1965c..a7b167e 100644 --- a/docs/hooks.md +++ b/docs/hooks.md @@ -89,6 +89,8 @@ import { buildPreventDeleteSystemPage } from '@intecion/ipal-kit' hooks: { beforeDelete: [buildPreventDeleteSystemPage({ settingsSlug: 'site-settings' })] } ``` +Site Settings czyta w tym samym `req` (transakcja usuwania). + Żeby usunąć — najpierw odłącz rolę w Site Settings (świadoma decyzja). --- @@ -184,6 +186,9 @@ hooks: { - **Tylko wypełnione pola** — wyczyszczenie obrazka w jednym języku nie zeruje go w innych (zachowawczo). - Guard `_ipalSyncingCrossLocale` zrywa pętlę `afterChange`→`update`. +- **W transakcji zapisu:** odczyt i zapis innych języków idą z `req` (Postgres/Mongo — rollback + obejmuje też synchronizację); `req.locale` / `req.fallbackLocale` wracają po hooku do wartości z + zapisu, więc odpowiedź API zostaje w języku redaktora. Parametry: `arrayField` (domyślnie `'layout'`), `guardFlag`. diff --git a/src/modules/hooks/buildCrossLocaleFieldSync.ts b/src/modules/hooks/buildCrossLocaleFieldSync.ts index 87bd2c6..54aadf4 100644 --- a/src/modules/hooks/buildCrossLocaleFieldSync.ts +++ b/src/modules/hooks/buildCrossLocaleFieldSync.ts @@ -103,6 +103,11 @@ export function buildCrossLocaleFieldSync( if (otherLocales.length === 0) {return doc} // single-locale site: nothing to sync const payload = req.payload + // Passing `req` keeps the reads and writes in the save's transaction, but + // the Local API sets req.locale / req.fallbackLocale to the call's locale — + // restored in `finally`, or the rest of the save (afterRead, the response) + // would run in the last target locale. + const { fallbackLocale, locale: requestLocale } = req reqAny[guardFlag] = true try { @@ -114,6 +119,7 @@ export function buildCrossLocaleFieldSync( collection: collection as never, depth: 0, locale: locale as never, + req, })) as Record } catch { continue // locale not yet created for this doc — nothing to sync into @@ -167,6 +173,8 @@ export function buildCrossLocaleFieldSync( payload.logger?.error?.({ err, msg: `[ipal] cross-locale ${arrayField} sync failed` }) } finally { reqAny[guardFlag] = false + req.locale = requestLocale + req.fallbackLocale = fallbackLocale } return doc diff --git a/src/modules/hooks/preventDeleteSystemPage.ts b/src/modules/hooks/preventDeleteSystemPage.ts index b6b7460..2f85ddd 100644 --- a/src/modules/hooks/preventDeleteSystemPage.ts +++ b/src/modules/hooks/preventDeleteSystemPage.ts @@ -25,7 +25,7 @@ export function buildPreventDeleteSystemPage( return async ({ id, req }) => { const settings = (await req.payload - .findGlobal({ slug: settingsSlug as never, depth: 0 }) + .findGlobal({ slug: settingsSlug as never, depth: 0, req }) .catch(() => null)) as null | Record if (!settings) {return} diff --git a/test/hooksReq.test.ts b/test/hooksReq.test.ts new file mode 100644 index 0000000..ff5540c --- /dev/null +++ b/test/hooksReq.test.ts @@ -0,0 +1,46 @@ +/** Hooks run their Local API calls inside the save's request (transaction) without corrupting it. */ +import { describe, expect, test, vi } from 'vitest' + +import { buildCrossLocaleFieldSync } from '../src/modules/hooks/buildCrossLocaleFieldSync.js' +import { buildPreventDeleteSystemPage } from '../src/modules/hooks/preventDeleteSystemPage.js' + +const config = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'PL' }, { code: 'en', label: 'EN' }] } + +/** Mimics the Local API: a call with `req` sets req.locale / req.fallbackLocale to its own. */ +function fakeReq() { + const req: Record = { fallbackLocale: 'pl', locale: 'pl' } + const adopt = (args: { locale?: string; req?: Record }) => { + if (args.req && args.locale) { + args.req.locale = args.locale + args.req.fallbackLocale = 'null' + } + } + const findByID = vi.fn(async (args: { locale?: string; req?: Record }) => { + adopt(args) + return { id: 1, layout: [{ id: 'r1', blockType: 'hero', image: 7 }] } + }) + const update = vi.fn(async (args: { locale?: string; req?: Record }) => { + adopt(args) + return {} + }) + req.payload = { findByID, findGlobal: vi.fn(async () => ({})), logger: { error: vi.fn() }, update } + return { findByID, req, update } +} + +describe('hooks pass req', () => { + test('cross-locale sync reads and writes in the request, then restores its locale', async () => { + const { findByID, req, update } = fakeReq() + const hook = buildCrossLocaleFieldSync({ collection: 'pages', config, fields: ['image'] }) + await hook({ doc: { id: 1, layout: [{ id: 'r1', blockType: 'hero', image: 9 }] }, req } as never) + expect(findByID).toHaveBeenCalledWith(expect.objectContaining({ locale: 'en', req })) + expect(update).toHaveBeenCalledWith(expect.objectContaining({ locale: 'en', req })) + expect(req.locale).toBe('pl') + expect(req.fallbackLocale).toBe('pl') + }) + + test('preventDeleteSystemPage reads Site Settings in the request', async () => { + const { req } = fakeReq() + await buildPreventDeleteSystemPage()({ id: 3, req } as never) + expect((req.payload as { findGlobal: ReturnType }).findGlobal).toHaveBeenCalledWith(expect.objectContaining({ req })) + }) +})