Files
Saxonite/SaxoniteCoreWebOnline/Online/Controllers/OrganisationController-Ajax-Events.cs
T
2021-12-08 18:23:15 +00:00

440 lines
20 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.Dbas;
using Sarand.Net.Standard.Base.Utils;
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 - Events - Ajax actions.
//=======================================================================
partial class OrganisationController
{
//-------------------------------------------------------------------
// Organisation Event data.
private class EventData_List : AjaxResponse_Data.ItemData_Base
{
public String EventDateFmt { get; set; }
public String EventTimeFmt { get; set; }
public String TypeCode { get; set; }
public String TypeDesc { get; set; }
public String Summary { get; set; }
public String Details { get; set; }
public String SportCaptionPrimary { get; set; }
public String RegionCaptionCountry { get; set; }
public String PublishedOnFmt { get; set; }
public String ExpiresOnFmt { get; set; }
}
private class EventData_Detail : EventData_List
{
public String EventOn_Calendar { get; set; }
public String EventOn_YYYYMMDD { get; set; }
public String EventAt_HHMM { get; set; }
public String SportIdPrimary { get; set; }
public String RegionIdCountry { get; set; }
public String PublishedOn_YYYYMMDD { get; set; }
public String ExpiresOn_YYYYMMDD { get; set; }
}
private void PopulateEventResponseData(Model_Organisation_Events model, SaxOrganisationEvent organisationEvent, EventData_List data)
{
data.Id = model.EncryptId(organisationEvent.Id);
data.EventDateFmt = Localiser.FormatDate(organisationEvent.EventAt);
data.EventTimeFmt = DateUtil.Format(organisationEvent.EventAt, "HH:mm");
data.TypeCode = EventTypeCodes.GetCodeForType(organisationEvent.EventType);
data.TypeDesc = Localiser.GetEventTypeCaption(organisationEvent.EventType);
data.Summary = organisationEvent.Summary;
data.Details = organisationEvent.Details;
data.SportCaptionPrimary = model.GetSportCaption(organisationEvent.SportIdPrimary);
data.RegionCaptionCountry = model.GetRegionCaption(organisationEvent.RegionIdCountry);
data.CreatedOnFmt = Localiser.FormatDate(organisationEvent.CreateStamp.On);
data.PublishedOnFmt = Localiser.FormatDate(organisationEvent.PublishedAt);
data.ExpiresOnFmt = Localiser.FormatDate(organisationEvent.ExpiresAt);
data.StatusCode = organisationEvent.StatusCode;
data.StatusDesc = organisationEvent.StatusDesc;
}
private void PopulateEventResponseData(Model_Organisation_Events model, SaxOrganisationEvent organisationEvent, EventData_Detail data)
{
PopulateEventResponseData(model, organisationEvent, data as EventData_List);
data.EventOn_Calendar = model.FormatDateForForm(organisationEvent.EventAt);
data.EventOn_YYYYMMDD = DateUtil.Format(organisationEvent.EventAt, "yyyyMMdd");
data.EventAt_HHMM = DateUtil.Format(organisationEvent.EventAt, "HH:mm");
data.RegionIdCountry = model.GetEncryptedRegionId(organisationEvent.RegionIdCountry);
data.SportIdPrimary = model.GetEncryptedSportId(organisationEvent.SportIdPrimary);
data.PublishedOn_YYYYMMDD = DateUtil.Format(organisationEvent.PublishedAt, "yyyyMMdd");
data.ExpiresOn_YYYYMMDD = DateUtil.Format(organisationEvent.ExpiresAt, "yyyyMMdd");
}
//-------------------------------------------------------------------
// Get Event List ajax request.
//-------------------------------------------------------------------
private class AjaxResponse_EventList : AjaxResponse_Data
{
// Construction.
public AjaxResponse_EventList(SaxModel_Online model) : base(model) { }
// Event list.
public List<EventData_List> Events { get; } = new List<EventData_List>();
// User profile.
public class UserData
{
public bool CanManage { get; set; }
}
public UserData User { get; } = new UserData();
// Organisation.
public class OrganisationData
{
public String Id { get; set; }
public String Name { get; set; }
public String SportId { get; set; }
public String SportName { get; set; }
public String RegionId { get; set; }
public String RegionName { get; set; }
}
public OrganisationData Organisation { get; } = new OrganisationData();
// Reference data.
public List<CodeDesc> EventTypes { get; } = new List<CodeDesc>();
}
//-------------------------------------------------------------------
[HttpPost]
[Route("Organisation/Ajax/GetEventList")]
[Route("Organisation/Ajax/GetEvents")]
public IActionResult Ajax_GetEventList(Model_Organisation_Events model)
{
String fn = GetType().Name + ".Ajax_GetEventList";
String messageTitle = Localiser["organisation.caption.events"];
AjaxResponse_EventList responseData = new AjaxResponse_EventList(model);
// Validate session and referrer.
if (!ValidReferer) return Forbid();
if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData);
RefreshModel(model);
SysId userAccountId = UserAccountId;
// Record disposable items as they are created so all can be disposed when needed.
using (DisposableCollection<SacsHierarchyItem> disposableItems = new DisposableCollection<SacsHierarchyItem>())
{
// Organisation.
SysId organisationId = model.ExtractSysId(model.Filter.OrganisationId);
SaxOrganisation organisation = AccountMgr.GetOrganisationById(organisationId);
disposableItems.Add(organisation);
if (organisation == null || !organisation.IsStatusActive)
{
SaLogger.Warn(fn + " " + (organisation == null ? "Invalid" : "Inactive") + " organisation for Event list. OrganisationId: '" + model.Filter.OrganisationId + "'");
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.system"]));
}
if (organisation != null)
{
// Store filter data.
SessionData_Online.Filter.SetMemberOrganisation(organisation);
// Reference data - organisationEvent types.
foreach (TypeCodeDesc eventType in EventTypeCodes)
responseData.EventTypes.Add(eventType.Code, Localiser.GetEventTypeCaption((Sax.EventType)eventType.Type));
// User organisation.
SaxUserOrganisation.Params prmUserOrganisation = new SaxUserOrganisation.Params();
prmUserOrganisation.UserAccountIdsIncluded.Add(userAccountId);
prmUserOrganisation.OrganisationIdsIncluded.Add(organisationId);
prmUserOrganisation.UserAccountStatusCat = SaxUserAccount.StatusCat.ActiveOnly;
prmUserOrganisation.StatusCat = SaxUserOrganisation.StatusCat.ActiveOnly;
SaxUserOrganisation userOrganisation = AccountMgr.GetUserOrganisation(prmUserOrganisation);
disposableItems.Add(userOrganisation);
if (userOrganisation == null)
{
SaLogger.Warn(fn + " Invalid/inactive user organisation for Event list. UserAccountId: " + SysId.ToString(userAccountId) + ". OrganisationId: " + model.Filter.OrganisationId + ".");
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.system"]));
}
// Organisation.
responseData.Organisation.Id = SysId.ToString(organisation.Id);
responseData.Organisation.Name = organisation.Name;
responseData.Organisation.SportId = model.GetEncryptedSportId(organisation.SportIdPrimary);
responseData.Organisation.SportName = model.GetSportCaption(organisation.SportIdPrimary);
responseData.Organisation.RegionId = model.GetEncryptedRegionId(organisation.RegionIdPrimary);
responseData.Organisation.RegionName = model.GetRegionCaption(organisation.RegionIdPrimary);
// User details.
responseData.User.CanManage = userOrganisation.CanManageSchedule;
// Item filter.
SaxOrganisationEvent.Params prmEvent = new SaxOrganisationEvent.Params();
prmEvent.UserAccountIdsIncluded.Add(userAccountId);
prmEvent.OrganisationIdsIncluded.Add(organisationId);
prmEvent.EventFilter = model.Filter.Event;
prmEvent.StatusCat = SaxOrganisationEvent.StatusCat.NotDeleted;
model.Paging.ItemTotal = (ulong)ActivityMgr.GetOrganisationEventCount(prmEvent);
model.PopulatePagingTo(prmEvent.ResultsPaging);
// Item columns.
ColumnDfn colId = ActivityMgr.GetOrganisationEventColumnDfn(SaxOrganisationEvent.Column.Id);
ColumnDfn colEventAt = ActivityMgr.GetOrganisationEventColumnDfn(SaxOrganisationEvent.Column.EventAt_DateTime);
ColumnDfn colEventTypeCode = ActivityMgr.GetOrganisationEventColumnDfn(SaxOrganisationEvent.Column.EventTypeCode);
ColumnDfn colSummary = ActivityMgr.GetOrganisationEventColumnDfn(SaxOrganisationEvent.Column.Summary);
ColumnDfn colDetails = ActivityMgr.GetOrganisationEventColumnDfn(SaxOrganisationEvent.Column.Details);
ColumnDfn colRegionId = ActivityMgr.GetOrganisationEventColumnDfn(SaxOrganisationEvent.Column.RegionIdCountry);
ColumnDfn colRegionName = ActivityMgr.GetOrganisationEventColumnDfn(SaxOrganisationEvent.Column.Region_Name);
ColumnDfn colSportId = ActivityMgr.GetOrganisationEventColumnDfn(SaxOrganisationEvent.Column.SportIdPrimary);
ColumnDfn colSportName = ActivityMgr.GetOrganisationEventColumnDfn(SaxOrganisationEvent.Column.Sport_Name);
ColumnDfn colPublishedOn = ActivityMgr.GetOrganisationEventColumnDfn(SaxOrganisationEvent.Column.PublishedAt_Date);
ColumnDfn colExpiresOn = ActivityMgr.GetOrganisationEventColumnDfn(SaxOrganisationEvent.Column.ExpiresAt_Date);
ColumnDfn colStatus = ActivityMgr.GetOrganisationEventColumnDfn(SaxOrganisationEvent.Column.Status);
ColumnDfn colCreatedOn = ActivityMgr.GetOrganisationEventColumnDfn(SaxOrganisationEvent.Column.CreateOn_Date);
List<ColumnDfn> columns = new List<ColumnDfn>()
{
colId, colEventAt, colEventTypeCode, colSummary, colDetails,
colRegionId, colRegionName, colSportId, colSportName,
colPublishedOn, colExpiresOn,
colStatus, colCreatedOn
};
// Sort columns.
colCreatedOn.SortAscending = false;
prmEvent.SortColumns.Add(colCreatedOn);
// Query items.
using (DbResultSet resultSet = ActivityMgr.ExecuteOrganisationEventQuery(columns, prmEvent))
{
while (resultSet != null && resultSet.HasRows && resultSet.NextRecord())
{
NamedValue.Map values = resultSet.GetRecordValues();
EventData_List eventData = new EventData_List()
{
Id = model.EncryptId(new SysId(values.GetLong(colId.DataName))),
EventDateFmt = Localiser.FormatDate(values.GetDateTime(colEventAt.DataName)),
EventTimeFmt = DateUtil.Format(values.GetDateTime(colEventAt.DataName), "HH:mm"),
TypeCode = values.GetString(colEventTypeCode.DataName),
TypeDesc = Localiser.GetEventTypeCaption((Sax.EventType)EventTypeCodes.GetTypeForCode(values.GetString(colEventTypeCode.DataName))),
Summary = ActivityMgr.GetOrganisationEventDataValue(colSummary, values).AsString(),
Details = ActivityMgr.GetOrganisationEventDataValue(colDetails, values).AsString(),
RegionCaptionCountry = model.GetRegionCaption(values.GetString(colRegionId.DataName).ToSysId()),
SportCaptionPrimary = model.GetSportCaption(values.GetString(colSportId.DataName).ToSysId()),
CreatedOnFmt = Localiser.FormatDate(values.GetDateTime(colCreatedOn.DataName)),
PublishedOnFmt = Localiser.FormatDate(values.GetDateTime(colPublishedOn.DataName)),
ExpiresOnFmt = Localiser.FormatDate(values.GetDateTime(colExpiresOn.DataName)),
StatusCode = values.GetString(colStatus.DataName),
StatusDesc = SacsSystem.GetStatusDescForCode(values.GetString(colStatus.DataName))
};
responseData.Events.Add(eventData);
}
}
}
}
if (model.Paging.IsPaged)
model.Paging.ItemCount = (ushort)responseData.Events.Count;
// Complete response.
responseData.SetPaging(model);
responseData.Success = true;
return Json(responseData);
}
//-------------------------------------------------------------------
// Get Event ajax request.
//-------------------------------------------------------------------
private class AjaxResponse_Event : AjaxResponse_Data
{
// Construction.
public AjaxResponse_Event(SaxModel_Online model) : base(model) { }
// Event details.
public EventData_Detail Event { get; } = new EventData_Detail();
}
//-------------------------------------------------------------------
[HttpPost]
[Route("Organisation/Ajax/GetEvent")]
public IActionResult Ajax_GetEvent(Model_Organisation_Events model)
{
String fn = GetType().Name + ".Ajax_GetEvent";
String messageTitle = Localiser["organisation.caption.organisationEvent"];
AjaxResponse_Event responseData = new AjaxResponse_Event(model);
// 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 eventId = model.ExtractSysId(model.EventId);
if (!SysId.IsValid(eventId))
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.specified"]));
// Retrieve item.
SaxOrganisationEvent organisationEvent = ActivityMgr.GetOrganisationEventById(eventId);
disposableItems.Add(organisationEvent);
if (organisationEvent == null)
{
SaLogger.Warn(fn + " Failed to retrieve Organisation Event for Id: '" + model.EventId + "'");
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"]));
}
// Validate organisation.
SaxOrganisation organisation = AccountMgr.GetOrganisationById(organisationEvent.OrganisationId);
disposableItems.Add(organisation);
if (organisation == null || !organisation.IsStatusActive)
{
SaLogger.Warn(fn + " " + (organisation == null ? "Invalid" : "Inactive") + " organisation for Event. OrganisationId: '" + organisationEvent.OrganisationId + "'");
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.system"]));
}
// Make a note of the selected Organisation.
SessionData_Online.Filter.SetMemberOrganisation(organisation);
// Construct response.
PopulateEventResponseData(model, organisationEvent, responseData.Event);
}
// Complete response.
responseData.Success = true;
return Json(responseData);
}
//-------------------------------------------------------------------
// Store Event ajax request.
//-------------------------------------------------------------------
[HttpPost]
[Route("Organisation/Ajax/StoreEvent")]
public IActionResult Ajax_StoreEvent(Model_Organisation_Events model)
{
String fn = GetType().Name + ".Ajax_StoreEvent";
String messageTitle = Localiser["organisation.caption.organisationEvent"];
AjaxResponse_Event responseData = new AjaxResponse_Event(model);
// 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>())
{
// Validate item properties.
model.ValidateEventData(responseData);
if (responseData.HasControlErrors())
return Json(responseData);
// Validate Event against Id against 'Exists' flag.
SysId eventId = model.ExtractSysId(model.EventId);
bool eventExists = StringUtil.ToBool(model.EventExists);
if (eventExists && !SysId.IsValid(eventId))
{
SaLogger.Warn(fn + " Id not specified for existing Event.");
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
}
if (!eventExists && SysId.IsValid(eventId))
{
SaLogger.Warn(fn + " Id specified for new Event: '" + model.EventId + "'");
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
}
// Retrieve existing item.
SaxOrganisationEvent organisationEvent = ActivityMgr.GetOrganisationEventById(eventId);
disposableItems.Add(organisationEvent);
if (organisationEvent == null && SysId.IsValid(eventId))
{
SaLogger.Warn(fn + " Failed to retrieve organisationEvent for Id: '" + model.EventId + "'");
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"]));
}
// Validate the Organisation.
SysId organisationId = model.ExtractSysId(model.EventOrganisationId);
if (!SysId.IsValid(organisationId))
{
SaLogger.Warn(fn + " Organisation Id not specified for Event.");
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
}
SaxOrganisation organisation = AccountMgr.GetOrganisationById(organisationId);
disposableItems.Add(organisation);
if (organisation == null || !organisation.IsStatusActive)
{
SaLogger.Warn(fn + " " + (organisation == null ? "Invalid" : "Inactive") + " organisation for Event. OrganisationId: '" + model.EventOrganisationId + "'");
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.system"]));
}
if (organisationEvent != null && !organisationEvent.OrganisationId.Equals(organisationId))
{
SaLogger.Warn(fn + " Inconsistent Organisation Id specified for Event. Expected: " + organisationEvent.OrganisationId + ". Found: " + organisationId);
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
}
// Ensure that the user has the correct role to update Events for this organisation.
if (!AccountMgr.UserHasAnyOrganisationRole(UserAccountId, organisationId, new[] { Sax.OrganisationRole.Admin, Sax.OrganisationRole.ScheduleAdmin }))
{
SaLogger.Warn(fn + " User doesn't have 'Event Admin' role for Organisation Id: " + organisationId);
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
}
// Create new item if required.
if (organisationEvent == null)
{
organisationEvent = ActivityMgr.CreateOrganisationEvent(organisationId);
disposableItems.Add(organisationEvent);
}
// Populate item properties.
if (model.EventSportIdPrimary != null)
organisationEvent.SportIdPrimary = model.ExtractSysId(model.EventSportIdPrimary);
if (model.EventRegionIdCountry != null)
organisationEvent.RegionIdCountry = model.ExtractSysId(model.EventRegionIdCountry);
if (model.EventDate != null)
{
organisationEvent.EventAt = model.ParseDateFromForm(model.EventDate);
if (!StringUtil.IsEmpty(model.EventTime))
organisationEvent.EventAt = DateUtil.SetTime(organisationEvent.EventAt, DateUtil.ParseExact(model.EventTime, "HH:mm"));
}
if (model.EventTypeCode != null)
organisationEvent.EventType = (Sax.EventType)EventTypeCodes.GetTypeForCode(model.EventTypeCode);
if (model.EventSummary != null)
organisationEvent.Summary = model.EventSummary;
if (model.EventDetails != null)
organisationEvent.Details = model.EventDetails;
if (model.EventStatusCode != null)
organisationEvent.Status = model.EventStatus;
// Store item.
if (!SysId.IsValid(organisationEvent.Id))
{
if (!ActivityMgr.InsertOrganisationEvent(organisationEvent, DbUpdateInfo))
{
SaLogger.Warn(fn + " Failed to insert organisationEvent: " + ActivityMgr.ErrorStateMessage);
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.insert.failed"]));
}
}
else
{
if (!ActivityMgr.UpdateOrganisationEvent(organisationEvent, DbUpdateInfo))
{
SaLogger.Warn(fn + " Failed to update organisationEvent [Id:" + SysId.ToString(organisationEvent.Id) + "]: " + ActivityMgr.ErrorStateMessage);
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.update.failed"]));
}
}
// Store Event Activity Index data.
ActivityConsole.StoreActivityIndex(organisationEvent, DbUpdateInfo);
// Construct response.
PopulateEventResponseData(model, organisationEvent, responseData.Event);
}
// Complete response.
responseData.Success = true;
return Json(responseData);
}
//-------------------------------------------------------------------
}
//=======================================================================
}