Files
Saxonite/SaxoniteCoreWebOnline/Online/Controllers/AccountController-Ajax-Organisations.cs
T
2022-05-23 21:16:36 +01:00

535 lines
26 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;
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Dbas;
using Saxonite.Net.Standard.Base.App;
namespace Saxonite.Net.Core.Web.Online.Controllers
{
//=======================================================================
// Online Account Controller - Organisations - Ajax actions.
//=======================================================================
partial class AccountController
{
//-------------------------------------------------------------------
/// <summary>Organisation properteis for list population.</summary>
private class ProfileOrganisationListData : AjaxResponse_Data.ItemData_Base
{
public String Name { get; set; }
public String CategoryDesc { get; set; }
public String TypeDesc { get; set; }
public String SportName { get; set; }
public String RegionNameCountry { get; set; }
public long MemberCount { get; set; }
public long OpportunityCount { get; set; }
public bool? Admin { get; set; } = null;
public bool? MemberAdmin { get; set; } = null;
public bool? OpportunitiyAdmin { get; set; } = null;
public bool? ScheduleAdmin { get; set; } = null;
public bool? ArticleAdmin { get; set; } = null;
}
/// <summary>Organisation properties for popup dialog and form.</summary>
private class ProfileOrganisationItemData : ProfileOrganisationListData
{
public String CategoryCode { get; set; }
public String TypeCode { get; set; }
public String SportId { get; set; }
public String RegionIdCountry { get; set; }
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; }
}
/// <summary>Populate organisation properties for list.</summary>
private void PopulateOrganisationResponseListData(SaxModel_Online model, SaxOrganisation organisation, SaxUserOrganisation userOrganisation, ProfileOrganisationListData data)
{
data.Id = model.EncryptId(organisation.Id);
data.Name = organisation.Name;
data.SportName = model.GetSportCaption(organisation.SportIdPrimary);
data.RegionNameCountry = model.GetRegionCaption(organisation.RegionIdPrimary);
if (userOrganisation != null)
{
List<String> roleCodes = userOrganisation.GetOrganisationRoleCodes();
if (roleCodes.ContainsCI(OrganisationRoleCodes.GetCodeForType(Sax.OrganisationRole.Admin)))
data.Admin = true;
if (roleCodes.ContainsCI(OrganisationRoleCodes.GetCodeForType(Sax.OrganisationRole.MemberAdmin)))
data.MemberAdmin = true;
if (roleCodes.ContainsCI(OrganisationRoleCodes.GetCodeForType(Sax.OrganisationRole.OpportunityAdmin)))
data.OpportunitiyAdmin = true;
if (roleCodes.ContainsCI(OrganisationRoleCodes.GetCodeForType(Sax.OrganisationRole.ScheduleAdmin)))
data.ScheduleAdmin = true;
if (roleCodes.ContainsCI(OrganisationRoleCodes.GetCodeForType(Sax.OrganisationRole.ArticleAdmin)))
data.ArticleAdmin = true;
}
// NB Member and Opportunity counts are only populated for the Organisation list.
data.CreatedOnFmt = Localiser.FormatDate(organisation.CreateStamp.On);
data.UpdatedOnFmt = Localiser.FormatDate(organisation.UpdateStamp.On);
data.StatusCode = organisation.StatusCode;
data.StatusDesc = organisation.StatusDesc;
}
/// <summary>Populate organisation properties for popup dialog and form.</summary>
private void PopulateOrganisationResponseItemData(SaxModel_Online model, SaxOrganisation organisation, SaxUserOrganisation userOrganisation, SaxItemAddress address, ProfileOrganisationItemData data)
{
PopulateOrganisationResponseListData(model, organisation, userOrganisation, (ProfileOrganisationListData)data);
data.CategoryCode = OrganisationCategoryCodes.GetCodeForType(organisation.OrganisationCategory);
data.CategoryDesc = Localiser.GetOrganisationCategoryCaption(organisation.OrganisationCategory);
data.TypeCode = OrganisationTypeCodes.GetCodeForType(organisation.OrganisationType);
data.TypeDesc = Localiser.GetOrganisationTypeCaption(organisation.OrganisationType);
data.SportId = model.GetEncryptedSportId(organisation.SportIdPrimary);
data.RegionIdCountry = model.GetEncryptedRegionId(organisation.RegionIdPrimary);
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 && !address.IsStatusDeleted)
{
data.Address = new AjaxResponse_Data.AddressData();
data.Address.Id = model.EncryptId(address.Id);
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;
}
}
//-------------------------------------------------------------------
// Account - Get Profile Organisations.
//-------------------------------------------------------------------
private class AjaxResponse_ProfileOrganisationList : AjaxResponse_Data
{
// Construction.
public AjaxResponse_ProfileOrganisationList(SaxModel_Online model) : base(model) { }
// Reference data.
public List<AjaxResponse_Data.ItemCodeDesc> ContactMethods { get; } = new List<ItemCodeDesc>();
// Organisation list.
public List<ProfileOrganisationListData> Organisations { get; } = new List<ProfileOrganisationListData>();
}
// Ajax request.
[HttpPost]
[Route("Account/Ajax/GetProfileOrganisations")]
[Route("Account/Ajax/GetProfileOrganisationList")]
public IActionResult GetProfileOrganisationList(Model_Account model)
{
String fn = GetType().Name + ".GetProfileOrganisationList";
AjaxResponse_ProfileOrganisationList responseData = new AjaxResponse_ProfileOrganisationList(model);
String messageTitle = Localiser["account.caption.profile.organisations"];
// Validate session and referrer.
if (!ValidReferer) return Forbid();
if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData);
RefreshModel(model);
// 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) });
}
// Record disposable items as they are created so all can be disposed when needed.
using (DisposableCollection<SacsHierarchyItem> disposableItems = new DisposableCollection<SacsHierarchyItem>())
{
// Query organisations that user is a member of.
SaxUserOrganisation.Params prmUserOrganisation = new SaxUserOrganisation.Params();
prmUserOrganisation.UserAccountIdsIncluded.Add(UserAccountId);
prmUserOrganisation.OrganisationStatusCat = SaxOrganisation.StatusCat.ActiveOnly;
prmUserOrganisation.OrganisationMembersOnly = true;
prmUserOrganisation.StatusCat = SaxUserOrganisation.StatusCat.ActiveOnly;
List<SaxUserOrganisation> userOrganisations = AccountMgr.GetUserOrganisationList(prmUserOrganisation);
List<SysId> organisationIds = userOrganisations.GetOrganisationIds();
// Item columns.
// NB Although UserOrgansiation data is queried, the response data is a list of Organisations.
ColumnDfn colId = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.OrganisationId);
ColumnDfn colOrganisationName = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.Organisation_Name);
ColumnDfn colOrganisationCategory = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.Organisation_Category);
ColumnDfn colOrganisationType = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.Organisation_Type);
ColumnDfn colOrganisationSportName = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.Organisation_Sport_Name);
ColumnDfn colOrganisationRegionName = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.Organisation_Region_Name);
ColumnDfn colMemberCount = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.Organisation_Member_Count);
ColumnDfn colOpportunityCount = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.Organisation_Opportunity_Count);
ColumnDfn colStatus = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.Status);
ColumnDfn colCreatedOn = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.CreateOn_Date);
ColumnDfn colRoleCodes = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.OrganisationRoleCodes);
List<ColumnDfn> columns = new List<ColumnDfn>()
{
colId, colOrganisationName, colOrganisationCategory, colOrganisationType,
colOrganisationSportName, colOrganisationRegionName,
colMemberCount, colOpportunityCount,
colStatus, colStatus, colCreatedOn,
colRoleCodes
};
// Sort columns.
colCreatedOn.SortAscending = false;
prmUserOrganisation.SortColumns.Add(colOrganisationName);
// Query items.
using (DbResultSet resultSet = AccountMgr.ExecuteUserOrganisationQuery(columns, prmUserOrganisation))
{
while (resultSet != null && resultSet.HasRows && resultSet.NextRecord())
{
NamedValue.Map values = resultSet.GetRecordValues();
Sax.OrganisationCategory organisationCategory = (Sax.OrganisationCategory)OrganisationCategoryCodes.GetTypeForCode(values.GetString(colOrganisationCategory.DataName));
Sax.OrganisationType organisationType = (Sax.OrganisationType)OrganisationTypeCodes.GetTypeForCode(values.GetString(colOrganisationType.DataName));
ProfileOrganisationListData organisationData = new ProfileOrganisationListData()
{
Id = model.EncryptId(new SysId(values.GetLong(colId.DataName))),
Name = values.GetString(colOrganisationName.DataName),
CategoryDesc = Localiser.GetOrganisationCategoryCaption(organisationCategory),
TypeDesc = Localiser.GetOrganisationTypeCaption(organisationType),
//SportId = model.EncryptId(new SysId(values.GetLong(colOrganisationSportId.DataName))),
SportName = values.GetString(colOrganisationSportName.DataName),
//RegionId = model.EncryptId(new SysId(values.GetLong(colOrganisationRegionId.DataName))),
RegionNameCountry = values.GetString(colOrganisationRegionName.DataName),
MemberCount = values.GetLong(colMemberCount.DataName),
OpportunityCount = values.GetLong(colOpportunityCount.DataName)
};
// Roles.
List<String> roleCodes = StringListUtil.Split(values.GetString(colRoleCodes.DataName), SaxUserOrganisation.UserOrgnisationRoleDelim);
if (roleCodes.ContainsCI(OrganisationRoleCodes.GetCodeForType(Sax.OrganisationRole.Admin)))
organisationData.Admin = true;
if (roleCodes.ContainsCI(OrganisationRoleCodes.GetCodeForType(Sax.OrganisationRole.MemberAdmin)))
organisationData.MemberAdmin = true;
if (roleCodes.ContainsCI(OrganisationRoleCodes.GetCodeForType(Sax.OrganisationRole.OpportunityAdmin)))
organisationData.OpportunitiyAdmin = true;
if (roleCodes.ContainsCI(OrganisationRoleCodes.GetCodeForType(Sax.OrganisationRole.ScheduleAdmin)))
organisationData.ScheduleAdmin = true;
if (roleCodes.ContainsCI(OrganisationRoleCodes.GetCodeForType(Sax.OrganisationRole.ArticleAdmin)))
organisationData.ArticleAdmin = true;
responseData.Organisations.Add(organisationData);
}
}
}
if (model.Paging.IsPaged)
model.Paging.ItemCount = (ushort)responseData.Organisations.Count;
// Complete response.
responseData.SetPaging(model);
responseData.Success = true;
return Json(responseData);
}
//-------------------------------------------------------------------
// Account - Get Profile Organisation.
//-------------------------------------------------------------------
private class AjaxResponse_ProfileOrganisation : AjaxResponse_Data
{
// Construction.
public AjaxResponse_ProfileOrganisation(SaxModel_Online model) : base(model) { }
// Reference data.
public List<AjaxResponse_Data.ItemCodeDesc> Categories { get; } = new List<ItemCodeDesc>();
public List<AjaxResponse_Data.ItemCodeDesc> Types { get; } = new List<ItemCodeDesc>();
// Organisation list.
public ProfileOrganisationItemData Organisation { get; set; }
}
// Ajax request.
[HttpPost]
[Route("Account/Ajax/GetProfileOrganisation")]
public IActionResult GetProfileOrganisation(Model_Account model)
{
String fn = GetType().Name + ".GetProfileOrganisation";
AjaxResponse_ProfileOrganisation responseData = new AjaxResponse_ProfileOrganisation(model);
String messageTitle = Localiser["label.organisation"];
// Validate session and referrer.
if (!ValidReferer) return Forbid();
if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData);
RefreshModel(model);
// Record disposable items as they are created so all can be disposed when needed.
using (DisposableCollection<SacsHierarchyItem> disposableItems = new DisposableCollection<SacsHierarchyItem>())
{
// Identify item.
SysId organisationId = model.ExtractSysId(model.Organisation.Id);
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: " + organisationId.ToString());
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"]));
}
// Organisation address.
SaxItemAddress address = ItemDataMgr.GetFirstItemAddressByItemAndType(organisation.GetItemId());
disposableItems.Add(address);
// Check whether user is a member of this organisation.
SaxUserOrganisation userOrganisation = AccountMgr.GetUserOrganisationByUserOrganisation(UserAccountId, organisationId);
disposableItems.Add(userOrganisation);
if (userOrganisation != null)
{
// Make a note of the selected Organisation.
SessionData_Online.Filter.SetMemberOrganisation(organisation);
}
// Construct response.
responseData.Organisation = new ProfileOrganisationItemData();
PopulateOrganisationResponseItemData(model, organisation, userOrganisation, address, responseData.Organisation);
// Reference data.
foreach (TypeCodeDesc organisationCategory in OrganisationCategoryCodes)
{
// Categories only include Partner/Other if selected organisation has that category.
if (organisationCategory.Type != (int)organisation.OrganisationCategory && organisationCategory.Type.In(new[] { (int)Sax.OrganisationCategory.Partner, (int)Sax.OrganisationCategory.Other }))
continue;
responseData.Categories.Add(new AjaxResponse_Data.ItemCodeDesc() { Code = organisationCategory.Code, Desc = Localiser.GetOrganisationCategoryCaption((Sax.OrganisationCategory)organisationCategory.Type) });
}
foreach (TypeCodeDesc organisationType in OrganisationTypeCodes)
{
responseData.Types.Add(new AjaxResponse_Data.ItemCodeDesc() { Code = organisationType.Code, Desc = Localiser.GetOrganisationTypeCaption((Sax.OrganisationType)organisationType.Type) });
}
}
// Complete response.
responseData.Success = true;
return Json(responseData);
}
//-------------------------------------------------------------------
// Account - Store Profile Organisation.
//-------------------------------------------------------------------
private class AjaxResponse_StoreProfileOrganisation : AjaxResponse_Data
{
// Construction.
public AjaxResponse_StoreProfileOrganisation(SaxModel_Online model) : base(model) { }
// Organisation list.
public ProfileOrganisationItemData Organisation { get; set; }
}
// Ajax request.
[HttpPost]
[Route("Account/Ajax/StoreProfileOrganisation")]
public IActionResult StoreProfileOrganisation(Model_Account model)
{
String fn = GetType().Name + ".StoreProfileOrganisation";
AjaxResponse_StoreProfileOrganisation responseData = new AjaxResponse_StoreProfileOrganisation(model);
String messageTitle = Localiser["label.organisation"];
// Validate session and referrer.
if (!ValidReferer) return Forbid();
if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData);
RefreshModel(model);
// Record disposable items as they are created so all can be disposed when needed.
using (DisposableCollection<SacsHierarchyItem> disposableItems = new DisposableCollection<SacsHierarchyItem>())
{
// Retrieve organisation.
SysId organisationId = model.ExtractSysId(model.Organisation.Id);
if (!SysId.IsValid(organisationId))
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.specified"]));
SaxOrganisation organisation = AccountMgr.GetOrganisationById(organisationId);
disposableItems.Add(organisation);
if (organisation == null)
{
SaLogger.Warn(fn + " Failed to retrieve Organisation for Id: " + organisationId.ToString() + ". " + GetLogSuffix());
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"]));
}
// Organisation address.
SaxItemAddress organisationAddress = ItemDataMgr.GetFirstItemAddressByItemAndType(organisation.GetItemId());
disposableItems.Add(organisationAddress);
if (!StringUtil.IsEmpty(model.Organisation.Address.Id))
{
// Validate supplied Address Id.
SysId organisationAddressId = model.ExtractSysId(model.Organisation.Address.Id);
if (organisationAddress != null)
{
if (!organisationAddress.Id.Equals(organisationAddressId))
{
SaLogger.Warn(fn + " Organisation Address Id mismatch. Supplied: " + organisationAddressId + ". Found: " + organisationAddress.Id + ". " + GetLogSuffix());
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
}
}
else
{
SaLogger.Warn(fn + " Organisation Address Id supplied (" + organisationAddressId + ") but no address found for Organisation. " + GetLogSuffix());
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
}
}
else
{
// Address Id not supplied.
if (organisationAddress == null)
{
organisationAddress = ItemDataMgr.CreateItemAddress(organisation.GetItemId(), SaxItemAddress.DefaultOrganisationAddressType);
disposableItems.Add(organisationAddress);
}
else if (!organisationAddress.IsStatusDeleted)
{
SaLogger.Warn(fn + " Organisation Address Id not supplied but no address found for Organisation. Id: " + organisationAddress.Id + ". " + GetLogSuffix());
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
}
}
// Check whether user is a member of this organisation.
SaxUserOrganisation userOrganisation = AccountMgr.GetUserOrganisationByUserOrganisation(UserAccountId, organisationId);
disposableItems.Add(userOrganisation);
if (userOrganisation == null)
{
SaLogger.Warn(fn + " Account: " + SessionData.User.EmailAddress + ". OrganisationId: " + organisationId.ToString() + ". User Organisation not found. " + GetLogSuffix());
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
}
if (!userOrganisation.GetOrganisationRoles().Contains(Sax.OrganisationRole.Admin))
{
SaLogger.Warn(fn + " Account: " + SessionData.User.EmailAddress + ". OrganisationId: " + organisationId.ToString() + ". User does not have Admin role to update organisation. " + GetLogSuffix());
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
}
// Validate organisation data.
model.ValidateProfileOrganisationData(responseData);
if (responseData.HasControlErrors())
return Json(responseData);
// Regions.
SysId regionIdCountry = model.ExtractSysId(model.Organisation.RegionIdCountry);
SaxRegion.Data regionCountry = Internals.GetRegion(regionIdCountry);
if (SysId.IsValid(regionIdCountry) && regionCountry == null)
{
SaLogger.Warn(fn + " Country region not found. Id: " + model.Organisation.RegionIdCountry + ". " + 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"]));
}
// Update organisation.
if (model.Organisation.Name != null)
organisation.Name = model.Organisation.Name;
if (model.Organisation.Type != null)
organisation.OrganisationType = model.Organisation.GetOrganisationType();
if (model.Organisation.Category != null)
organisation.OrganisationCategory = model.Organisation.GetOrganisationCategory();
if (model.Organisation.SportId != null)
organisation.SportIdPrimary = model.ExtractSysId(model.Organisation.SportId);
if (model.Organisation.RegionIdCountry != null)
organisation.RegionIdPrimary = model.ExtractSysId(model.Organisation.RegionIdCountry);
if (model.Organisation.About != null)
organisation.About = model.Organisation.About;
if (model.Organisation.Facilities != null)
organisation.Facilities = model.Organisation.Facilities;
if (model.Organisation.ContactData != null)
organisation.ContactData = model.Organisation.ContactData.GetContactData();
if (!AccountMgr.UpdateOrganisation(organisation, DbUpdateInfo))
{
SaLogger.Warn(fn + " Failed to update organisation - " + AccountMgr.ErrorStateMessage);
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
}
// Store address.
if (model.Organisation.Address.IsSpecified())
{
// The form includes address fields.
if (model.Organisation.Address.IsPopulated())
{
// At least one address field has been populated.
if (model.Organisation.Address.Line1 != null)
organisationAddress.Line1 = model.Organisation.Address.Line1;
if (model.Organisation.Address.Line2 != null)
organisationAddress.Line2 = model.Organisation.Address.Line2;
if (model.Organisation.Address.Line3 != null)
organisationAddress.Line3 = model.Organisation.Address.Line3;
if (model.Organisation.Address.Town != null)
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;
organisationAddress.Status = SaxItemAddress.DefaultStatus;
// Store address.
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"]));
}
}
}
else
{
// Address not specified - delete it.
if (!ItemDataMgr.DeleteItemAddress(organisationAddress, DbUpdateInfo))
{
SaLogger.Warn(fn + " Failed to delete organisation address - " + ItemDataMgr.ErrorStateMessage);
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
}
}
}
// Construct response.
responseData.Organisation = new ProfileOrganisationItemData();
PopulateOrganisationResponseItemData(model, organisation, userOrganisation, organisationAddress, responseData.Organisation);
}
// Complete response.
responseData.Success = true;
return Json(responseData);
}
//-------------------------------------------------------------------
}
//=======================================================================
}