|
| 1 | +import type { Request, Response } from 'express' |
| 2 | + |
| 3 | +import { NotFoundError } from '@crowd/common' |
| 4 | +import { |
| 5 | + findMembersByGithubHandles, |
| 6 | + findVerifiedEmailsByMemberIds, |
| 7 | + optionsQx, |
| 8 | + resolveAffiliationsByMemberIds, |
| 9 | +} from '@crowd/data-access-layer' |
| 10 | + |
| 11 | +import { ok } from '@/utils/api' |
| 12 | + |
| 13 | +export async function getAffiliationByHandle(req: Request, res: Response): Promise<void> { |
| 14 | + const handle = req.params.githubHandle.toLowerCase() |
| 15 | + const qx = optionsQx(req) |
| 16 | + |
| 17 | + const members = await findMembersByGithubHandles(qx, [handle]) |
| 18 | + if (members.length === 0) { |
| 19 | + throw new NotFoundError(`No LFX profile found for GitHub login '${req.params.githubHandle}'.`) |
| 20 | + } |
| 21 | + |
| 22 | + const member = members[0] |
| 23 | + const memberIds = [member.memberId] |
| 24 | + |
| 25 | + const [emailRows, affiliationsByMember] = await Promise.all([ |
| 26 | + findVerifiedEmailsByMemberIds(qx, memberIds), |
| 27 | + resolveAffiliationsByMemberIds(qx, memberIds), |
| 28 | + ]) |
| 29 | + |
| 30 | + ok(res, { |
| 31 | + githubHandle: member.githubHandle, |
| 32 | + name: member.displayName, |
| 33 | + emails: emailRows.map((r) => r.email), |
| 34 | + affiliations: affiliationsByMember.get(member.memberId) ?? [], |
| 35 | + }) |
| 36 | +} |
0 commit comments