fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma strängvärde byggs med konkatenering i stället för att matcha mönstret.
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* The cron shell around the digest: authorization and summary aggregation.
|
||||
* The digest logic itself is covered by
|
||||
* lib/notifications/__tests__/bookkeeping-digest.test.ts.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { NextResponse } from 'next/server'
|
||||
|
||||
vi.mock('@/lib/auth/cron', () => ({
|
||||
verifyCronSecret: vi.fn(() => null),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/init', () => ({
|
||||
ensureInitialized: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/supabase/server', () => ({
|
||||
createServiceClient: vi.fn(() => ({})),
|
||||
}))
|
||||
|
||||
const mockRunDigest = vi.fn()
|
||||
vi.mock('@/lib/notifications/bookkeeping-digest', () => ({
|
||||
runBookkeepingDigest: (...args: unknown[]) => mockRunDigest(...args),
|
||||
}))
|
||||
|
||||
import { verifyCronSecret } from '@/lib/auth/cron'
|
||||
import { createServiceClient } from '@/lib/supabase/server'
|
||||
import { GET } from '../route'
|
||||
|
||||
function request(): Request {
|
||||
return new Request('https://app.testbrand.example/api/notifications/bookkeeping-digest/cron')
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
vi.mocked(verifyCronSecret).mockReturnValue(null)
|
||||
})
|
||||
|
||||
describe('GET /api/notifications/bookkeeping-digest/cron', () => {
|
||||
it('rejects an unauthorized caller without touching the database', async () => {
|
||||
vi.mocked(verifyCronSecret).mockReturnValueOnce(
|
||||
NextResponse.json({ error: 'Unauthorized' }, { status: 401 }),
|
||||
)
|
||||
|
||||
const response = await GET(request())
|
||||
|
||||
expect(response.status).toBe(401)
|
||||
expect(vi.mocked(createServiceClient)).not.toHaveBeenCalled()
|
||||
expect(mockRunDigest).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('runs the digest and returns its summary', async () => {
|
||||
mockRunDigest.mockResolvedValueOnce({
|
||||
optedInUsers: 2,
|
||||
companiesConsidered: 1,
|
||||
sent: 2,
|
||||
skippedEmpty: 0,
|
||||
skippedDuplicate: 0,
|
||||
failed: 0,
|
||||
})
|
||||
|
||||
const response = await GET(request())
|
||||
const body = await response.json()
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(body).toMatchObject({ success: true, sent: 2, optedInUsers: 2 })
|
||||
expect(mockRunDigest).toHaveBeenCalledTimes(1)
|
||||
expect(mockRunDigest.mock.calls[0][1]).toBeInstanceOf(Date)
|
||||
})
|
||||
|
||||
it('maps a thrown digest failure to the canonical error envelope', async () => {
|
||||
mockRunDigest.mockRejectedValueOnce(new Error('boom'))
|
||||
|
||||
const response = await GET(request())
|
||||
|
||||
expect(response.status).toBeGreaterThanOrEqual(500)
|
||||
const body = await response.json()
|
||||
expect(body.error).toBeDefined()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,30 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { ensureInitialized } from '@/lib/init'
|
||||
import { withCronContext } from '@/lib/api/with-cron-context'
|
||||
import { createServiceClient } from '@/lib/supabase/server'
|
||||
import { runBookkeepingDigest } from '@/lib/notifications/bookkeeping-digest'
|
||||
|
||||
ensureInitialized()
|
||||
|
||||
/**
|
||||
* GET /api/notifications/bookkeeping-digest/cron, daily 05:45 UTC.
|
||||
*
|
||||
* Emails opted-in users a "nytt att bokföra" summary: bank transactions and
|
||||
* inbox documents that arrived in the last 24 hours. Runs after the 05:00
|
||||
* bank sync so the night's imports are in the counts. Strictly opt-in via
|
||||
* notification_settings.email_digest_enabled (default false); with nobody
|
||||
* opted in the run is a single cheap query.
|
||||
*/
|
||||
export const GET = withCronContext('cron.bookkeeping_digest', async (_request, ctx) => {
|
||||
const supabase = createServiceClient()
|
||||
const summary = await runBookkeepingDigest(supabase, new Date())
|
||||
|
||||
ctx.log.info('bookkeeping digest summary', { ...summary })
|
||||
|
||||
return NextResponse.json({ success: true, ...summary })
|
||||
})
|
||||
|
||||
export const POST = GET
|
||||
|
||||
/** Sequential per-company counting plus a handful of emails. */
|
||||
export const maxDuration = 300
|
||||
@@ -0,0 +1,29 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { ensureInitialized } from '@/lib/init'
|
||||
import { withCronContext } from '@/lib/api/with-cron-context'
|
||||
import { createServiceClient } from '@/lib/supabase/server'
|
||||
import { runMultiUserGraceReminders } from '@/lib/notifications/multi-user-grace'
|
||||
|
||||
ensureInitialized()
|
||||
|
||||
/**
|
||||
* GET /api/notifications/multi-user-grace/cron, daily 07:00 UTC.
|
||||
*
|
||||
* Mails company owners when the multi_user grace window opens (their extra
|
||||
* members will pause in 20 days) and again on its last day. Windowed on the
|
||||
* grant expiry timestamps so a once-daily run sends each mail exactly once;
|
||||
* see lib/notifications/multi-user-grace.ts.
|
||||
*/
|
||||
export const GET = withCronContext('cron.multi_user_grace', async (_request, ctx) => {
|
||||
const supabase = createServiceClient()
|
||||
const summary = await runMultiUserGraceReminders(supabase, new Date())
|
||||
|
||||
ctx.log.info('multi-user grace reminder summary', { ...summary })
|
||||
|
||||
return NextResponse.json({ success: true, ...summary })
|
||||
})
|
||||
|
||||
export const POST = GET
|
||||
|
||||
/** A grants scan plus a handful of emails. */
|
||||
export const maxDuration = 300
|
||||
Reference in New Issue
Block a user