|
1 | 1 | import { |
| 2 | + AttributeValue, |
2 | 3 | DynamoDBClient, |
| 4 | + GetItemCommand, |
| 5 | + PutItemCommand, |
| 6 | + PutItemCommandInput, |
3 | 7 | QueryCommand, |
4 | 8 | ScanCommand, |
5 | 9 | } from "@aws-sdk/client-dynamodb"; |
6 | 10 | import { unmarshall } from "@aws-sdk/util-dynamodb"; |
7 | | -import { OrganizationList } from "common/orgs.js"; |
| 11 | +import { DatabaseInsertError } from "common/errors/index.js"; |
| 12 | +import { OrganizationList, orgIds2Name } from "common/orgs.js"; |
8 | 13 | import { |
9 | 14 | SigDetailRecord, |
10 | 15 | SigMemberCount, |
11 | 16 | SigMemberRecord, |
| 17 | + SigMemberUpdateRecord, |
12 | 18 | } from "common/types/siglead.js"; |
13 | 19 | import { transformSigLeadToURI } from "common/utils.js"; |
| 20 | +import { KeyObject } from "crypto"; |
14 | 21 | import { string } from "zod"; |
15 | 22 |
|
16 | 23 | export async function fetchMemberRecords( |
@@ -84,31 +91,82 @@ export async function fetchSigCounts( |
84 | 91 |
|
85 | 92 | const result = await dynamoClient.send(scan); |
86 | 93 |
|
87 | | - const ids2Name: Record<string, string> = {}; |
88 | | - OrganizationList.forEach((org) => { |
89 | | - const sigid = transformSigLeadToURI(org); |
90 | | - ids2Name[sigid] = org; |
91 | | - }); |
92 | | - |
93 | 94 | const counts: Record<string, number> = {}; |
| 95 | + // Object.entries(orgIds2Name).forEach(([id, _]) => { |
| 96 | + // counts[id] = 0; |
| 97 | + // }); |
| 98 | + |
94 | 99 | (result.Items || []).forEach((item) => { |
95 | 100 | const sigGroupId = item.sigGroupId?.S; |
96 | 101 | if (sigGroupId) { |
97 | 102 | counts[sigGroupId] = (counts[sigGroupId] || 0) + 1; |
98 | 103 | } |
99 | 104 | }); |
100 | 105 |
|
101 | | - const joined: Record<string, [string, number]> = {}; |
102 | | - Object.keys(counts).forEach((sigid) => { |
103 | | - joined[sigid] = [ids2Name[sigid], counts[sigid]]; |
104 | | - }); |
105 | | - |
106 | | - const countsArray: SigMemberCount[] = Object.entries(joined).map( |
107 | | - ([sigid, [signame, count]]) => ({ |
108 | | - sigid, |
109 | | - signame, |
| 106 | + const countsArray: SigMemberCount[] = Object.entries(counts).map( |
| 107 | + ([id, count]) => ({ |
| 108 | + sigid: id, |
| 109 | + signame: orgIds2Name[id], |
110 | 110 | count, |
111 | 111 | }), |
112 | 112 | ); |
| 113 | + console.log(countsArray); |
113 | 114 | return countsArray; |
114 | 115 | } |
| 116 | + |
| 117 | +export async function addMemberToSigDynamo( |
| 118 | + sigMemberTableName: string, |
| 119 | + sigMemberUpdateRequest: SigMemberUpdateRecord, |
| 120 | + dynamoClient: DynamoDBClient, |
| 121 | +) { |
| 122 | + const item: Record<string, AttributeValue> = {}; |
| 123 | + Object.entries(sigMemberUpdateRequest).forEach(([k, v]) => { |
| 124 | + item[k] = { S: v }; |
| 125 | + }); |
| 126 | + |
| 127 | + // put into table |
| 128 | + const put = new PutItemCommand({ |
| 129 | + Item: item, |
| 130 | + ReturnConsumedCapacity: "TOTAL", |
| 131 | + TableName: sigMemberTableName, |
| 132 | + }); |
| 133 | + try { |
| 134 | + const response = await dynamoClient.send(put); |
| 135 | + console.log(response); |
| 136 | + } catch (e) { |
| 137 | + console.error("Put to dynamo db went wrong."); |
| 138 | + throw e; |
| 139 | + } |
| 140 | + |
| 141 | + // fetch from db and check if fetched item update time = input item update time |
| 142 | + const validatePutQuery = new GetItemCommand({ |
| 143 | + TableName: sigMemberTableName, |
| 144 | + Key: { |
| 145 | + sigGroupId: { S: sigMemberUpdateRequest.sigGroupId }, |
| 146 | + email: { S: sigMemberUpdateRequest.email }, |
| 147 | + }, |
| 148 | + ProjectionExpression: "updatedAt", |
| 149 | + }); |
| 150 | + |
| 151 | + try { |
| 152 | + const response = await dynamoClient.send(validatePutQuery); |
| 153 | + const item = response.Item; |
| 154 | + |
| 155 | + if (!item || !item.updatedAt?.S) { |
| 156 | + throw new Error("Item not found or missing 'updatedAt'"); |
| 157 | + } |
| 158 | + |
| 159 | + if (item.updatedAt.S !== sigMemberUpdateRequest.updatedAt) { |
| 160 | + throw new DatabaseInsertError({ |
| 161 | + message: "The member exists, but was updated by someone else!", |
| 162 | + }); |
| 163 | + } |
| 164 | + } catch (e) { |
| 165 | + console.error("Validate DynamoDB get went wrong.", e); |
| 166 | + throw e; |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +export async function addMemberToSigEntra() { |
| 171 | + // uuid validation not implemented yet |
| 172 | +} |
0 commit comments