|
| 1 | +import { AllOrganizationList } from "@acm-uiuc/js-shared"; |
| 2 | +import { QueryCommand, type DynamoDBClient } from "@aws-sdk/client-dynamodb"; |
| 3 | +import { unmarshall } from "@aws-sdk/util-dynamodb"; |
| 4 | +import { genericConfig } from "common/config.js"; |
| 5 | +import { |
| 6 | + BaseError, |
| 7 | + DatabaseFetchError, |
| 8 | + ValidationError, |
| 9 | +} from "common/errors/index.js"; |
| 10 | +import { OrgRole, orgRoles } from "common/roles.js"; |
| 11 | +import { getOrganizationInfoResponse } from "common/types/organizations.js"; |
| 12 | +import { type FastifyBaseLogger } from "fastify"; |
| 13 | +import pino from "pino"; |
| 14 | +import z from "zod"; |
| 15 | + |
| 16 | +export interface GetOrgInfoInputs { |
| 17 | + id: string; |
| 18 | + dynamoClient: DynamoDBClient; |
| 19 | + logger: FastifyBaseLogger | pino.Logger; |
| 20 | +} |
| 21 | + |
| 22 | +export interface GetUserOrgRolesInputs { |
| 23 | + username: string; |
| 24 | + dynamoClient: DynamoDBClient; |
| 25 | + logger: FastifyBaseLogger | pino.Logger; |
| 26 | +} |
| 27 | + |
| 28 | +export async function getOrgInfo({ |
| 29 | + id, |
| 30 | + dynamoClient, |
| 31 | + logger, |
| 32 | +}: GetOrgInfoInputs) { |
| 33 | + const query = new QueryCommand({ |
| 34 | + TableName: genericConfig.SigInfoTableName, |
| 35 | + KeyConditionExpression: `primaryKey = :definitionId`, |
| 36 | + ExpressionAttributeValues: { |
| 37 | + ":definitionId": { S: `DEFINE#${id}` }, |
| 38 | + }, |
| 39 | + }); |
| 40 | + let response = { leads: [] } as { |
| 41 | + leads: { name: string; username: string; title: string | undefined }[]; |
| 42 | + }; |
| 43 | + try { |
| 44 | + const responseMarshall = await dynamoClient.send(query); |
| 45 | + if (!responseMarshall.Items || responseMarshall.Items.length === 0) { |
| 46 | + logger.debug( |
| 47 | + `Could not find SIG information for ${id}, returning default.`, |
| 48 | + ); |
| 49 | + return { id }; |
| 50 | + } |
| 51 | + const temp = unmarshall(responseMarshall.Items[0]); |
| 52 | + temp.id = temp.primaryKey.replace("DEFINE#", ""); |
| 53 | + delete temp.primaryKey; |
| 54 | + response = { ...temp, ...response }; |
| 55 | + } catch (e) { |
| 56 | + if (e instanceof BaseError) { |
| 57 | + throw e; |
| 58 | + } |
| 59 | + logger.error(e); |
| 60 | + throw new DatabaseFetchError({ |
| 61 | + message: "Failed to get org metadata.", |
| 62 | + }); |
| 63 | + } |
| 64 | + // Get leads |
| 65 | + const leadsQuery = new QueryCommand({ |
| 66 | + TableName: genericConfig.SigInfoTableName, |
| 67 | + KeyConditionExpression: "primaryKey = :leadName", |
| 68 | + ExpressionAttributeValues: { |
| 69 | + ":leadName": { S: `LEAD#${id}` }, |
| 70 | + }, |
| 71 | + }); |
| 72 | + try { |
| 73 | + const responseMarshall = await dynamoClient.send(leadsQuery); |
| 74 | + if (responseMarshall.Items) { |
| 75 | + const unmarshalledLeads = responseMarshall.Items.map((x) => unmarshall(x)) |
| 76 | + .filter((x) => x.username) |
| 77 | + .map( |
| 78 | + (x) => |
| 79 | + ({ |
| 80 | + name: x.name, |
| 81 | + username: x.username, |
| 82 | + title: x.title, |
| 83 | + }) as { name: string; username: string; title: string | undefined }, |
| 84 | + ); |
| 85 | + response = { ...response, leads: unmarshalledLeads }; |
| 86 | + } |
| 87 | + } catch (e) { |
| 88 | + if (e instanceof BaseError) { |
| 89 | + throw e; |
| 90 | + } |
| 91 | + logger.error(e); |
| 92 | + throw new DatabaseFetchError({ |
| 93 | + message: "Failed to get org leads.", |
| 94 | + }); |
| 95 | + } |
| 96 | + return response as z.infer<typeof getOrganizationInfoResponse>; |
| 97 | +} |
| 98 | + |
| 99 | +export async function getUserOrgRoles({ |
| 100 | + username, |
| 101 | + dynamoClient, |
| 102 | + logger, |
| 103 | +}: GetUserOrgRolesInputs) { |
| 104 | + const query = new QueryCommand({ |
| 105 | + TableName: genericConfig.SigInfoTableName, |
| 106 | + IndexName: "UsernameIndex", |
| 107 | + KeyConditionExpression: `username = :username`, |
| 108 | + ExpressionAttributeValues: { |
| 109 | + ":username": { S: username }, |
| 110 | + }, |
| 111 | + }); |
| 112 | + try { |
| 113 | + const response = await dynamoClient.send(query); |
| 114 | + if (!response.Items) { |
| 115 | + return []; |
| 116 | + } |
| 117 | + const unmarshalled = response.Items.map((x) => unmarshall(x)).map( |
| 118 | + (x) => |
| 119 | + ({ username: x.username, rawRole: x.primaryKey }) as { |
| 120 | + username: string; |
| 121 | + rawRole: string; |
| 122 | + }, |
| 123 | + ); |
| 124 | + const cleanedRoles = []; |
| 125 | + for (const item of unmarshalled) { |
| 126 | + const splits = item.rawRole.split("#"); |
| 127 | + if (splits.length !== 2) { |
| 128 | + logger.warn(`Invalid PK in role definition: ${JSON.stringify(item)}`); |
| 129 | + continue; |
| 130 | + } |
| 131 | + const [role, org] = splits; |
| 132 | + if (!orgRoles.includes(role as OrgRole)) { |
| 133 | + logger.warn(`Invalid role in role definition: ${JSON.stringify(item)}`); |
| 134 | + continue; |
| 135 | + } |
| 136 | + if (!AllOrganizationList.includes(org)) { |
| 137 | + logger.warn(`Invalid org in role definition: ${JSON.stringify(item)}`); |
| 138 | + continue; |
| 139 | + } |
| 140 | + cleanedRoles.push({ |
| 141 | + org, |
| 142 | + role, |
| 143 | + } as { org: (typeof AllOrganizationList)[number]; role: OrgRole }); |
| 144 | + } |
| 145 | + return cleanedRoles; |
| 146 | + } catch (e) { |
| 147 | + if (e instanceof BaseError) { |
| 148 | + throw e; |
| 149 | + } |
| 150 | + logger.error(e); |
| 151 | + throw new DatabaseFetchError({ |
| 152 | + message: "Could not get roles for user.", |
| 153 | + }); |
| 154 | + } |
| 155 | +} |
0 commit comments