Openstatus www.openstatus.dev

small fix (#1797)

* small fix

* ci: apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

authored by

Thibault Le Ouay
autofix-ci[bot]
and committed by
GitHub
c0f52eae 77dec981

+61 -7
+24 -5
apps/server/src/routes/rpc/services/status-report/__tests__/status-report.test.ts
··· 379 379 ); 380 380 }); 381 381 382 - test("derives pageId from components when creating status report", async () => { 382 + test("returns error when pageId does not match components page", async () => { 383 + const res = await connectRequest( 384 + "CreateStatusReport", 385 + { 386 + title: `${TEST_PREFIX}-pageid-mismatch`, 387 + status: "STATUS_REPORT_STATUS_INVESTIGATING", 388 + message: "Test pageId mismatch with components.", 389 + date: new Date().toISOString(), 390 + pageId: "1", // This doesn't match testPage2ComponentId's page 391 + pageComponentIds: [String(testPage2ComponentId)], 392 + }, 393 + { "x-openstatus-key": "1" }, 394 + ); 395 + 396 + expect(res.status).toBe(400); 397 + const data = await res.json(); 398 + expect(data.message).toContain("does not match the page ID"); 399 + }); 400 + 401 + test("creates status report when pageId matches component page", async () => { 383 402 const res = await connectRequest( 384 403 "CreateStatusReport", 385 404 { 386 - title: `${TEST_PREFIX}-derived-pageid`, 405 + title: `${TEST_PREFIX}-matching-pageid`, 387 406 status: "STATUS_REPORT_STATUS_INVESTIGATING", 388 - message: "Test deriving pageId from components.", 407 + message: "Test with matching pageId and components.", 389 408 date: new Date().toISOString(), 390 - pageId: "1", // This is ignored, pageId is derived from components 409 + pageId: String(testPage2Id), // Matching the component's page 391 410 pageComponentIds: [String(testPage2ComponentId)], 392 411 }, 393 412 { "x-openstatus-key": "1" }, ··· 401 420 String(testPage2ComponentId), 402 421 ); 403 422 404 - // Verify the pageId was derived from the component (page 2) 423 + // Verify the pageId was set correctly 405 424 const createdReport = await db 406 425 .select() 407 426 .from(statusReport)
+19
apps/server/src/routes/rpc/services/status-report/errors.ts
··· 10 10 STATUS_REPORT_UPDATE_FAILED: "STATUS_REPORT_UPDATE_FAILED", 11 11 PAGE_COMPONENT_NOT_FOUND: "PAGE_COMPONENT_NOT_FOUND", 12 12 PAGE_COMPONENTS_MIXED_PAGES: "PAGE_COMPONENTS_MIXED_PAGES", 13 + PAGE_ID_COMPONENT_MISMATCH: "PAGE_ID_COMPONENT_MISMATCH", 13 14 INVALID_DATE_FORMAT: "INVALID_DATE_FORMAT", 14 15 INVALID_STATUS: "INVALID_STATUS", 15 16 } as const; ··· 139 140 { "status-value": String(statusValue) }, 140 141 ); 141 142 } 143 + 144 + /** 145 + * Creates a "page ID and component page mismatch" error. 146 + */ 147 + export function pageIdComponentMismatchError( 148 + providedPageId: string, 149 + componentPageId: string, 150 + ): ConnectError { 151 + return createError( 152 + `Page ID ${providedPageId} does not match the page ID ${componentPageId} of the provided components`, 153 + Code.InvalidArgument, 154 + ErrorReason.PAGE_ID_COMPONENT_MISMATCH, 155 + { 156 + "provided-page-id": providedPageId, 157 + "component-page-id": componentPageId, 158 + }, 159 + ); 160 + }
+18 -2
apps/server/src/routes/rpc/services/status-report/index.ts
··· 37 37 invalidDateFormatError, 38 38 pageComponentNotFoundError, 39 39 pageComponentsMixedPagesError, 40 + pageIdComponentMismatchError, 40 41 statusReportCreateFailedError, 41 42 statusReportIdRequiredError, 42 43 statusReportNotFoundError, ··· 280 281 tx, 281 282 ); 282 283 283 - // Derive pageId from validated components (ensures consistency) 284 - const pageId = validatedComponents.pageId; 284 + // Validate that provided pageId matches the components' page 285 + const derivedPageId = validatedComponents.pageId; 286 + const providedPageId = req.pageId?.trim(); 287 + if ( 288 + derivedPageId !== null && 289 + providedPageId && 290 + providedPageId !== "" && 291 + Number(providedPageId) !== derivedPageId 292 + ) { 293 + throw pageIdComponentMismatchError( 294 + providedPageId, 295 + String(derivedPageId), 296 + ); 297 + } 298 + 299 + // Use the derived pageId from components (null if no components) 300 + const pageId = derivedPageId; 285 301 286 302 // Create the status report 287 303 const report = await tx