public async Task <IActionResult> GetAllDailyResourceUsage()
        {
            var currentBillingPeriod = await _billRepository.GetCurrentBillingPeriod();

            if (this.UserInRole(Role.Tenant))
            {
                var userId   = this.UserIdFromApiKey();
                var tenantId = await _tenantRepository.TenantIdFromUserId(userId);

                if (tenantId == null)
                {
                    var err = new DTO.ErrorBuilder()
                              .Message("Not a tenant")
                              .Code(400)
                              .Build();
                    return(err);
                }

                var usages = await _billRepository.GetDailyResourceUsage((int)tenantId, currentBillingPeriod);

                return(new ObjectResult(usages));
            }
            else
            {
                var err = new DTO.ErrorBuilder()
                          .Message("You are not authorized to view resource usage.")
                          .Code(403)
                          .Build();
                _logger.LogWarning($"Unauthorized access attempt to view resource usage.");
                return(err);
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> GetAgreementTemplates()
        {
            if (this.UserInRole(Role.Tenant))
            {
                var userId   = this.UserIdFromApiKey();
                var tenantId = await _tenantRepository.TenantIdFromUserId(userId);

                if (tenantId == null)
                {
                    var err = new DTO.ErrorBuilder()
                              .Message("You are not a tenant of this property")
                              .Code(403)
                              .Build();
                    return(err);
                }
                var allAgreements = await _agreementRepository.GetAllAgreements();

                return(new ObjectResult(allAgreements));
            }
            else if (this.UserInRole(Role.Manager) || this.UserInRole(Role.Admin))
            {
                var allAgreements = await _agreementRepository.GetAllAgreements();

                return(new ObjectResult(allAgreements));
            }
            else
            {
                var err = new DTO.ErrorBuilder()
                          .Message("You are not authorized to view agreements.")
                          .Code(403)
                          .Build();
                return(err);
            }
        }
        public async Task <IActionResult> GetMaintenanceRequests(int limit)
        {
            if (this.UserInRole(Role.Tenant))
            {
                var userId   = this.UserIdFromApiKey();
                var tenantId = await _tenantRepository.TenantIdFromUserId(userId);

                if (tenantId == null)
                {
                    var err = new DTO.ErrorBuilder()
                              .Message("Not a tenant.")
                              .Code(400)
                              .Build();
                    return(err);
                }
                var unit = await _tenantRepository.UnitFromTenantId((int)tenantId);

                if (unit == null)
                {
                    var err = new DTO.ErrorBuilder()
                              .Message("Tenant not assigned a unit.")
                              .Code(400)
                              .Build();
                    return(err);
                }

                var requests = await _maintenanceRepository.GetMaintenanceRequests(unit.UnitNumber);

                var requestDTOs = await MakeDTORequests(requests);

                return(new ObjectResult(requestDTOs));
            }
            else if (this.UserInRole(Role.Manager) || this.UserInRole(Role.Admin))
            {
                var requests = await _maintenanceRepository.GetMaintenanceRequests();

                var requestDTOs = await MakeDTORequests(requests);

                return(new ObjectResult(requestDTOs));
            }
            else
            {
                var err = new DTO.ErrorBuilder()
                          .Message("You are not authorized to view maintenance requests.")
                          .Code(403)
                          .Build();
                _logger.LogWarning($"Unauthorized access attempt to view maintenance requests.");
                return(err);
            }
        }
        public async Task <IActionResult> GetAgreements()
        {
            if (this.UserInRole(Role.Tenant))
            {
                var userId   = this.UserIdFromApiKey();
                var tenantId = await _tenantRepository.TenantIdFromUserId(userId);

                if (tenantId == null)
                {
                    var err = new DTO.ErrorBuilder()
                              .Message("You are not a tenant of this property")
                              .Code(403)
                              .Build();
                    _logger.LogWarning($"User {userId} attempted to access tenant information without being a tenant.");
                    return(err);
                }

                var agreements = await _agreementRepository.GetAgreements((int)tenantId);

                return(new ObjectResult(agreements));
            }
            else if (this.UserInRole(Role.Manager) || this.UserInRole(Role.Admin))
            {
                var allAgreements = await _agreementRepository.GetAgreements();

                return(new ObjectResult(allAgreements));
            }
            else
            {
                var err = new DTO.ErrorBuilder()
                          .Message("You are not authorized to view agreements.")
                          .Code(403)
                          .Build();
                _logger.LogWarning($"Unauthorized access attempt to view agreements.");
                return(err);
            }
        }
Esempio n. 5
0
        public async Task <IActionResult> UpdateTenantInfo(DTO.TenantInfoDTO info)
        {
            if (this.UserInRole(Role.Tenant))
            {
                var userId   = this.UserIdFromApiKey();
                var tenantId = await _tenantRepository.TenantIdFromUserId(userId);

                if (tenantId == null)
                {
                    var err = new DTO.ErrorBuilder()
                              .Message("Not a tenant")
                              .Code(400)
                              .Build();
                    return(err);
                }

                var unitNumber = await _tenantRepository.UnitFromTenantId((int)tenantId);

                if (unitNumber == null)
                {
                    var err = new DTO.ErrorBuilder()
                              .Message("Not assigned to a unit")
                              .Code(400)
                              .Build();
                    return(err);
                }

                // Prevent user from changing their own unit number.
                info.UnitNumber = unitNumber.UnitNumber;

                var tenant = await _tenantRepository.UpdateTenantInfo((int)tenantId, info);

                return(new ObjectResult(tenant));
            }
            else if (this.UserInRole(Role.Manager) || this.UserInRole(Role.Admin))
            {
                var tenant = await _tenantRepository.UpdateTenantInfo(info.TenantId, info);

                return(new ObjectResult(tenant));
            }
            else
            {
                var err = new DTO.ErrorBuilder()
                          .Message("Not authorized to edit tenant information.")
                          .Code(403)
                          .Build();
                return(err);
            }
        }
        public async Task <IActionResult> GetBillsInCurrentPeriod()
        {
            var currentBillingPeriod = await _billRepository.GetCurrentBillingPeriod();

            if (this.UserInRole(Role.Tenant))
            {
                var userId   = this.UserIdFromApiKey();
                var tenantId = await _tenantRepository.TenantIdFromUserId(userId);

                if (tenantId == null)
                {
                    var err = new DTO.ErrorBuilder()
                              .Message("Not a tenant")
                              .Code(400)
                              .Build();
                    return(err);
                }
                var bills = await _billRepository.GetBills((int)tenantId, currentBillingPeriod);

                var billDTOs = bills.Select(b => new DTO.BillDTO(b)).ToList();

                return(new ObjectResult(billDTOs));
            }
            else if (this.UserInRole(Role.Manager) || this.UserInRole(Role.Admin))
            {
                var bills = await _billRepository.GetBills(currentBillingPeriod);

                var billDTOs = bills.Select(b => new DTO.BillDTO(b)).ToList();
                return(new ObjectResult(billDTOs));
            }
            else
            {
                var err = new DTO.ErrorBuilder()
                          .Message("You are not authorized to view billing information.")
                          .Code(403)
                          .Build();
                _logger.LogWarning($"Unauthorized access attempt to view billing information.");
                return(err);
            }
        }