Skip to content

Commit 5c531c3

Browse files
committed
fix: refactor v1Router
Signed-off-by: Umberto Sgueglia <usgueglia@contractor.linuxfoundation.org>
1 parent 19d1e63 commit 5c531c3

5 files changed

Lines changed: 38 additions & 20 deletions

File tree

backend/src/api/public/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
import { Router } from 'express'
22

3-
import { AUTH0_CONFIG } from '../../conf'
4-
53
import { errorHandler } from './middlewares/errorHandler'
6-
import { oauth2Middleware } from './middlewares/oauth2Middleware'
7-
import { staticApiKeyMiddleware } from './middlewares/staticApiKeyMiddleware'
84
import { v1Router } from './v1'
9-
import { devStatsRouter } from './v1/dev-stats'
105

116
export function publicRouter(): Router {
127
const router = Router()
138

14-
router.use('/v1', staticApiKeyMiddleware(), devStatsRouter())
15-
router.use('/v1', oauth2Middleware(AUTH0_CONFIG), v1Router())
9+
router.use('/v1', v1Router())
1610
router.use(errorHandler)
1711

1812
return router

backend/src/api/public/v1/dev-stats/getAffiliations.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,42 @@ import {
1111
import { ok } from '@/utils/api'
1212
import { validateOrThrow } from '@/utils/validation'
1313

14-
const MAX_HANDLES = 1000
14+
const MAX_HANDLES = 100
15+
const DEFAULT_PAGE_SIZE = 50
1516

1617
const bodySchema = z.object({
1718
githubHandles: z
1819
.array(z.string().min(1))
1920
.min(1)
2021
.max(MAX_HANDLES, `Maximum ${MAX_HANDLES} handles per request`),
22+
page: z.number().int().min(1).default(1),
23+
pageSize: z.number().int().min(1).max(MAX_HANDLES).default(DEFAULT_PAGE_SIZE),
2124
})
2225

2326
export async function getAffiliations(req: Request, res: Response): Promise<void> {
24-
const { githubHandles } = validateOrThrow(bodySchema, req.body)
27+
const { githubHandles, page, pageSize } = validateOrThrow(bodySchema, req.body)
2528
const qx = optionsQx(req)
2629

2730
const lowercasedHandles = githubHandles.map((h) => h.toLowerCase())
2831

32+
const offset = (page - 1) * pageSize
33+
const pageHandles = lowercasedHandles.slice(offset, offset + pageSize)
34+
2935
// Step 1: find verified members by github handles
30-
const memberRows = await findMembersByGithubHandles(qx, lowercasedHandles)
36+
const memberRows = await findMembersByGithubHandles(qx, pageHandles)
3137

3238
const foundHandles = new Set(memberRows.map((r) => r.githubHandle.toLowerCase()))
33-
const notFound = githubHandles.filter((h) => !foundHandles.has(h.toLowerCase()))
39+
const notFound = pageHandles.filter((h) => !foundHandles.has(h))
3440

3541
if (memberRows.length === 0) {
36-
ok(res, { total_found: 0, contributors: [], notFound })
42+
ok(res, {
43+
total: githubHandles.length,
44+
page,
45+
pageSize,
46+
total_found: 0,
47+
contributors: [],
48+
notFound,
49+
})
3750
return
3851
}
3952

@@ -60,5 +73,12 @@ export async function getAffiliations(req: Request, res: Response): Promise<void
6073
affiliations: affiliationsByMember.get(member.memberId) ?? [],
6174
}))
6275

63-
ok(res, { total_found: contributors.length, contributors, notFound })
76+
ok(res, {
77+
total: githubHandles.length,
78+
page,
79+
pageSize,
80+
total_found: contributors.length,
81+
contributors,
82+
notFound,
83+
})
6484
}

backend/src/api/public/v1/dev-stats/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function devStatsRouter(): Router {
1414

1515
router.use(rateLimiter)
1616

17-
router.post('/affiliations', requireScopes([SCOPES.READ_AFFILIATIONS]), safeWrap(getAffiliations))
17+
router.post('/', requireScopes([SCOPES.READ_AFFILIATIONS]), safeWrap(getAffiliations))
1818

1919
return router
2020
}

backend/src/api/public/v1/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import { Router } from 'express'
22

3+
import { AUTH0_CONFIG } from '../../../conf'
4+
5+
import { oauth2Middleware } from '../middlewares/oauth2Middleware'
6+
import { staticApiKeyMiddleware } from '../middlewares/staticApiKeyMiddleware'
7+
import { devStatsRouter } from './dev-stats'
38
import { membersRouter } from './members'
49
import { organizationsRouter } from './organizations'
510

611
export function v1Router(): Router {
712
const router = Router()
813

9-
router.use('/members', membersRouter())
10-
router.use('/organizations', organizationsRouter())
14+
router.use('/members', oauth2Middleware(AUTH0_CONFIG), membersRouter())
15+
router.use('/organizations', oauth2Middleware(AUTH0_CONFIG), organizationsRouter())
16+
router.use('/affiliations', staticApiKeyMiddleware(), devStatsRouter())
1117

1218
return router
1319
}

services/libs/data-access-layer/src/affiliations/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export async function findWorkExperiencesBulk(
3333
): Promise<IWorkRow[]> {
3434
const rows: IWorkRow[] = await qx.select(
3535
`
36-
/* TODO: re-enable memberCount tiebreaker once performance is acceptable
3736
WITH relevant_orgs AS (
3837
SELECT DISTINCT "organizationId"
3938
FROM "memberOrganizations"
@@ -53,7 +52,6 @@ export async function findWorkExperiencesBulk(
5352
)
5453
GROUP BY osa."organizationId"
5554
)
56-
*/
5755
SELECT
5856
mo.id,
5957
mo."memberId",
@@ -64,12 +62,12 @@ export async function findWorkExperiencesBulk(
6462
mo."dateEnd",
6563
mo."createdAt",
6664
COALESCE(ovr."isPrimaryWorkExperience", false) AS "isPrimaryWorkExperience",
67-
0 AS "memberCount", -- TODO: restore COALESCE(a.total_count, 0) when re-enabling aggs
65+
COALESCE(a.total_count, 0) AS "memberCount",
6866
NULL::text AS "segmentId"
6967
FROM "memberOrganizations" mo
7068
JOIN organizations o ON mo."organizationId" = o.id
7169
LEFT JOIN "memberOrganizationAffiliationOverrides" ovr ON ovr."memberOrganizationId" = mo.id
72-
-- LEFT JOIN aggs a ON a."organizationId" = mo."organizationId" -- TODO: restore when re-enabling aggs
70+
LEFT JOIN aggs a ON a."organizationId" = mo."organizationId"
7371
WHERE mo."memberId" IN ($(memberIds:csv))
7472
AND mo."deletedAt" IS NULL
7573
AND COALESCE(ovr."allowAffiliation", true) = true

0 commit comments

Comments
 (0)