Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,11 @@ public async Task<IActionResult> Patch([FromRoute] string key, [FromBody] JsonPa
if (!customerGroup.Any()) return NotFound();

var cr = customerGroup.FirstOrDefault();
model.ApplyTo(cr);
if (cr is { IsSystem: false })
{
await _mediator.Send(new UpdateCustomerGroupCommand { Model = cr });
return Ok();
}
if (cr!.IsSystem) return BadRequest(ModelState);

return BadRequest(ModelState);
model.ApplyTo(cr);
await _mediator.Send(new UpdateCustomerGroupCommand { Model = cr });
return Ok();
}

[EndpointDescription("Delete entity in CustomerGroup")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class CustomerGroupProfile : Profile, IAutoMapperProfile
public CustomerGroupProfile()
{
CreateMap<CustomerGroupDto, CustomerGroup>()
.ForMember(dest => dest.UserFields, mo => mo.Ignore());
.ForMember(dest => dest.UserFields, mo => mo.Ignore())
.ForMember(dest => dest.IsSystem, mo => mo.Ignore());

CreateMap<CustomerGroup, CustomerGroupDto>();
}
Expand Down
19 changes: 19 additions & 0 deletions src/Tests/Grand.Mapping.Tests/AdminShared/CustomerMappingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ public Task CustomerGroupModel_ToDomain()
return Verify(_mapper.Map<CustomerGroup>(model));
}

[TestMethod]
public void CustomerGroupModel_ToDomain_KeepsStoredIsSystem()
{
var group = new CustomerGroup { Id = "cg-001", Name = "Administrators", SystemName = "Administrators", IsSystem = true };
var model = new CustomerGroupModel { Id = "cg-001", Name = "Administrators", SystemName = "Administrators", IsSystem = false };

_mapper.Map(model, group);

Assert.IsTrue(group.IsSystem);
}

[TestMethod]
public void CustomerGroupModel_ToDomain_NewGroupIsNotSystem()
{
var model = new CustomerGroupModel { Name = "VIP Customers", SystemName = "vip", IsSystem = true };

Assert.IsFalse(_mapper.Map<CustomerGroup>(model).IsSystem);
}

// ── CustomerAttribute ─────────────────────────────────────────────────────

[TestMethod]
Expand Down
19 changes: 19 additions & 0 deletions src/Tests/Grand.Mapping.Tests/Api/ApiMappingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,25 @@ public Task CustomerGroupDto_ToEntity()
return Verify(_mapper.Map<CustomerGroup>(dto));
}

[TestMethod]
public void CustomerGroupDto_ToEntity_KeepsStoredIsSystem()
{
var group = new CustomerGroup { Id = "cg-1", Name = "Administrators", SystemName = "Administrators", IsSystem = true };
var dto = new CustomerGroupDto { Id = "cg-1", Name = "Administrators", SystemName = "Administrators", IsSystem = false };

_mapper.Map(dto, group);

Assert.IsTrue(group.IsSystem);
}

[TestMethod]
public void CustomerGroupDto_ToEntity_NewGroupIsNotSystem()
{
var dto = new CustomerGroupDto { Name = "VIP Customers", SystemName = "Vip", IsSystem = true };

Assert.IsFalse(_mapper.Map<CustomerGroup>(dto).IsSystem);
}

[TestMethod]
public Task CustomerGroup_ToDto()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@model CustomerGroupModel
@* one card of two-column fields; IsSystem is not editable, so it reads as a fact and posts back
through its hidden input *@
@* one card of two-column fields; IsSystem is not editable, so it reads as a fact and is never posted *@
<admin-card>
<div class="grand-fields grand-fields--2col">
<admin-field asp-for="Name"/>
Expand All @@ -18,5 +17,4 @@
<div class="grand-fact__value">@Model.IsSystem.ToString()</div>
</div>
</div>
<input asp-for="IsSystem" type="hidden"/>
</admin-card>
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public async Task<IActionResult> Edit(CustomerGroupModel model, bool continueEdi
}

//If we got this far, something failed, redisplay form
model.IsSystem = customerGroup.IsSystem;
return View(model);
}
catch (Exception exc)
Expand Down
3 changes: 2 additions & 1 deletion src/Web/Grand.Web.AdminShared/Mapper/CustomerGroupProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public CustomerGroupProfile()
{
CreateMap<CustomerGroup, CustomerGroupModel>();
CreateMap<CustomerGroupModel, CustomerGroup>()
.ForMember(dest => dest.Id, mo => mo.Ignore());
.ForMember(dest => dest.Id, mo => mo.Ignore())
.ForMember(dest => dest.IsSystem, mo => mo.Ignore());
}

public int Order => 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Grand.Infrastructure.ModelBinding;
using Grand.Infrastructure.Models;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.ComponentModel.DataAnnotations;

namespace Grand.Web.AdminShared.Models.Customers;
Expand All @@ -20,6 +21,11 @@ public class CustomerGroupModel : BaseEntityModel
[GrandResourceDisplayName("Admin.Customers.CustomerGroups.Fields.Active")]
public bool Active { get; set; }

/// <summary>
/// Shown, never edited: the system groups are seeded by the installer. Never bound, so a form
/// cannot take the protection off a system group or give it to a new one.
/// </summary>
[BindNever]
[GrandResourceDisplayName("Admin.Customers.CustomerGroups.Fields.IsSystem")]
public bool IsSystem { get; set; }

Expand Down
Loading