174 lines
6.9 KiB
C#
174 lines
6.9 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Saxonite.Net.Core.Web.Online.Models;
|
|
using Saxonite.Net.Core.Web.Base.App;
|
|
using Saxonite.Net.Core.Web.Base.Controllers;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Saxonite.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
|
|
namespace Saxonite.Net.Core.Web.Online.Controllers
|
|
{
|
|
//=======================================================================
|
|
// Online Account Controller - Contact - Ajax actions.
|
|
//=======================================================================
|
|
partial class AccountController
|
|
{
|
|
//-------------------------------------------------------------------
|
|
public class ContactResponseData
|
|
{
|
|
public String EmailAddress { get; set; }
|
|
public String DisplayName { get; set; }
|
|
public String Title { get; set; }
|
|
public String FirstName { get; set; }
|
|
public String LastName { get; set; }
|
|
// Regions.
|
|
public int RegionIdCountry { get; set; }
|
|
public String RegionCaptionCountry { get; set; }
|
|
public int RegionIdProvince { get; set; }
|
|
public String RegionCaptionProvince { get; set; }
|
|
public String LocationCaption { get; set; }
|
|
}
|
|
private void PopulateContactResponseData(ContactResponseData data, SaxUserAccount userAccount)
|
|
{
|
|
data.EmailAddress = userAccount.EmailAddress;
|
|
data.DisplayName = userAccount.GetDisplayName();
|
|
data.Title = userAccount.Title;
|
|
data.FirstName = userAccount.FirstName;
|
|
data.LastName = userAccount.LastName;
|
|
data.RegionIdCountry = SysId.ToInt(userAccount.RegionIdCountry);
|
|
data.RegionIdProvince = SysId.ToInt(userAccount.RegionIdProvince);
|
|
using (SaxRegion regionCountry = Internals.GetRegion(userAccount.RegionIdCountry))
|
|
using (SaxRegion regionProvince = Internals.GetRegion(userAccount.RegionIdProvince))
|
|
{
|
|
data.RegionCaptionCountry = Localiser.GetRegionName(regionCountry);
|
|
data.RegionCaptionProvince = Localiser.GetRegionName(regionProvince);
|
|
List<String> locations = new List<string>() { data.RegionCaptionProvince, data.RegionCaptionCountry };
|
|
data.LocationCaption = locations.Concat(", ", "", "", true, true);
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Account - Get Contact Details.
|
|
//-------------------------------------------------------------------
|
|
public class AjaxResponse_AccountContactData : AjaxResponse_Data
|
|
{
|
|
public ContactResponseData Contact { get; set; }
|
|
}
|
|
// Ajax request.
|
|
[HttpPost]
|
|
[Route("Account/Ajax/GetProfileContact")]
|
|
public IActionResult GetProfileContact(Model_Account model)
|
|
{
|
|
String fn = GetType().Name + ".GetProfileContact";
|
|
AjaxResponse_AccountContactData responseData = new AjaxResponse_AccountContactData();
|
|
String messageTitle = Localiser["account.caption.profile.contact"];
|
|
|
|
// Validate session and referrer.
|
|
if (!ValidReferer)
|
|
return Forbid();
|
|
if (!ValidUserSession)
|
|
return GetAjaxResult_InvalidSession(responseData);
|
|
|
|
// Retrieve user details.
|
|
SaxUserAccount userAccount = AccountMgr.GetUserAccountById(SessionData.User.UserAccountId);
|
|
if (userAccount == null)
|
|
{
|
|
SaLogger.Warn(fn + " Failed to retrieve user account for Id: " + SysId.ToString(SessionData.User.UserAccountId));
|
|
return Json(responseData.SetGoHome(messageTitle, @Localiser.GetJavascriptText("msg.something.gone.wrong")));
|
|
}
|
|
|
|
// Populate data to response.
|
|
responseData.Contact = new ContactResponseData();
|
|
PopulateContactResponseData(responseData.Contact, userAccount);
|
|
responseData.Success = true;
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Account - Store Contact Details.
|
|
//-------------------------------------------------------------------
|
|
public class AjaxResponse_StoreAccountContactDetails : AjaxResponse_Data
|
|
{
|
|
public ContactResponseData Contact { get; set; }
|
|
}
|
|
// Ajax request.
|
|
[HttpPost]
|
|
[Route("Account/Ajax/StoreProfileContact")]
|
|
public IActionResult StoreProfileContact(Model_Account model)
|
|
{
|
|
String fn = GetType().Name + ".StoreProfileContact";
|
|
AjaxResponse_StoreAccountContactDetails responseData = new AjaxResponse_StoreAccountContactDetails();
|
|
String messageTitle = Localiser["account.caption.profile.contact"];
|
|
|
|
// Validate session and referrer.
|
|
if (!ValidReferer)
|
|
return Forbid();
|
|
if (!ValidUserSession)
|
|
return GetAjaxResult_InvalidSession(responseData);
|
|
|
|
// Validate data.
|
|
model.ValidateContactDetails(responseData);
|
|
if (responseData.HasControlErrors())
|
|
return Json(responseData);
|
|
|
|
// Retrieve user details.
|
|
SaxUserAccount userAccount = AccountMgr.GetUserAccountById(SessionData.User.UserAccountId);
|
|
if (userAccount == null)
|
|
{
|
|
SaLogger.Warn(fn + " Failed to retrieve user account for Id: " + SysId.ToString(SessionData.User.UserAccountId));
|
|
return Json(responseData.SetGoHome(messageTitle, @Localiser.GetJavascriptText("msg.something.gone.wrong")));
|
|
}
|
|
if (!userAccount.EmailAddress.EqualsCI(SessionData.User.EmailAddress))
|
|
{
|
|
SaLogger.Warn(fn + " Unable to update user account '" + userAccount.EmailAddress + ". Email doesn't match session: " + SessionData.User.EmailAddress);
|
|
return Json(responseData.SetGoHome(messageTitle, @Localiser.GetJavascriptText("msg.something.gone.wrong")));
|
|
}
|
|
|
|
// Populate user properties.
|
|
userAccount.Changed = false;
|
|
if (model.AccountTitle != null)
|
|
userAccount.Title = model.AccountTitle;
|
|
if (model.AccountFirstName != null)
|
|
userAccount.FirstName = model.AccountFirstName;
|
|
if (model.AccountLastName != null)
|
|
userAccount.LastName = model.AccountLastName;
|
|
// Regions.
|
|
if (model.AccountRegionIdCountry != null)
|
|
userAccount.RegionIdCountry = model.AccountRegionIdCountry.ToSysId();
|
|
if (model.AccountRegionIdProvince != null)
|
|
userAccount.RegionIdProvince = model.AccountRegionIdProvince.ToSysId();
|
|
|
|
// Update User Account.
|
|
if (userAccount.Changed) SaLogger.Info(fn + " Updating user account '" + userAccount.EmailAddress + "'. Id: " + SysId.ToString(userAccount.Id), true);
|
|
if (AccountMgr.UpdateUserAccount(userAccount, DbUpdateInfo))
|
|
{
|
|
// ToDo Refresh Session with updated organisation details.
|
|
}
|
|
else
|
|
{
|
|
SaLogger.Warn(fn + " Failed to update user account '" + userAccount.EmailAddress + "': " + AccountMgr.ErrorStateMessage);
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.update.failed"]));
|
|
}
|
|
|
|
// Refresh profile contact details to session.
|
|
RefreshSessionProfileContact(userAccount);
|
|
|
|
// Construct response.
|
|
responseData.Contact = new ContactResponseData();
|
|
PopulateContactResponseData(responseData.Contact, userAccount);
|
|
responseData.Success = true;
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|