/// <summary> /// Creates Autorized contact /// Get Customer Relationships Get /v{version}/customer/bp-relationships ///Check to see if customer is existing(and active) contact customer /// If yes and active – no action required /// If yes and not currently valid – Update valid to date to 1231999? /// If no – create relationship type contact customer between the two BP;s /// From Date: System Date /// To Date: 12/29/9999 /// </summary> /// <param name="authorizedContactRequest"></param> /// <param name="bpId"></param> /// <param name="jwt"></param> /// <returns></returns> public async Task <AuthorizedContactResponse> CreateAuthorizedContact(AuthorizedContactRequest authorizedContactRequest, string bpId, string jwt) { _logger.LogInformation($"CreateAuthorizedContact: CreateAuthorizedContact({nameof(authorizedContactRequest)} : {authorizedContactRequest.ToJson()})"); try { var authorizedContactresponse = new AuthorizedContactResponse(); //Do a bp search var bpSearch = MapToBpSearchrequest(authorizedContactRequest.AuthorizedContact); var bpExists = await GetDuplicateBusinessPartnerIfExists(bpSearch); var loggedInBp = bpId.ToString(); if (bpExists == null || !bpExists.MatchFound) { //if bp doesn't exist // create bp //Create a relation ship _logger.LogInformation($"There is not a match for an existing Business Partner based on the information provided."); var bp = await CreateBusinessPartner(authorizedContactRequest.AuthorizedContact); // create bp authorizedContactresponse.BpId = bp.BpId; //Create a Bp relation ship var bpCreateRelation = new CreateBpRelationshipRequest() { FirstAccountBpId = bpId, SecondAccountBpId = bp.BpId, Relationshipcategory = authorizedContactRequest.AuthorizedContact?.Relationshipcategory.GetEnumMemberValue(), TenantBpId = authorizedContactRequest.TenantBpId }; var createRelation = _customerLogic.CreateBpRelationshipAsync(bpCreateRelation, jwt); } else { //if bp exist // check if Bp relation ship exist , if yes update relationship // if no relation ship exist then create a relation ship var contactBp = bpExists.BpId.ToString(); authorizedContactresponse.BpId = bpExists.BpId.ToString(); var bpRelationParam = new BpRelationshipRequestParam() { LoggedInBp = loggedInBp, Jwt = jwt, TenantBp = authorizedContactRequest.TenantBpId }; var checkRelationShip = await GetBprelationships(bpRelationParam); var hasRelation = CheckRelationWithContact(checkRelationShip, contactBp); var hasActiveRelation = IsrelationShipActive(hasRelation); if (hasRelation != null && !hasActiveRelation) { //Update var relationShipToupdate = GetRelationshipToupdate(hasRelation); relationShipToupdate.TenantBpId = ParseBpFromString(authorizedContactRequest.TenantBpId); _mcfClient.UpdateBusinessPartnerRelationship(relationShipToupdate, jwt); } if (hasRelation == null) { //Create Bp relationship var bpCreateRelation = new CreateBpRelationshipRequest() { FirstAccountBpId = bpId, SecondAccountBpId = contactBp, Relationshipcategory = authorizedContactRequest.AuthorizedContact?.Relationshipcategory.GetEnumMemberValue(), TenantBpId = authorizedContactRequest.TenantBpId }; var createRelation = _customerLogic.CreateBpRelationshipAsync(bpCreateRelation, jwt); } } return(authorizedContactresponse); } catch (Exception ex) { _logger.LogError(ex, $"Failed to CreateAuthorizedContact"); throw ex; } }
public async Task <IActionResult> CreateAuthorizedContact([FromBody] AuthorizedContactRequest authorizedContactRequest) { IActionResult result; _logger.LogInformation($"CreateAuthorizedContact({nameof(authorizedContactRequest)}: {authorizedContactRequest.ToJson()})"); try { if (authorizedContactRequest == null || authorizedContactRequest.AuthorizedContact == null) { return(BadRequest()); } var bpId = GetBpIdFromClaims(); var jwt = GetJWToken(); var contact = await _moveInLogic.CreateAuthorizedContact(authorizedContactRequest, bpId.ToString(), jwt); result = Ok(contact); } catch (Exception ex) { _logger.LogError(ex, "Unable Create Authorized Contact", ex.Message); result = ex.ToActionResult(); } return(result); }