Skip to content

Commit c6dfe9f

Browse files
committed
feat: enhance updateTask to support dynamic update and removal of optional fields
1 parent 1d50647 commit c6dfe9f

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

src/services/task-service.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ export const updateTask = async (id: string, updateTaskDto: UpdateTaskDto): Prom
141141
const now = new Date().toISOString();
142142

143143
// Build update expression dynamically
144-
let updateExpression = 'SET title = :title, isComplete = :isComplete, updatedAt = :updatedAt';
144+
const setExpressions: string[] = ['title = :title', 'isComplete = :isComplete', 'updatedAt = :updatedAt'];
145+
const removeExpressions: string[] = [];
145146
const expressionAttributeValues: Record<string, unknown> = {
146147
':title': updateTaskDto.title,
147148
':isComplete': updateTaskDto.isComplete,
@@ -150,22 +151,32 @@ export const updateTask = async (id: string, updateTaskDto: UpdateTaskDto): Prom
150151

151152
// Handle optional detail field
152153
if (updateTaskDto.detail !== undefined) {
153-
updateExpression += ', detail = :detail';
154+
setExpressions.push('detail = :detail');
154155
expressionAttributeValues[':detail'] = updateTaskDto.detail;
155156
} else {
156157
// Remove detail if not present in request
157-
updateExpression += ' REMOVE detail';
158+
removeExpressions.push('detail');
158159
}
159160

160161
// Handle optional dueAt field
161162
if (updateTaskDto.dueAt !== undefined) {
162-
updateExpression += ', dueAt = :dueAt';
163+
setExpressions.push('dueAt = :dueAt');
163164
expressionAttributeValues[':dueAt'] = updateTaskDto.dueAt;
164165
} else {
165166
// Remove dueAt if not present in request
166-
updateExpression += ' REMOVE dueAt';
167+
removeExpressions.push('dueAt');
167168
}
168169

170+
// Construct the update expression with proper syntax
171+
const updateExpressionParts: string[] = [];
172+
if (setExpressions.length > 0) {
173+
updateExpressionParts.push(`SET ${setExpressions.join(', ')}`);
174+
}
175+
if (removeExpressions.length > 0) {
176+
updateExpressionParts.push(`REMOVE ${removeExpressions.join(', ')}`);
177+
}
178+
const updateExpression = updateExpressionParts.join(' ');
179+
169180
const command = new UpdateCommand({
170181
TableName: config.TASKS_TABLE,
171182
Key: {

0 commit comments

Comments
 (0)