From c83a1b172064aed631ac04eba4bcd0c74026e8de Mon Sep 17 00:00:00 2001
From: esantangelo <enzo020895@gmail.com>
Date: Sat, 12 Dec 2020 10:04:35 -0300
Subject: [PATCH] fix survey

---
 .../Tsi1.BusinessLayer/Services/SurveyService.cs   | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/SurveyService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/SurveyService.cs
index 3f6ba29..e10755c 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/SurveyService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/SurveyService.cs
@@ -183,7 +183,7 @@ namespace Tsi1.BusinessLayer.Services
         {
             var result = new ServiceResult<List<SurveyResponseDetailDto>>();
 
-            var tenant = await _context.Tenants.FirstOrDefaultAsync(x => x.Id == tenantId);
+            var tenant = await _context.Tenants.AsNoTracking().FirstOrDefaultAsync(x => x.Id == tenantId);
 
             if (tenant == null)
             {
@@ -192,7 +192,13 @@ namespace Tsi1.BusinessLayer.Services
                 return result;
             }
 
-            var survey = await _context.Surveys.FirstOrDefaultAsync(x => x.Id == surveyId);
+            var survey = await _context.Surveys
+                .AsNoTracking()
+                .Include(x => x.Tenant)
+                .Include(x => x.SectionItem)
+                    .ThenInclude(x => x.Section)
+                        .ThenInclude(x => x.Course)
+                .FirstOrDefaultAsync(x => x.Id == surveyId);
 
             if (survey == null)
             {
@@ -201,7 +207,9 @@ namespace Tsi1.BusinessLayer.Services
                 return result;
             }
 
-            if (userType == UserTypes.Professor)
+            var surveyTenant = survey.IsGlobal ? survey.Tenant.Id : survey.SectionItem.Section.Course.TenantId;
+
+            if ((userType == UserTypes.Professor || userType == UserTypes.FacultyAdmin) && tenantId != surveyTenant)
             {
                 result.HasError = true;
                 result.AddMessage(string.Format(ErrorMessages.InvalidSurvey, surveyId));
-- 
GitLab