Skip to content

Commit

Permalink
Merge branch 'master' into yq-connect-video-to-questions
Browse files Browse the repository at this point in the history
  • Loading branch information
yangxqiao authored Apr 23, 2020
2 parents 6ef1d6d + e1e1055 commit 44bfa0f
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
43 changes: 43 additions & 0 deletions __tests__/server/user-questions-route.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]', 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)
})
})
43 changes: 43 additions & 0 deletions server/api/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

21 changes: 21 additions & 0 deletions src/screens/Main/RecordStack/QuestionsScreen/QuestionsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ function QuestionsScreen(props: Props) {
})
})

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 (
<View style={styles.container}>
<FlatList<Question>
Expand All @@ -67,6 +87,7 @@ function QuestionsScreen(props: Props) {
[
{text: 'Re-record',
onPress: () => {
removeQuestion(item.ID)
push('Record', {question: item.text, questionID: item.ID})
}
},
Expand Down

0 comments on commit 44bfa0f

Please sign in to comment.