diff --git a/api/dashboard/lc/lc_support_functions.py b/api/dashboard/lc/dash_ig_helper.py similarity index 61% rename from api/dashboard/lc/lc_support_functions.py rename to api/dashboard/lc/dash_ig_helper.py index 9f5528b4..9128069e 100644 --- a/api/dashboard/lc/lc_support_functions.py +++ b/api/dashboard/lc/dash_ig_helper.py @@ -1,5 +1,7 @@ from datetime import datetime, timedelta +from db.learning_circle import UserCircleLink, LearningCircle + def get_today_start_end(date_time): start_of_day = datetime( @@ -41,3 +43,24 @@ def get_week_start_end(date_time): ) return start_of_week, end_of_week + +def is_learning_circle_member(user_id, circle_id): + user_circle_link = UserCircleLink.objects.filter( + user_id=user_id, + circle_id=circle_id, + accepted=True + ).exists() + + if user_circle_link: + return True + return False + + +def is_valid_learning_circle(circle_id): + learning_circle = LearningCircle.objects.filter( + id=circle_id + ).exists() + + if learning_circle: + return True + return False diff --git a/api/dashboard/lc/dash_lc_serializer.py b/api/dashboard/lc/dash_lc_serializer.py index dad4aaf1..7a2fda4c 100644 --- a/api/dashboard/lc/dash_lc_serializer.py +++ b/api/dashboard/lc/dash_lc_serializer.py @@ -12,7 +12,8 @@ from utils.types import OrganizationType from utils.utils import DateTimeUtils from utils.types import Lc -from .lc_support_functions import get_today_start_end, get_week_start_end +from .dash_ig_helper import get_today_start_end, get_week_start_end + class LearningCircleSerializer(serializers.ModelSerializer): created_by = serializers.CharField(source='created_by.fullname') diff --git a/api/dashboard/lc/dash_lc_view.py b/api/dashboard/lc/dash_lc_view.py index a444ccbd..6181ed36 100644 --- a/api/dashboard/lc/dash_lc_view.py +++ b/api/dashboard/lc/dash_lc_view.py @@ -21,6 +21,8 @@ LearningCircleMemberListSerializer, MeetRecordsCreateEditDeleteSerializer, IgTaskDetailsSerializer, \ ScheduleMeetingSerializer, ListAllMeetRecordsSerializer, AddMemberSerializer +from .dash_ig_helper import is_learning_circle_member, is_valid_learning_circle, get_today_start_end, get_week_start_end + domain = config("FR_DOMAIN_NAME") from_mail = config("FROM_MAIL") @@ -250,23 +252,19 @@ class LearningCircleDetailsApi(APIView): def get(self, request, circle_id, member_id=None): user_id = JWTUtils.fetch_user_id(request) - user_circle_link = UserCircleLink.objects.filter( - user_id=user_id, - circle_id=circle_id, - accepted=True - ).first() - - if user_circle_link is None: + if not is_valid_learning_circle(circle_id): return CustomResponse( - general_message='your are not a part of this learning circle circle' + general_message='invalid learning circle' ).get_failure_response() - if not LearningCircle.objects.filter(id=circle_id).exists(): + if not is_learning_circle_member(user_id, circle_id): return CustomResponse( - general_message='Learning Circle not found' + general_message='unauthorized access' ).get_failure_response() - learning_circle = LearningCircle.objects.filter(id=circle_id).first() + learning_circle = LearningCircle.objects.filter( + id=circle_id + ).first() serializer = LearningCircleDetailsSerializer( learning_circle, @@ -719,3 +717,49 @@ def post(self, request, circle_id): return CustomResponse( message=serializer.errors ).get_failure_response() + + +class ValidateUserMeetCreateAPI(APIView): + def get(self, request, circle_id): + user_id = JWTUtils.fetch_user_id(request) + + if not is_valid_learning_circle(circle_id): + return CustomResponse( + general_message='invalid learning circle' + ).get_failure_response() + + if not is_learning_circle_member(user_id, circle_id): + return CustomResponse( + general_message='unauthorized access' + ).get_failure_response() + + today_date_time = DateTimeUtils.get_current_utc_time() + + start_of_day, end_of_day = get_today_start_end(today_date_time) + start_of_week, end_of_week = get_week_start_end(today_date_time) + + if CircleMeetingLog.objects.filter( + circle_id=circle_id, + meet_time__range=( + start_of_day, + end_of_day + ) + ).exists(): + return CustomResponse( + general_message=f'Another meet already scheduled on {today_date_time.date()}' + ).get_failure_response() + + if CircleMeetingLog.objects.filter( + circle_id=circle_id, + meet_time__range=( + start_of_week, + end_of_week + ) + ).count() >= 5: + return CustomResponse( + general_message='you can create only 5 meeting in a week' + ).get_failure_response() + + return CustomResponse( + general_message='success' + ).get_success_response() diff --git a/api/dashboard/lc/urls.py b/api/dashboard/lc/urls.py index 6b81105a..970ec5f4 100644 --- a/api/dashboard/lc/urls.py +++ b/api/dashboard/lc/urls.py @@ -4,9 +4,13 @@ urlpatterns = [ path('user-list/', dash_lc_view.UserLearningCircleListApi.as_view(), name='main'), # list all lc's of user + # URL issue + path('list-all//', dash_lc_view.TotalLearningCircleListApi.as_view(), name='list-all-search'), path('/details/', dash_lc_view.LearningCircleDetailsApi.as_view(), name='lc-detailed'), # individual ls details + # dashboard search listing path('/schedule-meet/', dash_lc_view.ScheduleMeetAPI.as_view(), name='schedule-meet'), path('/report/create/', dash_lc_view.SingleReportDetailAPI.as_view(), name='create-report'), + path('/report/create/validate/', dash_lc_view.ValidateUserMeetCreateAPI.as_view(), name='validate-report'), path('/report//show/', dash_lc_view.SingleReportDetailAPI.as_view(), name='show-report'), path('/add-member/', dash_lc_view.AddMemberAPI.as_view(), name='add-member'), path('create/', dash_lc_view.LearningCircleCreateApi.as_view(), name='create'), @@ -22,7 +26,6 @@ path('//', dash_lc_view.LearningCircleDetailsApi.as_view()), # user accept or reject, also for removal path('list/', dash_lc_view.LearningCircleMainApi.as_view(), name='list'), # public page listing path('list-all/', dash_lc_view.TotalLearningCircleListApi.as_view(), name='list-all'), # dashboard search listing - path('list-all//', dash_lc_view.TotalLearningCircleListApi.as_view(), name='list-all-search'), # dashboard search listing # path('data/', dash_lc_view.LearningCircleDataAPI.as_view(), name='data'), # path('list-members//', dash_lc_view.LearningCircleListMembersApi.as_view(), name='list-members'),