"""Lambda send_org_invite_email function module.""" from config import app_logger as logger from marshmallow import EXCLUDE from marshmallow import ValidationError from owsrequest import request import config from src.schemas.send_org_invite_email import SendOrgInviteEmail class RetryException(Exception): """Retry Exception.""" pass class BadRequest(Exception): """Bad Request cannot retry Exception.""" pass def handler(event, context): """Lambda entry point.""" try: input_data = event or {} result = SendOrgInviteEmail().load(input_data, unknown=EXCLUDE) logger.info( f"Successfully validated the request for {result['email']} " 'Now calling auth0 invite organization-member.') # call ows to send user invitation. ows_data = { 'brand': result['brand'], 'email': result['email'], 'admin_name': config.ORG_INVITE_ADMIN_NAME, 'auth0_application_name': config.ORG_INVITE_TO_APPLICATION, 'user_metadata': { 'orchardIdentityId': result['identity_id'], 'user_types': ['label'], 'username': result['email'], 'type': 'alw', } } ows_data['user_metadata']['use_new_unified_template'] = False if result['brand'] != 'knr' and result.get('label_profile_id'): # add vend_contact_id only for non-KNR brands, # since for that moment a user with 'knr' brand should # have only Settings and Moneyhub profiles ows_data['user_metadata']['vend_contact_id'] = result[ 'label_profile_id'] if input_data.get('source') == 'event.knrAccountCreation': ows_data['admin_name'] = config.ORG_INVITE_ADMIN_KNR_NAME ows_result = request.process( config.LAMBDA_NAME, config.ENVIRONMENT, config.OWS_SERVICE_ENDPOINT_METHOD, config.OWS_SERVICE_NAME, config.OWS_SERVICE_ENDPOINT, json=ows_data, headers=config.OWS_REQUEST_HEADERS) if 200 <= ows_result.status_code < 300: return ows_result.json() logger.info( f'Error ows response status_code={ows_result.status_code}' f' error={ows_result.text} ') raise RetryException('Ows Request failed for valid request, so retry.') except ValidationError as err: logger.error('Invalid request.', err.messages) raise BadRequest('Invalid request.', err.messages)