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); } }
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); } }