app.bettersight.io/backend/repositories/client_trips_repository.py

59 lines
2.0 KiB
Python

import uuid
from repositories.pocketbase_client import pb_client
from repositories.repo_config import USE_MOCK_REPOSITORIES
COLLECTION = 'client_trips'
class ClientTripsRepository:
"""
Data access for the `client_trips` collection (Phase 6) — the PM's
own product catalogue, used as the "client" side of semantic
comparable-trip matching against competitor products.
"""
def create(self, data):
return pb_client.create(COLLECTION, data)
def update(self, trip_id, data):
return pb_client.update(COLLECTION, trip_id, data)
def get_by_tenant_and_name(self, tenant_id, trip_name):
"""Used to upsert on repeated /research submissions instead of duplicating rows."""
safe_name = trip_name.replace('"', '\\"')
return pb_client.get_first(COLLECTION, f'tenant_id = "{tenant_id}" && trip_name = "{safe_name}"')
def list_for_tenant(self, tenant_id):
return pb_client.list(COLLECTION, filter_str=f'tenant_id = "{tenant_id}"', per_page=200)
class MockClientTripsRepository:
"""In-memory stand-in for ClientTripsRepository."""
def __init__(self):
self._records = {}
def create(self, data):
record_id = data.get('id') or str(uuid.uuid4())
record = {'id': record_id, **data}
self._records[record_id] = record
return record
def update(self, trip_id, data):
if trip_id not in self._records:
return None
self._records[trip_id] = {**self._records[trip_id], **data}
return self._records[trip_id]
def get_by_tenant_and_name(self, tenant_id, trip_name):
return next((
r for r in self._records.values()
if r.get('tenant_id') == tenant_id and r.get('trip_name') == trip_name
), None)
def list_for_tenant(self, tenant_id):
return [r for r in self._records.values() if r.get('tenant_id') == tenant_id]
client_trips_repository = MockClientTripsRepository() if USE_MOCK_REPOSITORIES else ClientTripsRepository()