From 06e1692ee229e93f39e1e238af1163919304a083 Mon Sep 17 00:00:00 2001
From: esantangelo <enzo020895@gmail.com>
Date: Sat, 28 Nov 2020 20:13:02 -0300
Subject: [PATCH] surveys

---
 .../Tsi1.Api/Controllers/SurveyController.cs  | 12 +++------
 .../Interfaces/ISurveyService.cs              |  2 +-
 .../Services/SurveyService.cs                 | 25 +++++++++++++------
 3 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/Tsi1.Api/Tsi1.Api/Controllers/SurveyController.cs b/Tsi1.Api/Tsi1.Api/Controllers/SurveyController.cs
index 3914c15..c08beda 100644
--- a/Tsi1.Api/Tsi1.Api/Controllers/SurveyController.cs
+++ b/Tsi1.Api/Tsi1.Api/Controllers/SurveyController.cs
@@ -64,15 +64,11 @@ namespace Tsi1.Api.Controllers
 
         [Authorize(Roles = UserTypes.FacultyAdmin + ", " + UserTypes.UdelarAdmin)]
         [HttpGet("GetAllGlobalSurveys")]
-        public async Task<IActionResult> GetAllGlobalSurveys(int tenantId)
+        public async Task<IActionResult> GetAllGlobalSurveys()
         {
-            var userType = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role).Value;
-            if (userType == UserTypes.FacultyAdmin)
-            {
-                tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value);
-            }
-           
-            var result = await _surveyService.GetAllGlobalSurveys(tenantId, userType);
+            var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value);
+                       
+            var result = await _surveyService.GetAllGlobalSurveys(tenantId);
 
             if (result.HasError)
             {
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISurveyService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISurveyService.cs
index 6ec00d5..8f652ec 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISurveyService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISurveyService.cs
@@ -11,7 +11,7 @@ namespace Tsi1.BusinessLayer.Interfaces
     {
         Task<ServiceResult<int>> CreateGlobalSurvey(SurveyCreateDto newSurvey, int tenantId);
         Task<ServiceResult<bool>> DeleteGlobalSurvey(int surveyId, string userType, int tenantId);
-        Task<ServiceResult<List<SurveyPreviewDto>>> GetAllGlobalSurveys(int tenantId, string userType);
+        Task<ServiceResult<List<SurveyPreviewDto>>> GetAllGlobalSurveys(int tenantId);
         Task<ServiceResult<SurveyDetailDto>> GetMySurvey(int surveyId, int userId);
         Task<ServiceResult<bool>> Complete(SurveyResponseCreateDto surveyResponse);
         Task<ServiceResult<List<SurveyResponseDetailDto>>> GetAllResponses(int surveyId, string userType, int tenantId);
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/SurveyService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/SurveyService.cs
index c27512f..3f6ba29 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/SurveyService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/SurveyService.cs
@@ -83,25 +83,36 @@ namespace Tsi1.BusinessLayer.Services
             return result;
         }
 
-        public async Task<ServiceResult<List<SurveyPreviewDto>>> GetAllGlobalSurveys(int tenantId, string userType)
+        public async Task<ServiceResult<List<SurveyPreviewDto>>> GetAllGlobalSurveys(int tenantId)
         {
             var result = new ServiceResult<List<SurveyPreviewDto>>();
 
             var tenant = await _context.Tenants.FirstOrDefaultAsync(x => x.Id == tenantId);
-            var tenantAdmin = await _context.Tenants.FirstOrDefaultAsync(x => x.Name == TenantAdmin.Name);
 
-            if (tenant == null || userType == UserTypes.UdelarAdmin && tenantAdmin.Id == tenantId)
+            if (tenant == null)
             {
                 result.HasError = true;
                 result.AddMessage(string.Format(ErrorMessages.TenantDoesNotExist, tenantId));
                 return result;
             }
 
-            var surveys = await _context.Surveys
-                .Include(x => x.Tenant)
-                .Where(x => x.IsGlobal && x.Tenant.Id == tenantId)
-                .ToListAsync();
+            var surveys = new List<Survey>();
 
+            if (tenant.Name == TenantAdmin.Name)
+            {
+                surveys = await _context.Surveys
+                    .Include(x => x.Tenant)
+                    .Where(x => x.IsGlobal)
+                    .ToListAsync();
+            }
+            else
+            {
+                surveys = await _context.Surveys
+                    .Include(x => x.Tenant)
+                    .Where(x => x.IsGlobal && x.Tenant.Id == tenantId)
+                    .ToListAsync();
+            }
+            
             result.Data = _mapper.Map<List<SurveyPreviewDto>>(surveys);
 
             return result;
-- 
GitLab