"""Career domain write tools: track and update applications."""
import logging
from datetime import datetime
import uuid
from ...models import Application, JobListing
from ...utils.storage import get_db_connection
logger = logging.getLogger(__name__)
async def track_application(
job_url: str,
company: str,
title: str,
status: str,
notes: str = ""
) -> Application:
"""
Track a new job application.
Args:
job_url: URL to the job posting
company: Company name
title: Job title
status: Application status
notes: Optional notes about the application
Returns:
Created application record
"""
logger.info(f"Tracking new application: {company} - {title}")
app_id = str(uuid.uuid4())
applied_date = datetime.now()
try:
async with get_db_connection() as db:
await db.execute(
"""
INSERT INTO applications
(id, job_url, company, title, applied_date, status, notes)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(app_id, job_url, company, title, applied_date.isoformat(), status, notes)
)
await db.commit()
job = JobListing(
title=title,
url=job_url,
company=company,
location=None,
posted_date=None,
source="tracked"
)
application = Application(
id=app_id,
job=job,
applied_date=applied_date,
status=status,
notes=notes,
next_action=None
)
logger.info(f"Application tracked successfully: {app_id}")
return application
except Exception as e:
logger.error(f"Error tracking application: {e}", exc_info=True)
raise
async def update_application_status(
app_id: str,
status: str,
notes: str = ""
) -> Application:
"""
Update an existing job application status.
Args:
app_id: Application ID to update
status: New application status
notes: Additional notes to append
Returns:
Updated application record
"""
logger.info(f"Updating application {app_id} to status: {status}")
try:
async with get_db_connection() as db:
# Get existing application
cursor = await db.execute(
"""
SELECT job_url, company, title, location, applied_date, notes
FROM applications
WHERE id = ?
""",
(app_id,)
)
row = await cursor.fetchone()
if not row:
raise ValueError(f"Application not found: {app_id}")
# Append new notes to existing notes
existing_notes = row[5] or ""
updated_notes = f"{existing_notes}\n\n{notes}" if existing_notes else notes
# Update status and notes
await db.execute(
"""
UPDATE applications
SET status = ?, notes = ?
WHERE id = ?
""",
(status, updated_notes, app_id)
)
await db.commit()
job = JobListing(
title=row[2],
url=row[0],
company=row[1],
location=row[3],
posted_date=None,
source="tracked"
)
application = Application(
id=app_id,
job=job,
applied_date=datetime.fromisoformat(row[4]),
status=status,
notes=updated_notes,
next_action=None
)
logger.info(f"Application updated successfully: {app_id}")
return application
except Exception as e:
logger.error(f"Error updating application: {e}", exc_info=True)
raise