From 79f4b67ab87d258951ec33155750d3868a6785d5 Mon Sep 17 00:00:00 2001 From: Lynne Date: Wed, 15 Apr 2020 19:41:36 -0400 Subject: [PATCH 1/4] update questions on re-record --- server/api/routes/user.js | 43 +++++++++++++++++++ .../QuestionsScreen/QuestionsScreen.tsx | 23 ++++++++++ 2 files changed, 66 insertions(+) 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 58b5911f..5067a727 100644 --- a/src/screens/Main/RecordStack/QuestionsScreen/QuestionsScreen.tsx +++ b/src/screens/Main/RecordStack/QuestionsScreen/QuestionsScreen.tsx @@ -26,6 +26,7 @@ interface Props { export default function QuestionsScreen(props: Props) { const [selected, setSelected] = useState(null) const [questions, setQuestions] = useState>([]) + let questionToRemove = 0 const {push} = props.navigation const [modalVisibility, setModalVisibility] = useState(false) @@ -41,6 +42,26 @@ export default function QuestionsScreen(props: Props) { }) }, []) + async function removeQuestion(){ + fetch(`${BASE_PATH}/api/user/questions`, { + method: 'DELETE', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + 'questionId': questionToRemove, + }), + }) + .then(res => res.json()) + .then(data => { + console.log(data) + }) + .catch(error => { + console.log('Error: ' + error) + }) + } + return ( @@ -56,12 +77,14 @@ export default function QuestionsScreen(props: Props) { {text: 'View Answer'}, {text: 'Re-record Answer', onPress: () => { + questionToRemove = item.ID Alert.alert( 'Are you sure you want to re-record your clip?', 'You\'ll lose your old clip', [ {text: 'Re-record', onPress: () => { + removeQuestion() push('Record', {question: item.text}) } }, From 4fadb3c15c7a796aead5f1dbe917188833731d22 Mon Sep 17 00:00:00 2001 From: Lynne Date: Fri, 17 Apr 2020 01:10:04 -0400 Subject: [PATCH 2/4] added testsd --- __tests__/server/user-questions-route.spec.js | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/__tests__/server/user-questions-route.spec.js b/__tests__/server/user-questions-route.spec.js index 9baece3a..b65b7595 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) + }) + + afterAll(async () => { + const baseName = 'Users' + const userTestID = 'recmAyOc3FPftHqZG' + const fieldName = 'Answered' + + await clearFieldsInSingleRecord(baseName, userTestID, fieldName) + }) +}) From d5fdba24a0cd12f4217cc3bb68ce05d09043c4a3 Mon Sep 17 00:00:00 2001 From: Lynne Date: Fri, 17 Apr 2020 01:15:33 -0400 Subject: [PATCH 3/4] test --- __tests__/server/user-questions-route.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/__tests__/server/user-questions-route.spec.js b/__tests__/server/user-questions-route.spec.js index b65b7595..646fc174 100644 --- a/__tests__/server/user-questions-route.spec.js +++ b/__tests__/server/user-questions-route.spec.js @@ -138,13 +138,13 @@ describe('Checks updated user is returned when question is deleted after confirm expect(res2.status).toBe(200) - // Verify question #7 is deleted from user's answered list + // 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) }) - afterAll(async () => { + afterEach(async () => { const baseName = 'Users' const userTestID = 'recmAyOc3FPftHqZG' const fieldName = 'Answered' From f6046a53365510f4684cf73efd3ded45ecb106e2 Mon Sep 17 00:00:00 2001 From: Lynne Date: Wed, 22 Apr 2020 21:28:37 -0400 Subject: [PATCH 4/4] fixes --- .../RecordStack/QuestionsScreen/QuestionsScreen.tsx | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/screens/Main/RecordStack/QuestionsScreen/QuestionsScreen.tsx b/src/screens/Main/RecordStack/QuestionsScreen/QuestionsScreen.tsx index 7bcad5c5..a40f0574 100644 --- a/src/screens/Main/RecordStack/QuestionsScreen/QuestionsScreen.tsx +++ b/src/screens/Main/RecordStack/QuestionsScreen/QuestionsScreen.tsx @@ -29,7 +29,6 @@ interface Props { export default function QuestionsScreen(props: Props) { const [selected, setSelected] = useState(null) const [questions, setQuestions] = useState>([]) - let questionToRemove = 0 const {push} = props.navigation const [modalVisibility, setModalVisibility] = useState(false) @@ -44,9 +43,9 @@ export default function QuestionsScreen(props: Props) { .catch(error => { console.log('Error' + error) }) - }, []) + }) - async function removeQuestion(){ + async function removeQuestion(id: number){ fetch(`${BASE_PATH}/api/user/questions`, { method: 'DELETE', headers: { @@ -54,7 +53,7 @@ export default function QuestionsScreen(props: Props) { 'Content-Type': 'application/json', }, body: JSON.stringify({ - 'questionId': questionToRemove, + 'questionId': id, }), }) .then(res => res.json()) @@ -81,14 +80,13 @@ export default function QuestionsScreen(props: Props) { {text: 'View Answer'}, {text: 'Re-record Answer', onPress: () => { - questionToRemove = item.ID Alert.alert( 'Are you sure you want to re-record your clip?', 'You\'ll lose your old clip', [ {text: 'Re-record', onPress: () => { - removeQuestion() + removeQuestion(item.ID) push('Record', {question: item.text}) } },