Crear Asunto funciona

This commit is contained in:
2026-03-19 12:41:05 -06:00
parent 7c278bcaf0
commit daf3a524c7
3 changed files with 82 additions and 48 deletions

View File

@@ -589,7 +589,6 @@ async def get_ticket_issues(
async def create_ticket_issue(
ticket_id: str,
issue: IssueCreate,
file: Optional[UploadFile] = File(None),
db: AsyncSession = Depends(get_db),
current_user: User = Depends(get_current_user),
current_tenant: Tenant = Depends(get_current_tenant),
@@ -633,21 +632,11 @@ async def create_ticket_issue(
detail=f"Usuarios no encontrados: {missing}",
)
attachment_path = None
attachment_filename = None
attachment_mime_type = None
if file and file.filename:
file_metadata = await file_handler.save_upload(file, current_tenant.id, ticket_uuid)
attachment_path = file_metadata["file_path"]
attachment_filename = file_metadata["original_filename"]
attachment_mime_type = file_metadata["mime_type"]
new_issue = TicketIssue(
id=uuid.uuid4(), ticket_id=ticket_uuid,
tenant_id=current_user.tenant_id, created_by=current_user.id,
tenant_id=ticket_obj.tenant_id, created_by=current_user.id,
content=issue.content, priority=TicketPriority[issue.priority],
attachment_path=attachment_path, attachment_filename=attachment_filename,
attachment_mime_type=attachment_mime_type,
attachment_path=None, attachment_filename=None, attachment_mime_type=None,
created_at=datetime.utcnow(), updated_at=datetime.utcnow(),
)
new_issue.tagged_users = tagged_users
@@ -677,4 +666,47 @@ async def create_ticket_issue(
attachment_filename=new_issue.attachment_filename,
attachment_mime_type=new_issue.attachment_mime_type,
created_at=new_issue.created_at, updated_at=new_issue.updated_at,
)
)
@router.post("/{ticket_id}/issues/{issue_id}/attachment", status_code=status.HTTP_200_OK)
async def upload_issue_attachment(
ticket_id: str,
issue_id: str,
file: UploadFile = File(...),
db: AsyncSession = Depends(get_db),
current_user: User = Depends(get_current_user),
current_tenant: Tenant = Depends(get_current_tenant),
):
"""Subir adjunto a un asunto existente."""
ticket_uuid = validate_uuid_param(ticket_id, "ticket ID")
issue_uuid = validate_uuid_param(issue_id, "issue ID")
result = await db.execute(
select(TicketIssue).where(
TicketIssue.id == issue_uuid,
TicketIssue.ticket_id == ticket_uuid,
)
)
db_issue = result.scalars().first()
if not db_issue:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Asunto no encontrado")
if db_issue.created_by != current_user.id and not current_user.role.is_global:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Sin permisos")
upload_tenant_id = db_issue.tenant_id
file_metadata = await file_handler.save_upload(file, upload_tenant_id, ticket_uuid)
db_issue.attachment_path = file_metadata["file_path"]
db_issue.attachment_filename = file_metadata["original_filename"]
db_issue.attachment_mime_type = file_metadata["mime_type"]
db_issue.updated_at = datetime.utcnow()
await db.commit()
return {
"message": "Adjunto subido correctamente",
"attachment_filename": db_issue.attachment_filename,
"attachment_mime_type": db_issue.attachment_mime_type,
}

View File

@@ -1,7 +1,7 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte';
import { api } from '$lib/utils/api';
import { toast } from '$lib/stores/toast';
import { api } from '$lib/utils/api';
import { createEventDispatcher } from 'svelte';
export let ticketId: string;
export let users: any[] = [];
@@ -43,20 +43,18 @@
isSubmitting = true;
try {
// Si hay archivo usamos FormData, si no JSON normal
// 1. Crear el issue con JSON
const newIssue = await api.post(`/tickets/${ticketId}/issues`, {
content: content.trim(),
priority,
tagged_user_ids: taggedUserIds
});
// 2. Si hay archivo, subirlo en request separado
if (file) {
const formData = new FormData();
formData.append('content', content.trim());
formData.append('priority', priority);
taggedUserIds.forEach(id => formData.append('tagged_user_ids', id));
formData.append('file', file);
await api.postForm(`/tickets/${ticketId}/issues`, formData);
} else {
await api.post(`/tickets/${ticketId}/issues`, {
content: content.trim(),
priority,
tagged_user_ids: taggedUserIds
});
await api.postForm(`/tickets/${ticketId}/issues/${newIssue.id}/attachment`, formData);
}
toast.success('Asunto creado correctamente');
@@ -83,7 +81,7 @@
on:click={handleClose}
role="button"
tabindex="-1"
on:keydown={(e) => e.key === 'Escape' && handleClose()}
on:keydown={e => e.key === 'Escape' && handleClose()}
/>
<!-- Modal -->
@@ -91,21 +89,21 @@
<!-- Header -->
<div class="flex items-center justify-between px-6 py-4 border-b border-gray-200">
<h3 class="text-lg font-semibold text-gray-900">Crear Asunto</h3>
<button
type="button"
on:click={handleClose}
class="text-gray-400 hover:text-gray-500"
>
<button type="button" on:click={handleClose} class="text-gray-400 hover:text-gray-500">
<span class="sr-only">Cerrar</span>
<svg class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</button>
</div>
<!-- Body -->
<div class="px-6 py-5 space-y-5">
<!-- Contenido -->
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">
@@ -141,10 +139,10 @@
<!-- Usuarios a etiquetar -->
{#if users.length > 0}
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">
Etiquetar usuarios
</label>
<div class="max-h-40 overflow-y-auto border border-gray-200 rounded-md divide-y divide-gray-100">
<label class="block text-sm font-medium text-gray-700 mb-2"> Etiquetar usuarios </label>
<div
class="max-h-40 overflow-y-auto border border-gray-200 rounded-md divide-y divide-gray-100"
>
{#each users as user}
<label class="flex items-center gap-3 px-3 py-2 hover:bg-gray-50 cursor-pointer">
<input
@@ -157,7 +155,8 @@
/>
<div class="flex flex-col">
<span class="text-sm font-medium text-gray-900">
{user.first_name} {user.last_name}
{user.first_name}
{user.last_name}
</span>
<span class="text-xs text-gray-500">{user.email}</span>
</div>
@@ -166,7 +165,10 @@
</div>
{#if taggedUserIds.length > 0}
<p class="text-xs text-blue-600 mt-1">
{taggedUserIds.length} usuario{taggedUserIds.length > 1 ? 's' : ''} seleccionado{taggedUserIds.length > 1 ? 's' : ''}
{taggedUserIds.length} usuario{taggedUserIds.length > 1 ? 's' : ''} seleccionado{taggedUserIds.length >
1
? 's'
: ''}
</p>
{/if}
</div>
@@ -174,9 +176,7 @@
<!-- Adjunto -->
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">
Adjunto (opcional)
</label>
<label class="block text-sm font-medium text-gray-700 mb-1"> Adjunto (opcional) </label>
<input
bind:this={fileInput}
type="file"
@@ -227,4 +227,4 @@
</div>
</div>
</div>
</div>
</div>

View File

@@ -73,9 +73,11 @@
isSubmittingComment = true;
try {
const comment = await api.post(`/tickets/${ticketId}/comments`, {
content: newComment.trim(),
is_internal: false
const comment = await api.post(`/tickets/${ticketId}/issues`, {
content: content.trim(),
priority,
tagged_user_ids: taggedUserIds
});
comments = [...comments, comment];
newComment = '';