90 lines
3.6 KiB
C#
90 lines
3.6 KiB
C#
using System.Text.Json;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Nop.Core.Domain.Customers;
|
|
using Nop.Plugin.Misc.FruitBankPlugin.Models;
|
|
using Nop.Plugin.Misc.FruitBankPlugin.Services;
|
|
using Nop.Services.Customers;
|
|
using Nop.Services.Security;
|
|
using Nop.Web.Framework;
|
|
using Nop.Web.Framework.Controllers;
|
|
using Nop.Web.Framework.Mvc.Filters;
|
|
|
|
namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers;
|
|
|
|
/// <summary>
|
|
/// Inline-save endpoints for the FruitBank "Customer attributes" block on the
|
|
/// admin customer page (rendered by CustomerCreditWidgetViewComponent).
|
|
/// Values are stored as store-agnostic (StoreId 0) customer generic attributes
|
|
/// via <see cref="FruitBankAttributeService"/>.
|
|
/// </summary>
|
|
[AuthorizeAdmin]
|
|
[Area(AreaNames.ADMIN)]
|
|
public class CustomerAttributesController : BasePluginController
|
|
{
|
|
private readonly FruitBankAttributeService _attributeService;
|
|
private readonly ICustomerService _customerService;
|
|
private readonly IPermissionService _permissionService;
|
|
|
|
public CustomerAttributesController(
|
|
FruitBankAttributeService attributeService,
|
|
ICustomerService customerService,
|
|
IPermissionService permissionService)
|
|
{
|
|
_attributeService = attributeService;
|
|
_customerService = customerService;
|
|
_permissionService = permissionService;
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("Admin/CustomerAttributes/SetEkaer")]
|
|
public async Task<IActionResult> SetEkaer(int customerId, bool value)
|
|
{
|
|
if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
|
|
return Json(new { success = false, error = "Access denied" });
|
|
|
|
var customer = await _customerService.GetCustomerByIdAsync(customerId);
|
|
if (customer == null)
|
|
return Json(new { success = false, error = "Customer not found" });
|
|
|
|
await _attributeService.InsertOrUpdateGenericAttributeAsync<Customer, bool>(
|
|
customerId, FruitBankPluginConst.IsEkaerAttribute, value, storeId: 0);
|
|
|
|
return Json(new { success = true, value });
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("Admin/CustomerAttributes/SaveSites")]
|
|
public async Task<IActionResult> SaveSites(int customerId, int[] siteAddressIds, int defaultAddressId)
|
|
{
|
|
if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
|
|
return Json(new { success = false, error = "Access denied" });
|
|
|
|
var customer = await _customerService.GetCustomerByIdAsync(customerId);
|
|
if (customer == null)
|
|
return Json(new { success = false, error = "Customer not found" });
|
|
|
|
// Only keep ids that actually belong to this customer (guards against tampering / stale ids)
|
|
var validIds = (await _customerService.GetAddressesByCustomerIdAsync(customerId))
|
|
.Select(a => a.Id)
|
|
.ToHashSet();
|
|
|
|
var selected = (siteAddressIds ?? Array.Empty<int>())
|
|
.Where(validIds.Contains)
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
// Default must be one of the selected sites; otherwise no default
|
|
var effectiveDefault = selected.Contains(defaultAddressId) ? defaultAddressId : 0;
|
|
|
|
var entries = selected
|
|
.Select(id => new CustomerSiteEntry { AddressId = id, IsDefault = id == effectiveDefault })
|
|
.ToList();
|
|
|
|
var json = JsonSerializer.Serialize(entries);
|
|
await _attributeService.InsertOrUpdateGenericAttributeAsync<Customer, string>(
|
|
customerId, FruitBankPluginConst.CustomerSitesAttribute, json, storeId: 0);
|
|
|
|
return Json(new { success = true, siteCount = entries.Count, defaultAddressId = effectiveDefault });
|
|
}
|
|
}
|