Exemple #1
0
        public async Task <ActionResult> AddPsychologist([FromBody] CreatePsychologistResource model)
        {
            var message = "";

            if (ModelState.IsValid)
            {
                var emailExists = await _userManager.FindByEmailAsync(model.EmailAddress);

                if (emailExists != null)
                {
                    message = "Account with provided email address  already exist.";
                    return(BadRequest(new { message }));
                }

                var psychologistWithContactNumberExists = _dbContext.Psychologists.Any(item => item.WorkContactNumber.Equals(model.WorkContactNumber));

                if (psychologistWithContactNumberExists)
                {
                    message = "An account with  the provided work contact number number already exist.";
                    return(BadRequest(new { message }));
                }


                var newUser = new ApplicationUser()
                {
                    UserName    = model.EmailAddress,
                    Email       = model.EmailAddress,
                    PhoneNumber = model.WorkContactNumber,
                };

                var assignedPassword = GenerateRandomPassword();
                var result           = await _userManager.CreateAsync(newUser, assignedPassword);

                if (result.Succeeded)
                {
                    var role = await _roleManager.FindByNameAsync("psychologist".ToLower());

                    await _userManager.AddToRoleAsync(newUser, role.Name);

                    var created = _userService.AddPsychologist(model, assignedPassword);


                    var emailToken = await _userManager.GenerateEmailConfirmationTokenAsync(newUser);

                    var code = HttpUtility.UrlEncode(emailToken);

                    //TODO: Once Deployed, Require Confirmed Email Address and Send Confirmation Link Via Email
                    //var confirmLink = "https://api_hosting_domain/api/account/confirmemail?userId=" + newUser.Id + "&token=" + code;

                    NotificationExtension.AddPsychologistNotification(created.PsychologistId);
                    return(Ok());
                }
                message = "Something went wrong. Please try again.";
                return(BadRequest(new { message }));
            }
            message = "Something went wrong. Please try again.";
            return(BadRequest(new { message }));
        }
        public Psychologist AddPsychologist(CreatePsychologistResource resource, string generatedPassword)
        {
            var newPsychologist = new Psychologist
            {
                FullName          = resource.FullName,
                PracticeNo        = resource.PracticeNo,
                HPCSANo           = resource.HPCSANo,
                About             = resource.About,
                PracticeTitle     = resource.PracticeTitle,
                WorkContactNumber = resource.WorkContactNumber,
                GenderId          = resource.GenderId,
                TitleId           = resource.TitleId,
                CellContactNumber = resource.CellContactNumber,
                EmailAddress      = resource.EmailAddress,
                GeneratedPassword = generatedPassword
            };

            _context.Psychologists.Add(newPsychologist);
            _context.SaveChanges();

            foreach (var resourceCentre in resource.Centres)
            {
                var assignPsychologistToCenter = new PsychologistCentre
                {
                    CentreId       = resourceCentre,
                    PsychologistId = newPsychologist.PsychologistId
                };
                _context.PsychologistCentres.Add(assignPsychologistToCenter);
                _context.SaveChanges();
            }

            foreach (var qualification in resource.Qualifications)
            {
                var newQualification = new PsychologistQualification
                {
                    Name           = qualification,
                    PsychologistId = newPsychologist.PsychologistId
                };
                _context.PsychologistQualifications.Add(newQualification);
                _context.SaveChanges();
            }

            foreach (var service in resource.Services)
            {
                var newService = new PsychologistService
                {
                    Name           = service,
                    PsychologistId = newPsychologist.PsychologistId
                };
                _context.PsychologistServices.Add(newService);
                _context.SaveChanges();
            }

            AuditLogExtenstion.LogActivity("Super Admin", SupportedLogOperation.Create, "Create a new Psychologist");
            return(newPsychologist);
        }