Skip to content

Commit

Permalink
[SPDBT-3468] Controlling Member Employee getting duplicated (#2067)
Browse files Browse the repository at this point in the history
# Description

This PR includes the following proposed change(s):

- [SPDBT-3468] Controlling Member Employee getting duplicated
  • Loading branch information
peggy-quartech authored Jan 8, 2025
1 parent fbcb08f commit 1eccf74
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ public async Task<BizLicApplicationCmdResp> CreateBizLicApplicationAsync(CreateB
SharedRepositoryFuncs.LinkServiceType(_context, cmd.ServiceTypeCode, app);
SharedRepositoryFuncs.LinkSubmittedByPortalUser(_context, cmd.SubmittedByPortalUserId, app);

if (cmd.CategoryCodes.Any(c => c == WorkerCategoryTypeEnum.PrivateInvestigator))
if (cmd.CategoryCodes.Any(c => c == WorkerCategoryTypeEnum.PrivateInvestigator) && cmd.PrivateInvestigatorSwlInfo != null)
{
spd_businesscontact businessContact = await UpsertPrivateInvestigator(cmd.PrivateInvestigatorSwlInfo, app, ct);
_context.SetLink(businessContact, nameof(spd_businesscontact.spd_OrganizationId), biz);
}
else
DeletePrivateInvestigatorLink(app);

//Associate of 1:N navigation property with Create of Update is not supported in CRM, so have to save first.
//then update category.
Expand Down Expand Up @@ -127,7 +129,7 @@ public async Task<BizLicApplicationCmdResp> SaveBizLicApplicationAsync(SaveBizLi
_context.SetLink(businessContact, nameof(spd_businesscontact.spd_OrganizationId), biz);
}
else
DeletePrivateInvestigator(app);
DeletePrivateInvestigatorLink(app);

await _context.SaveChangesAsync(ct);

Expand Down Expand Up @@ -168,9 +170,6 @@ public async Task<BizLicApplicationResp> GetBizLicApplicationAsync(Guid licenceA
var response = _mapper.Map<BizLicApplicationResp>(app);
var position = _context.LookupPosition(PositionEnum.PrivateInvestigatorManager.ToString());

if (position == null)
return response;

try
{
spd_businesscontact? bizContact = _context.spd_businesscontacts
Expand All @@ -182,19 +181,20 @@ public async Task<BizLicApplicationResp> GetBizLicApplicationAsync(Guid licenceA
.Where(b => b.statecode != DynamicsConstants.StateCode_Inactive)
.FirstOrDefault();

PrivateInvestigatorSwlContactInfo privateInvestigatorInfo = new()
if (bizContact != null)
{
ContactId = bizContact?.spd_ContactId?.contactid,
BizContactId = bizContact?.spd_businesscontactid,
GivenName = bizContact?.spd_firstname,
Surname = bizContact?.spd_surname,
MiddleName1 = bizContact?.spd_middlename1,
MiddleName2 = bizContact?.spd_middlename2,
EmailAddress = bizContact?.spd_email,
LicenceId = bizContact?._spd_swlnumber_value
};

response.PrivateInvestigatorSwlInfo = privateInvestigatorInfo;
response.PrivateInvestigatorSwlInfo = new()
{
ContactId = (Guid)bizContact.spd_ContactId.contactid,
BizContactId = bizContact?.spd_businesscontactid,
GivenName = bizContact?.spd_firstname,
Surname = bizContact?.spd_surname,
MiddleName1 = bizContact?.spd_middlename1,
MiddleName2 = bizContact?.spd_middlename2,
EmailAddress = bizContact?.spd_email,
LicenceId = (Guid)bizContact._spd_swlnumber_value
};
}

}
catch (DataServiceQueryException ex)
Expand All @@ -210,7 +210,7 @@ public async Task<BizLicApplicationResp> GetBizLicApplicationAsync(Guid licenceA

private async Task<spd_businesscontact> UpsertPrivateInvestigator(PrivateInvestigatorSwlContactInfo privateInvestigatorInfo, spd_application app, CancellationToken ct)
{
contact? contact = await _context.GetContactById((Guid)privateInvestigatorInfo.ContactId, ct);
contact? contact = await _context.GetContactById(privateInvestigatorInfo.ContactId, ct);
if (contact == null)
throw new ArgumentException($"cannot find the contact with contactId : {privateInvestigatorInfo.ContactId}");

Expand All @@ -230,10 +230,18 @@ private async Task<spd_businesscontact> UpsertPrivateInvestigator(PrivateInvesti
.Expand(b => b.spd_businesscontact_spd_application)
.Expand(b => b.spd_SWLNumber)
.Where(b => b.spd_position_spd_businesscontact.Any(p => p.spd_positionid == positionid))
.Where(b => b.spd_businesscontact_spd_application.Any(b => b.spd_applicationid == app.spd_applicationid))
.Where(b => b.spd_SWLNumber.spd_licenceid == privateInvestigatorInfo.LicenceId)
.FirstOrDefault();
if (existingBizContact == null)
{
//we probably do not need to make other private investigator inactive. - tbd
//find other pi bizContact, make them inactive. - not clear in requirement, should be fine to not do that.
//otherPiBizContact.statecode = DynamicsConstants.StateCode_Inactive;
//_context.UpdateObject(otherPiBizContact);

//for same application, that means user changed his pi link during the same application - partial save. then we need to delete the old bizcontact.
DeletePrivateInvestigatorLink(app);

//add new one
bizContact = _mapper.Map<spd_businesscontact>(privateInvestigatorInfo);
bizContact.spd_businesscontactid = Guid.NewGuid();
Expand All @@ -243,20 +251,8 @@ private async Task<spd_businesscontact> UpsertPrivateInvestigator(PrivateInvesti
}
else
{
if (existingBizContact.spd_SWLNumber.spd_licenceid != privateInvestigatorInfo.LicenceId)
{
_context.DeleteObject(existingBizContact);
//add new one
bizContact = _mapper.Map<spd_businesscontact>(privateInvestigatorInfo);
bizContact.spd_businesscontactid = Guid.NewGuid();
bizContact.spd_role = (int)BizContactRoleOptionSet.Employee;
_context.AddTospd_businesscontacts(bizContact);
AddPrivateInvestigatorLink(bizContact, app);
}
else
{
bizContact = existingBizContact;
}
bizContact = existingBizContact;
AddPrivateInvestigatorLink(bizContact, app);
}
}
else
Expand All @@ -274,7 +270,7 @@ private async Task<spd_businesscontact> UpsertPrivateInvestigator(PrivateInvesti
AddPrivateInvestigatorLink(bizContact, app);
}
_context.SetLink(bizContact, nameof(spd_businesscontact.spd_ContactId), contact);
spd_licence licence = GetLicence((Guid)privateInvestigatorInfo.LicenceId);
spd_licence licence = GetLicence(privateInvestigatorInfo.LicenceId);
_context.AddLink(licence, nameof(spd_licence.spd_licence_spd_businesscontact_SWLNumber), bizContact);
return bizContact;
}
Expand All @@ -295,39 +291,19 @@ private async Task SetApplicantSwlLicenceId(spd_application app, Guid? applicant

private void AddPrivateInvestigatorLink(spd_businesscontact bizContact, spd_application app)
{
_context.AddLink(bizContact, nameof(spd_application.spd_businesscontact_spd_application), app);
if (!bizContact.spd_businesscontact_spd_application.Contains(app))
_context.AddLink(bizContact, nameof(spd_application.spd_businesscontact_spd_application), app);

var position = _context.LookupPosition(PositionEnum.PrivateInvestigatorManager.ToString());

if (position != null)
if (!bizContact.spd_position_spd_businesscontact.Contains(position))
_context.AddLink(position, nameof(spd_businesscontact.spd_position_spd_businesscontact), bizContact);
}

private void DeletePrivateInvestigator(spd_application app)
{
DynamicsContextLookupHelpers.PositionDictionary.TryGetValue(PositionEnum.PrivateInvestigatorManager.ToString(), out Guid positionid);
spd_businesscontact? bizContact = _context.spd_businesscontacts
.Expand(b => b.spd_position_spd_businesscontact)
.Expand(b => b.spd_businesscontact_spd_application)
.Expand(b => b.spd_SWLNumber)
.Where(b => b.spd_position_spd_businesscontact.Any(p => p.spd_positionid == positionid))
.Where(b => b.spd_businesscontact_spd_application.Any(b => b.spd_applicationid == app.spd_applicationid))
.FirstOrDefault();

if (bizContact == null)
return;

_context.DeleteObject(bizContact);
//_context.SaveChanges();
}

private void DeletePrivateInvestigatorLink(spd_application app)
{
var position = _context.LookupPosition(PositionEnum.PrivateInvestigatorManager.ToString());

if (position == null)
return;

spd_businesscontact? bizContact = _context.spd_businesscontacts
.Expand(b => b.spd_position_spd_businesscontact)
.Expand(b => b.spd_businesscontact_spd_application)
Expand All @@ -339,24 +315,16 @@ private void DeletePrivateInvestigatorLink(spd_application app)
if (bizContact == null)
return;

_context.DeleteLink(app, nameof(spd_application.spd_businesscontact_spd_application), bizContact);
_context.SetLink(bizContact, nameof(spd_businesscontact.spd_ContactId), null);
_context.DeleteLink(position, nameof(spd_businesscontact.spd_position_spd_businesscontact), bizContact);

Guid? licenceId = bizContact.spd_SWLNumber.spd_licenceid;

if (licenceId == null)
return;

spd_licence? licence = _context.spd_licences
.Where(l => l.spd_licenceid == licenceId)
.Where(l => l.statecode == DynamicsConstants.StateCode_Active)
.FirstOrDefault();

if (licence == null)
return;
//if no spd_application connected with this bizContact, then set this bizContact inactive.
if (bizContact.spd_businesscontact_spd_application.ToArray().Count() == 1)
{
bizContact.statecode = DynamicsConstants.StateCode_Inactive;
bizContact.statuscode = DynamicsConstants.StatusCode_Inactive;
_context.UpdateObject(bizContact);
}

_context.DeleteLink(licence, nameof(spd_licence.spd_licence_spd_businesscontact_SWLNumber), bizContact);
_context.DeleteLink(app, nameof(spd_application.spd_businesscontact_spd_application), bizContact);
_context.SaveChanges();
}

//set biz manager info, applicant info, address and link biz to application
Expand Down
4 changes: 2 additions & 2 deletions src/Spd.Resource.Repository/BizLicApplication/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public record BizLicApplicationResp() : BizLicApplication

public record PrivateInvestigatorSwlContactInfo : ContactInfo
{
public Guid? ContactId { get; set; }
public Guid ContactId { get; set; }
public Guid? BizContactId { get; set; }
public Guid? LicenceId { get; set; }
public Guid LicenceId { get; set; }
}

public enum PositionEnum
Expand Down

0 comments on commit 1eccf74

Please sign in to comment.