diff --git a/__tests__/server/user-questions-route.spec.js b/__tests__/server/user-questions-route.spec.js index 9baece3a..646fc174 100644 --- a/__tests__/server/user-questions-route.spec.js +++ b/__tests__/server/user-questions-route.spec.js @@ -108,4 +108,47 @@ describe('Checks updated user is returned when question is answered', () => { }) }) +describe('Checks updated user is returned when question is deleted after confirming re-record', () => { + let session + let questionIDs + beforeAll(async () => { + // setup session and login + session = await supertestsession(app) + await session.post('/api/auth/login').send({ Email: 'a@b.com', Password: 'password' }) + + const questions = await request.get('/api/questions') + questionIDs = (questions.body).map(question => question.ID) + }) + it('should return successful w/ updated user w/ new question deleted in answered field ', async () => { + // answer question 7 and then retrieve questions + await session.post('/api/user/questions').send({ questionId: questionIDs[7] }) + const res = await session.get('/api/user/questions') + + expect(res.status).toBe(200) + + // Confirm the # of answered questions is only 1 and verify it is question #7 + const answered = res.body + expect(answered.length).toBe(10) + expect(answered[7].Answered).toBe(true) + + // delete question 7 and then retrieve questions + await session.delete('/api/user/questions').send({ questionId: questionIDs[7] }) + const res2 = await session.get('/api/user/questions') + + expect(res2.status).toBe(200) + + // Verify question 7 is deleted from user's answered list + const answered2 = res2.body + expect(answered2.length).toBe(10) + expect(answered2[7].Answered).toBe(false) + }) + + afterEach(async () => { + const baseName = 'Users' + const userTestID = 'recmAyOc3FPftHqZG' + const fieldName = 'Answered' + + await clearFieldsInSingleRecord(baseName, userTestID, fieldName) + }) +}) diff --git a/server/api/routes/user.js b/server/api/routes/user.js index c7e5e0c9..a16228b2 100644 --- a/server/api/routes/user.js +++ b/server/api/routes/user.js @@ -146,5 +146,48 @@ router.post('/questions', async (req, res) => { } }) +/* +Removes question for user given questionId + returns user object, including questions field + Takes ID of question in body request + { questionId: 2 } + Returns the updated User +*/ +router.delete('/questions', async (req, res) => { + try { + if(req.session && req.session.authenticated) { // user logged in + const user = req.session.authenticated + const { questionId } = req.body + const answeredQuestions = user.Answered // user's answered questions + + // Use question's ID to grab the whole object from questions table + const newQuestion = await getQuestion(questionId) + + // remove answered question + const updatedQuestions = answeredQuestions.filter(e => e !== newQuestion._record) + + // remove question to user's answered list + const updatedUser = await updateAnsweredQuestions(user, updatedQuestions) + + return res.status(200).send(updatedUser) + } else { + return res.status(404).send({ + message: 'user not logged in', + statusCode: 404, + }) + } + } catch (err) { + // when `statusCode` is not included, it is a server error 500 + if (err.statusCode === undefined) { + return res.status(500).send({ + status: 500, + message: err.message, + stack: err.stack + }) + } + return res.status(err.statusCode).send(err) + } +}) + module.exports = router diff --git a/src/screens/Main/RecordStack/QuestionsScreen/QuestionsScreen.tsx b/src/screens/Main/RecordStack/QuestionsScreen/QuestionsScreen.tsx index 2cc9852e..a40f0574 100644 --- a/src/screens/Main/RecordStack/QuestionsScreen/QuestionsScreen.tsx +++ b/src/screens/Main/RecordStack/QuestionsScreen/QuestionsScreen.tsx @@ -43,7 +43,27 @@ export default function QuestionsScreen(props: Props) { .catch(error => { console.log('Error' + error) }) - }, []) + }) + + async function removeQuestion(id: number){ + fetch(`${BASE_PATH}/api/user/questions`, { + method: 'DELETE', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + 'questionId': id, + }), + }) + .then(res => res.json()) + .then(data => { + console.log(data) + }) + .catch(error => { + console.log('Error: ' + error) + }) + } return ( @@ -66,6 +86,7 @@ export default function QuestionsScreen(props: Props) { [ {text: 'Re-record', onPress: () => { + removeQuestion(item.ID) push('Record', {question: item.text}) } },