Пример #1
0
 private CoopAdmin ToDescriptor(CooperativeAdmin admin) => new CoopAdmin
 {
     UserId            = admin.User?.Id ?? Guid.Empty,
     CooperativeId     = admin.Cooperative?.Id ?? Guid.Empty,
     CooperativeTitle  = admin.Cooperative?.Title,
     CooperativeStatus = admin.Cooperative?.Status ?? CooperativeStatus.Disabled
 };
Пример #2
0
        public Operation AddAdmin(Guid cooperativeId, Guid userId)
        => Operation.Try(async() =>
        {
            //validate argument
            cooperativeId.ThrowIf(
                default(Guid),
                new GaiaException(PolluxErrorCodes.InvalidArgument));

            //validate argument
            userId.ThrowIf(
                default(Guid),
                new GaiaException(PolluxErrorCodes.InvalidArgument));

            var cooperative = (await _queries
                               .GetCooperative(cooperativeId))
                              .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreQueryResult));

            var user = (await _queries
                        .GetUser(userId))
                       .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreQueryResult));

            var admins = await _GetAllAdmins(cooperativeId);

            //ensure that the current user is an admin for the Cooperative
            await _dataAccessAuthorizer.AuthorizeAccess(new CooperativeAdminDescriptor
            {
                Admins = admins
            });

            //make sure we are not adding ourselves
            if (admins.Any(admin => admin.User.Id == userId))
            {
                throw new GaiaException(ErrorCodes.DomainLogicError);
            }

            //finally add a new admin object for the cooperative
            var coopAdmin = new CooperativeAdmin
            {
                User        = user,
                Cooperative = cooperative
            };

            var storeCommand = _storeProvider.CommandFor(typeof(CooperativeAdmin).FullName);

            (await storeCommand
             .Add(coopAdmin))
            .ThrowIfNull(new GaiaException(ErrorCodes.InvalidStoreCommandResult));
        });