Files
TimeTracker/app/models/push_subscription.py
Dries Peeters 92893b188d fix: complete backend implementations and integration improvements
This commit addresses multiple incomplete implementations identified in the
codebase analysis, focusing on security, functionality, and error handling.

Backend Fixes:
- Issues module: Implement proper permission filtering for non-admin users
  - Users can only see issues for projects they have access to
  - Added permission checks to view_issue and edit_issue routes
  - Statistics now respect user permissions

- Push notifications: Implement proper subscription storage
  - Created PushSubscription model for browser push notification subscriptions
  - Updated routes to use new model with proper CRUD operations
  - Added support for multiple subscriptions per user
  - Added endpoint to list user subscriptions

Integration Improvements:
- GitHub: Implement webhook signature verification
  - Added HMAC SHA-256 signature verification using webhook secret
  - Uses constant-time comparison to prevent timing attacks
  - Added webhook_secret field to config schema

- QuickBooks: Implement customer and account mapping
  - Added support for customer mappings (client → QuickBooks customer)
  - Added support for item mappings (invoice items → QuickBooks items)
  - Added support for account mappings (expense categories → accounts)
  - Added default expense account configuration
  - Improved error handling and logging

- Xero: Add customer and account mapping support
  - Added contact mappings (client → Xero Contact ID)
  - Added item mappings (invoice items → Xero item codes)
  - Added account mappings (expense categories → Xero account codes)
  - Added default expense account configuration

- CalDAV: Implement bidirectional sync
  - Added TimeTracker to Calendar sync direction
  - Implemented iCalendar event generation from time entries
  - Added create_or_update_event method to CalDAVClient
  - Supports bidirectional sync (both directions simultaneously)
  - Improved error handling for event creation/updates

- Trello: Implement bidirectional sync
  - Added TimeTracker to Trello sync direction
  - Implemented task to card creation and updates
  - Automatic board creation for projects if needed
  - Maps task status to Trello lists
  - Supports bidirectional sync

- Exception handling: Improve error logging in integrations
  - Replaced silent pass statements with proper error logging
  - Added debug logging for non-critical failures (user info fetch)
  - Improved error visibility for debugging
  - Affected: Google Calendar, Outlook Calendar, Microsoft Teams, Asana, GitLab

All changes include proper error handling, logging, and follow existing code
patterns. Database migration required for push_subscriptions table.
2025-12-29 12:31:52 +01:00

71 lines
2.7 KiB
Python

"""
Push Subscription model for storing browser push notification subscriptions.
"""
from datetime import datetime
from app import db
from app.utils.timezone import now_in_app_timezone
import json
class PushSubscription(db.Model):
"""Model for storing browser push notification subscriptions"""
__tablename__ = "push_subscriptions"
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True)
# Push subscription data (JSON format from browser Push API)
endpoint = db.Column(db.Text, nullable=False) # Push service endpoint URL
keys = db.Column(db.JSON, nullable=False) # p256dh and auth keys
# Metadata
user_agent = db.Column(db.String(500), nullable=True) # Browser user agent
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
last_used_at = db.Column(db.DateTime, nullable=True) # Last time subscription was used
# Relationships
user = db.relationship("User", backref="push_subscriptions", lazy="joined")
def __init__(self, user_id, endpoint, keys, user_agent=None):
"""Create a push subscription"""
self.user_id = user_id
self.endpoint = endpoint
self.keys = keys if isinstance(keys, dict) else json.loads(keys) if isinstance(keys, str) else {}
self.user_agent = user_agent
def __repr__(self):
return f"<PushSubscription {self.id} for user {self.user_id}>"
def to_dict(self):
"""Convert subscription to dictionary for API responses"""
return {
"id": self.id,
"user_id": self.user_id,
"endpoint": self.endpoint,
"keys": self.keys,
"user_agent": self.user_agent,
"created_at": self.created_at.isoformat() if self.created_at else None,
"updated_at": self.updated_at.isoformat() if self.updated_at else None,
"last_used_at": self.last_used_at.isoformat() if self.last_used_at else None,
}
def update_last_used(self):
"""Update the last_used_at timestamp"""
self.last_used_at = now_in_app_timezone()
self.updated_at = now_in_app_timezone()
db.session.commit()
@classmethod
def get_user_subscriptions(cls, user_id):
"""Get all active subscriptions for a user"""
return cls.query.filter_by(user_id=user_id).order_by(cls.created_at.desc()).all()
@classmethod
def find_by_endpoint(cls, user_id, endpoint):
"""Find a subscription by user and endpoint"""
return cls.query.filter_by(user_id=user_id, endpoint=endpoint).first()