Skip to content

Commit

Permalink
Merge pull request #328 from UKGovernmentBEIS/dvs-299-duplicate-secon…
Browse files Browse the repository at this point in the history
…dary-contact-validation-not-working-in-edit

fix(register) : update secondary contact validation to work in edit flow
  • Loading branch information
KobithasanVasantharajah authored Dec 17, 2024
2 parents caa0ee2 + d016e6b commit a352d06
Showing 1 changed file with 72 additions and 60 deletions.
132 changes: 72 additions & 60 deletions DVSRegister/Controllers/CabProviderController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,47 +676,53 @@ public async Task<IActionResult> EditPrimaryContact(int providerId)

[HttpPost("edit-primary-contact")]
public async Task<IActionResult> UpdatePrimaryContact(PrimaryContactViewModel primaryContactViewModel)
{
ProfileSummaryViewModel profileSummary = GetProfileSummary();

{
int cabId = Convert.ToInt32(HttpContext?.Session.Get<int>("CabId"));
if (cabId <= 0 || primaryContactViewModel.ProviderId <= 0)
{
return RedirectToAction("CabHandleException", "Error");
}

// Fetch the latest provider data from the database
ProviderProfileDto providerProfileDto = await cabService.GetProvider(primaryContactViewModel.ProviderId, cabId);
if (providerProfileDto == null)
{
return RedirectToAction("CabHandleException", "Error");
}

ValidationHelper.ValidateDuplicateFields(
ModelState,
primaryContactViewModel.PrimaryContactEmail,
profileSummary.SecondaryContact?.SecondaryContactEmail,
ModelState,
primaryValue: primaryContactViewModel.PrimaryContactEmail,
secondaryValue: providerProfileDto.SecondaryContactEmail,
new ValidationHelper.FieldComparisonConfig(
"PrimaryContactEmail",
"SecondaryContactEmail",
"Email address of secondary contact cannot be the same as primary contact"
)
);
)
);

ValidationHelper.ValidateDuplicateFields(
ModelState,
ModelState,
primaryValue: primaryContactViewModel.PrimaryContactTelephoneNumber,
secondaryValue: profileSummary.SecondaryContact?.SecondaryContactTelephoneNumber,
secondaryValue: providerProfileDto.SecondaryContactTelephoneNumber,
new ValidationHelper.FieldComparisonConfig(
"PrimaryContactTelephoneNumber",
"SecondaryContactTelephoneNumber",
"Telephone number of secondary contact cannot be the same as primary contact"
)
);

if (ModelState.IsValid)
{
ProviderProfileDto providerProfileDto = new()
{
PrimaryContactFullName = primaryContactViewModel.PrimaryContactFullName,
PrimaryContactEmail = primaryContactViewModel.PrimaryContactEmail,
PrimaryContactJobTitle = primaryContactViewModel.PrimaryContactJobTitle,
PrimaryContactTelephoneNumber = primaryContactViewModel.PrimaryContactTelephoneNumber,
Id = primaryContactViewModel.ProviderId
};

GenericResponse genericResponse = await cabService.UpdatePrimaryContact(providerProfileDto, UserEmail);
if (genericResponse.Success)
{
return RedirectToAction("ProviderProfileDetails", "Cab",
new { providerId = providerProfileDto.Id });
)
);

if (ModelState.IsValid)
{
providerProfileDto.PrimaryContactFullName = primaryContactViewModel.PrimaryContactFullName;
providerProfileDto.PrimaryContactEmail = primaryContactViewModel.PrimaryContactEmail;
providerProfileDto.PrimaryContactJobTitle = primaryContactViewModel.PrimaryContactJobTitle;
providerProfileDto.PrimaryContactTelephoneNumber = primaryContactViewModel.PrimaryContactTelephoneNumber;

GenericResponse genericResponse = await cabService.UpdatePrimaryContact(providerProfileDto, UserEmail);
if (genericResponse.Success)
{
return RedirectToAction("ProviderProfileDetails", "Cab", new { providerId = providerProfileDto.Id });
}
else
{
Expand All @@ -728,7 +734,7 @@ public async Task<IActionResult> UpdatePrimaryContact(PrimaryContactViewModel pr
return View("EditPrimaryContact", primaryContactViewModel);
}
}

#endregion

#region Edit secondary contact
Expand Down Expand Up @@ -758,48 +764,54 @@ public async Task<IActionResult> EditSecondaryContact(int providerId)
}

[HttpPost("edit-secondary-contact")]
public async Task<IActionResult> UpdateSecondaryContact(SecondaryContactViewModel secondaryContactViewModel)
{
ProfileSummaryViewModel profileSummary = GetProfileSummary();

public async Task<IActionResult> UpdateSecondaryContact(SecondaryContactViewModel secondaryContactViewModel)
{
int cabId = Convert.ToInt32(HttpContext?.Session.Get<int>("CabId"));
if (cabId <= 0 || secondaryContactViewModel.ProviderId <= 0)
{
return RedirectToAction("CabHandleException", "Error");
}

// Fetch the latest provider data from the database
ProviderProfileDto providerProfileDto = await cabService.GetProvider(secondaryContactViewModel.ProviderId, cabId);
if (providerProfileDto == null)
{
return RedirectToAction("CabHandleException", "Error");
}

ValidationHelper.ValidateDuplicateFields(
ModelState,
primaryValue: profileSummary.PrimaryContact?.PrimaryContactEmail,
ModelState,
primaryValue: providerProfileDto.PrimaryContactEmail,
secondaryValue: secondaryContactViewModel.SecondaryContactEmail,
new ValidationHelper.FieldComparisonConfig(
"PrimaryContactEmail",
"SecondaryContactEmail",
"Email address of secondary contact cannot be the same as primary contact"
)
);

)
);
ValidationHelper.ValidateDuplicateFields(
ModelState,
primaryValue: profileSummary.PrimaryContact?.PrimaryContactTelephoneNumber,
ModelState,
primaryValue: providerProfileDto.PrimaryContactTelephoneNumber,
secondaryValue: secondaryContactViewModel.SecondaryContactTelephoneNumber,
new ValidationHelper.FieldComparisonConfig(
"PrimaryContactTelephoneNumber",
"SecondaryContactTelephoneNumber",
"Telephone number of secondary contact cannot be the same as primary contact"
)
);

if (ModelState.IsValid)
{
ProviderProfileDto providerProfileDto = new()
{
SecondaryContactFullName = secondaryContactViewModel.SecondaryContactFullName,
SecondaryContactEmail = secondaryContactViewModel.SecondaryContactEmail,
SecondaryContactJobTitle = secondaryContactViewModel.SecondaryContactJobTitle,
SecondaryContactTelephoneNumber = secondaryContactViewModel.SecondaryContactTelephoneNumber,
Id = secondaryContactViewModel.ProviderId
};
GenericResponse genericResponse =
await cabService.UpdateSecondaryContact(providerProfileDto, UserEmail);
if (genericResponse.Success)
{
return RedirectToAction("ProviderProfileDetails", "Cab",
new { providerId = providerProfileDto.Id });
)
);

if (ModelState.IsValid)
{
providerProfileDto.SecondaryContactFullName = secondaryContactViewModel.SecondaryContactFullName;
providerProfileDto.SecondaryContactEmail = secondaryContactViewModel.SecondaryContactEmail;
providerProfileDto.SecondaryContactJobTitle = secondaryContactViewModel.SecondaryContactJobTitle;
providerProfileDto.SecondaryContactTelephoneNumber = secondaryContactViewModel.SecondaryContactTelephoneNumber;

GenericResponse genericResponse = await cabService.UpdateSecondaryContact(providerProfileDto, UserEmail);
if (genericResponse.Success)
{
return RedirectToAction("ProviderProfileDetails", "Cab", new { providerId = providerProfileDto.Id });
}
else
{
Expand Down

0 comments on commit a352d06

Please sign in to comment.