Skip to content

Commit 81e5a51

Browse files
authored
Merge pull request #119 from ModusCreateOrg/ADE-66
[ADE-66] fix showing 'reference-ranges-missing' notice
2 parents 6465022 + 08fa37c commit 81e5a51

File tree

1 file changed

+87
-42
lines changed

1 file changed

+87
-42
lines changed

backend/src/reports/reports.service.ts

Lines changed: 87 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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,41 +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, #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-
'#updatedAt': 'updatedAt',
419-
},
420-
ExpressionAttributeValues: marshall({
421-
':title': report.title,
422-
':bookmarked': report.bookmarked,
423-
':category': report.category,
424-
':processingStatus': report.processingStatus,
425-
':labValues': report.labValues,
426-
':summary': report.summary,
427-
':confidence': report.confidence,
428-
':status': report.status,
429-
':updatedAt': report.updatedAt,
430-
':userId': report.userId,
431-
}),
455+
ExpressionAttributeNames: expressionAttributeNames,
456+
ExpressionAttributeValues: marshall(expressionAttributeValues),
432457
ReturnValues: 'ALL_NEW',
433458
});
434459

@@ -497,19 +522,39 @@ export class ReportsService {
497522
// First check if the report exists and belongs to the user
498523
const existingReport = await this.findOne(id, userId);
499524

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+
500548
const command = new UpdateItemCommand({
501549
TableName: this.tableName,
502550
Key: marshall({
503551
userId,
504552
id,
505553
}),
506-
UpdateExpression: 'SET bookmarked = :bookmarked, updatedAt = :updatedAt',
554+
UpdateExpression: updateExpression,
507555
ConditionExpression: 'userId = :userId', // Add condition to ensure we're updating the right user's report
508-
ExpressionAttributeValues: marshall({
509-
':bookmarked': bookmarked,
510-
':updatedAt': new Date().toISOString(),
511-
':userId': userId,
512-
}),
556+
ExpressionAttributeNames: expressionAttributeNames,
557+
ExpressionAttributeValues: marshall(expressionAttributeValues),
513558
ReturnValues: 'ALL_NEW',
514559
});
515560

0 commit comments

Comments
 (0)