Skip to content

Commit

Permalink
fix(register) : update secondary contact validation to work in edit flow
Browse files Browse the repository at this point in the history
  • Loading branch information
KobithasanVasantharajah committed Dec 17, 2024
1 parent 4273bb2 commit 50fa128
Showing 1 changed file with 84 additions and 71 deletions.
155 changes: 84 additions & 71 deletions DVSRegister/Controllers/CabProviderController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,59 +676,66 @@ 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",
"PrimaryContactEmail",
"SecondaryContactEmail",
"Email address of secondary contact cannot be the same as primary contact"
)
);
)
);

ValidationHelper.ValidateDuplicateFields(
ModelState,
primaryValue: primaryContactViewModel.PrimaryContactTelephoneNumber,
secondaryValue: profileSummary.SecondaryContact?.SecondaryContactTelephoneNumber,
ModelState,
primaryValue: primaryContactViewModel.PrimaryContactTelephoneNumber,
secondaryValue: providerProfileDto.SecondaryContactTelephoneNumber,
new ValidationHelper.FieldComparisonConfig(
"PrimaryContactTelephoneNumber",
"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
{
return RedirectToAction("CabHandleException", "Error");
return RedirectToAction("CabHandleException", "Error");
}
}
else
{
return View("EditPrimaryContact", primaryContactViewModel);
return View("EditPrimaryContact", primaryContactViewModel);
}
}


#endregion

#region Edit secondary contact
Expand Down Expand Up @@ -758,57 +765,63 @@ 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,
secondaryValue: secondaryContactViewModel.SecondaryContactEmail,
ModelState,
primaryValue: providerProfileDto.PrimaryContactEmail,
secondaryValue: secondaryContactViewModel.SecondaryContactEmail,
new ValidationHelper.FieldComparisonConfig(
"PrimaryContactEmail",
"PrimaryContactEmail",
"SecondaryContactEmail",
"Email address of secondary contact cannot be the same as primary contact"
)
);

)
);
ValidationHelper.ValidateDuplicateFields(
ModelState,
primaryValue: profileSummary.PrimaryContact?.PrimaryContactTelephoneNumber,
secondaryValue: secondaryContactViewModel.SecondaryContactTelephoneNumber,
ModelState,
primaryValue: providerProfileDto.PrimaryContactTelephoneNumber,
secondaryValue: secondaryContactViewModel.SecondaryContactTelephoneNumber,
new ValidationHelper.FieldComparisonConfig(
"PrimaryContactTelephoneNumber",
"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
else
{
return RedirectToAction("CabHandleException", "Error");
return RedirectToAction("CabHandleException", "Error");
}
}
else
{
return View("EditSecondaryContact", secondaryContactViewModel);
return View("EditSecondaryContact", secondaryContactViewModel);
}
}

Expand Down

0 comments on commit 50fa128

Please sign in to comment.