Esempio n. 1
0
        public TaskResult <int?> DoTask(User user)
        {
            try
            {
                var proId         = user.PerformingRightsOrganization?.Id ?? user.PerformingRightsOrganizationId;
                var publisherId   = user.Publisher?.Id ?? user.PublisherId;
                var recordLabelId = user.RecordLabel?.Id ?? user.RecordLabelId;

                user.PerformingRightsOrganization   = null;
                user.PerformingRightsOrganizationId = proId;
                user.Publisher     = null;
                user.PublisherId   = publisherId;
                user.RecordLabel   = null;
                user.RecordLabelId = recordLabelId;

                var person = user.Person;
                if (person != null)
                {
                    person.Email = user.AuthenticationId;
                    var addPersonResult = _addPersonTask.DoTask(person);
                    if (!addPersonResult.Success)
                    {
                        throw addPersonResult.Exception;
                    }

                    var personId = addPersonResult.Data;
                    user.Person   = null;
                    user.PersonId = personId;
                }

                user.SocialSecurityNumber = _formattingService.FormatSocialSecurityNumber(user.SocialSecurityNumber);
                user.PerformingRightsOrganizationMemberNumber = string.IsNullOrWhiteSpace(user.PerformingRightsOrganizationMemberNumber) ? null : user.PerformingRightsOrganizationMemberNumber;
                user.SoundExchangeAccountNumber = string.IsNullOrWhiteSpace(user.SoundExchangeAccountNumber) ? null : user.SoundExchangeAccountNumber;

                _dbContext.Users.Add(user);
                _dbContext.SaveChanges();

                user.Person = person;
                user.PerformingRightsOrganization = proId > 0 ?
                                                    _dbContext.PerformingRightsOrganizations.Where(p => p.Id == proId)
                                                    .Include(p => p.Country)
                                                    .SingleOrDefault() : null;
                user.Publisher = publisherId > 0 ?
                                 _dbContext.Publishers.Where(p => p.Id == publisherId)
                                 .Include(p => p.Address).ThenInclude(a => a.Country)
                                 .SingleOrDefault() : null;
                user.RecordLabel = recordLabelId > 0 ?
                                   _dbContext.RecordLabels.Where(l => l.Id == recordLabelId)
                                   .Include(p => p.Address).ThenInclude(a => a.Country)
                                   .SingleOrDefault() : null;

                return(new TaskResult <int?>(user.Id));
            }
            catch (Exception e)
            {
                return(new TaskResult <int?>(new TaskException(e)));
            }
        }
Esempio n. 2
0
        public TaskResult <User> DoTask(UserInvitation userInvitation)
        {
            try
            {
                var invitation = _dbContext.UserInvitations.Where(ui => ui.Uuid == userInvitation.Uuid)
                                 .Include(ui => ui.InvitedByUser)
                                 .Single();

                if (invitation.Type == UserType.Unassigned)
                {
                    throw new TaskException(SystemMessage("USER_INVITATION_UNASSIGNED"));
                }

                var newUser = userInvitation.CreatedUser;
                newUser.AuthenticationId = invitation.Email;
                newUser.Type             = invitation.Type;
                newUser.Roles            = invitation.Roles;

                var newPerson = newUser.Person;
                newPerson.Email = invitation.Email;
                var addPersonResult = _addPersonTask.DoTask(newPerson);
                if (!addPersonResult.Success)
                {
                    throw addPersonResult.Exception;
                }

                var personId      = addPersonResult.Data;
                var proId         = newUser.PerformingRightsOrganization?.Id ?? newUser.PerformingRightsOrganizationId;
                var publisherId   = invitation.Publisher?.Id ?? invitation.PublisherId;
                var recordLabelId = invitation.RecordLabel?.Id ?? invitation.RecordLabelId;
                var artistId      = invitation.Artist?.Id ?? invitation.ArtistId;

                newUser.Person   = null;
                newUser.PersonId = personId;
                newUser.PerformingRightsOrganization   = null;
                newUser.PerformingRightsOrganizationId = proId;
                newUser.Publisher     = null;
                newUser.PublisherId   = publisherId;
                newUser.RecordLabel   = null;
                newUser.RecordLabelId = recordLabelId;

                _dbContext.Users.Add(newUser);
                _dbContext.SaveChanges();

                invitation.CreatedUser   = null;
                invitation.CreatedUserId = newUser.Id;
                invitation.AcceptedOn    = DateTime.UtcNow;
                _dbContext.SaveChanges();

                invitation.CreatedUser = newUser;

                if (artistId.HasValue)
                {
                    var artist = _dbContext.Artists.Single(a => a.Id == artistId.Value);
                    if (invitation.Roles.HasFlag(SystemUserRoles.ArtistMember))
                    {
                        var artistMember = new ArtistMember
                        {
                            Artist    = artist,
                            Member    = newPerson,
                            StartedOn = DateTime.Today
                        };
                        var addArtistMemberResult = _addArtistMember.DoTask(artistMember);
                        if (!addArtistMemberResult.Success)
                        {
                            throw addArtistMemberResult.Exception;
                        }
                    }
                    else if (invitation.Roles.HasFlag(SystemUserRoles.ArtistManager))
                    {
                        var artistManager = new ArtistManager
                        {
                            Artist    = artist,
                            Manager   = newPerson,
                            StartedOn = DateTime.Today
                        };
                        var addArtistManagerResult = _addArtistManager.DoTask(artistManager);
                        if (!addArtistManagerResult.Success)
                        {
                            throw addArtistManagerResult.Exception;
                        }
                    }
                }

                newUser.Person = newPerson;
                newUser.PerformingRightsOrganization = proId > 0 ?
                                                       _dbContext.PerformingRightsOrganizations.Where(p => p.Id == proId)
                                                       .Include(p => p.Country)
                                                       .SingleOrDefault() : null;
                newUser.Publisher = publisherId > 0 ?
                                    _dbContext.Publishers.Where(p => p.Id == publisherId)
                                    .Include(p => p.Address).ThenInclude(a => a.Country)
                                    .SingleOrDefault() : null;
                newUser.RecordLabel = recordLabelId > 0 ?
                                      _dbContext.RecordLabels.Where(l => l.Id == recordLabelId)
                                      .Include(p => p.Address).ThenInclude(a => a.Country)
                                      .SingleOrDefault() : null;

                userInvitation.LoginLink = ApplicationSettings.Web.Domain + WebRoutes.Login;
                var installation  = _getInstallationTask.DoTask(null).Data;
                var emailTemplate = EmailTemplate($"{invitation.Type}Welcome.html");
                var body          = ReplaceTokens(emailTemplate, invitation, installation);
                var subject       = ReplaceTokens(_htmlService.GetTitle(emailTemplate), invitation, installation);

                _emailService.SendEmail(newUser.Person.FirstAndLastName, newUser.Person.Email,
                                        installation.Name, ApplicationSettings.Mail.From, subject, body);

                return(new TaskResult <User>(newUser));
            }
            catch (Exception e)
            {
                return(new TaskResult <User>(new TaskException(e)));
            }
        }
Esempio n. 3
0
        public TaskResult <Nothing> DoTask(User update)
        {
            try
            {
                var user = _dbContext.Users.Where(u => u.Id == update.Id)
                           .Include(u => u.Person)
                           .SingleOrDefault();

                if (user == null)
                {
                    throw new TaskException(SystemMessage("USER_NOT_FOUND"));
                }

                if (update.Person != null)
                {
                    update.Person.Email = update.AuthenticationId;
                    if (user.Person == null)
                    {
                        _addPersonTask.DoTask(update.Person);
                    }
                    else
                    {
                        update.Person.Id = user.Person.Id;
                        _updatePersonTask.DoTask(update.Person);
                    }
                }

                user.PerformingRightsOrganizationId = update.PerformingRightsOrganization?.Id;
                if (user.PerformingRightsOrganizationId.HasValue)
                {
                    var pro = _dbContext.PerformingRightsOrganizations.SingleOrDefault(r => r.Id == user.PerformingRightsOrganizationId);
                    user.PerformingRightsOrganization = pro ?? throw new TaskException(SystemMessage("PRO_NOT_FOUND"));
                }

                user.PublisherId = update.Publisher?.Id;
                if (user.PublisherId.HasValue)
                {
                    var publisher = _dbContext.Publishers.SingleOrDefault(p => p.Id == user.PublisherId);
                    user.Publisher = publisher ?? throw new TaskException(SystemMessage("PUBLISHER_NOT_FOUND"));
                }

                user.RecordLabelId = update.RecordLabel?.Id;
                if (user.RecordLabelId.HasValue)
                {
                    var recordLabel = _dbContext.RecordLabels.SingleOrDefault(p => p.Id == user.RecordLabelId);
                    user.RecordLabel = recordLabel ?? throw new TaskException(SystemMessage("RECORD_LABEL_NOT_FOUND"));
                }

                user.AuthenticationId = update.AuthenticationId;
                user.Type             = update.Type;
                user.Roles            = update.Roles;
                user.PerformingRightsOrganizationMemberNumber = string.IsNullOrWhiteSpace(update.PerformingRightsOrganizationMemberNumber) ? null : update.PerformingRightsOrganizationMemberNumber;
                user.SoundExchangeAccountNumber = string.IsNullOrWhiteSpace(update.SoundExchangeAccountNumber) ? null : update.SoundExchangeAccountNumber;
                user.SocialSecurityNumber       = _formattingService.FormatSocialSecurityNumber(update.SocialSecurityNumber);

                _dbContext.SaveChanges();

                return(new TaskResult <Nothing>(true));
            }
            catch (Exception e)
            {
                return(new TaskResult <Nothing>(new TaskException(e)));
            }
        }