Skip to content

Commit

Permalink
Ensure unique question numbers for Q&A entries (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert authored Aug 19, 2024
1 parent 09bd69a commit bc6bd06
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"devDependencies": {
"@rushstack/eslint-patch": "^1.10.3",
"@types/express": "^4.17.21",
"@types/mongodb": "^4.0.7",
"@types/uuid": "^10.0.0",
"eslint": "^8.57.0",
"eslint-config-typescript": "^3.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/repository/mongodb.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ const QAEntrySchema = new mongoose.Schema<QAEntry>({
answered: { type: Boolean, required:false }
})
QAEntrySchema.index({userid: 1, username: 1})
QAEntrySchema.index({talkId: 1, entryIndex: 1}, { unique: true, partialFilterExpression: { entryIndex: { $gt: 0 } } })
export const QAEntryModel = mongoose.model<QAEntry>('qa-entry', QAEntrySchema)

export interface QAEntryLike {
Expand Down
29 changes: 23 additions & 6 deletions src/socket/talkRoomQAEntries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import log from '../util/log'
import { qaEntryAnsweredToServerObject, qaEntryLikeToServerObject, qaEntryToServerObject, uuidString } from '../repository/validation.schema'
import isInputValid from '../util/isInputValid'
import { v4 as uuidv4 } from 'uuid'
import { MongoError } from 'mongodb'

export async function handleTalkRoomQAEntries(socket : Socket<ClientToServerEvents,ServerToClientEvents,InterServerEvents,SocketData>) {
const { userid, username, admin, qaadmin } = socket.data
Expand All @@ -20,25 +21,41 @@ export async function handleTalkRoomQAEntries(socket : Socket<ClientToServerEven
socket.on('qaEntryDelete', handleDelete)
socket.on('qaEntryLike', handleLike)

async function handleNew(newQaEntry: QAEntryToServer, callback: (result: OperationResult, entryIndex?: number) => void) {
async function handleNew(newQaEntry: QAEntryToServer, callback: (result: OperationResult, entryIndex?: number) => void, retryCount: number = 1) {
if (!isInputValid(qaEntryToServerObject, newQaEntry, callback)) {
return
}

const { id, talkId, text, anonymous, replyTo, highlight, answered } = newQaEntry
log.debug(`User ${username} created Q&A entry in ${talkId}: ${text}`)
const date = new Date()
const qaEntryUsername = anonymous ? undefined : username

if (retryCount > 100) {
log.error(`User ${username} tried to create Q&A entry in ${talkId}, unable to find unique entryIndex after 100 retries.`)
callback({success: false, error: 'Error creating Q&A entry: Unable to find unique entryIndex.'})
return
}

let entryIndex = 0
if (!replyTo) {
const maxEntryIndex = (await QAEntryModel.findOne({talkId}).sort({entryIndex:-1}).exec())?.entryIndex ?? 0
entryIndex = maxEntryIndex + 1
}

await QAEntryModel.create({ _id:id, talkId, date, userid, username: qaEntryUsername, text, entryIndex, replyTo, highlight, answered })
callback({success: true}, entryIndex)
socket.in(talkId).emit('qaEntries', [{id, date, userid, username: qaEntryUsername, text, entryIndex, replyTo, highlight, answered, likeUserIds: []}])
try {
await QAEntryModel.create({ _id:id, talkId, date, userid, username: qaEntryUsername, text, entryIndex, replyTo, highlight, answered })
log.debug(`User ${username} created Q&A entry in ${talkId}: ${text}`)
callback({success: true}, entryIndex)
socket.in(talkId).emit('qaEntries', [{id, date, userid, username: qaEntryUsername, text, entryIndex, replyTo, highlight, answered, likeUserIds: []}])
}
catch (error) {
if ((error instanceof MongoError) && error.code === 11000) {
log.debug(`User ${username} tried to create Q&A entry in ${talkId}, but entryIndex is a duplicate: ${entryIndex}; try again...`)
handleNew(newQaEntry, callback, retryCount + 1)
return
}
log.error(`User ${username} tried to create Q&A entry in ${talkId}, resulted in error: ${error}`)
callback({success: false, error: `Error creating Q&A entry: ${error}`})
}
}

/**
Expand Down

0 comments on commit bc6bd06

Please sign in to comment.