215 lines
9.5 KiB
C#
215 lines
9.5 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using Saxonite.Net.Core.Web.Online.App;
|
|
using Saxonite.Net.Core.Web.Online.Models;
|
|
using Saxonite.Net.Standard.Base.App;
|
|
using Saxonite.Net.Standard.Base.Bus;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Saxonite.Net.Core.Web.Online.Controllers
|
|
{
|
|
//=======================================================================
|
|
// Online Organisation Controller - Request - Ajax actions.
|
|
//=======================================================================
|
|
partial class OrganisationController
|
|
{
|
|
|
|
//-------------------------------------------------------------------
|
|
// Request Organisation ajax request.
|
|
//-------------------------------------------------------------------
|
|
private class AjaxResponse_RequestOrganisation : AjaxResponse_Data
|
|
{
|
|
// Construction.
|
|
public AjaxResponse_RequestOrganisation(SaxModel_Online model) : base(model) { }
|
|
// Organisation list.
|
|
public List<OrganisationListData> Organisations { get; } = new List<OrganisationListData>();
|
|
}
|
|
//-------------------------------------------------------------------
|
|
[HttpPost]
|
|
[Route("Organisation/Ajax/Request")]
|
|
[Route("Organisation/Ajax/Register")]
|
|
public IActionResult Ajax_RequestOrganisation(Model_Organisation model)
|
|
{
|
|
String fn = GetType().Name + ".Ajax_RequestOrganisation";
|
|
String messageTitle = Localiser["organisation.caption.organisation.request"];
|
|
AjaxResponse_RequestOrganisation responseData = new AjaxResponse_RequestOrganisation(model);
|
|
|
|
// Validate session and referrer. User does not need to be logged in.
|
|
if (!ValidReferer) return Forbid();
|
|
SaxSessionData_Online sessionData = SessionData_Online;
|
|
|
|
// Record disposable items as they are created so all can be disposed when needed.
|
|
using (DisposableCollection<SacsHierarchyItem> disposableItems = new DisposableCollection<SacsHierarchyItem>())
|
|
{
|
|
|
|
if (!model.ValidateOrganisationRequestData(responseData))
|
|
return Json(responseData);
|
|
|
|
// Check Regions.
|
|
SysId regionIdCountry = model.ExtractSysId(model.Organisation.RegionIdPrimary);
|
|
SaxRegion.Data regionCountry = Internals.GetRegion(regionIdCountry);
|
|
if (SysId.IsValid(regionIdCountry) && regionCountry == null)
|
|
{
|
|
SaLogger.Warn(fn + " Country region not found. Id: " + model.Organisation.RegionIdPrimary + ". " + GetLogSuffix());
|
|
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
|
|
}
|
|
SysId regionIdAddressProvince = model.ExtractSysId(model.Organisation.Address.RegionIdProvince);
|
|
SaxRegion.Data regionProvince = Internals.GetRegion(regionIdAddressProvince);
|
|
if (SysId.IsValid(regionIdAddressProvince) && regionProvince == null)
|
|
{
|
|
SaLogger.Warn(fn + " Province region not found. Id: " + model.Organisation.Address.RegionIdProvince + ". " + GetLogSuffix());
|
|
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
|
|
}
|
|
|
|
// User does not need to be logged in, but user details must be supplied if not.
|
|
SaxUserAccount userAccount = null;
|
|
if (ValidUserSession)
|
|
{
|
|
// Check that an email address hasn't been supplied.
|
|
// NB Shouldn't happen as contact controls aren't displayed if user is logged in,
|
|
// hence 'System' error response and nothing more specific.
|
|
if (!StringUtil.IsEmpty(model.ContactEmailAddress))
|
|
{
|
|
SaLogger.Warn(fn + " Contact Email supplied when user logged in. " + GetLogSuffix());
|
|
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
|
|
}
|
|
// Retrieve user account.
|
|
userAccount = AccountMgr.GetUserAccountById(sessionData.User.UserAccountId);
|
|
disposableItems.Add(userAccount);
|
|
if (userAccount == null)
|
|
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
|
|
}
|
|
else
|
|
{
|
|
// Double check that contact details have been supplied - validated by model above.
|
|
if (StringUtil.IsEmpty(model.ContactEmailAddress) || StringUtil.IsEmpty(model.ContactFirstName) || StringUtil.IsEmpty(model.ContactLastName))
|
|
{
|
|
SaLogger.Warn(fn + " Incomplete contact details supplied.");
|
|
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
|
|
}
|
|
// Check that the email doesn't exist.
|
|
// NB This is validated by the model and shouldn't be required here.
|
|
userAccount = AccountMgr.GetUserAccountByEmailAddress(model.ContactEmailAddress);
|
|
disposableItems.Add(userAccount);
|
|
if (userAccount != null)
|
|
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
|
|
userAccount = AccountMgr.CreateUserAccount();
|
|
userAccount.EmailAddress = model.ContactEmailAddress;
|
|
userAccount.FirstName = model.ContactFirstName;
|
|
userAccount.LastName = model.ContactLastName;
|
|
if (!AccountMgr.InsertUserAccount(userAccount, DbUpdateInfo))
|
|
{
|
|
SaLogger.Warn(fn + " Failed to insert user contact - " + AccountMgr.ErrorStateMessage);
|
|
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
|
|
}
|
|
}
|
|
|
|
SysId organisationSportId = model.ExtractSysId(model.Organisation.SportId);
|
|
if (SysId.IsValid(organisationSportId))
|
|
{
|
|
// Add User Sport if not already present.
|
|
// NB This will also reactivate an inactive or deleted User Sport.
|
|
SaxUserSport.Params prmUserSport = new SaxUserSport.Params();
|
|
prmUserSport.UserAccountIdsIncluded.Add(userAccount.Id);
|
|
prmUserSport.SportIdsIncluded.Add(organisationSportId);
|
|
prmUserSport.StatusCat = SaxUserSport.StatusCat.Any;
|
|
SaxUserSport userSport = AccountMgr.GetUserSport(prmUserSport);
|
|
if (userSport == null)
|
|
userSport = AccountMgr.CreateUserSport(userAccount.Id, organisationSportId);
|
|
disposableItems.Add(userSport);
|
|
userSport.Status = SaxUserSport.DefaultStatus;
|
|
if (!SysId.IsValid(userSport.Id))
|
|
{
|
|
if (!AccountMgr.InsertUserSport(userSport, DbUpdateInfo))
|
|
{
|
|
// Logg error but don't abort the request.
|
|
SaLogger.Warn(fn + " Failed to insert User Sport: " + AccountMgr.ErrorStateMessage);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!AccountMgr.UpdateUserSport(userSport, DbUpdateInfo))
|
|
{
|
|
// Logg error but don't abort the request.
|
|
SaLogger.Warn(fn + " Failed to update User Sport: " + AccountMgr.ErrorStateMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Create Organisation.
|
|
SaxOrganisation organisation = AccountMgr.CreateOrganisation();
|
|
disposableItems.Add(organisation);
|
|
organisation.Name = model.Organisation.Name;
|
|
organisation.OrganisationType = model.Organisation.GetOrganisationType();
|
|
organisation.OrganisationCategory = model.Organisation.GetOrganisationCategory();
|
|
organisation.SportIdPrimary = organisationSportId;
|
|
organisation.RegionIdPrimary = regionIdCountry;
|
|
organisation.Status = SaStatus.Awaiting;
|
|
if (!AccountMgr.InsertOrganisation(organisation, DbUpdateInfo))
|
|
{
|
|
SaLogger.Warn(fn + " Failed to insert organisation - " + AccountMgr.ErrorStateMessage);
|
|
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
|
|
}
|
|
|
|
// Add user as member of organisation.
|
|
SaxUserOrganisation userOrganisation = AccountMgr.CreateUserOrganisation(userAccount.Id, organisation.Id);
|
|
disposableItems.Add(userOrganisation);
|
|
userOrganisation.OrganisationRoles = new[] { Sax.OrganisationRole.Member, Sax.OrganisationRole.Admin };
|
|
if (!AccountMgr.InsertUserOrganisation(userOrganisation, DbUpdateInfo))
|
|
{
|
|
SaLogger.Warn(fn + " Failed to insert user organisation - " + AccountMgr.ErrorStateMessage);
|
|
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
|
|
}
|
|
|
|
// Create Organisation address.
|
|
SaxItemAddress organisationAddress = ItemDataMgr.CreateItemAddress(organisation.GetItemId(), SaxItemAddress.DefaultOrganisationAddressType);
|
|
disposableItems.Add(organisationAddress);
|
|
organisationAddress.Line1 = model.Organisation.Address.Line1;
|
|
organisationAddress.Line2 = model.Organisation.Address.Line2;
|
|
organisationAddress.Line3 = model.Organisation.Address.Line3;
|
|
organisationAddress.Town = model.Organisation.Address.Town;
|
|
if (regionProvince != null)
|
|
{
|
|
organisationAddress.Province = regionProvince.Name;
|
|
organisationAddress.RegionIdProvince = regionProvince.Id;
|
|
}
|
|
else
|
|
{
|
|
if (model.Organisation.Address.Province != null)
|
|
organisationAddress.Province = model.Organisation.Address.Province;
|
|
}
|
|
if (regionCountry != null)
|
|
{
|
|
organisationAddress.Country = regionCountry.Name;
|
|
organisationAddress.RegionIdCountry = regionCountry.Id;
|
|
}
|
|
else
|
|
{
|
|
if (model.Organisation.Address.Country != null)
|
|
organisationAddress.Country = model.Organisation.Address.Country;
|
|
}
|
|
organisationAddress.PostalCode = model.Organisation.Address.PostalCode;
|
|
if (!ItemDataMgr.InsertItemAddress(organisationAddress, DbUpdateInfo))
|
|
{
|
|
SaLogger.Warn(fn + " Failed to insert organisation address - " + ItemDataMgr.ErrorStateMessage);
|
|
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
|
|
}
|
|
}
|
|
|
|
// Complete response.
|
|
responseData.SetMessage(messageTitle, Localiser["msg.organisation.request.success"].ToString().ReplaceAll("\n", "<br/><br/>"));
|
|
responseData.Success = true;
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|