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