Skip to content

Commit 076ff4b

Browse files
authored
refactor(analytics): replace any casts with typed prehandler and prisma where clause (#546) (#563)
* refactor(backend): type authenticate decorator and jwt user payload * refactor(backend): use typed authenticate prehandler and jwt user in analytics routes * refactor(backend): type analytics where clause with prisma cardviewwhereinput
1 parent f6e043e commit 076ff4b

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

apps/backend/src/routes/analytics.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Prisma } from '@prisma/client';
12
import type {
23
FastifyInstance,
34
FastifyRequest,
@@ -12,14 +13,14 @@ export async function analyticsRoutes(
1213
'/overview',
1314
{
1415
// eslint-disable-next-line @typescript-eslint/unbound-method
15-
preHandler: [async (request, reply) => { const server = request.server as any; if (typeof server?.authenticate === 'function') { await server.authenticate(request, reply); return } if (typeof (app as any).authenticate === 'function') { await (app as any).authenticate(request, reply); return } try { await request.jwtVerify() } catch (e) { reply.status(401).send({ error: 'Unauthorized' }) } }],
16+
preHandler: [app.authenticate],
1617
},
1718
async (
1819
request: FastifyRequest,
1920
_reply: FastifyReply
2021
) => {
21-
const userId = (request.user as any).id;
22-
const username = (request.user as any).username;
22+
const userId = request.user.id;
23+
const username = request.user.username;
2324

2425
const today = new Date();
2526
today.setHours(0, 0, 0, 0);
@@ -98,7 +99,7 @@ export async function analyticsRoutes(
9899
'/views',
99100
{
100101
// eslint-disable-next-line @typescript-eslint/unbound-method
101-
preHandler: [async (request, reply) => { const server = request.server as any; if (typeof server?.authenticate === 'function') { await server.authenticate(request, reply); return } if (typeof (app as any).authenticate === 'function') { await (app as any).authenticate(request, reply); return } try { await request.jwtVerify() } catch (e) { reply.status(401).send({ error: 'Unauthorized' }) } }],
102+
preHandler: [app.authenticate],
102103
},
103104
async (
104105
request: FastifyRequest<{
@@ -109,12 +110,12 @@ export async function analyticsRoutes(
109110
}>,
110111
_reply: FastifyReply
111112
) => {
112-
const userId = (request.user as any).id;
113+
const userId = request.user.id;
113114
const page = parseInt(request.query.page || '1', 10);
114115
const limit = 20;
115116
const skip = (page - 1) * limit;
116117

117-
const whereClause: any = { ownerId: userId };
118+
const whereClause: Prisma.CardViewWhereInput = { ownerId: userId };
118119

119120
if (request.query.cardId) {
120121
whereClause.cardId = request.query.cardId;

0 commit comments

Comments
 (0)