Skip to content
Snippets Groups Projects
Commit e0027976 authored by Remco Tukker's avatar Remco Tukker
Browse files

opening and closing surveys

parent 957bfb7b
No related branches found
No related tags found
1 merge request!52Feature/admin workflow surveys page
......@@ -38,6 +38,65 @@ def get_nrens() -> Any:
return jsonify(entries)
# TODO admin only
@routes.route('/new', methods=['POST'])
@common.require_accepts_json
def start_new_survey() -> Any:
all_surveys = db.session.scalars(select(Survey).options(load_only(Survey.status)))
if any([survey.status != SurveyStatus.published for survey in all_surveys]):
return "All earlier surveys should be published before starting a new one", 400
last_survey = db.session.scalar(
select(Survey).order_by(Survey.year.desc()).limit(1)
)
new_year = last_survey.year + 1
new_survey = last_survey.survey
new_survey = Survey(year=new_year, survey=new_survey, status=SurveyStatus.closed)
db.session.add(new_survey)
db.session.commit()
return {'success': True}
# TODO admin only
@routes.route('/open/<int:year>', methods=['POST'])
@common.require_accepts_json
def open_survey(year) -> Any:
survey = db.session.scalar(select(Survey).where(Survey.year == year))
if not survey:
return "Survey not found", 404
if survey.status != SurveyStatus.closed:
return "Survey is not closed and can therefore not be opened", 400
# TODO check if there are no other open surveys
survey.status = SurveyStatus.open
db.session.commit()
return {'success': True}
# TODO admin only
@routes.route('/close/<int:year>', methods=['POST'])
@common.require_accepts_json
def close_survey(year) -> Any:
survey = db.session.scalar(select(Survey).where(Survey.year == year))
if not survey:
return "Survey not found", 404
if survey.status != SurveyStatus.open:
return "Survey is not open and can therefore not be closed", 400
survey.status = SurveyStatus.closed
db.session.commit()
return {'success': True}
# TODO publish
# TODO admin only
@routes.route('/list', methods=['GET'])
@common.require_accepts_json
......@@ -60,16 +119,16 @@ def list_surveys() -> Any:
for entry in surveys
]
# TODO i suppose we should also add response entries for the nrens that didnt start the survey, with a not started status
# TODO i suppose we should also add response entries for the nrens that didnt start the survey, with a not started status? (only when open?)
# TODO fix the ordering of the responses by nren name, i couldnt convince SA yet..
return jsonify(entries)
@routes.route('/open/<string:nren_name>', methods=['GET'])
@routes.route('/respond/<string:nren_name>', methods=['GET'])
@common.require_accepts_json
def open_survey(nren_name) -> Any:
def respond_to_survey(nren_name) -> Any:
# just a hardcoded year for development for now
nren = db.session.execute(select(NREN).filter(NREN.name == nren_name)).scalar_one()
......
......@@ -82,7 +82,7 @@ function SurveyComponent({ nrenName }) {
}
async function getModel() {
const response = await fetch('/api/survey/open/' + nrenName);
const response = await fetch('/api/survey/respond/' + nrenName);
const json = await response.json();
for (const questionName in json["verification_status"]) {
......
......@@ -30,21 +30,42 @@ function SurveyManagementComponent() {
const [surveys, setSurveys] = useState<Survey[]>([]);
useEffect(() => {
// Fetch user
fetchSurveys().then((userList) => {
setSurveys(userList);
fetchSurveys().then((surveyList) => {
setSurveys(surveyList);
});
}, []);
async function newSurvey() {
await fetch('/api/survey/new', { method: 'POST' });
fetchSurveys().then((surveyList) => {
setSurveys(surveyList);
});
}
async function postSurveyStatus(year, status) {
await fetch('/api/survey/' + status + '/' + year, { method: 'POST' });
fetchSurveys().then((surveyList) => {
setSurveys(surveyList);
});
}
console.log(surveys);
const newSurveyAllowed = surveys.every(s => s.status == 'published');
const openSurveys = surveys.some(s => s.status == 'open');
return (
<div>
<Button>start new survey - only available when all surveys published (or not started)</Button>
<Button onClick={newSurvey} disabled={!newSurveyAllowed}>start new survey</Button>
<Accordion>
{surveys.map(survey => (
<Accordion.Item eventKey={survey.year.toString()} key={survey.year}>
<Accordion.Header>{survey.year} - {survey.status} - <Button>inspect - show whole survey with all questions visible</Button> <Button>open - only available when closed and no other surveys open</Button> <Button>close - only available when open</Button> <Button>publish - only available when closed and all answers checked</Button></Accordion.Header>
<Accordion.Header>{survey.year} - {survey.status} -
<Button>inspect - show whole survey with all questions visible</Button>
<Button onClick={() => postSurveyStatus(survey.year, 'open')} disabled={openSurveys || survey.status!='closed'}>open</Button>
<Button onClick={() => postSurveyStatus(survey.year, 'close')} disabled={survey.status!='open'}>close</Button>
<Button onClick={() => postSurveyStatus(survey.year, 'publish')} disabled={survey.status!='closed' || !survey.responses.every(r => r.status == 'checked')}>publish</Button>
</Accordion.Header>
<Accordion.Body>
<Table>
<tbody>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment