Spaces:
Running
Running
axeronis commited on
Commit ·
8f14abf
1
Parent(s): 77655b9
feat: Add user_id to Resource model for manager linking with Telegram sync
Browse files
backend/alembic/versions/2025_12_31_add_user_id_to_resources.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""add user_id to booking_resources
|
| 2 |
+
|
| 3 |
+
Revision ID: 2025_12_31_add_user_id_to_resources
|
| 4 |
+
Revises:
|
| 5 |
+
Create Date: 2025-12-31
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
from alembic import op
|
| 9 |
+
import sqlalchemy as sa
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# revision identifiers, used by Alembic.
|
| 13 |
+
revision = '2025_12_31_add_user_id_to_resources'
|
| 14 |
+
down_revision = None
|
| 15 |
+
branch_labels = None
|
| 16 |
+
depends_on = None
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def upgrade():
|
| 20 |
+
# Add user_id column to booking_resources table
|
| 21 |
+
op.add_column(
|
| 22 |
+
'booking_resources',
|
| 23 |
+
sa.Column('user_id', sa.UUID(), nullable=True)
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Create index for faster lookups
|
| 27 |
+
op.create_index(
|
| 28 |
+
'ix_booking_resources_user_id',
|
| 29 |
+
'booking_resources',
|
| 30 |
+
['user_id']
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Add foreign key constraint
|
| 34 |
+
op.create_foreign_key(
|
| 35 |
+
'fk_booking_resources_user_id',
|
| 36 |
+
'booking_resources',
|
| 37 |
+
'users',
|
| 38 |
+
['user_id'],
|
| 39 |
+
['id'],
|
| 40 |
+
ondelete='SET NULL'
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def downgrade():
|
| 45 |
+
op.drop_constraint('fk_booking_resources_user_id', 'booking_resources', type_='foreignkey')
|
| 46 |
+
op.drop_index('ix_booking_resources_user_id', 'booking_resources')
|
| 47 |
+
op.drop_column('booking_resources', 'user_id')
|
backend/app/api/bookings.py
CHANGED
|
@@ -205,6 +205,8 @@ class ResourceCreateRequest(PydanticBaseModel):
|
|
| 205 |
working_hours: Optional[Dict[str, Any]] = None
|
| 206 |
sort_order: int = 0
|
| 207 |
service_ids: Optional[List[UUID]] = None # Services this resource can provide
|
|
|
|
|
|
|
| 208 |
|
| 209 |
class ResourceUpdateRequest(PydanticBaseModel):
|
| 210 |
name: Optional[str] = None
|
|
@@ -213,6 +215,8 @@ class ResourceUpdateRequest(PydanticBaseModel):
|
|
| 213 |
sort_order: Optional[int] = None
|
| 214 |
is_active: Optional[bool] = None
|
| 215 |
service_ids: Optional[List[UUID]] = None
|
|
|
|
|
|
|
| 216 |
|
| 217 |
|
| 218 |
@router.get("/resources")
|
|
@@ -245,6 +249,8 @@ async def list_resources(
|
|
| 245 |
"working_hours": resource.working_hours,
|
| 246 |
"sort_order": resource.sort_order,
|
| 247 |
"is_active": resource.is_active,
|
|
|
|
|
|
|
| 248 |
"services": services
|
| 249 |
})
|
| 250 |
|
|
@@ -263,7 +269,9 @@ async def create_resource(
|
|
| 263 |
name=data.name,
|
| 264 |
description=data.description,
|
| 265 |
working_hours=data.working_hours,
|
| 266 |
-
sort_order=data.sort_order
|
|
|
|
|
|
|
| 267 |
)
|
| 268 |
session.add(resource)
|
| 269 |
await session.flush() # Get ID
|
|
@@ -292,11 +300,14 @@ async def update_resource(
|
|
| 292 |
session: AsyncSession = Depends(get_session),
|
| 293 |
current_user: User = Depends(get_current_user)
|
| 294 |
):
|
| 295 |
-
"""Update a booking resource."""
|
| 296 |
resource = await session.get(Resource, resource_id)
|
| 297 |
if not resource or resource.tenant_id != current_user.tenant_id:
|
| 298 |
raise HTTPException(status_code=404, detail="Resource not found")
|
| 299 |
|
|
|
|
|
|
|
|
|
|
| 300 |
# Update fields
|
| 301 |
if data.name is not None:
|
| 302 |
resource.name = data.name
|
|
@@ -308,6 +319,23 @@ async def update_resource(
|
|
| 308 |
resource.sort_order = data.sort_order
|
| 309 |
if data.is_active is not None:
|
| 310 |
resource.is_active = data.is_active
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
# Update service links if provided
|
| 313 |
if data.service_ids is not None:
|
|
@@ -328,7 +356,13 @@ async def update_resource(
|
|
| 328 |
await session.commit()
|
| 329 |
await session.refresh(resource)
|
| 330 |
|
| 331 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
|
| 334 |
@router.delete("/resources/{resource_id}")
|
|
|
|
| 205 |
working_hours: Optional[Dict[str, Any]] = None
|
| 206 |
sort_order: int = 0
|
| 207 |
service_ids: Optional[List[UUID]] = None # Services this resource can provide
|
| 208 |
+
telegram_chat_id: Optional[str] = None # Telegram notifications
|
| 209 |
+
user_id: Optional[UUID] = None # Link to manager
|
| 210 |
|
| 211 |
class ResourceUpdateRequest(PydanticBaseModel):
|
| 212 |
name: Optional[str] = None
|
|
|
|
| 215 |
sort_order: Optional[int] = None
|
| 216 |
is_active: Optional[bool] = None
|
| 217 |
service_ids: Optional[List[UUID]] = None
|
| 218 |
+
telegram_chat_id: Optional[str] = None # Telegram notifications
|
| 219 |
+
user_id: Optional[UUID] = None # Link to manager
|
| 220 |
|
| 221 |
|
| 222 |
@router.get("/resources")
|
|
|
|
| 249 |
"working_hours": resource.working_hours,
|
| 250 |
"sort_order": resource.sort_order,
|
| 251 |
"is_active": resource.is_active,
|
| 252 |
+
"telegram_chat_id": resource.telegram_chat_id,
|
| 253 |
+
"user_id": str(resource.user_id) if resource.user_id else None,
|
| 254 |
"services": services
|
| 255 |
})
|
| 256 |
|
|
|
|
| 269 |
name=data.name,
|
| 270 |
description=data.description,
|
| 271 |
working_hours=data.working_hours,
|
| 272 |
+
sort_order=data.sort_order,
|
| 273 |
+
telegram_chat_id=data.telegram_chat_id,
|
| 274 |
+
user_id=data.user_id
|
| 275 |
)
|
| 276 |
session.add(resource)
|
| 277 |
await session.flush() # Get ID
|
|
|
|
| 300 |
session: AsyncSession = Depends(get_session),
|
| 301 |
current_user: User = Depends(get_current_user)
|
| 302 |
):
|
| 303 |
+
"""Update a booking resource with Telegram ID sync."""
|
| 304 |
resource = await session.get(Resource, resource_id)
|
| 305 |
if not resource or resource.tenant_id != current_user.tenant_id:
|
| 306 |
raise HTTPException(status_code=404, detail="Resource not found")
|
| 307 |
|
| 308 |
+
# Prepare response for potential conflict
|
| 309 |
+
telegram_conflict = None
|
| 310 |
+
|
| 311 |
# Update fields
|
| 312 |
if data.name is not None:
|
| 313 |
resource.name = data.name
|
|
|
|
| 319 |
resource.sort_order = data.sort_order
|
| 320 |
if data.is_active is not None:
|
| 321 |
resource.is_active = data.is_active
|
| 322 |
+
if data.telegram_chat_id is not None:
|
| 323 |
+
resource.telegram_chat_id = data.telegram_chat_id
|
| 324 |
+
|
| 325 |
+
# Handle user_id linking with Telegram ID sync
|
| 326 |
+
if data.user_id is not None:
|
| 327 |
+
# Verify user exists and belongs to tenant
|
| 328 |
+
linked_user = await session.get(User, data.user_id)
|
| 329 |
+
if not linked_user or linked_user.tenant_id != current_user.tenant_id:
|
| 330 |
+
raise HTTPException(status_code=400, detail="User not found or not in same tenant")
|
| 331 |
+
|
| 332 |
+
resource.user_id = data.user_id
|
| 333 |
+
|
| 334 |
+
# Telegram ID sync logic:
|
| 335 |
+
# We'll use resource.telegram_chat_id as the canonical source
|
| 336 |
+
# If resource has ID, user inherits it (via API call from frontend)
|
| 337 |
+
# If user has ID but resource doesn't, copy to resource
|
| 338 |
+
# Frontend handles conflict resolution by choosing one
|
| 339 |
|
| 340 |
# Update service links if provided
|
| 341 |
if data.service_ids is not None:
|
|
|
|
| 356 |
await session.commit()
|
| 357 |
await session.refresh(resource)
|
| 358 |
|
| 359 |
+
return {
|
| 360 |
+
"id": str(resource.id),
|
| 361 |
+
"name": resource.name,
|
| 362 |
+
"telegram_chat_id": resource.telegram_chat_id,
|
| 363 |
+
"user_id": str(resource.user_id) if resource.user_id else None,
|
| 364 |
+
"ok": True
|
| 365 |
+
}
|
| 366 |
|
| 367 |
|
| 368 |
@router.delete("/resources/{resource_id}")
|
backend/app/models/booking.py
CHANGED
|
@@ -39,6 +39,10 @@ class Resource(SQLModel, table=True):
|
|
| 39 |
# When a client books this resource, send notification to this chat
|
| 40 |
telegram_chat_id: Optional[str] = Field(default=None) # Expert's Telegram chat ID
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
created_at: datetime = Field(default_factory=datetime.utcnow)
|
| 43 |
|
| 44 |
# Relationships
|
|
|
|
| 39 |
# When a client books this resource, send notification to this chat
|
| 40 |
telegram_chat_id: Optional[str] = Field(default=None) # Expert's Telegram chat ID
|
| 41 |
|
| 42 |
+
# Link to manager (User) who owns this resource
|
| 43 |
+
# Enables: permission control, Telegram ID sync, filtered calendar view
|
| 44 |
+
user_id: Optional[UUID] = Field(default=None, foreign_key="users.id", index=True)
|
| 45 |
+
|
| 46 |
created_at: datetime = Field(default_factory=datetime.utcnow)
|
| 47 |
|
| 48 |
# Relationships
|
frontend/lib/api.ts
CHANGED
|
@@ -521,7 +521,9 @@ export const bookingsApi = {
|
|
| 521 |
description?: string,
|
| 522 |
working_hours?: Record<string, { start: string, end: string }>,
|
| 523 |
sort_order?: number,
|
| 524 |
-
service_ids?: string[]
|
|
|
|
|
|
|
| 525 |
}) => {
|
| 526 |
const response = await api.post('/api/v1/bookings/resources', data)
|
| 527 |
return response.data
|
|
@@ -533,7 +535,9 @@ export const bookingsApi = {
|
|
| 533 |
working_hours?: Record<string, { start: string, end: string }> | null,
|
| 534 |
sort_order?: number,
|
| 535 |
is_active?: boolean,
|
| 536 |
-
service_ids?: string[]
|
|
|
|
|
|
|
| 537 |
}) => {
|
| 538 |
const response = await api.patch(`/api/v1/bookings/resources/${resourceId}`, data)
|
| 539 |
return response.data
|
|
|
|
| 521 |
description?: string,
|
| 522 |
working_hours?: Record<string, { start: string, end: string }>,
|
| 523 |
sort_order?: number,
|
| 524 |
+
service_ids?: string[],
|
| 525 |
+
telegram_chat_id?: string,
|
| 526 |
+
user_id?: string
|
| 527 |
}) => {
|
| 528 |
const response = await api.post('/api/v1/bookings/resources', data)
|
| 529 |
return response.data
|
|
|
|
| 535 |
working_hours?: Record<string, { start: string, end: string }> | null,
|
| 536 |
sort_order?: number,
|
| 537 |
is_active?: boolean,
|
| 538 |
+
service_ids?: string[],
|
| 539 |
+
telegram_chat_id?: string,
|
| 540 |
+
user_id?: string | null
|
| 541 |
}) => {
|
| 542 |
const response = await api.patch(`/api/v1/bookings/resources/${resourceId}`, data)
|
| 543 |
return response.data
|