From 027a47a043db028c04d7dff3418b984fe7825e3c Mon Sep 17 00:00:00 2001
From: "saket.agrahari" <saket.agrahari@geant.org>
Date: Wed, 17 Jan 2024 19:21:11 +0000
Subject: [PATCH] [COMP-329] : User getting redirected with wrong year of
survey
---
Changelog.md | 3 +++
compendium_v2/routes/survey.py | 40 +++++++++++++++++++++++++++++++
survey-frontend/src/Landing.tsx | 13 ++++++----
survey-frontend/src/api/survey.ts | 19 ++++++++++++++-
4 files changed, 70 insertions(+), 5 deletions(-)
diff --git a/Changelog.md b/Changelog.md
index 9978a796..df185b62 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -2,6 +2,9 @@
All notable changes to this project will be documented in this file.
+## [0.44] - 2023-12-07
+- COMP-329: User getting redirected with wrong year of survey
+
## [0.43] - 2023-12-07
- COMP-306: Renamed PIONEER to PSNC
- COMP-268: Renamed ANAS to AzScienceNet
diff --git a/compendium_v2/routes/survey.py b/compendium_v2/routes/survey.py
index 35766d19..c06a1452 100644
--- a/compendium_v2/routes/survey.py
+++ b/compendium_v2/routes/survey.py
@@ -46,6 +46,46 @@ LIST_SURVEYS_RESPONSE_SCHEMA = {
'items': {'$ref': '#/definitions/survey'}
}
+SURVEY_ACTIVE_YEAR_RESPONSE_SCHEMA = {
+ "$schema": "http://json-schema.org/draft-07/schema#",
+ "type": "object",
+ "properties": {
+ "year": {
+ "type": "string"
+ }
+ },
+ "required": ["year"],
+ "additionalProperties": False
+}
+
+@routes.route('/active/year', methods=['GET'])
+@common.require_accepts_json
+@login_required
+def get_active_survey_year() -> Any:
+ """
+ retrieve a year of latest active survey
+
+ response will be formatted as:
+
+ .. asjson::
+ compendium_v2.routes.survey.SURVEY_ACTIVE_YEAR_RESPONSE_SCHEMA
+
+ """
+ if not (current_user.is_admin or current_user.is_observer):
+ return {'message': 'Insufficient privileges to access this resource'}, 403
+
+ survey_record = db.session.query(Survey.year).filter(
+ Survey.status == 'open'
+ ).order_by(Survey.year.desc()).first()
+ if survey_record:
+ year = survey_record.year
+ return {'year': year}
+ else:
+ return {'message': 'No open survey found.'}, 404
+
+
+
+
@routes.route('/list', methods=['GET'])
@common.require_accepts_json
diff --git a/survey-frontend/src/Landing.tsx b/survey-frontend/src/Landing.tsx
index c57e21ec..b18d2464 100644
--- a/survey-frontend/src/Landing.tsx
+++ b/survey-frontend/src/Landing.tsx
@@ -2,7 +2,7 @@ import React, { ReactElement, useContext, useState, useEffect } from "react";
import { useNavigate, Link } from "react-router-dom";
import { Table, Container, Row } from "react-bootstrap";
import { userContext } from "./providers/UserProvider";
-import { fetchSurveys } from "./api/survey";
+import { fetchSurveys, fetchActiveSurveyYear } from "./api/survey";
import { Survey } from "./api/types";
@@ -16,9 +16,14 @@ function Landing(): ReactElement {
const isAdmin = loggedIn ? user.permissions.admin : false;
const isObserver = loggedIn ? user.role === 'observer' : false;
- const moveToSurvey = () => {
- const currentYear = new Date().getFullYear();
- navigate(`/survey/response/${currentYear}/${activeNren}`);
+ const moveToSurvey = () => {
+ const [activeSurveyYear, setActiveSurveyYear] = useState<string>()
+ useEffect(() => {
+ fetchActiveSurveyYear().then((year) => {
+ setActiveSurveyYear(year);
+ });
+ }, []);
+ navigate(`/survey/response/${activeSurveyYear}/${activeNren}`);
return <></>
}
diff --git a/survey-frontend/src/api/survey.ts b/survey-frontend/src/api/survey.ts
index 5f4eff5a..d0762be2 100644
--- a/survey-frontend/src/api/survey.ts
+++ b/survey-frontend/src/api/survey.ts
@@ -9,4 +9,21 @@ export async function fetchSurveys(): Promise<Survey[]> {
console.log('failed fetching survey list..');
return [];
}
-}
\ No newline at end of file
+}
+
+export async function fetchActiveSurveyYear(): Promise<string> {
+ try {
+ const response = await fetch('/api/survey/active/year');
+ const data = await response.json();
+ if ('year' in data) {
+ const year = data.year;
+ return year.toString();
+ } else {
+ console.log('Invalid response format: Failed fetching active survey year.');
+ return "";
+ }
+ } catch (error) {
+ console.error('Failed fetching active survey year:', error);
+ return "";
+ }
+}
--
GitLab