public async Task <ActionResult <Result> > ConnectVisitorToTest([FromForm] string visitorCode, [FromForm] string testCode)
        {
            try
            {
                if (!User.IsRegistrationManager(userRepository, placeProviderRepository) && !User.IsMedicTester(userRepository, placeProviderRepository))
                {
                    throw new Exception(localizer["Only user with Registration Manager role or Medic Tester role is allowed to register user to test"].Value);
                }

                if (string.IsNullOrEmpty(visitorCode))
                {
                    throw new ArgumentException(localizer[Controllers_ResultController.Visitor_code_must_not_be_empty].Value);
                }

                if (string.IsNullOrEmpty(testCode))
                {
                    throw new ArgumentException(localizer[Controllers_ResultController.Test_code_must_not_be_empty].Value);
                }


                var codeClear     = FormatBarCode(visitorCode);
                var testCodeClear = FormatBarCode(testCode);
                if (int.TryParse(codeClear, out var codeInt))
                {
                    return(Ok(await visitorRepository.ConnectVisitorToTest(codeInt, testCodeClear, User.GetEmail(), HttpContext.GetIPAddress())));
                }
                throw new Exception(localizer[Controllers_ResultController.Invalid_visitor_code]);
            }
            catch (ArgumentException exc)
            {
                logger.LogError(exc.Message);
                return(BadRequest(new ProblemDetails()
                {
                    Detail = exc.Message
                }));
            }
            catch (Exception exc)
            {
                logger.LogError(exc, exc.Message);
                return(BadRequest(new ProblemDetails()
                {
                    Detail = exc.Message
                }));
            }
        }
Example #2
0
        public async Task <ActionResult <Visitor> > RegisterEmployeeByDocumenter(
            [FromForm] string employeeId,
            [FromForm] string email,
            [FromForm] string phone,
            [FromForm] DateTimeOffset time,
            [FromForm] string productId,
            [FromForm] string result
            )
        {
            try
            {
                if (string.IsNullOrEmpty(employeeId))
                {
                    throw new ArgumentException($"'{nameof(employeeId)}' cannot be null or empty", nameof(employeeId));
                }

                if (string.IsNullOrEmpty(email))
                {
                    throw new ArgumentException($"'{nameof(email)}' cannot be null or empty", nameof(email));
                }

                if (string.IsNullOrEmpty(phone))
                {
                    throw new ArgumentException($"'{nameof(phone)}' cannot be null or empty", nameof(phone));
                }

                if (string.IsNullOrEmpty(productId))
                {
                    throw new ArgumentException($"'{nameof(productId)}' cannot be null or empty", nameof(productId));
                }

                if (result != TestResult.PositiveWaitingForCertificate && result != TestResult.NegativeWaitingForCertificate)
                {
                    throw new ArgumentException($"'{nameof(result)}' must be positive or negative", nameof(result));
                }

                if (!User.IsDocumentManager(userRepository, placeProviderRepository))
                {
                    throw new Exception("Only user with Document Manager role is allowed to register self tests and external tests");
                }
                if (time > DateTimeOffset.Now.AddMinutes(5))
                {
                    throw new Exception("Uskutočnený čas testu musí byť v minulosti");
                }

                var pp = await placeProviderRepository.GetPlaceProvider(User.GetPlaceProvider());

                if (pp == null)
                {
                    throw new Exception("Miesto má nastavené chybnú spoločnosť. Prosím kontaktujte podporu s chybou 0x027561");
                }
                var hash  = visitorRepository.MakeCompanyPeronalNumberHash(pp.CompanyId, employeeId);
                var regId = await visitorRepository.GetRegistrationIdFromHashedId(hash);

                var reg = await visitorRepository.GetRegistration(regId);

                if (reg == null)
                {
                    throw new ArgumentException($"Neplatné osobné číslo zamestnanca");
                }
                var phoneFormatted = phone.FormatPhone();
                var regUpdated     = false;
                if (phoneFormatted.IsValidPhoneNumber())
                {
                    if (string.IsNullOrEmpty(reg.CustomPhone))
                    {
                        if (reg.Phone != phoneFormatted)
                        {
                            reg.CustomPhone = phoneFormatted;
                            regUpdated      = true;
                        }
                    }
                    else
                    {
                        if (reg.CustomPhone != phoneFormatted)
                        {
                            reg.CustomPhone = phoneFormatted;
                            regUpdated      = true;
                        }
                    }
                }

                if (email.IsValidEmail())
                {
                    if (string.IsNullOrEmpty(reg.CustomEmail))
                    {
                        if (reg.Email != email)
                        {
                            reg.CustomEmail = email;
                            regUpdated      = true;
                        }
                    }
                    else
                    {
                        if (reg.CustomEmail != email)
                        {
                            reg.CustomEmail = email;
                            regUpdated      = true;
                        }
                    }
                }

                if (regUpdated)
                {
                    await visitorRepository.SetRegistration(reg, false);
                }
                var visitor = new Visitor()
                {
                };
                visitor.EmployeeId                   = reg.CompanyIdentifiers?.Where(c => !string.IsNullOrEmpty(c.EmployeeId))?.FirstOrDefault()?.EmployeeId;
                visitor.Result                       = TestResult.TestIsBeingProcessing;
                visitor.PersonType                   = string.IsNullOrEmpty(reg.PersonType) ? "idcard" : reg.PersonType;
                visitor.FirstName                    = reg.FirstName;
                visitor.LastName                     = reg.LastName;
                visitor.BirthDayDay                  = reg.BirthDayDay;
                visitor.BirthDayMonth                = reg.BirthDayMonth;
                visitor.BirthDayYear                 = reg.BirthDayYear;
                visitor.City                         = reg.City;
                visitor.Street                       = reg.Street;
                visitor.StreetNo                     = reg.StreetNo;
                visitor.Insurance                    = reg.InsuranceCompany;
                visitor.Gender                       = reg.Gender;
                visitor.Nationality                  = reg.Nationality;
                visitor.ZIP                          = reg.ZIP;
                visitor.Email                        = reg.CustomEmail ?? reg.Email;
                visitor.Phone                        = reg.CustomPhone ?? reg.Phone;
                visitor.PersonType                   = reg.PersonType;
                visitor.Passport                     = reg.Passport;
                visitor.RC                           = reg.RC;
                visitor.Product                      = productId;
                visitor.RegistrationTime             = DateTimeOffset.UtcNow;
                visitor.SelfRegistration             = false;
                visitor.RegistrationUpdatedByManager = User.GetEmail();
                visitor.TestingTime                  = time;
                visitor.ChosenSlot                   = time.UtcTicks;
                visitor.PlaceProviderId              = User.GetPlaceProvider();

                logger.LogInformation($"RegisterEmployeeByDocumenter: {User.GetEmail()} {Helpers.Hash.GetSHA256Hash(visitor.Id.ToString())}");
                var saved = await visitorRepository.Register(visitor, User.GetEmail(), false);

                var id = Guid.NewGuid().ToString().FormatDocument();
                await visitorRepository.ConnectVisitorToTest(saved.Id, id, User.GetEmail(), User.GetPlaceProvider(), HttpContext.GetIPAddress(), silent : true);

                var ret = await visitorRepository.SetTestResult(id, result, isAdmin : true, silent : true);

                // Set testing time to the chosen slot time
                var toUpdate = await visitorRepository.GetVisitor(saved.Id, false, true);

                toUpdate.TestingTime      = visitor.ChosenSlotTime;
                toUpdate.ResultNotifiedAt = DateTimeOffset.UtcNow;
                visitor = await visitorRepository.SetVisitor(toUpdate, false);

                if (result == TestResult.PositiveWaitingForCertificate)
                {
                    visitor.Result = result;
                    logger.LogInformation($"Going to notify {visitor.Result}");
                    var sent = await visitorRepository.NotifyWhenSick(visitor);

                    logger.LogInformation($"Sent emails: {sent}");
                }
                else
                {
                    logger.LogInformation($"Not going to notify. Result is {result}");
                }
                return(Ok(ret));
            }
            catch (ArgumentException exc)
            {
                logger.LogError(exc.Message);
                return(BadRequest(new ProblemDetails()
                {
                    Detail = exc.Message
                }));
            }
            catch (Exception exc)
            {
                logger.LogError(exc, exc.Message);
                return(BadRequest(new ProblemDetails()
                {
                    Detail = exc.Message
                }));
            }
        }