@@ -241,22 +241,39 @@ export class ReportsService {
241241 // First check if the report exists and belongs to the user
242242 const existingReport = await this . findOne ( id , userId ) ;
243243
244+ // Create objects to hold UpdateExpression parts and attributes
245+ const updateFields = [
246+ { field : 'status' , value : updateDto . status } ,
247+ { field : 'updatedAt' , value : new Date ( ) . toISOString ( ) } ,
248+ ] ;
249+
250+ // Dynamically build UpdateExpression, ExpressionAttributeNames, and ExpressionAttributeValues
251+ const expressionAttributeNames : Record < string , string > = { } ;
252+ const expressionAttributeValues : Record < string , any > = {
253+ ':userId' : userId ,
254+ } ;
255+
256+ const setExpressions = updateFields . map ( ( { field } ) => `#${ field } = :${ field } ` ) ;
257+
258+ // Build expression attribute names and values
259+ updateFields . forEach ( ( { field, value } ) => {
260+ expressionAttributeNames [ `#${ field } ` ] = field ;
261+ expressionAttributeValues [ `:${ field } ` ] = value ;
262+ } ) ;
263+
264+ // Create the UpdateExpression
265+ const updateExpression = `SET ${ setExpressions . join ( ', ' ) } ` ;
266+
244267 const command = new UpdateItemCommand ( {
245268 TableName : this . tableName ,
246269 Key : marshall ( {
247270 userId, // Partition key
248271 id, // Sort key
249272 } ) ,
250- UpdateExpression : 'SET #status = :status, updatedAt = :updatedAt' ,
273+ UpdateExpression : updateExpression ,
251274 ConditionExpression : 'userId = :userId' , // Ensure the report belongs to the user
252- ExpressionAttributeNames : {
253- '#status' : 'status' ,
254- } ,
255- ExpressionAttributeValues : marshall ( {
256- ':status' : updateDto . status ,
257- ':updatedAt' : new Date ( ) . toISOString ( ) ,
258- ':userId' : userId ,
259- } ) ,
275+ ExpressionAttributeNames : expressionAttributeNames ,
276+ ExpressionAttributeValues : marshall ( expressionAttributeValues ) ,
260277 ReturnValues : 'ALL_NEW' ,
261278 } ) ;
262279
@@ -394,45 +411,49 @@ export class ReportsService {
394411 // Set the updatedAt timestamp
395412 report . updatedAt = new Date ( ) . toISOString ( ) ;
396413
414+ // Create objects to hold UpdateExpression parts and attributes
415+ const updateFields = [
416+ { field : 'title' , value : report . title } ,
417+ { field : 'bookmarked' , value : report . bookmarked } ,
418+ { field : 'category' , value : report . category } ,
419+ { field : 'processingStatus' , value : report . processingStatus } ,
420+ { field : 'labValues' , value : report . labValues } ,
421+ { field : 'summary' , value : report . summary } ,
422+ { field : 'confidence' , value : report . confidence } ,
423+ { field : 'status' , value : report . status } ,
424+ { field : 'missingInformation' , value : report . missingInformation } ,
425+ { field : 'isMedicalReport' , value : report . isMedicalReport } ,
426+ { field : 'updatedAt' , value : report . updatedAt } ,
427+ ] ;
428+
429+ // Dynamically build UpdateExpression, ExpressionAttributeNames, and ExpressionAttributeValues
430+ const expressionAttributeNames : Record < string , string > = { } ;
431+ const expressionAttributeValues : Record < string , any > = {
432+ ':userId' : report . userId ,
433+ } ;
434+
435+ const setExpressions = updateFields . map ( ( { field } ) => `#${ field } = :${ field } ` ) ;
436+
437+ // Build expression attribute names and values
438+ updateFields . forEach ( ( { field, value } ) => {
439+ expressionAttributeNames [ `#${ field } ` ] = field ;
440+ expressionAttributeValues [ `:${ field } ` ] = value ;
441+ } ) ;
442+
443+ // Create the UpdateExpression
444+ const updateExpression = `SET ${ setExpressions . join ( ', ' ) } ` ;
445+
397446 // Update report in DynamoDB
398447 const command = new UpdateItemCommand ( {
399448 TableName : this . tableName ,
400449 Key : marshall ( {
401450 userId : report . userId , // Partition key
402451 id : report . id , // Sort key
403452 } ) ,
404- UpdateExpression :
405- 'SET #title = :title, #bookmarked = :bookmarked, #category = :category, ' +
406- '#processingStatus = :processingStatus, #labValues = :labValues, #summary = :summary, ' +
407- '#confidence = :confidence, #status = :status, #missingInformation = :missingInformation, #isMedicalReport = :isMedicalReport, #updatedAt = :updatedAt' ,
453+ UpdateExpression : updateExpression ,
408454 ConditionExpression : 'userId = :userId' , // Ensure the report belongs to the user
409- ExpressionAttributeNames : {
410- '#title' : 'title' ,
411- '#bookmarked' : 'bookmarked' ,
412- '#category' : 'category' ,
413- '#processingStatus' : 'processingStatus' ,
414- '#labValues' : 'labValues' ,
415- '#summary' : 'summary' ,
416- '#confidence' : 'confidence' ,
417- '#status' : 'status' ,
418- '#missingInformation' : 'missingInformation' ,
419- '#isMedicalReport' : 'isMedicalReport' ,
420- '#updatedAt' : 'updatedAt' ,
421- } ,
422- ExpressionAttributeValues : marshall ( {
423- ':title' : report . title ,
424- ':bookmarked' : report . bookmarked ,
425- ':category' : report . category ,
426- ':processingStatus' : report . processingStatus ,
427- ':labValues' : report . labValues ,
428- ':summary' : report . summary ,
429- ':confidence' : report . confidence ,
430- ':status' : report . status ,
431- ':missingInformation' : report . missingInformation ,
432- ':isMedicalReport' : report . isMedicalReport ,
433- ':updatedAt' : report . updatedAt ,
434- ':userId' : report . userId ,
435- } ) ,
455+ ExpressionAttributeNames : expressionAttributeNames ,
456+ ExpressionAttributeValues : marshall ( expressionAttributeValues ) ,
436457 ReturnValues : 'ALL_NEW' ,
437458 } ) ;
438459
@@ -501,19 +522,39 @@ export class ReportsService {
501522 // First check if the report exists and belongs to the user
502523 const existingReport = await this . findOne ( id , userId ) ;
503524
525+ // Create objects to hold UpdateExpression parts and attributes
526+ const updateFields = [
527+ { field : 'bookmarked' , value : bookmarked } ,
528+ { field : 'updatedAt' , value : new Date ( ) . toISOString ( ) } ,
529+ ] ;
530+
531+ // Dynamically build UpdateExpression, ExpressionAttributeNames, and ExpressionAttributeValues
532+ const expressionAttributeNames : Record < string , string > = { } ;
533+ const expressionAttributeValues : Record < string , any > = {
534+ ':userId' : userId ,
535+ } ;
536+
537+ const setExpressions = updateFields . map ( ( { field } ) => `#${ field } = :${ field } ` ) ;
538+
539+ // Build expression attribute names and values
540+ updateFields . forEach ( ( { field, value } ) => {
541+ expressionAttributeNames [ `#${ field } ` ] = field ;
542+ expressionAttributeValues [ `:${ field } ` ] = value ;
543+ } ) ;
544+
545+ // Create the UpdateExpression
546+ const updateExpression = `SET ${ setExpressions . join ( ', ' ) } ` ;
547+
504548 const command = new UpdateItemCommand ( {
505549 TableName : this . tableName ,
506550 Key : marshall ( {
507551 userId,
508552 id,
509553 } ) ,
510- UpdateExpression : 'SET bookmarked = :bookmarked, updatedAt = :updatedAt' ,
554+ UpdateExpression : updateExpression ,
511555 ConditionExpression : 'userId = :userId' , // Add condition to ensure we're updating the right user's report
512- ExpressionAttributeValues : marshall ( {
513- ':bookmarked' : bookmarked ,
514- ':updatedAt' : new Date ( ) . toISOString ( ) ,
515- ':userId' : userId ,
516- } ) ,
556+ ExpressionAttributeNames : expressionAttributeNames ,
557+ ExpressionAttributeValues : marshall ( expressionAttributeValues ) ,
517558 ReturnValues : 'ALL_NEW' ,
518559 } ) ;
519560
0 commit comments