|
1 | | -import { FastifyInstance, FastifyPluginAsync } from "fastify"; |
2 | | -import { allAppRoles, AppRoles } from "../../common/roles.js"; |
3 | | -import { |
4 | | - addToTenant, |
5 | | - getEntraIdToken, |
6 | | - listGroupMembers, |
7 | | - modifyGroup, |
8 | | - patchUserProfile, |
9 | | -} from "../functions/entraId.js"; |
10 | | -import { |
11 | | - BaseError, |
12 | | - DatabaseFetchError, |
13 | | - DatabaseInsertError, |
14 | | - EntraGroupError, |
15 | | - EntraInvitationError, |
16 | | - InternalServerError, |
17 | | - NotFoundError, |
18 | | - UnauthorizedError, |
19 | | -} from "../../common/errors/index.js"; |
20 | | -import { PutItemCommand } from "@aws-sdk/client-dynamodb"; |
| 1 | +import { FastifyPluginAsync } from "fastify"; |
| 2 | +import { DatabaseFetchError } from "../../common/errors/index.js"; |
| 3 | + |
21 | 4 | import { genericConfig } from "../../common/config.js"; |
22 | | -import { marshall } from "@aws-sdk/util-dynamodb"; |
| 5 | + |
23 | 6 | import { |
24 | | - InviteUserPostRequest, |
25 | | - invitePostRequestSchema, |
26 | | - GroupMappingCreatePostRequest, |
27 | | - groupMappingCreatePostSchema, |
28 | | - entraActionResponseSchema, |
29 | | - groupModificationPatchSchema, |
30 | | - GroupModificationPatchRequest, |
31 | | - EntraGroupActions, |
32 | | - entraGroupMembershipListResponse, |
33 | | - ProfilePatchRequest, |
34 | | - entraProfilePatchRequest, |
35 | | -} from "../../common/types/iam.js"; |
| 7 | + SigDetailRecord, |
| 8 | + SigleadGetRequest, |
| 9 | + SigMemberCount, |
| 10 | + SigMemberRecord, |
| 11 | +} from "common/types/siglead.js"; |
36 | 12 | import { |
37 | | - AUTH_DECISION_CACHE_SECONDS, |
38 | | - getGroupRoles, |
39 | | -} from "../functions/authorization.js"; |
40 | | -import { OrganizationList } from "common/orgs.js"; |
41 | | -import { z } from "zod"; |
| 13 | + fetchMemberRecords, |
| 14 | + fetchSigCounts, |
| 15 | + fetchSigDetail, |
| 16 | +} from "api/functions/siglead.js"; |
| 17 | +import { intersection } from "api/plugins/auth.js"; |
| 18 | + |
| 19 | +const sigleadRoutes: FastifyPluginAsync = async (fastify, _options) => { |
| 20 | + const limitedRoutes: FastifyPluginAsync = async (fastify) => { |
| 21 | + /*fastify.register(rateLimiter, { |
| 22 | + limit: 30, |
| 23 | + duration: 60, |
| 24 | + rateLimitIdentifier: "linkry", |
| 25 | + });*/ |
42 | 26 |
|
43 | | -const OrganizationListEnum = z.enum(OrganizationList as [string, ...string[]]); |
44 | | -export type Org = z.infer<typeof OrganizationListEnum>; |
| 27 | + fastify.get<SigleadGetRequest>( |
| 28 | + "/sigmembers/:sigid", |
| 29 | + { |
| 30 | + onRequest: async (request, reply) => { |
| 31 | + /*await fastify.authorize(request, reply, [ |
| 32 | + AppRoles.LINKS_MANAGER, |
| 33 | + AppRoles.LINKS_ADMIN, |
| 34 | + ]);*/ |
| 35 | + }, |
| 36 | + }, |
| 37 | + async (request, reply) => { |
| 38 | + const { sigid } = request.params; |
| 39 | + const tableName = genericConfig.SigleadDynamoSigMemberTableName; |
45 | 40 |
|
46 | | -type Member = { name: string; email: string }; |
47 | | -type OrgMembersResponse = { org: Org; members: Member[] }; |
| 41 | + // First try-catch: Fetch owner records |
| 42 | + let memberRecords: SigMemberRecord[]; |
| 43 | + try { |
| 44 | + memberRecords = await fetchMemberRecords( |
| 45 | + sigid, |
| 46 | + tableName, |
| 47 | + fastify.dynamoClient, |
| 48 | + ); |
| 49 | + } catch (error) { |
| 50 | + request.log.error( |
| 51 | + `Failed to fetch member records: ${error instanceof Error ? error.toString() : "Unknown error"}`, |
| 52 | + ); |
| 53 | + throw new DatabaseFetchError({ |
| 54 | + message: "Failed to fetch member records from Dynamo table.", |
| 55 | + }); |
| 56 | + } |
48 | 57 |
|
49 | | -const sigleadRoutes: FastifyPluginAsync = async (fastify, _options) => { |
50 | | - fastify.get<{ |
51 | | - Reply: OrgMembersResponse[]; |
52 | | - }>("/groups", async (request, reply) => { |
53 | | - const entraIdToken = await getEntraIdToken( |
| 58 | + // Send the response |
| 59 | + reply.code(200).send(memberRecords); |
| 60 | + }, |
| 61 | + ); |
| 62 | + |
| 63 | + fastify.get<SigleadGetRequest>( |
| 64 | + "/sigdetail/:sigid", |
54 | 65 | { |
55 | | - smClient: fastify.secretsManagerClient, |
56 | | - dynamoClient: fastify.dynamoClient, |
| 66 | + onRequest: async (request, reply) => { |
| 67 | + /*await fastify.authorize(request, reply, [ |
| 68 | + AppRoles.LINKS_MANAGER, |
| 69 | + AppRoles.LINKS_ADMIN, |
| 70 | + ]);*/ |
| 71 | + }, |
| 72 | + }, |
| 73 | + async (request, reply) => { |
| 74 | + const { sigid } = request.params; |
| 75 | + const tableName = genericConfig.SigleadDynamoSigDetailTableName; |
| 76 | + |
| 77 | + // First try-catch: Fetch owner records |
| 78 | + let sigDetail: SigDetailRecord; |
| 79 | + try { |
| 80 | + sigDetail = await fetchSigDetail( |
| 81 | + sigid, |
| 82 | + tableName, |
| 83 | + fastify.dynamoClient, |
| 84 | + ); |
| 85 | + } catch (error) { |
| 86 | + request.log.error( |
| 87 | + `Failed to fetch sig detail record: ${error instanceof Error ? error.toString() : "Unknown error"}`, |
| 88 | + ); |
| 89 | + throw new DatabaseFetchError({ |
| 90 | + message: "Failed to fetch sig detail record from Dynamo table.", |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + // Send the response |
| 95 | + reply.code(200).send(sigDetail); |
57 | 96 | }, |
58 | | - fastify.environmentConfig.AadValidClientId, |
59 | 97 | ); |
60 | 98 |
|
61 | | - const data = await Promise.all( |
62 | | - OrganizationList.map(async (org) => { |
63 | | - const members: Member[] = await listGroupMembers(entraIdToken, org); |
64 | | - return { org, members } as OrgMembersResponse; |
65 | | - }), |
| 99 | + // fetch sig count |
| 100 | + fastify.get<SigleadGetRequest>( |
| 101 | + "/sigcount", |
| 102 | + { |
| 103 | + onRequest: async (request, reply) => { |
| 104 | + /*await fastify.authorize(request, reply, [ |
| 105 | + AppRoles.LINKS_MANAGER, |
| 106 | + AppRoles.LINKS_ADMIN, |
| 107 | + ]);*/ |
| 108 | + }, |
| 109 | + }, |
| 110 | + async (request, reply) => { |
| 111 | + // First try-catch: Fetch owner records |
| 112 | + let sigMemCounts: SigMemberCount[]; |
| 113 | + try { |
| 114 | + sigMemCounts = await fetchSigCounts( |
| 115 | + genericConfig.SigleadDynamoSigMemberTableName, |
| 116 | + fastify.dynamoClient, |
| 117 | + ); |
| 118 | + } catch (error) { |
| 119 | + request.log.error( |
| 120 | + `Failed to fetch sig member counts record: ${error instanceof Error ? error.toString() : "Unknown error"}`, |
| 121 | + ); |
| 122 | + throw new DatabaseFetchError({ |
| 123 | + message: |
| 124 | + "Failed to fetch sig member counts record from Dynamo table.", |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + // Send the response |
| 129 | + reply.code(200).send(sigMemCounts); |
| 130 | + }, |
66 | 131 | ); |
| 132 | + }; |
67 | 133 |
|
68 | | - reply.status(200).send(data); |
69 | | - }); |
| 134 | + fastify.register(limitedRoutes); |
70 | 135 | }; |
71 | 136 |
|
72 | 137 | export default sigleadRoutes; |
0 commit comments