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,56 +358,66 @@ 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) => {
"documentId": e['documentId'],
"fileName": e['fileName'],
"contentType": e['contentType'],
"fileSize": 0,
"description": "",
"url": e['url'],
"isActive": e['isActive'] ?? true,
"base64Data": e['isActive'] == false ? null : e['base64Data'],
})
.toList();
final newAttachmentPayloads = // Determine if attachments were changed
await Future.wait(attachments.map((file) async { bool attachmentsChanged =
final bytes = await file.readAsBytes(); attachments.isNotEmpty || existingAttachments.any((e) => e['isActive'] == false);
return {
"fileName": file.path.split('/').last,
"base64Data": base64Encode(bytes),
"contentType": lookupMimeType(file.path) ?? 'application/octet-stream',
"fileSize": await file.length(),
"description": "",
};
}));
final type = selectedExpenseType.value!; // Existing attachments payload
return { final existingAttachmentPayloads = attachmentsChanged
if (isEditMode.value && editingExpenseId != null) "id": editingExpenseId, ? existingAttachments.map((e) => {
"projectId": projectsMap[selectedProject.value]!, "documentId": e['documentId'],
"expensesTypeId": type.id, "fileName": e['fileName'],
"paymentModeId": selectedPaymentMode.value!.id, "contentType": e['contentType'],
"paidById": selectedPaidBy.value!.id, "fileSize": 0,
"transactionDate": (selectedTransactionDate.value?.toUtc() ?? now.toUtc()) "description": "",
.toIso8601String(), "url": e['url'],
"transactionId": transactionIdController.text, "isActive": e['isActive'] ?? true,
"description": descriptionController.text, // If attachment removed, base64Data should be empty array
"location": locationController.text, "base64Data": e['isActive'] == false ? "" : e['base64Data'],
"supplerName": supplierController.text, }).toList()
"amount": double.parse(amountController.text.trim()), : [];
"noOfPersons": type.noOfPersonsRequired == true
? int.tryParse(noOfPersonsController.text.trim()) ?? 0 // New attachments payload
: 0, final newAttachmentPayloads = attachmentsChanged
"billAttachments": [ ? await Future.wait(attachments.map((file) async {
...existingAttachmentPayloads, final bytes = await file.readAsBytes();
...newAttachmentPayloads return {
], "fileName": file.path.split('/').last,
}; "base64Data": base64Encode(bytes),
} "contentType": lookupMimeType(file.path) ?? 'application/octet-stream',
"fileSize": await file.length(),
"description": "",
};
}))
: [];
final type = selectedExpenseType.value!;
return {
if (isEditMode.value && editingExpenseId != null) "id": editingExpenseId,
"projectId": projectsMap[selectedProject.value]!,
"expensesTypeId": type.id,
"paymentModeId": selectedPaymentMode.value!.id,
"paidById": selectedPaidBy.value!.id,
"transactionDate": (selectedTransactionDate.value?.toUtc() ?? now.toUtc())
.toIso8601String(),
"transactionId": transactionIdController.text,
"description": descriptionController.text,
"location": locationController.text,
"supplerName": supplierController.text,
"amount": double.parse(amountController.text.trim()),
"noOfPersons": type.noOfPersonsRequired == true
? int.tryParse(noOfPersonsController.text.trim()) ?? 0
: 0,
// Attachments logic
"billAttachments": isEditMode.value && !attachmentsChanged
? null
: [...existingAttachmentPayloads, ...newAttachmentPayloads],
};
}
String validateForm() { String validateForm() {
final missing = <String>[]; final missing = <String>[];