Openstatus www.openstatus.dev

fix: empty inArray (#831)

authored by

Maximilian Kaske and committed by
GitHub
2bc0c905 7adf2b85

+56 -52
+13 -13
apps/server/src/v1/page.ts
··· 100 100 { monitorId: 1, order: 0 }, 101 101 { monitorId: 2, order: 1 }, 102 102 ], 103 - }), 103 + }) 104 104 ) 105 105 .optional(), 106 106 }); ··· 147 147 { monitorId: 1, order: 0 }, 148 148 { monitorId: 2, order: 1 }, 149 149 ], 150 - }), 150 + }) 151 151 ) 152 152 .nullish(), 153 153 ··· 221 221 { monitorId: 1, order: 0 }, 222 222 { monitorId: 2, order: 1 }, 223 223 ], 224 - }), 224 + }) 225 225 ) 226 226 .nullish(), 227 227 ··· 311 311 .where( 312 312 and( 313 313 eq(pageSubscriber.email, input.email), 314 - eq(pageSubscriber.pageId, pageId), 315 - ), 314 + eq(pageSubscriber.pageId, pageId) 315 + ) 316 316 ) 317 317 .get(); 318 318 if (alreadySubscribed) ··· 507 507 508 508 const { monitors, ...rest } = input; 509 509 510 - if (monitors) { 510 + if (monitors?.length) { 511 511 const monitorIds = isNumberArray(monitors) 512 512 ? monitors 513 513 : monitors.map((m) => m.monitorId); ··· 518 518 .where( 519 519 and( 520 520 inArray(monitor.id, monitorIds), 521 - eq(monitor.workspaceId, workspaceId), 522 - ), 521 + eq(monitor.workspaceId, workspaceId) 522 + ) 523 523 ) 524 524 .all(); 525 525 if (monitorsData.length !== monitors.length) ··· 631 631 : monitors.map((m) => m.monitorId) 632 632 : []; 633 633 634 - if (monitors) { 634 + if (monitors?.length) { 635 635 const monitorsData = await db 636 636 .select() 637 637 .from(monitor) 638 638 .where( 639 639 and( 640 640 inArray(monitor.id, monitorIds), 641 - eq(monitor.workspaceId, workspaceId), 642 - ), 641 + eq(monitor.workspaceId, workspaceId) 642 + ) 643 643 ) 644 644 .all(); 645 645 if (monitorsData.length !== monitors.length) ··· 670 670 .where( 671 671 and( 672 672 inArray(monitorsToPages.monitorId, removedMonitors), 673 - eq(monitorsToPages.pageId, newPage.id), 674 - ), 673 + eq(monitorsToPages.pageId, newPage.id) 674 + ) 675 675 ); 676 676 } 677 677
+43 -39
packages/api/src/router/page.ts
··· 65 65 where: and( 66 66 inArray(monitor.id, monitorIds), 67 67 eq(monitor.workspaceId, opts.ctx.workspace.id), 68 - isNull(monitor.deletedAt), 68 + isNull(monitor.deletedAt) 69 69 ), 70 70 }); 71 71 ··· 95 95 const firstPage = await opts.ctx.db.query.page.findFirst({ 96 96 where: and( 97 97 eq(page.id, opts.input.id), 98 - eq(page.workspaceId, opts.ctx.workspace.id), 98 + eq(page.workspaceId, opts.ctx.workspace.id) 99 99 ), 100 100 with: { 101 101 monitorsToPages: { ··· 132 132 .where( 133 133 and( 134 134 eq(page.id, pageInput.id), 135 - eq(page.workspaceId, opts.ctx.workspace.id), 136 - ), 135 + eq(page.workspaceId, opts.ctx.workspace.id) 136 + ) 137 137 ) 138 138 .returning() 139 139 .get(); 140 140 141 - // We should make sure the user has access to the monitors 142 - const allMonitors = await opts.ctx.db.query.monitor.findMany({ 143 - where: and( 144 - inArray(monitor.id, monitorIds), 145 - eq(monitor.workspaceId, opts.ctx.workspace.id), 146 - isNull(monitor.deletedAt), 147 - ), 148 - }); 141 + if (monitorIds.length) { 142 + // We should make sure the user has access to the monitors 143 + const allMonitors = await opts.ctx.db.query.monitor.findMany({ 144 + where: and( 145 + inArray(monitor.id, monitorIds), 146 + eq(monitor.workspaceId, opts.ctx.workspace.id), 147 + isNull(monitor.deletedAt) 148 + ), 149 + }); 149 150 150 - if (allMonitors.length !== monitorIds.length) { 151 - throw new TRPCError({ 152 - code: "FORBIDDEN", 153 - message: "You don't have access to all the monitors.", 154 - }); 151 + if (allMonitors.length !== monitorIds.length) { 152 + throw new TRPCError({ 153 + code: "FORBIDDEN", 154 + message: "You don't have access to all the monitors.", 155 + }); 156 + } 155 157 } 156 158 157 159 // TODO: check for monitor order! ··· 171 173 .where( 172 174 and( 173 175 inArray(monitorsToPages.monitorId, removedMonitors), 174 - eq(monitorsToPages.pageId, currentPage.id), 175 - ), 176 + eq(monitorsToPages.pageId, currentPage.id) 177 + ) 176 178 ); 177 179 } 178 180 ··· 182 184 monitorId, 183 185 })); 184 186 185 - await opts.ctx.db 186 - .insert(monitorsToPages) 187 - .values(values) 188 - .onConflictDoUpdate({ 189 - target: [monitorsToPages.monitorId, monitorsToPages.pageId], 190 - set: { order: sql.raw("excluded.`order`") }, 191 - }); 187 + if (values.length) { 188 + await opts.ctx.db 189 + .insert(monitorsToPages) 190 + .values(values) 191 + .onConflictDoUpdate({ 192 + target: [monitorsToPages.monitorId, monitorsToPages.pageId], 193 + set: { order: sql.raw("excluded.`order`") }, 194 + }); 195 + } 192 196 }), 193 197 delete: protectedProcedure 194 198 .input(z.object({ id: z.number() })) ··· 198 202 .where( 199 203 and( 200 204 eq(page.id, opts.input.id), 201 - eq(page.workspaceId, opts.ctx.workspace.id), 202 - ), 205 + eq(page.workspaceId, opts.ctx.workspace.id) 206 + ) 203 207 ) 204 208 .run(); 205 209 }), ··· 241 245 .leftJoin(monitor, eq(monitorsToPages.monitorId, monitor.id)) 242 246 .where( 243 247 // make sur only active monitors are returned! 244 - and(eq(monitorsToPages.pageId, result.id), eq(monitor.active, true)), 248 + and(eq(monitorsToPages.pageId, result.id), eq(monitor.active, true)) 245 249 ) 246 250 .all(); 247 251 248 252 const monitorsId = monitorsToPagesResult.map( 249 - ({ monitors_to_pages }) => monitors_to_pages.monitorId, 253 + ({ monitors_to_pages }) => monitors_to_pages.monitorId 250 254 ); 251 255 252 256 const monitorsToStatusReportResult = ··· 265 269 .all(); 266 270 267 271 const monitorStatusReportIds = monitorsToStatusReportResult.map( 268 - ({ statusReportId }) => statusReportId, 272 + ({ statusReportId }) => statusReportId 269 273 ); 270 274 271 275 const pageStatusReportIds = statusReportsToPagesResult.map( 272 - ({ statusReportId }) => statusReportId, 276 + ({ statusReportId }) => statusReportId 273 277 ); 274 278 275 279 const statusReportIds = Array.from( 276 - new Set([...monitorStatusReportIds, ...pageStatusReportIds]), 280 + new Set([...monitorStatusReportIds, ...pageStatusReportIds]) 277 281 ); 278 282 279 283 const statusReports = ··· 300 304 and( 301 305 inArray(monitor.id, monitorsId), 302 306 eq(monitor.active, true), 303 - isNull(monitor.deletedAt), 304 - ), // REMINDER: this is hardcoded 307 + isNull(monitor.deletedAt) 308 + ) // REMINDER: this is hardcoded 305 309 ) 306 310 .all() 307 311 : []; ··· 314 318 .where( 315 319 inArray( 316 320 incidentTable.monitorId, 317 - monitors.map((m) => m.id), 318 - ), 321 + monitors.map((m) => m.id) 322 + ) 319 323 ) 320 324 .all() 321 325 : []; ··· 344 348 // had filter on some words we want to keep for us 345 349 if ( 346 350 ["api", "app", "www", "docs", "checker", "time", "help"].includes( 347 - opts.input.slug, 351 + opts.input.slug 348 352 ) 349 353 ) { 350 354 return false; ··· 357 361 358 362 addCustomDomain: protectedProcedure 359 363 .input( 360 - z.object({ customDomain: z.string().toLowerCase(), pageId: z.number() }), 364 + z.object({ customDomain: z.string().toLowerCase(), pageId: z.number() }) 361 365 ) 362 366 .mutation(async (opts) => { 363 367 // TODO Add some check ?