Skip to content

Commit 6466349

Browse files
committed
Moving the validation to the top of CreateDevice user, so we fail fast.
1 parent c6c972b commit 6466349

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

eFormAPI/Plugins/BackendConfiguration.Pn/BackendConfiguration.Pn/Infrastructure/Helpers/BackendConfigurationAssignmentWorkerServiceHelper.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,20 @@ await core.EntityItemUpdate(entityItem.Id, entityItem.Name, entityItem.Descripti
462462
public static async Task<OperationDataResult<int>> CreateDeviceUser(DeviceUserModel deviceUserModel, Core core,
463463
int userId, TimePlanningPnDbContext timePlanningDbContext)
464464
{
465+
466+
var sdkDbContext = core.DbContextHelper.GetDbContext();
467+
468+
if (sdkDbContext.Workers.AsNoTracking().Any(x => x.Email == deviceUserModel.WorkerEmail && x.WorkflowState != Constants.WorkflowStates.Removed))
469+
{
470+
// this email is already in use
471+
return new OperationDataResult<int>(false, "EmailIsAlreadyInUse");
472+
}
473+
465474
deviceUserModel.UserFirstName = deviceUserModel.UserFirstName.Trim();
466475
deviceUserModel.UserLastName = deviceUserModel.UserLastName.Trim();
467476
// var result = await _deviceUsersService.Create(deviceUserModel);
468477
var siteName = deviceUserModel.UserFirstName + " " + deviceUserModel.UserLastName;
469478

470-
var sdkDbContext = core.DbContextHelper.GetDbContext();
471479
var site = await sdkDbContext.Sites.AsNoTracking().SingleOrDefaultAsync(x => x.Name == deviceUserModel.UserFirstName + " " + deviceUserModel.UserLastName && x.WorkflowState != Constants.WorkflowStates.Removed);
472480

473481
if (site != null)
@@ -482,12 +490,6 @@ public static async Task<OperationDataResult<int>> CreateDeviceUser(DeviceUserMo
482490

483491
var worker = await sdkDbContext.Workers.SingleAsync(x => x.MicrotingUid == siteDto.WorkerUid).ConfigureAwait(false);
484492

485-
if (sdkDbContext.Workers.AsNoTracking().Any(x => x.Email == deviceUserModel.WorkerEmail && x.MicrotingUid != siteDto.WorkerUid && x.WorkflowState != Constants.WorkflowStates.Removed))
486-
{
487-
// this email is already in use
488-
return new OperationDataResult<int>(false, "EmailIsAlreadyInUse");
489-
}
490-
491493
worker.EmployeeNo = deviceUserModel.EmployeeNo;
492494
worker.PinCode = deviceUserModel.PinCode;
493495
worker.Email = deviceUserModel.WorkerEmail;

0 commit comments

Comments
 (0)