You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.9 KiB

using System.ComponentModel.DataAnnotations;
2 years ago
using Connected.Annotations;
using Connected.Common.Documents;
using Connected.Contacts.Types;
2 years ago
using Connected.Data;
using Connected.Logistics.Types.Warehouses;
2 years ago
using Connected.Security.Identity;
using Connected.Validation;
namespace Connected.Logistics.Documents.Receive;
2 years ago
[Priority(0)]
internal sealed class InsertReceiveDocumentValidator : InsertDocumentValidator<InsertReceiveDocumentArgs>
{
public InsertReceiveDocumentValidator(IUserService users, IBusinessPartnerService businessPartners, IWarehouseService warehouses) : base(users)
{
BusinessPartners = businessPartners;
Warehouses = warehouses;
}
private IBusinessPartnerService BusinessPartners { get; }
private IWarehouseService Warehouses { get; }
protected override async Task OnValidating()
{
await ValidateSupplier();
await ValidateWarehouse();
}
private async Task ValidateSupplier()
{
/*
* If supplier is not set there is no need for a validation.
*/
if (Arguments.Supplier is null)
return;
/*
* Check is business partner exists.
*/
if (await BusinessPartners.Select((int)Arguments.Supplier) is not IBusinessPartner supplier)
throw ValidationExceptions.NotFound(nameof(Arguments.Supplier), Arguments.Supplier);
/*
* Check if business partner has Supplier role which means it's actually a supplier.
*/
if (!supplier.Roles.HasFlag(CustomerRoles.Supplier))
throw new ValidationException($"{SR.ValPartnerNotSupplier} ({Arguments.Supplier})");
}
private async Task ValidateWarehouse()
{
if (Arguments.Warehouse is null)
return;
/*
* Check is warehouse exists.
*/
if (await Warehouses.Select(Arguments.Warehouse) is not IWarehouse warehouse)
throw ValidationExceptions.NotFound(nameof(Arguments.Warehouse), Arguments.Warehouse);
/*
* Only Enabled warehouses can be used.
*/
if (warehouse.Status == Status.Disabled)
throw ValidationExceptions.Disabled(nameof(Arguments.Warehouse));
}
}