This commit is contained in:
2022-05-23 17:28:34 +01:00
parent 56f6b0bb7e
commit 25386026af
86 changed files with 2395 additions and 664 deletions
@@ -28,7 +28,7 @@ namespace Saxonite.Net.Core.Web.Admin.Controllers
// Construction.
public AjaxResponse_OrganisationActivationInfo(SaxModel_Admin model) : base(model) { }
// Organisation details.
public OrganisationData_Full organisation { get; } = new OrganisationData_Full();
public OrganisationItemData organisation { get; } = new OrganisationItemData();
// Member awaiting activation.
public List<MemberData_List> Members { get; } = new List<MemberData_List>();
}
@@ -69,8 +69,12 @@ namespace Saxonite.Net.Core.Web.Admin.Controllers
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"]));
}
// Organisation address.
SaxItemAddress address = ItemDataMgr.GetFirstItemAddressByItemAndType(organisation.GetItemId());
disposableItems.Add(address);
// Construct response.
PopulateOrganisationResponseData(organisation, responseData.organisation);
PopulateOrganisationResponseItemData(organisation, address, responseData.organisation);
GetOrganisationMembers(model, organisationId, responseData.Members);
}
@@ -119,12 +123,16 @@ namespace Saxonite.Net.Core.Web.Admin.Controllers
SysId organisationId = model.OrganisationId.ToSysId();
SaxOrganisation organisation = AccountMgr.GetOrganisationById(organisationId);
disposableItems.Add(organisation);
if (organisation == null && SysId.IsValid(organisationId))
if (organisation == null)
{
SaLogger.Warn(fn + " Failed to retrieve organisation for. : '" + model.OrganisationId + "'");
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"]));
}
// Organisation address.
SaxItemAddress address = ItemDataMgr.GetFirstItemAddressByItemAndType(organisation.GetItemId());
disposableItems.Add(address);
// Retrieve users.
List<OrganisationMemberData> organisationMembersToActivate = new List<OrganisationMemberData>();
foreach (String userOrganisationId in model.OrganisationMemberIds)
@@ -206,7 +214,7 @@ namespace Saxonite.Net.Core.Web.Admin.Controllers
}
// Construct response.
PopulateOrganisationResponseData(organisation, responseData.organisation);
PopulateOrganisationResponseItemData(organisation, address, responseData.organisation);
}
// Complete response.
@@ -21,7 +21,7 @@ namespace Saxonite.Net.Core.Web.Admin.Controllers
{
//-------------------------------------------------------------------
// Organisation data.
private class OrganisationData_List : AjaxResponse_Data.ItemData_Base
private class OrganisationListData : AjaxResponse_Data.ItemData_Base
{
public String Name { get; set; }
public String TypeCode { get; set; }
@@ -38,12 +38,14 @@ namespace Saxonite.Net.Core.Web.Admin.Controllers
// Address.
public String AddressSummary { get; set; }
}
private class OrganisationData_Full : OrganisationData_List
private class OrganisationItemData : OrganisationListData
{
public String About { get; set; }
public String Facilities { get; set; }
public List<AjaxResponse_Data.ContactMethodData> ContactMethods { get; set; }
public AjaxResponse_Data.AddressData Address { get; set; }
}
private void PopulateOrganisationResponseData(SaxOrganisation organisation, OrganisationData_List data)
private void PopulateOrganisationResponseListData(SaxOrganisation organisation, OrganisationListData data)
{
data.Id = SysId.ToString(organisation.Id);
data.Name = organisation.Name;
@@ -64,11 +66,32 @@ namespace Saxonite.Net.Core.Web.Admin.Controllers
data.StatusCode = organisation.StatusCode;
data.StatusDesc = organisation.StatusDesc;
}
private void PopulateOrganisationResponseData(SaxOrganisation organisation, OrganisationData_Full data)
private void PopulateOrganisationResponseItemData(SaxOrganisation organisation, SaxItemAddress address, OrganisationItemData data)
{
PopulateOrganisationResponseData(organisation, data as OrganisationData_List);
PopulateOrganisationResponseListData(organisation, data as OrganisationListData);
data.About = organisation.About;
data.Facilities = organisation.Facilities;
// Contact methods.
SaxContactData contactData = new SaxContactData(organisation.ContactData);
foreach (Sax.ContactMethod method in contactData.Methods)
{
if (data.ContactMethods == null) data.ContactMethods = new List<AjaxResponse_Data.ContactMethodData>();
data.ContactMethods.Add(new AjaxResponse_Data.ContactMethodData() { Method = method.ToString(), MethodCaption = Localiser.GetContactMethodCaption(method), Value = contactData.GetContactMethodValue(method) });
}
// Address.
if (address != null)
{
data.Address = new AjaxResponse_Data.AddressData();
data.Address.Id = address.Id.ToString();
data.Address.Line1 = address.Line1;
data.Address.Line2 = address.Line2;
data.Address.Line3 = address.Line3;
data.Address.Town = address.Town;
data.Address.Province = address.Province;
data.Address.Country = address.Country;
data.Address.PostalCode = address.PostalCode;
}
// Common properties.
data.Notes = organisation.Notes;
}
@@ -80,8 +103,9 @@ namespace Saxonite.Net.Core.Web.Admin.Controllers
// Construction.
public AjaxResponse_OrganisationList(SaxModel_Admin model) : base(model) { }
// Organisation list.
public List<OrganisationData_List> Organisations { get; } = new List<OrganisationData_List>();
public List<OrganisationListData> Organisations { get; } = new List<OrganisationListData>();
// Reference data.
public List<AjaxResponse_Data.ItemCodeDesc> ContactMethods { get; } = new List<ItemCodeDesc>();
public List<CodeDesc> Statuses { get; } = new List<CodeDesc>();
public String DefaultStatusCode { get; set; }
}
@@ -98,6 +122,15 @@ namespace Saxonite.Net.Core.Web.Admin.Controllers
if (!ValidUserSession)
return GetAjaxResult_InvalidSession(responseData);
// Reference data - contact methods.
List<Sax.ContactMethod> validContactMethods = SaxOrganisation.ValidContactMethods;
foreach (TypeCodeDesc contactMethod in ContactMethodCodes)
{
// Skip methods that aren't associated with Organisations.
if (!validContactMethods.Contains((Sax.ContactMethod)contactMethod.Type)) continue;
responseData.ContactMethods.Add(new AjaxResponse_Data.ItemCodeDesc() { Code = contactMethod.Code, Desc = Localiser.GetContactMethodCaption((Sax.ContactMethod)contactMethod.Type) });
}
// Reference data - statuses.
foreach (SaStatus status in SaxOrganisation.ValidStatuses.FindAll(item => item != SaStatus.Deleted))
responseData.Statuses.Add(SacsSystem.GetStatusCode(status), Localiser.GetStatusCaption(status));
@@ -149,7 +182,7 @@ namespace Saxonite.Net.Core.Web.Admin.Controllers
while (resultSet != null && resultSet.HasRows && resultSet.NextRecord())
{
NamedValue.Map values = resultSet.GetRecordValues();
OrganisationData_List organisationData = new OrganisationData_List()
OrganisationListData organisationData = new OrganisationListData()
{
Id = values.GetLong(colId.DataName).ToString(),
Name = values.GetString(colName.DataName),
@@ -185,7 +218,7 @@ namespace Saxonite.Net.Core.Web.Admin.Controllers
// Construction.
public AjaxResponse_Organisation(SaxModel_Admin model) : base(model) { }
// Organisation details.
public OrganisationData_Full organisation { get; } = new OrganisationData_Full();
public OrganisationItemData organisation { get; } = new OrganisationItemData();
}
//-------------------------------------------------------------------
[HttpPost]
@@ -200,25 +233,34 @@ namespace Saxonite.Net.Core.Web.Admin.Controllers
if (!ValidUserSession)
return GetAjaxResult_InvalidSession(responseData);
// Identify item.
SysId organisationId = model.OrganisationId.ToSysId();
if (!SysId.IsValid(organisationId))
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.specified"]));
// Retrieve item.
SaxOrganisation organisation = AccountMgr.GetOrganisationById(organisationId);
if (organisation == null)
// Record disposable items as they are created so all can be disposed when needed.
using (DisposableCollection<SacsHierarchyItem> disposableItems = new DisposableCollection<SacsHierarchyItem>())
{
SaLogger.Warn(fn + " Failed to retrieve organisation for Id: '" + model.OrganisationId + "'");
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"]));
// Identify item.
SysId organisationId = model.OrganisationId.ToSysId();
if (!SysId.IsValid(organisationId))
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.specified"]));
// Retrieve item.
SaxOrganisation organisation = AccountMgr.GetOrganisationById(organisationId);
disposableItems.Add(organisation);
if (organisation == null)
{
SaLogger.Warn(fn + " Failed to retrieve organisation for Id: '" + model.OrganisationId + "'");
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"]));
}
// Organisation address.
SaxItemAddress address = ItemDataMgr.GetFirstItemAddressByItemAndType(organisation.GetItemId());
disposableItems.Add(address);
// Construct response.
PopulateOrganisationResponseItemData(organisation, address, responseData.organisation);
// Complete response.
responseData.Success = true;
}
// Construct response.
PopulateOrganisationResponseData(organisation, responseData.organisation);
// Complete response.
responseData.Success = true;
return Json(responseData);
}
@@ -237,69 +279,148 @@ namespace Saxonite.Net.Core.Web.Admin.Controllers
if (!ValidUserSession)
return GetAjaxResult_InvalidSession(responseData);
// Validate item properties.
model.ValidateOrganisationData(responseData);
if (responseData.HasControlErrors())
return Json(responseData);
// Retrieve existing item.
SysId organisationId = model.OrganisationId.ToSysId();
SaxOrganisation organisation = AccountMgr.GetOrganisationById(organisationId);
if (organisation == null && SysId.IsValid(organisationId))
// Record disposable items as they are created so all can be disposed when needed.
using (DisposableCollection<SacsHierarchyItem> disposableItems = new DisposableCollection<SacsHierarchyItem>())
{
SaLogger.Warn(fn + " Failed to retrieve organisation for Id: '" + model.OrganisationId + "'");
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"]));
}
// Create new item if required.
if (organisation == null)
organisation = AccountMgr.CreateOrganisation();
// Validate item properties.
model.ValidateOrganisationData(responseData);
if (responseData.HasControlErrors())
return Json(responseData);
// Populate item properties.
if (model.OrganisationName != null)
organisation.Name = model.OrganisationName;
if (model.OrganisationTypeValue != null)
organisation.OrganisationType = model.OrganisationType;
if (model.OrganisationCategoryValue != null)
organisation.OrganisationCategory = model.OrganisationCategory;
if (model.OrganisationSportId != null)
organisation.SportIdPrimary = model.OrganisationSportId.ToSysId();
if (model.OrganisationRegionIdCountry != null)
organisation.RegionIdPrimary = model.OrganisationRegionIdCountry.ToSysId();
if (model.OrganisationAbout != null)
organisation.About = model.OrganisationAbout;
if (model.OrganisationFacilities != null)
organisation.Facilities = model.OrganisationFacilities;
// Common properties.
if (model.OrganisationStatusCode != null)
organisation.Status = model.OrganisationStatus;
if (model.OrganisationNotes != null)
organisation.Notes = model.OrganisationNotes;
// Store item.
if (!SysId.IsValid(organisation.Id))
{
if (!AccountMgr.InsertOrganisation(organisation, DbUpdateInfo))
// Retrieve existing item.
SysId organisationId = model.OrganisationId.ToSysId();
SaxOrganisation organisation = AccountMgr.GetOrganisationById(organisationId);
disposableItems.Add(organisation);
if (organisation == null && SysId.IsValid(organisationId))
{
SaLogger.Warn(fn + " Failed to insert organisation: " + AccountMgr.ErrorStateMessage);
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.insert.failed"] + "\n\n" + AccountMgr.ErrorStateMessage));
SaLogger.Warn(fn + " Failed to retrieve organisation for Id: '" + model.OrganisationId + "'");
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"]));
}
}
else
{
if (!AccountMgr.UpdateOrganisation(organisation, DbUpdateInfo))
// Create new item if required.
if (organisation == null)
{
SaLogger.Warn(fn + " Failed to update organisation [Id:" + SysId.ToString(organisation.Id) + "]: " + AccountMgr.ErrorStateMessage);
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.update.failed"] + "\n\n" + AccountMgr.ErrorStateMessage));
organisation = AccountMgr.CreateOrganisation();
disposableItems.Add(organisation);
}
// Organisation address.
SaxItemAddress organisationAddress = ItemDataMgr.GetFirstItemAddressByItemAndType(organisation.GetItemId());
disposableItems.Add(organisationAddress);
// Populate item properties.
if (model.OrganisationName != null)
organisation.Name = model.OrganisationName;
if (model.OrganisationTypeValue != null)
organisation.OrganisationType = model.OrganisationType;
if (model.OrganisationCategoryValue != null)
organisation.OrganisationCategory = model.OrganisationCategory;
if (model.OrganisationSportId != null)
organisation.SportIdPrimary = model.OrganisationSportId.ToSysId();
if (model.OrganisationRegionIdCountry != null)
organisation.RegionIdPrimary = model.OrganisationRegionIdCountry.ToSysId();
if (model.OrganisationAbout != null)
organisation.About = model.OrganisationAbout;
if (model.OrganisationFacilities != null)
organisation.Facilities = model.OrganisationFacilities;
if (model.ContactData != null)
organisation.ContactData = model.ContactData.GetContactData();
// Common properties.
if (model.OrganisationStatusCode != null)
organisation.Status = model.OrganisationStatus;
if (model.OrganisationNotes != null)
organisation.Notes = model.OrganisationNotes;
// Store item.
if (!SysId.IsValid(organisation.Id))
{
if (!AccountMgr.InsertOrganisation(organisation, DbUpdateInfo))
{
SaLogger.Warn(fn + " Failed to insert organisation: " + AccountMgr.ErrorStateMessage);
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.insert.failed"] + "\n\n" + AccountMgr.ErrorStateMessage));
}
}
else
{
if (!AccountMgr.UpdateOrganisation(organisation, DbUpdateInfo))
{
SaLogger.Warn(fn + " Failed to update organisation [Id:" + SysId.ToString(organisation.Id) + "]: " + AccountMgr.ErrorStateMessage);
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.update.failed"] + "\n\n" + AccountMgr.ErrorStateMessage));
}
}
if (model.Address.IsSpecified())
{
// Organisation regions.
SaxRegion.Data regionCountry = Internals.GetRegion(organisation.RegionIdPrimary);
if (regionCountry != null && regionCountry.RegionType != Sax.RegionType.Country)
regionCountry = null;
SaxRegion.Data regionProvince = null;
// Create Organisation address if required.
if (organisationAddress == null)
{
organisationAddress = ItemDataMgr.CreateItemAddress(organisation.GetItemId(), SaxItemAddress.DefaultOrganisationAddressType);
disposableItems.Add(organisationAddress);
}
// Store address.
if (model.Address.Line1 != null)
organisationAddress.Line1 = model.Address.Line1;
if (model.Address.Line2 != null)
organisationAddress.Line2 = model.Address.Line2;
if (model.Address.Line3 != null)
organisationAddress.Line3 = model.Address.Line3;
if (model.Address.Town != null)
organisationAddress.Town = model.Address.Town;
if (regionProvince != null)
{
organisationAddress.Province = regionProvince.Name;
organisationAddress.RegionIdProvince = regionProvince.Id;
}
else
{
if (model.Address.Province != null)
organisationAddress.Province = model.Address.Province;
}
if (regionCountry != null)
{
organisationAddress.Country = regionCountry.Name;
organisationAddress.RegionIdCountry = regionCountry.Id;
}
else
{
if (model.Address.Country != null)
organisationAddress.Country = model.Address.Country;
}
organisationAddress.PostalCode = model.Address.PostalCode;
if (!SysId.IsValid(organisationAddress.Id))
{
// Create new address.
if (!ItemDataMgr.InsertItemAddress(organisationAddress, DbUpdateInfo))
{
SaLogger.Warn(fn + " Failed to insert organisation address - " + ItemDataMgr.ErrorStateMessage);
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
}
}
else
{
// Update existing address.
if (!ItemDataMgr.UpdateItemAddress(organisationAddress, DbUpdateInfo))
{
SaLogger.Warn(fn + " Failed to update organisation address - " + ItemDataMgr.ErrorStateMessage);
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
}
}
}
// Construct response.
PopulateOrganisationResponseItemData(organisation, organisationAddress, responseData.organisation);
// Complete response.
responseData.Success = true;
}
// Construct response.
PopulateOrganisationResponseData(organisation, responseData.organisation);
// Complete response.
responseData.Success = true;
return Json(responseData);
}
@@ -0,0 +1,64 @@
using Microsoft.AspNetCore.Mvc;
using Sarand.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.Utils;
using Saxonite.Net.Core.Web.Admin.Models;
using Saxonite.Net.Core.Web.Base.Models;
using Saxonite.Net.Standard.Base.App;
using Saxonite.Net.Standard.Base.Helpers;
using System;
namespace Saxonite.Net.Core.Web.Admin.Controllers
{
//=======================================================================
// Admin Reference Controller - Contact Method Ajax actions only.
//=======================================================================
partial class ReferenceController
{
//-------------------------------------------------------------------
// Validate Contact Method.
//-------------------------------------------------------------------
private class AjaxResponse_ValidateContactMethod : AjaxResponse_Data
{
public AjaxResponse_ValidateContactMethod(SaxModel_Base model) : base(model) { }
}
[HttpPost]
[Route("Reference/Ajax/ValidateContactMethod")]
public IActionResult Ajax_ValidateContactMethod(Model_ContactMethod model)
{
String fn = GetType().Name + ".Ajax_ValidateContactMethod";
String messageTitle = Localiser["caption.contact.method"];
AjaxResponse_ValidateContactMethod responseData = new AjaxResponse_ValidateContactMethod(model);
// Validate session and referrer.
if (!ValidReferer) return Forbid();
if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData);
// Validate method.
Sax.ContactMethod method = EnumUtil.ParseEnum(model.ContactMethod, Sax.ContactMethod.Unknown);
if (model.ContactMethod.IsEmpty() || (int)method < 1)
responseData.SetControlError(nameof(model.ContactMethod), Localiser["msg.validation.mandatory.value"]);
// Validate method.
if (model.ContactValue.IsEmpty())
responseData.SetControlError(nameof(model.ContactValue), Localiser["msg.validation.mandatory.value"]);
// Method specific value validation.
if (!SaxContactMethodHelper.IsValidContactMethodValue(method, model.ContactValue))
{
// Log invalid values so that the validation can be adjusted if needed.
SaLogger.Warn(fn + " Invalid contact method value. Mathod: " + method.ToString() + ". Value: " + model.ContactValue);
responseData.SetControlError(nameof(model.ContactValue), Localiser["msg.validation.invalid.value"]);
}
if (responseData.HasControlErrors())
return Json(responseData);
// Complete response.
responseData.Success = true;
return Json(responseData);
}
//-------------------------------------------------------------------
}
//=======================================================================
}
@@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Sarand.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.Bus;
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Dbas;
using Saxonite.Net.Core.Web.Base.App;
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.Admin.Controllers
{
//=======================================================================
// Admin Reference Controller - Ajax actions only.
//=======================================================================
public partial class ReferenceController : SaxController_Admin
{
//-------------------------------------------------------------------
// Construction.
public ReferenceController(SaxLocaliser localiser, ILogger<HomeController> logger) :
base(localiser, logger)
{}
//-------------------------------------------------------------------
}
//=======================================================================
}