feat: Enhance expense payload construction with attachment change detection

This commit is contained in:
Vaibhav Surve 2025-09-11 18:03:14 +05:30
parent bd6f175ca7
commit 5c923bb48b

View File

@ -358,10 +358,16 @@ class AddExpenseController extends GetxController {
}
}
Future<Map<String, dynamic>> _buildExpensePayload() async {
Future<Map<String, dynamic>> _buildExpensePayload() async {
final now = DateTime.now();
final existingAttachmentPayloads = existingAttachments
.map((e) => {
// Determine if attachments were changed
bool attachmentsChanged =
attachments.isNotEmpty || existingAttachments.any((e) => e['isActive'] == false);
// Existing attachments payload
final existingAttachmentPayloads = attachmentsChanged
? existingAttachments.map((e) => {
"documentId": e['documentId'],
"fileName": e['fileName'],
"contentType": e['contentType'],
@ -369,12 +375,14 @@ class AddExpenseController extends GetxController {
"description": "",
"url": e['url'],
"isActive": e['isActive'] ?? true,
"base64Data": e['isActive'] == false ? null : e['base64Data'],
})
.toList();
// If attachment removed, base64Data should be empty array
"base64Data": e['isActive'] == false ? "" : e['base64Data'],
}).toList()
: [];
final newAttachmentPayloads =
await Future.wait(attachments.map((file) async {
// New attachments payload
final newAttachmentPayloads = attachmentsChanged
? await Future.wait(attachments.map((file) async {
final bytes = await file.readAsBytes();
return {
"fileName": file.path.split('/').last,
@ -383,9 +391,11 @@ class AddExpenseController extends GetxController {
"fileSize": await file.length(),
"description": "",
};
}));
}))
: [];
final type = selectedExpenseType.value!;
return {
if (isEditMode.value && editingExpenseId != null) "id": editingExpenseId,
"projectId": projectsMap[selectedProject.value]!,
@ -402,12 +412,12 @@ class AddExpenseController extends GetxController {
"noOfPersons": type.noOfPersonsRequired == true
? int.tryParse(noOfPersonsController.text.trim()) ?? 0
: 0,
"billAttachments": [
...existingAttachmentPayloads,
...newAttachmentPayloads
],
// Attachments logic
"billAttachments": isEditMode.value && !attachmentsChanged
? null
: [...existingAttachmentPayloads, ...newAttachmentPayloads],
};
}
}
String validateForm() {
final missing = <String>[];