public async Task <ActionResult <Entity> > Post([FromBody] EntityViewModel input)
        {
            var userEnt = _userEntityRepository.GetByUserId(input.creator, input.ParentEntityId);

            System.Diagnostics.Debug.WriteLine(input.creator);
            var Newentity = new Entity(input.Name, input.Description, input.adresse);

            Newentity.Parent_id = input.ParentEntityId;
            //User user = await _userRepository.Get(input.creator);

            //var userParentEnt = _userEntityRepository.GetByUserId(input.creator, input.ParentEntityId);
            var userParentEnt = _userEntityRepository.GetByUserId(input.creator, input.ParentEntityId);

            if (input.ParentEntityId != 0 && !(userParentEnt is null)) ///  the parent 3andha manager houwa il creator && parentid mawjouda fil input
            {
                await _entityRepository.Add(Newentity);

                Console.WriteLine("Creating new entity:" + Newentity.Name);
                System.Diagnostics.Debug.WriteLine("parent id exist ");
                System.Diagnostics.Debug.WriteLine((userParentEnt is null));
                System.Diagnostics.Debug.WriteLine((userParentEnt.Role));
                if (userParentEnt.Role == 0) // the creator is a manager for the parentEntity
                {
                    System.Diagnostics.Debug.WriteLine("the user is a manager for the entity parent ");
                    var wallPerm  = new WallPermission(input.creator, Newentity.Id, true, true, true, true, true, true, true);
                    var okrPerm   = new OKRPermission(input.creator, Newentity.Id, true, true, true, true, true, true, true, true, true, true);
                    var orgPerm   = new OrganizationPermission(input.creator, Newentity.Id, true, true, true);
                    var notifPerm = new NotificationPermission(input.creator, Newentity.Id, true);
                    await _wallRepository.Add(wallPerm);

                    await _okrRepository.Add(okrPerm);

                    await _organizationRepository.Add(orgPerm);

                    await _notificationRepository.Add(notifPerm);

                    User user = await _userRepository.Get(input.creator);

                    await _userEntityRepository.Add(new User_Entity(user, Newentity, Role.Manager));    // il creator devient manager lil entity input

                    Console.WriteLine("Creating Manager for the new entity:" + Newentity.Name);
                    var cc = _userEntityRepository.GetAdminsOfEntity(input.ParentEntityId); // nlawwjou 3al managers mta3 il parent entity
                    foreach (User_Entity i in cc)
                    {
                        if (i.UserId != input.creator)
                        {
                            System.Diagnostics.Debug.WriteLine("the admin of the parent entity");
                            System.Diagnostics.Debug.WriteLine(i.UserId);
                            User usr = await _userRepository.Get(i.UserId);  /// il admin mta3 il parent

                            if (_userEntityRepository.GetByUserId(usr.Id, Newentity.Id) is null)
                            {
                                await _userEntityRepository.Add(new User_Entity(usr, Newentity, Role.Manager));  // il manager mta3 il parent ywalliw manager lil entity zeda

                                Console.WriteLine("Creating Manager for the new entity:" + usr.Id);
                            }
                            var ParentwallPerm  = new WallPermission(i.UserId, Newentity.Id, true, true, true, true, true, true, true);
                            var ParentokrPerm   = new OKRPermission(i.UserId, Newentity.Id, true, true, true, true, true, true, true, true, true, true);
                            var ParentorgPerm   = new OrganizationPermission(i.UserId, Newentity.Id, true, true, true);
                            var ParentnotifPerm = new NotificationPermission(i.UserId, Newentity.Id, true);
                            await _wallRepository.Add(ParentwallPerm);

                            await _okrRepository.Add(ParentokrPerm);

                            await _organizationRepository.Add(ParentorgPerm);

                            await _notificationRepository.Add(ParentnotifPerm);
                        }
                    }
                    var parent = _entityRepository.Get(input.ParentEntityId);                    // il entity parent
                    await _childEntityRepository.Add(new ChildEntity(parent.Result, Newentity)); // nzidou lil entity parent il entity as a child

                    return(Ok(Newentity));
                }
                else if (userParentEnt.Role != 0)   // louken il parent mawjouda ama il creator mta3 il child mouch manager fil parent
                {
                    System.Diagnostics.Debug.WriteLine("the creator is not a manager for the entity parent ");
                    return(Ok("the creator is not a manager for the parent Entity"));
                }
            }
            else if (input.ParentEntityId == 0)
            {
                await _entityRepository.Add(Newentity);

                //await this._busClient.PublishAsync(new EntityCreated(Newentity.Name, Newentity.Id.ToString()));
                //Console.WriteLine("Creating new entity:" + Newentity.Name);
                System.Diagnostics.Debug.WriteLine("there is no parent here ");
                var wallPerm  = new WallPermission(input.creator, Newentity.Id, true, true, true, true, true, true, true);
                var okrPerm   = new OKRPermission(input.creator, Newentity.Id, true, true, true, true, true, true, true, true, true, true);
                var orgPerm   = new OrganizationPermission(input.creator, Newentity.Id, true, true, true);
                var notifPerm = new NotificationPermission(input.creator, Newentity.Id, true);
                await _wallRepository.Add(wallPerm);

                await _okrRepository.Add(okrPerm);

                await _organizationRepository.Add(orgPerm);

                await _notificationRepository.Add(notifPerm);

                User user = await _userRepository.Get(input.creator);

                await _userEntityRepository.Add(new User_Entity(user, Newentity, Role.Manager));

                //addeed last
                Console.WriteLine("Creating Manager for the new entity:" + Newentity.Name);
                return(Ok(Newentity));
            }
            return(Ok("there is no relation between the creator and the entity parent "));
        }
Beispiel #2
0
        public async Task <ActionResult <User_Entity> > PostManager([FromBody] InviteViewModel input)
        {
            var  user    = _userEntityRepository.GetByUserId(input.InviterId, input.EntityId);
            var  Newuser = _userEntityRepository.GetByUserId(input.NewMemberId, input.EntityId);
            User New     = await _userRepository.Get(input.NewMemberId);

            Entity ent = await _entityRepository.Get(input.EntityId);

            var     userEntity = new User_Entity(New, ent, Role.Manager);
            Boolean admin      = false;

            if (user.Role == Role.Manager)
            {
                admin = true;
            }
            var        childEntity = _childRepository.GetByParentId(input.EntityId);
            List <int> childs      = new List <int>();

            foreach (ChildEntity ee in childEntity)
            {
                int childId = ee.ChildId;
                childs.Add(childId);
            }
            if (Newuser is null)    // sig que the new user is not a (member , manager or observer ) in this entity
            {
                System.Diagnostics.Debug.WriteLine("the new user is not a (member , manager or observer ) in this entity  ");
                if (admin)
                {
                    if (input.NewRole == 0)       // we want to invite the new user as a manager to the entity
                    {
                        foreach (int i in childs) // we make the journey on all the  chids of the entity to make the newuser as a manager also at thoses entities
                        {
                            Entity ii = await _entityRepository.Get(i);

                            var childuserEnt   = new User_Entity(New, ii, Role.Manager); //make the new user as a manager to the child af the entity
                            var childwallPerm  = new WallPermission(input.NewMemberId, i, true, true, true, true, true, true, true);
                            var childokrPerm   = new OKRPermission(input.NewMemberId, i, true, true, true, true, true, true, true, true, true, true);
                            var childorgPerm   = new OrganizationPermission(input.NewMemberId, i, true, true, true);
                            var childnotifPerm = new NotificationPermission(input.NewMemberId, i, true);
                            await _wallRepository.Add(childwallPerm);

                            await _okrRepository.Add(childokrPerm);

                            await _organizationRepository.Add(childorgPerm);

                            await _notificationRepository.Add(childnotifPerm);

                            await _userEntityRepository.Add(childuserEnt);
                        }
                        var wallPerm  = new WallPermission(input.NewMemberId, input.EntityId, true, true, true, true, true, true, true);
                        var okrPerm   = new OKRPermission(input.NewMemberId, input.EntityId, true, true, true, true, true, true, true, true, true, true);
                        var orgPerm   = new OrganizationPermission(input.NewMemberId, input.EntityId, true, true, true);
                        var notifPerm = new NotificationPermission(input.NewMemberId, input.EntityId, true);
                        await _wallRepository.Add(wallPerm);

                        await _okrRepository.Add(okrPerm);

                        await _organizationRepository.Add(orgPerm);

                        await _notificationRepository.Add(notifPerm);

                        await _userEntityRepository.Add(userEntity);

                        Console.WriteLine("Creating Manager:" + New.UserId);
                    }
                }
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("the new user is  a (member , manager or observer ) in this entity  ");
                Newuser.Role = 0;
                System.Diagnostics.Debug.WriteLine(Newuser.Role);
                //await _userEntityRepository.Update(Newuser);
                User_Entity NewChange = await _userEntityRepository.Update(Newuser);

                var wallPerm1  = _wallRepository.GetByUserEntityId(input.NewMemberId, input.EntityId);
                var okrPer1    = _okrRepository.GetByUserEntityId(input.NewMemberId, input.EntityId);
                var orgPerm1   = _organizationRepository.GetOrgPermission(input.NewMemberId, input.EntityId);
                var notifPerm1 = _notificationRepository.GetByUserEntityId(input.NewMemberId, input.EntityId);
                System.Diagnostics.Debug.WriteLine(wallPerm1.Add);
                wallPerm1.Add              = true; wallPerm1.AddEmployees = true; wallPerm1.Delete = true; wallPerm1.DeleteEmployees = true;  wallPerm1.DisplayFivePointQuestionUser = true;  wallPerm1.Observe = true; wallPerm1.Update = true;
                okrPer1.Add                = true; okrPer1.AddEmployees = true; okrPer1.Delete = true; okrPer1.DeleteEmployees = true; okrPer1.Observe = true; okrPer1.Update = true; okrPer1.ObserveKResult = true; okrPer1.updateKResult = true; okrPer1.DeleteKResult = true; okrPer1.AddKResult = true;
                orgPerm1.AddEntities       = true; orgPerm1.ChangeOrganizationProfile = true; orgPerm1.ChangeSettings = true;
                notifPerm1.GetNotification = true;
                await _wallRepository.Update(wallPerm1);

                await _okrRepository.Update(okrPer1);

                await _organizationRepository.Update(orgPerm1);

                await _notificationRepository.Update(notifPerm1);

                foreach (int i in childs)   // we make the journey on all the  chids of the entity to make the newuser as a manager also at thoses entities
                {
                    Entity ii = await _entityRepository.Get(i);

                    var entityuserchild = _userEntityRepository.GetByUserId(input.NewMemberId, i);
                    entityuserchild.Role = input.NewRole;
                    await _userEntityRepository.Update(entityuserchild);

                    var wallPerm  = _wallRepository.GetByUserEntityId(Newuser.Id, i);
                    var okrPer    = _okrRepository.GetByUserEntityId(Newuser.Id, i);
                    var orgPerm   = _organizationRepository.GetOrgPermission(Newuser.Id, i);
                    var notifPerm = _notificationRepository.GetByUserEntityId(Newuser.Id, i);
                    wallPerm.Add              = true; wallPerm.AddEmployees = true; wallPerm.Delete = true; wallPerm.DeleteEmployees = true; wallPerm.DisplayFivePointQuestionUser = true; wallPerm.Observe = true; wallPerm.Update = true;
                    okrPer.Add                = true; okrPer.AddEmployees = true; okrPer.Delete = true; okrPer.DeleteEmployees = true; okrPer.Observe = true; okrPer.Update = true; okrPer.ObserveKResult = true; okrPer.updateKResult = true; okrPer.DeleteKResult = true; okrPer.AddKResult = true;
                    orgPerm.AddEntities       = true; orgPerm.ChangeOrganizationProfile = true; orgPerm.ChangeSettings = true;
                    notifPerm.GetNotification = true;
                    await _wallRepository.Update(wallPerm);

                    await _okrRepository.Update(okrPer);

                    await _organizationRepository.Update(orgPerm);

                    await _notificationRepository.Update(notifPerm);

                    await context.SaveChangesAsync();
                }
                Console.WriteLine("Creating Manager:" + New.UserId);
                return(CreatedAtAction("Get", NewChange));
            }
            return(CreatedAtAction("Get", userEntity));
        }