351 lines
15 KiB
C#
351 lines
15 KiB
C#
using AutoMapper;
|
|
using gehGassi.Common.Data;
|
|
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Core.Services;
|
|
using gehGassi.Domain.Common;
|
|
using gehGassi.Permissions;
|
|
using gehGassi.Web.Auth;
|
|
using gehGassi.Web.Auth.Attributes;
|
|
using gehGassi.Web.Helper;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Localization;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using gehGassi.Web.Models;
|
|
using System.Threading.Tasks;
|
|
using System;
|
|
using gehGassi.Domain.Messages;
|
|
using gehGassi.External.Services;
|
|
using NetTopologySuite.Geometries;
|
|
using NetTopologySuite;
|
|
using System.IO;
|
|
|
|
namespace gehGassi.Web.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Controller für die Verwaltung von Systemnachrichten
|
|
/// </summary>
|
|
public class SystemMessageController : BaseController
|
|
{
|
|
private readonly IMapper _mapper;
|
|
private readonly IStringLocalizer<SystemMessageController> _localizer;
|
|
private readonly ISystemMessageService _systemMessageService;
|
|
private readonly ICountryService _countryService;
|
|
private readonly IAppUserService _appUserService;
|
|
private readonly IPushNotificationService _pushNotificationService;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="mapper">Instanz eines IMapper</param>
|
|
/// <param name="localizer">Instanz eines IStringLocalizer</param>
|
|
/// <param name="systemMessageService">Instanz eines ISystemMessageService</param>
|
|
/// <param name="countryService">Instanz eines ICountryService</param>
|
|
/// <param name="appUserService">Instanz eines IAppUserService</param>
|
|
/// <param name="pushNotificationService">Instanz eines IPushNotificationService</param>
|
|
public SystemMessageController(IMapper mapper, IStringLocalizer<SystemMessageController> localizer, ISystemMessageService systemMessageService, ICountryService countryService, IAppUserService appUserService,
|
|
IPushNotificationService pushNotificationService)
|
|
{
|
|
_mapper = mapper;
|
|
_localizer = localizer;
|
|
_systemMessageService = systemMessageService;
|
|
_countryService = countryService;
|
|
_appUserService = appUserService;
|
|
_pushNotificationService = pushNotificationService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt einen View für die Systemnachrichten-Verwaltung zurück
|
|
/// </summary>
|
|
/// <returns>View</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.AppUsersManage, Permission.AppUsersSystemMessages)]
|
|
public IActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von Entitäten basierend auf Abfragekriterien zurück
|
|
/// </summary>
|
|
/// <param name="dm">Abfragekriterien</param>
|
|
/// <returns>Liste von gefundenen Entitäten</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.AppUsersManage, Permission.AppUsersSystemMessages)]
|
|
[HttpPost]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult GetSystemMessages([FromBody] DataManager dm)
|
|
{
|
|
if (dm != null)
|
|
{
|
|
var propList = new List<ComplexProperty>();
|
|
dm.SetComplexProperties(propList);
|
|
|
|
if (dm.Where != null)
|
|
{
|
|
foreach (var whereFilter in dm.Where)
|
|
{
|
|
if (whereFilter.predicates == null)
|
|
continue;
|
|
foreach (var whereFilterPredicate in whereFilter.predicates)
|
|
{
|
|
if (whereFilterPredicate.Field == "sex")
|
|
whereFilterPredicate.value = (Sex)((int)((long)whereFilterPredicate.value));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var resultList = _systemMessageService.FilterSystemMessagesToSend(dm?.SearchValue ?? "");
|
|
|
|
//Sortierung
|
|
resultList = dm.ApplySorting(resultList);
|
|
//Filter
|
|
resultList = dm.ApplyFiltering(resultList, out var countFiltered);
|
|
//Paging
|
|
resultList = dm.ApplyPaging(resultList);
|
|
|
|
var resultListVm = resultList.ToList().Select(item => _mapper.Map<SystemMessageToSendListVm>(item)).ToList();
|
|
|
|
foreach (var itemVm in resultListVm)
|
|
{
|
|
itemVm.SexText = itemVm.Sex.GetDisplayName(AnnotationsLocalizer);
|
|
itemVm.AppUserTypeText = itemVm.AppUserType.GetDisplayName(AnnotationsLocalizer);
|
|
}
|
|
|
|
//FilterPreview?
|
|
if (!dm.RequiresCounts)
|
|
return Json(resultListVm);
|
|
|
|
return Json(new { result = resultListVm, count = countFiltered });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen einer Systemnachricht
|
|
/// </summary>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.AppUsersManage, Permission.AppUsersSystemMessages)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public async Task<IActionResult> Create()
|
|
{
|
|
var model = new SystemMessageToSendCrudVm()
|
|
{
|
|
AppUserType = AppUserTypeVm.Both,
|
|
Sex = SexVm.Undefined,
|
|
VerifiedOnly = false,
|
|
CountryName = _localizer["Common_All"],
|
|
StateName = _localizer["Common_All"],
|
|
};
|
|
|
|
return PartialView("_Create", model);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen einer Systemnachricht
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.AppUsersManage, Permission.AppUsersSystemMessages)]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Create(SystemMessageToSendCrudVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var item = _systemMessageService.CreateToSend();
|
|
_mapper.Map(model, item);
|
|
item.CreatedBy = User.Identity.Name;
|
|
|
|
//Zählen wie viele Nachrichten verschickt werden sollen
|
|
item.AppUserCount = await _appUserService.CountForSystemMessageAsync(new SystemMessageAppUserQuery(){AppUserType = item.AppUserType, City = item.City, Country = item.Country, Sex = item.Sex, State = item.State, VerifiedOnly = item.VerifiedOnly, Zip = item.Zip});
|
|
|
|
_systemMessageService.AddToSend(item);
|
|
await _systemMessageService.CommitAsync(User.Identity.Name);
|
|
|
|
result.Data = item.ToCamelCaseJson();
|
|
result.Success = true;
|
|
result.Html = string.Empty;
|
|
return Json(result);
|
|
}
|
|
|
|
ModelState.Remove("CountryName");
|
|
var country = _countryService.GetCountry(model.Country);
|
|
model.CountryName = country != null ? country.Name : _localizer["Common_All"].Value;
|
|
|
|
ModelState.Remove("StateName");
|
|
var state = _countryService.GetState(model.Country, model.State);
|
|
model.StateName = state != null ? state.Name : _localizer["Common_All"].Value;
|
|
|
|
result.Html = await PartialView("_Create", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten einer Systemnachricht
|
|
/// </summary>
|
|
/// <param name="id">Id der Systemnachricht</param>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.AppUsersManage, Permission.AppUsersSystemMessages)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public async Task<IActionResult> Edit(long id)
|
|
{
|
|
var item = await _systemMessageService.GetToSendAsync(id);
|
|
if (item != null)
|
|
{
|
|
var model = _mapper.Map<SystemMessageToSendCrudVm>(item);
|
|
|
|
var country = _countryService.GetCountry(item.Country);
|
|
model.CountryName = country != null ? country.Name : _localizer["Common_All"].Value;
|
|
|
|
var state = _countryService.GetState(item.Country, item.State);
|
|
model.StateName = state != null ? state.Name : _localizer["Common_All"].Value;
|
|
|
|
return PartialView("_Edit", model);
|
|
}
|
|
return PartialView("_Error");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten einer Systemnachricht
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.AppUsersManage, Permission.AppUsersSystemMessages)]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Edit(SystemMessageToSendCrudVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var item = await _systemMessageService.GetToSendAsync(model.Id);
|
|
|
|
_mapper.Map(model, item);
|
|
|
|
//Zählen wie viele Nachrichten verschickt werden sollen
|
|
item.AppUserCount = await _appUserService.CountForSystemMessageAsync(new SystemMessageAppUserQuery() { AppUserType = item.AppUserType, City = item.City, Country = item.Country, Sex = item.Sex, State = item.State, VerifiedOnly = item.VerifiedOnly, Zip = item.Zip });
|
|
|
|
await _systemMessageService.CommitAsync(User.Identity.Name);
|
|
|
|
result.Data = item.ToCamelCaseJson();
|
|
result.Success = true;
|
|
result.Html = string.Empty;
|
|
return Json(new { result.Success, result.Html, result.Data });
|
|
}
|
|
|
|
ModelState.Remove("CountryName");
|
|
var country = _countryService.GetCountry(model.Country);
|
|
model.CountryName = country != null ? country.Name : _localizer["Common_All"].Value;
|
|
|
|
ModelState.Remove("StateName");
|
|
var state = _countryService.GetState(model.Country, model.State);
|
|
model.StateName = state != null ? state.Name : _localizer["Common_All"].Value;
|
|
|
|
result.Html = await PartialView("_Edit", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen von Systemnachrichten die zu senden sind
|
|
/// </summary>
|
|
/// <param name="ids">Liste Id der Entität</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.AppUsersManage, Permission.AppUsersSystemMessages)]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Delete(List<long> ids)
|
|
{
|
|
var batchErrorHeader = $"<p><strong>{_localizer["Common_BatchDelete_Failed"].Value}</strong></p>";
|
|
var batchResult = new BatchResponseVm(batchErrorHeader) { Success = true };
|
|
|
|
foreach (var id in ids)
|
|
{
|
|
var item = await _systemMessageService.GetToSendAsync(id);
|
|
if (item != null)
|
|
{
|
|
//await _customerService.ResetTypeAsync(item.Id); //Spezial zurücksetzen wenn nötig
|
|
|
|
var specialCount = 0;
|
|
if (specialCount == 0)
|
|
{
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = item.Id.ToString(), Name = item.Title, Success = true, NotFound = false, ErrorMessage = "" });
|
|
|
|
_systemMessageService.RemoveToSend(item);
|
|
}
|
|
else
|
|
{
|
|
if (specialCount != 0)
|
|
{
|
|
batchResult.Success = false;
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = item.Title, Success = false, NotFound = false, ErrorMessage = string.Format(_localizer["Common_BatchDelete_InUse"], $"<strong>{item.Title}</strong>", $"<strong>{specialCount}</strong>") + "<br/>" });
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
batchResult.Success = false;
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = "", Success = false, NotFound = true, ErrorMessage = string.Format(_localizer["Common_BatchDelete_NotFound"], $"<strong>{id}</strong>") + "<br/>" });
|
|
}
|
|
}
|
|
if (batchResult.BatchResponseList.Any(c => c.Success))
|
|
await _systemMessageService.CommitAsync(User.Identity.Name);
|
|
return Json(batchResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Senden von Systemnachrichten
|
|
/// </summary>
|
|
/// <param name="id">Id der Systemnachricht die gesendet werden soll</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.AppUsersManage, Permission.AppUsersSystemMessages)]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Send(long id)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
var item = await _systemMessageService.GetToSendAsync(id);
|
|
if (item != null && item.HasBeenSent == false)
|
|
{
|
|
item.AppUserCount = await _appUserService.CountForSystemMessageAsync(new SystemMessageAppUserQuery() { AppUserType = item.AppUserType, City = item.City, Country = item.Country, Sex = item.Sex, State = item.State, VerifiedOnly = item.VerifiedOnly, Zip = item.Zip });
|
|
item.SentCount = item.AppUserCount;
|
|
item.HasBeenSent = true;
|
|
item.SentDate = DateTimeOffset.UtcNow;
|
|
item.SentBy = User.Identity.Name;
|
|
|
|
await _systemMessageService.CommitAsync(User.Identity.Name);
|
|
|
|
var appUsers = await _appUserService.GetForSystemMessageAsync(new SystemMessageAppUserQuery() { AppUserType = item.AppUserType, City = item.City, Country = item.Country, Sex = item.Sex, State = item.State, VerifiedOnly = item.VerifiedOnly, Zip = item.Zip });
|
|
var sentCount = 0;
|
|
foreach (var appUser in appUsers)
|
|
{
|
|
sentCount += 1;
|
|
_systemMessageService.Add(appUser.Id, appUser.Type, item.Id.ToString(), DateTimeOffset.UtcNow.AddDays(14), item.Message);
|
|
if (item.SendPushNotification)
|
|
{
|
|
await _pushNotificationService.SendNewTextMessageAsync(appUser.Id, item.Id.ToString());
|
|
}
|
|
}
|
|
|
|
if (sentCount > 0)
|
|
{
|
|
await _systemMessageService.CommitAsync(User.Identity.Name);
|
|
}
|
|
|
|
result.Success = true;
|
|
return Json(result);
|
|
}
|
|
|
|
return Json(result);
|
|
}
|
|
}
|
|
}
|