Add calendar sync utilities and Beteiligte sync cron job
- Implemented calendar_sync_utils.py for shared utility functions including DB connection, Google Calendar service initialization, Redis client setup, and employee sync operations. - Created beteiligte_sync_cron_step.py to handle periodic sync of Beteiligte entities, checking for new, modified, failed, and stale records, and emitting sync events accordingly.
This commit is contained in:
112
steps/advoware_cal_sync/calendar_sync_api_step.py
Normal file
112
steps/advoware_cal_sync/calendar_sync_api_step.py
Normal file
@@ -0,0 +1,112 @@
|
||||
"""
|
||||
Calendar Sync API Step
|
||||
|
||||
HTTP API endpoint for manual calendar sync triggering.
|
||||
Supports syncing a single employee or all employees.
|
||||
"""
|
||||
import sys
|
||||
from pathlib import Path
|
||||
sys.path.insert(0, str(Path(__file__).parent))
|
||||
from calendar_sync_utils import get_redis_client, set_employee_lock, log_operation
|
||||
|
||||
from motia import http, ApiRequest, ApiResponse, FlowContext
|
||||
|
||||
|
||||
config = {
|
||||
'name': 'Calendar Sync API Trigger',
|
||||
'description': 'API endpoint for manual calendar sync triggering',
|
||||
'flows': ['advoware'],
|
||||
'triggers': [
|
||||
http('POST', '/advoware/calendar/sync')
|
||||
],
|
||||
'enqueues': ['calendar_sync_employee', 'calendar_sync_all']
|
||||
}
|
||||
|
||||
|
||||
async def handler(request: ApiRequest, ctx: FlowContext) -> ApiResponse:
|
||||
"""
|
||||
HTTP handler for manual calendar sync triggering.
|
||||
|
||||
Request body:
|
||||
{
|
||||
"kuerzel": "SB" // or "ALL" for all employees
|
||||
}
|
||||
"""
|
||||
try:
|
||||
# Get kuerzel from request body
|
||||
body = request.body
|
||||
kuerzel = body.get('kuerzel')
|
||||
if not kuerzel:
|
||||
return ApiResponse(
|
||||
status=400,
|
||||
body={
|
||||
'error': 'kuerzel required',
|
||||
'message': 'Bitte kuerzel im Body angeben'
|
||||
}
|
||||
)
|
||||
|
||||
kuerzel_upper = kuerzel.upper()
|
||||
|
||||
if kuerzel_upper == 'ALL':
|
||||
# Emit sync-all event
|
||||
log_operation('info', "Calendar Sync API: Emitting sync-all event", context=ctx)
|
||||
await ctx.enqueue({
|
||||
"topic": "calendar_sync_all",
|
||||
"data": {
|
||||
"triggered_by": "api"
|
||||
}
|
||||
})
|
||||
return ApiResponse(
|
||||
status=200,
|
||||
body={
|
||||
'status': 'triggered',
|
||||
'message': 'Calendar sync wurde für alle Mitarbeiter ausgelöst',
|
||||
'triggered_by': 'api'
|
||||
}
|
||||
)
|
||||
else:
|
||||
# Single employee sync
|
||||
redis_client = get_redis_client(ctx)
|
||||
|
||||
if not set_employee_lock(redis_client, kuerzel_upper, 'api', ctx):
|
||||
log_operation('info', f"Calendar Sync API: Sync already active for {kuerzel_upper}, skipping", context=ctx)
|
||||
return ApiResponse(
|
||||
status=409,
|
||||
body={
|
||||
'status': 'conflict',
|
||||
'message': f'Calendar sync already active for {kuerzel_upper}',
|
||||
'kuerzel': kuerzel_upper,
|
||||
'triggered_by': 'api'
|
||||
}
|
||||
)
|
||||
|
||||
log_operation('info', f"Calendar Sync API called for {kuerzel_upper}", context=ctx)
|
||||
|
||||
# Lock successfully set, now emit event
|
||||
await ctx.enqueue({
|
||||
"topic": "calendar_sync_employee",
|
||||
"data": {
|
||||
"kuerzel": kuerzel_upper,
|
||||
"triggered_by": "api"
|
||||
}
|
||||
})
|
||||
|
||||
return ApiResponse(
|
||||
status=200,
|
||||
body={
|
||||
'status': 'triggered',
|
||||
'message': f'Calendar sync was triggered for {kuerzel_upper}',
|
||||
'kuerzel': kuerzel_upper,
|
||||
'triggered_by': 'api'
|
||||
}
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
log_operation('error', f"Error in API trigger: {e}", context=ctx)
|
||||
return ApiResponse(
|
||||
status=500,
|
||||
body={
|
||||
'error': 'Internal server error',
|
||||
'details': str(e)
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user