from internal.queries import http_query from internal.helpers import get_user_id, generate_schema_id, get_email, rds_query, get_schema_id, \ get_id_and_management_schema_for_workspace from management.commons import ROLE_OWNER, EVENT_LOG_CREATE_WORKSPACE, EVENT_LOG_RENAME_WORKSPACE from management.helpers import get_default_workspace, add_role, add_event_log, \ add_workspace_management, has_some_role, get_alliance_member, update_caw_meta, get_raw_workspaces import logging log = logging.getLogger().getChild("management.workspace") def create_workspace(endpoint, req, event): user_id = get_user_id(event) if 'name' in req and req['name'] is not None: req['name'] = req['name'].strip() if len(req['name']) > 0: # management_schema = get_default_workspace(event) # if first workspace doesn't have a name, we just set a name and pretend we created a workspace. management_schema = req['management_schema'] for workspace in get_raw_workspaces(event): if workspace['num'] == 1: ws = get_workspace(endpoint, {'workspace_schema': workspace['id'], 'management_schema': management_schema}, event) if ws['name'] is None: req['workspace_schema'] = workspace['id'] return update_workspace(endpoint, req, event) break new_workspace_id = generate_schema_id(event, marker='w') req['new_schema'] = new_workspace_id req['user_id'] = user_id req['caw'] = 'workspace' req['email'] = get_email(event) http_query(endpoint, req, event) # add to commons.company_alliance add_workspace_management(management_schema, new_workspace_id, req['name'], req['description']) # add manager user role add_role(user_id, new_workspace_id, management_schema, ROLE_OWNER, 'workspace') add_event_log(new_workspace_id, None, user_id, EVENT_LOG_CREATE_WORKSPACE, f"Created workspace {req['name']}") req['workspace_schema'] = new_workspace_id return get_workspace(endpoint, req, event) raise RuntimeError('Please enter a name for the workspace') def update_workspace(endpoint, req, event): workspace_schema, management_schema = req['workspace_schema'], req['management_schema'] user_id = get_user_id(event) # check for rights if has_some_role(user_id, workspace_schema, management_schema, ROLE_OWNER): update_caw_meta(workspace_schema, req) if 'name' in req and req['name'] is not None: add_event_log(workspace_schema, None, user_id, EVENT_LOG_RENAME_WORKSPACE, f"Renamed workspace to {req['name']}") else: raise RuntimeError('Access Denied') return get_workspace(endpoint, req, event) def delete_workspace(endpoint, req, event): try: user_id = get_user_id(event) default_schema = req['management_schema'] if 'workspace_schema' in req and req['workspace_schema'] != default_schema: workspace_id = req['workspace_schema'] if has_some_role(user_id, workspace_id, default_schema, ROLE_OWNER): http_query(endpoint, req, event) return { '__typename': 'Workspace', 'id': workspace_id, 'status': 'deleted' } raise RuntimeError('Access Denied') except Exception as e: log.exception("DELETE WORKSPACE FAILED", exc_info=e) raise e def get_workspace(endpoint, req, event): workspace_schema = req['workspace_schema'] meta_data = {meta_line['key']: meta_line['val'] for meta_line in rds_query(f"SELECT key,val FROM {workspace_schema}.meta_data " f"WHERE key IN ('name', 'description', 'created', 'creator', 'updated')")} for glob in rds_query(f"SELECT cc.id, cc.status FROM {workspace_schema}.collection cc " f"WHERE cc.status != 'finished' ORDER BY cc.id DESC LIMIT 1"): status = glob['status'] break else: status = 'finished' if 'creator' in meta_data: req['memberId'] = meta_data['creator'] req['alliance_owner_id'] = meta_data['creator'] return { '__typename': 'Workspace', 'id': req['workspace_schema'], 'name': meta_data.get('name', None), 'description': meta_data.get('description', None), 'status': status, 'dateCreated': meta_data.get('created', None), 'lastUpdated': meta_data.get('updated', meta_data.get('created', None)), 'createdBy': get_alliance_member(endpoint, req, event) } def get_workspaces(endpoint, req, event): management_schema = get_schema_id(event) workspaces = [get_workspace(endpoint, { 'workspace_schema': workspace['id'], 'management_schema': management_schema }, event) for workspace in get_raw_workspaces(event)] return [ws for ws in workspaces if ws['name'] is not None] def check_workspace_channel_access(endpoint, req, event): get_id_and_management_schema_for_workspace(get_default_workspace(event), req['channelId']) def get_workspace_totalprofiles(endpoint, req, event): workspace_schema = req['workspace_schema'] return rds_query(f"""SELECT count(DISTINCT fan_id) count FROM {workspace_schema}.collection_fan""")[0]['count']