11import {
22 AttributeValue ,
33 DynamoDBClient ,
4+ GetItemCommand ,
45 PutItemCommand ,
56 PutItemCommandInput ,
67 QueryCommand ,
78 ScanCommand ,
89} from "@aws-sdk/client-dynamodb" ;
910import { unmarshall } from "@aws-sdk/util-dynamodb" ;
11+ import { DatabaseInsertError } from "common/errors/index.js" ;
1012import { OrganizationList , orgIds2Name } from "common/orgs.js" ;
1113import {
1214 DynamoDBItem ,
@@ -113,7 +115,7 @@ export async function fetchSigCounts(
113115 return countsArray ;
114116}
115117
116- export async function addMemberToSig (
118+ export async function addMemberToSigDynamo (
117119 sigMemberTableName : string ,
118120 sigMemberUpdateRequest : SigMemberUpdateRecord ,
119121 dynamoClient : DynamoDBClient ,
@@ -122,12 +124,50 @@ export async function addMemberToSig(
122124 Object . entries ( sigMemberUpdateRequest ) . forEach ( ( [ k , v ] ) => {
123125 item [ k ] = { S : v } ;
124126 } ) ;
125- const input : PutItemCommandInput = {
127+
128+ // put into table
129+ const put = new PutItemCommand ( {
126130 Item : item ,
127131 ReturnConsumedCapacity : "TOTAL" ,
128132 TableName : sigMemberTableName ,
129- } ;
130- // console.log(input);
131- const put = new PutItemCommand ( input ) ;
132- const response = await dynamoClient . send ( put ) ;
133+ } ) ;
134+ try {
135+ const response = await dynamoClient . send ( put ) ;
136+ console . log ( response ) ;
137+ } catch ( e ) {
138+ console . error ( "Put to dynamo db went wrong." ) ;
139+ throw e ;
140+ }
141+
142+ // fetch from db and check if fetched item update time = input item update time
143+ const validatePutQuery = new GetItemCommand ( {
144+ TableName : sigMemberTableName ,
145+ Key : {
146+ sigGroupId : { S : sigMemberUpdateRequest . sigGroupId } ,
147+ email : { S : sigMemberUpdateRequest . email } ,
148+ } ,
149+ ProjectionExpression : "updatedAt" ,
150+ } ) ;
151+
152+ try {
153+ const response = await dynamoClient . send ( validatePutQuery ) ;
154+ const item = response . Item ;
155+
156+ if ( ! item || ! item . updatedAt ?. S ) {
157+ throw new Error ( "Item not found or missing 'updatedAt'" ) ;
158+ }
159+
160+ if ( item . updatedAt . S !== sigMemberUpdateRequest . updatedAt ) {
161+ throw new DatabaseInsertError ( {
162+ message : "The member exists, but was updated by someone else!" ,
163+ } ) ;
164+ }
165+ } catch ( e ) {
166+ console . error ( "Validate DynamoDB get went wrong." , e ) ;
167+ throw e ;
168+ }
169+ }
170+
171+ export async function addMemberToSigEntra ( ) {
172+ // uuid validation not implemented yet
133173}
0 commit comments