public async Task<UpdateModel<AccessCodeCreateModel>> GenerateAccessCode(AccessCodeCreateModel model) { try { string message; if (model.EmployeeId.IsNullOrEmpty() || model.EmployeeName.IsNullOrEmpty() || model.WorkLocation.IsNullOrEmpty()) { message = "Os dados são incompletos!<br/>Precisa fornecer:" + "<ul><li>O nome do funcionario</li><li>A data</li><li>O local onde vai trabalhar</li></ul"; return new UpdateModel<AccessCodeCreateModel>(false, message, model); } var accessCode = await _randomNumberService.GenerateRandomNumber(); var employeeName = model.EmployeeName.Replace(" ", ""); var workLocation = model.WorkLocation.Replace(" ", ""); var date = model.Date.ToString("yyyy-MM-dd"); model.GeneratedAccessCode = accessCode; model.FileName = $"cornhouse_{employeeName}_{workLocation}_{date}"; var entity = new AccessCode(); Mapper.Map(model, entity); await Session.StoreAsync(entity); await Session.SaveChangesAsync(); var result = await GetAccessCodes(); model.AccessCodesList = result.List; model.Id = entity.Id; message = $"O código do accesso para {model.EmployeeName} é {model.GeneratedAccessCode}."; return new UpdateModel<AccessCodeCreateModel>(true, message, model); } catch (Exception e) { return new UpdateModel<AccessCodeCreateModel>(e); } }
public async Task GenerateAccessCode_should_generate_a_unique_onetime_code_and_update_the_access_codes_list() { using (var store = CreateDocumentStore()) { using (var session = store.OpenAsyncSession()) { // Arrange await CreateCodesAndEmployees(session); // Act var service = GetAdminService(session); var model = new AccessCodeCreateModel { Id = null, Date = new DateTime(2013, 1, 1), EmployeeId = "employees-1", EmployeeName = "Jeremy Holt", GeneratedAccessCode = -1, WorkLocation = "Fazenda Modelo" }; var result = await service.GenerateAccessCode(model); // Assert var actual = result.Model; actual.EmployeeId.Should().Be(model.EmployeeId); actual.EmployeeName.Should().Be("Jeremy Holt"); actual.GeneratedAccessCode.Should().BeGreaterOrEqualTo(10000).And.BeLessOrEqualTo(99999); actual.Date.Should().Be(model.Date); actual.WorkLocation.Should().Be(model.WorkLocation); actual.Id.Should().Be("AccessCodes-4"); actual.FileName.Should().Be("cornhouse_JeremyHolt_FazendaModelo_2013-01-01"); result.Message.Should().StartWith("O código do accesso para Jeremy Holt é "); result.Success.Should().BeTrue(); actual.AccessCodesList.Should().HaveCount(4); var dbCode = await session.LoadAsync<AccessCode>(model.Id); dbCode.GeneratedAccessCode.Should().Be(actual.GeneratedAccessCode); dbCode.EmployeeName.Should().Be("Jeremy Holt"); } } }