Example #1
0
        private async Task <IEnumerable <OrgTransaction> > ConvertToApiModels(IEnumerable <AcademyCloud.Expenses.Protos.Transactions.OrgTransaction> input)
        {
            // Pick all ids and types
            var subjects = input.Select(x => x.Payer)
                           .Concat(input.Select(x => x.Receiver))
                           .Select(x => new AcademyCloud.Identity.Protos.Interop.GetNamesRequest.Types.Subject
            {
                Id   = x.Id,
                Type = (AcademyCloud.Identity.Protos.Interop.GetNamesRequest.Types.SubjectType)x.Type,
            });

            // Send to Identity service to check their names
            var nameMapResp = await(await factory.GetIdentityInteropClientAsync())
                              .GetNamesAsync(new AcademyCloud.Identity.Protos.Interop.GetNamesRequest
            {
                Subjects = { subjects }
            });

            var nameMap = nameMapResp.IdNameMap;

            // Set their names to their respective transaction
            return(input.Select(x => new OrgTransaction
            {
                Id = x.Id,
                Amount = x.Amount,
                PayerId = x.Payer.Id,
                PayerName = nameMap[x.Payer.Id],
                ReceiverId = x.Receiver.Id,
                ReceiverName = nameMap[x.Receiver.Id],
                Reason = x.Reason,
                Time = x.Time.ToDateTime(),
            }));
        }
        public async Task <ActionResult <GetDomainsResponse> > GetDomains()
        {
            var resp = await(await factory.GetDomainsClientAsync())
                       .GetDomainsAsync(new AcademyCloud.Identity.Protos.Domains.GetDomainsRequest
            {
            });

            var subjects = resp.Domains.Select(x => new AcademyCloud.Expenses.Protos.Interop.Subject
            {
                Id   = x.Id,
                Type = AcademyCloud.Expenses.Protos.Common.SubjectType.Domain,
            });

            // get pay users
            var payUsers = await(await factory.GetExpensesInteropClientAsync())
                           .GetPayUserAsync(new AcademyCloud.Expenses.Protos.Interop.GetPayUserRequest
            {
                Subjects = { subjects }
            });

            // get pay user's names
            var payUserNames = await(await factory.GetIdentityInteropClientAsync())
                               .GetNamesAsync(new AcademyCloud.Identity.Protos.Interop.GetNamesRequest
            {
                Subjects =
                {
                    payUsers.PayUsers.Values.Select(x => new AcademyCloud.Identity.Protos.Interop.GetNamesRequest.Types.Subject
                    {
                        Id   = x,
                        Type = AcademyCloud.Identity.Protos.Interop.GetNamesRequest.Types.SubjectType.User,
                    })
                }
            });

            // get resources
            var quotas = await(await factory.GetExpensesInteropClientAsync())
                         .GetQuotaAsync(new AcademyCloud.Expenses.Protos.Interop.GetQuotaRequest
            {
                Subjects = { subjects }
            });

            return(new GetDomainsResponse
            {
                Domains = resp.Domains.Select(x => new Domain
                {
                    Id = x.Id,
                    Name = x.Name,
                    Active = true,
                    Admins = x.Admins.Select(FromGrpc),
                    PayUser = new User {
                        Id = payUsers.PayUsers[x.Id], Name = payUserNames.IdNameMap[payUsers.PayUsers[x.Id]]
                    },
                    Quota = quotas.Quotas[x.Id],
                })
            });
        }
        public async Task <BillingsResponse <CurrentAllocatedBilling> > GetCurrentAllocatedBillings([FromRoute] SubjectType subjectType)
        {
            var resp = await(await factory.GetBillingClient())
                       .GetCurrentAllocatedBillingsAsync(new AcademyCloud.Expenses.Protos.Billing.GetCurrentAllocatedBillingsRequest
            {
                SubjectType = subjectType
            });

            // get names
            var subjects = new List <GetNamesRequest.Types.Subject>(resp.Billings.Count * 2);

            resp.Billings.ForEach(x =>
            {
                subjects.Add(new GetNamesRequest.Types.Subject()
                {
                    Type = GetNamesRequest.Types.SubjectType.User, Id = x.PayerId
                });
                subjects.Add(new GetNamesRequest.Types.Subject()
                {
                    Type = (GetNamesRequest.Types.SubjectType)subjectType, Id = x.SubjectId
                });
            });

            var namesResp = await(await factory.GetIdentityInteropClientAsync())
                            .GetNamesAsync(new GetNamesRequest {
                Subjects = { subjects }
            });

            return(new BillingsResponse <CurrentAllocatedBilling>
            {
                Billings = resp.Billings.Select(x => FromGrpc(x, namesResp.IdNameMap)),
            });
        }
        public async Task <GetAccessibleProjectsResponse> GetAccessibleProjects()
        {
            var resp = await(await factory.GetProjectsClientAsync())
                       .GetAccessibleProjectsAsync(new AcademyCloud.Identity.Protos.Projects.GetAccessibleProjectsRequest
            {
            });

            var subjects = resp.Projects
                           .Select(x => new AcademyCloud.Expenses.Protos.Interop.Subject
            {
                Id   = x.Id,
                Type = AcademyCloud.Expenses.Protos.Common.SubjectType.Project,
            });


            // get pay users
            var payUsers = await(await factory.GetExpensesInteropClientAsync())
                           .GetPayUserAsync(new AcademyCloud.Expenses.Protos.Interop.GetPayUserRequest
            {
                Subjects = { subjects }
            });

            // get pay user's names
            var payUserNames = await(await factory.GetIdentityInteropClientAsync())
                               .GetNamesAsync(new AcademyCloud.Identity.Protos.Interop.GetNamesRequest
            {
                Subjects =
                {
                    payUsers.PayUsers.Values.Select(x => new AcademyCloud.Identity.Protos.Interop.GetNamesRequest.Types.Subject
                    {
                        Id   = x,
                        Type = AcademyCloud.Identity.Protos.Interop.GetNamesRequest.Types.SubjectType.User,
                    })
                }
            });

            var quotaSubjects = subjects
                                .Concat(
                resp.Projects
                .Select(x => x.Admins)
                .Concat(resp.Projects.Select(x => x.Members))
                .SelectMany(x => x)
                .Select(x => new AcademyCloud.Expenses.Protos.Interop.Subject
            {
                Id   = x.UserProjectAssignmentId.ToString(),
                Type = AcademyCloud.Expenses.Protos.Common.SubjectType.UserProjectAssignment
            }
                        ));

            // get quotas
            var quotas = await(await factory.GetExpensesInteropClientAsync())
                         .GetQuotaAsync(new AcademyCloud.Expenses.Protos.Interop.GetQuotaRequest
            {
                Subjects = { quotaSubjects }
            });


            return(new GetAccessibleProjectsResponse()
            {
                Projects = resp.Projects.Select(x => new Project()
                {
                    Id = x.Id,
                    Name = x.Name,
                    Active = true,
                    Admins = x.Admins.Select(x => FromGrpc(x.User)),
                    Members = x.Members.Select(x => FromGrpc(x.User)),
                    PayUser = new Models.Identity.Common.User
                    {
                        Id = payUsers.PayUsers[x.Id],
                        Name = payUserNames.IdNameMap[payUsers.PayUsers[x.Id]],
                    },
                    Quota = quotas.Quotas[x.Id],
                    UserQuotas = x.Admins.Concat(x.Members).ToDictionary(user => user.User.Id, user => (Resources)quotas.Quotas[user.UserProjectAssignmentId])
                })
            });
        }