fix: sync user↔company in user_companies + fix calendar field names
Some checks are pending
NordaBiz Tests / Unit & Integration Tests (push) Waiting to run
NordaBiz Tests / E2E Tests (Playwright) (push) Blocked by required conditions
NordaBiz Tests / Smoke Tests (Production) (push) Blocked by required conditions
NordaBiz Tests / Send Failure Notification (push) Blocked by required conditions
Some checks are pending
NordaBiz Tests / Unit & Integration Tests (push) Waiting to run
NordaBiz Tests / E2E Tests (Playwright) (push) Blocked by required conditions
NordaBiz Tests / Smoke Tests (Production) (push) Blocked by required conditions
NordaBiz Tests / Send Failure Notification (push) Blocked by required conditions
- Migration 065: backfill user_companies from legacy User.company_id - Auth register: create UserCompany record on registration - Admin assign company: sync user_companies when assigning via admin panel - Fix NordaEvent field names: start_time→time_start, end_time→time_end, max_participants→max_attendees, remove non-existent fields Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
193ad2b3aa
commit
9a91fe3224
@ -24,7 +24,7 @@ from . import bp
|
||||
from database import (
|
||||
SessionLocal, User, Company, CompanyRecommendation,
|
||||
MembershipFee, MembershipFeeConfig, NordaEvent, EventAttendee,
|
||||
SystemRole
|
||||
SystemRole, UserCompany
|
||||
)
|
||||
from utils.decorators import role_required
|
||||
from utils.helpers import sanitize_html
|
||||
@ -390,9 +390,21 @@ def admin_user_assign_company(user_id):
|
||||
return jsonify({'success': False, 'error': 'Firma nie znaleziona'}), 404
|
||||
user.company_id = company_id
|
||||
company_name = company.name
|
||||
|
||||
# Sync user_companies table
|
||||
existing_uc = db.query(UserCompany).filter_by(user_id=user.id, company_id=company_id).first()
|
||||
if not existing_uc:
|
||||
# Remove old primary associations
|
||||
db.query(UserCompany).filter_by(user_id=user.id, is_primary=True).update({'is_primary': False})
|
||||
uc = UserCompany(user_id=user.id, company_id=company_id, role='MANAGER', is_primary=True)
|
||||
db.add(uc)
|
||||
else:
|
||||
existing_uc.is_primary = True
|
||||
else:
|
||||
user.company_id = None
|
||||
company_name = None
|
||||
# Remove primary flag from all user_companies
|
||||
db.query(UserCompany).filter_by(user_id=user.id, is_primary=True).update({'is_primary': False})
|
||||
|
||||
db.commit()
|
||||
|
||||
@ -903,15 +915,14 @@ def admin_calendar_new():
|
||||
title=request.form.get('title', '').strip(),
|
||||
description=sanitize_html(request.form.get('description', '').strip()),
|
||||
event_date=datetime.strptime(request.form.get('event_date'), '%Y-%m-%d').date(),
|
||||
start_time=request.form.get('start_time') or None,
|
||||
end_time=request.form.get('end_time') or None,
|
||||
time_start=request.form.get('time_start') or None,
|
||||
time_end=request.form.get('time_end') or None,
|
||||
location=request.form.get('location', '').strip() or None,
|
||||
event_type=event_type,
|
||||
max_participants=request.form.get('max_participants', type=int) or None,
|
||||
registration_required=request.form.get('registration_required') == 'on',
|
||||
is_public=request.form.get('is_public') == 'on',
|
||||
max_attendees=request.form.get('max_attendees', type=int) or None,
|
||||
access_level=access_level,
|
||||
created_by=current_user.id
|
||||
created_by=current_user.id,
|
||||
source='manual'
|
||||
)
|
||||
|
||||
db.add(event)
|
||||
|
||||
@ -16,7 +16,7 @@ from flask_login import login_required, login_user, logout_user, current_user
|
||||
from werkzeug.security import generate_password_hash, check_password_hash
|
||||
|
||||
from . import bp
|
||||
from database import SessionLocal, User, Company, UserBlock
|
||||
from database import SessionLocal, User, Company, UserBlock, UserCompany
|
||||
from utils.helpers import sanitize_input, validate_email, validate_password
|
||||
from extensions import limiter
|
||||
from security_service import log_audit
|
||||
@ -155,6 +155,13 @@ def register():
|
||||
)
|
||||
|
||||
db.add(user)
|
||||
db.flush() # Get user.id before creating UserCompany
|
||||
|
||||
# Create user_companies record if company matched
|
||||
if company_id:
|
||||
uc = UserCompany(user_id=user.id, company_id=company_id, role='MANAGER', is_primary=True)
|
||||
db.add(uc)
|
||||
|
||||
db.commit()
|
||||
|
||||
# Build verification URL
|
||||
|
||||
11
database/migrations/065_sync_user_companies.sql
Normal file
11
database/migrations/065_sync_user_companies.sql
Normal file
@ -0,0 +1,11 @@
|
||||
-- Migration 065: Sync legacy User.company_id to user_companies table
|
||||
-- For all users with company_id set but missing user_companies record
|
||||
|
||||
INSERT INTO user_companies (user_id, company_id, role, is_primary, created_at, updated_at)
|
||||
SELECT u.id, u.company_id, 'MANAGER', true, NOW(), NOW()
|
||||
FROM users u
|
||||
WHERE u.company_id IS NOT NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM user_companies uc
|
||||
WHERE uc.user_id = u.id AND uc.company_id = u.company_id
|
||||
);
|
||||
Loading…
Reference in New Issue
Block a user