1+ import {
2+ checkPaidMembershipFromEntra ,
3+ checkPaidMembershipFromTable ,
4+ setPaidMembershipInTable ,
5+ } from "api/functions/membership.js" ;
16import { validateNetId } from "api/functions/validation.js" ;
2- import { NotImplementedError } from "common/errors/index.js" ;
37import { FastifyPluginAsync } from "fastify" ;
4- import { ValidationError } from "zod-validation-error" ;
8+ import { ValidationError } from "common/errors/index.js" ;
9+ import { getEntraIdToken } from "api/functions/entraId.js" ;
510
611const membershipPlugin : FastifyPluginAsync = async ( fastify , _options ) => {
712 fastify . get < {
@@ -24,9 +29,44 @@ const membershipPlugin: FastifyPluginAsync = async (fastify, _options) => {
2429 async ( request , reply ) => {
2530 const netId = ( request . params as Record < string , string > ) . netId ;
2631 if ( ! validateNetId ( netId ) ) {
27- throw new ValidationError ( `${ netId } is not a valid Illinois NetID!` ) ;
32+ throw new ValidationError ( {
33+ message : `${ netId } is not a valid Illinois NetID!` ,
34+ } ) ;
2835 }
29- throw new NotImplementedError ( { } ) ;
36+ const isDynamoMember = await checkPaidMembershipFromTable (
37+ netId ,
38+ fastify . dynamoClient ,
39+ ) ;
40+ // check Dynamo cache first
41+ if ( isDynamoMember ) {
42+ return reply
43+ . header ( "X-ACM-Data-Source" , "dynamo" )
44+ . send ( { netId, isPaidMember : true } ) ;
45+ }
46+ // check AAD
47+ const entraIdToken = await getEntraIdToken (
48+ {
49+ smClient : fastify . secretsManagerClient ,
50+ dynamoClient : fastify . dynamoClient ,
51+ } ,
52+ fastify . environmentConfig . AadValidClientId ,
53+ ) ;
54+ const paidMemberGroup = fastify . environmentConfig . PaidMemberGroupId ;
55+ const isAadMember = await checkPaidMembershipFromEntra (
56+ netId ,
57+ entraIdToken ,
58+ paidMemberGroup ,
59+ ) ;
60+ if ( isAadMember ) {
61+ reply
62+ . header ( "X-ACM-Data-Source" , "aad" )
63+ . send ( { netId, isPaidMember : true } ) ;
64+ await setPaidMembershipInTable ( netId , fastify . dynamoClient ) ;
65+ return ;
66+ }
67+ return reply
68+ . header ( "X-ACM-Data-Source" , "aad" )
69+ . send ( { netId, isPaidMember : false } ) ;
3070 } ,
3171 ) ;
3272} ;
0 commit comments