52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
import uuid
|
|
from repositories.pocketbase_client import pb_client
|
|
from repositories.repo_config import USE_MOCK_REPOSITORIES
|
|
|
|
COLLECTION = 'comparable_matches'
|
|
|
|
|
|
class ComparableRepository:
|
|
"""Data access for the `comparable_matches` collection (Phase 6)."""
|
|
|
|
def create(self, data):
|
|
return pb_client.create(COLLECTION, data)
|
|
|
|
def list_for_tenant(self, tenant_id, include_dismissed=False):
|
|
filter_str = f'tenant_id = "{tenant_id}"'
|
|
if not include_dismissed:
|
|
filter_str += ' && dismissed = false'
|
|
return pb_client.list(COLLECTION, filter_str=filter_str, sort='-similarity_score', per_page=200)
|
|
|
|
def dismiss(self, match_id):
|
|
return pb_client.update(COLLECTION, match_id, {'dismissed': True})
|
|
|
|
|
|
class MockComparableRepository:
|
|
"""In-memory stand-in for ComparableRepository."""
|
|
|
|
def __init__(self):
|
|
self._records = {}
|
|
|
|
def create(self, data):
|
|
record_id = data.get('id') or str(uuid.uuid4())
|
|
record = {'id': record_id, 'dismissed': False, **data}
|
|
self._records[record_id] = record
|
|
return record
|
|
|
|
def list_for_tenant(self, tenant_id, include_dismissed=False):
|
|
items = [
|
|
r for r in self._records.values()
|
|
if r.get('tenant_id') == tenant_id and (include_dismissed or not r.get('dismissed'))
|
|
]
|
|
items.sort(key=lambda r: r.get('similarity_score') or 0, reverse=True)
|
|
return items
|
|
|
|
def dismiss(self, match_id):
|
|
if match_id not in self._records:
|
|
return None
|
|
self._records[match_id]['dismissed'] = True
|
|
return self._records[match_id]
|
|
|
|
|
|
comparable_repository = MockComparableRepository() if USE_MOCK_REPOSITORIES else ComparableRepository()
|