Fix send to client: use primary submission, add error handling

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Krao Hasanee
2026-03-27 00:41:15 -04:00
parent 33c2ad8460
commit 71b34a4700
+16 -16
View File
@@ -105,21 +105,27 @@ export default function TaskDetail() {
const handleSendToClient = async (e) => {
e.preventDefault();
setSaving(true);
const latestSub = submissions[submissions.length - 1];
if (!latestSub) return;
try {
// Use the primary (non-amendment) submission for the current version
const currentVersion = (task.current_version || 0) + 1;
const primarySub = submissions.find(s => s.version_number === currentVersion && s.type !== 'amendment')
|| submissions.filter(s => s.type !== 'amendment').pop();
if (!primarySub) { setSaving(false); return; }
const uploadedFiles = [];
for (const file of sendForm.files) {
const path = `${id}/${Date.now()}_${file.name}`;
const { data: uploaded } = await supabase.storage.from('deliveries').upload(path, file);
const { data: uploaded, error } = await supabase.storage.from('deliveries').upload(path, file);
if (error) throw new Error(`Upload failed: ${error.message}`);
if (uploaded) uploadedFiles.push({ name: file.name, storage_path: path, size: file.size });
}
const { data: delivery } = await supabase.from('deliveries').insert({
submission_id: latestSub.id,
const { data: delivery, error: deliveryError } = await supabase.from('deliveries').insert({
submission_id: primarySub.id,
sent_by: currentUser?.name,
message: sendForm.message,
}).select().single();
if (deliveryError) throw new Error(`Delivery failed: ${deliveryError.message}`);
if (delivery && uploadedFiles.length > 0) {
await supabase.from('delivery_files').insert(
@@ -137,20 +143,14 @@ export default function TaskDetail() {
.order('version_number');
setSubmissions(subs || []);
if (company?.email) {
sendEmail('sent_to_client', company.email, {
clientFirstName: company.name,
serviceType: task.title,
projectName: project?.name,
message: sendForm.message,
taskId: id,
});
}
setShowSendForm(false);
setSendForm({ files: [], message: '' });
setNotification(`✓ Sent to client — ${company?.name} has been notified with ${uploadedFiles.length} file${uploadedFiles.length !== 1 ? 's' : ''}.`);
setNotification(`✓ Sent to client — ${uploadedFiles.length} file${uploadedFiles.length !== 1 ? 's' : ''} delivered.`);
} catch (err) {
setNotification(`✗ Error: ${err.message}`);
} finally {
setSaving(false);
}
};
const getFileUrl = async (path) => {