|
| 1 | +import { AvailableSQSFunctions } from "common/types/sqsMessage.js"; |
| 2 | +import { currentEnvironmentConfig, SQSHandlerFunction } from "../index.js"; |
| 3 | +import { |
| 4 | + getEntraIdToken, |
| 5 | + getUserProfile, |
| 6 | +} from "../../../api/functions/entraId.js"; |
| 7 | +import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; |
| 8 | +import { genericConfig } from "../../../common/config.js"; |
| 9 | + |
| 10 | +import { setPaidMembership } from "api/functions/membership.js"; |
| 11 | +import { createAuditLogEntry } from "api/functions/auditLog.js"; |
| 12 | +import { Modules } from "common/modules.js"; |
| 13 | +import { getAuthorizedClients } from "../utils.js"; |
| 14 | +import { emailMembershipPassHandler } from "./emailMembershipPassHandler.js"; |
| 15 | + |
| 16 | +export const provisionNewMemberHandler: SQSHandlerFunction< |
| 17 | + AvailableSQSFunctions.ProvisionNewMember |
| 18 | +> = async (payload, metadata, logger) => { |
| 19 | + const { email } = payload; |
| 20 | + const commonConfig = { region: genericConfig.AwsRegion }; |
| 21 | + const clients = await getAuthorizedClients(logger, commonConfig); |
| 22 | + const entraToken = await getEntraIdToken( |
| 23 | + clients, |
| 24 | + currentEnvironmentConfig.AadValidClientId, |
| 25 | + ); |
| 26 | + logger.info("Got authorized clients and Entra ID token."); |
| 27 | + const { updated } = await setPaidMembership({ |
| 28 | + netId: email.replace("@illinois.edu", ""), |
| 29 | + dynamoClient: clients.dynamoClient, |
| 30 | + entraToken, |
| 31 | + paidMemberGroup: currentEnvironmentConfig.PaidMemberGroupId, |
| 32 | + }); |
| 33 | + if (updated) { |
| 34 | + const logPromise = createAuditLogEntry({ |
| 35 | + entry: { |
| 36 | + module: Modules.PROVISION_NEW_MEMBER, |
| 37 | + actor: metadata.initiator, |
| 38 | + target: email, |
| 39 | + message: "Marked target as a paid member.", |
| 40 | + }, |
| 41 | + }); |
| 42 | + logger.info( |
| 43 | + `${email} added as a paid member. Emailing their membership pass.`, |
| 44 | + ); |
| 45 | + await emailMembershipPassHandler(payload, metadata, logger); |
| 46 | + await logPromise; |
| 47 | + } else { |
| 48 | + logger.info(`${email} was already a paid member.`); |
| 49 | + } |
| 50 | +}; |
0 commit comments