Esempio n. 1
0
        public async Task <ActionResult <PlaceProvider> > Register([FromBody] PlaceProvider testingPlaceProvider)
        {
            try
            {
                if (testingPlaceProvider is null)
                {
                    throw new ArgumentNullException(nameof(testingPlaceProvider));
                }

                if (string.IsNullOrEmpty(testingPlaceProvider.MainContact))
                {
                    throw new Exception("Place provide your name in the registration form");
                }
                if (string.IsNullOrEmpty(testingPlaceProvider.MainEmail) || !testingPlaceProvider.MainEmail.IsValidEmail())
                {
                    throw new Exception("Place provide valid main email");
                }
                testingPlaceProvider.PrivatePhone = testingPlaceProvider.PrivatePhone.FormatPhone();
                if (string.IsNullOrEmpty(testingPlaceProvider.PrivatePhone) || !testingPlaceProvider.PrivatePhone.IsValidPhoneNumber())
                {
                    throw new Exception("Place provide valid contact phone number in form +421 907 000 000");
                }
                if (!string.IsNullOrEmpty(configuration["MaxPlaceProviders"]))
                {
                    if (int.TryParse(configuration["MaxPlaceProviders"], out var limit))
                    {
                        var list = await placeProviderRepository.ListPublic();

                        if (limit <= list.Count())
                        {
                            throw new Exception("Place provider limit has been reached");
                        }
                    }
                }
                var ret = await placeProviderRepository.Register(testingPlaceProvider);

                if (ret != null)
                {
                    try
                    {
                        await userRepository.Invite(new Invitation()
                        {
                            CompanyName       = testingPlaceProvider.CompanyName,
                            Email             = testingPlaceProvider.MainEmail,
                            InvitationMessage = "Registrácia správcu odberných miest",
                            InvitationTime    = DateTimeOffset.Now,
                            InviterName       = "System administrator",
                            Name            = testingPlaceProvider.MainContact,
                            Phone           = testingPlaceProvider.PrivatePhone,
                            PlaceProviderId = ret.PlaceProviderId,
                            Status          = InvitationStatus.Invited,
                        });
                    }
                    catch (Exception exc)
                    {
                        logger.LogInformation(exc.Message);
                        // user exists
                    }
                }
                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
                }));
            }
        }