Skip to content

Commit

Permalink
no numToAlpha
Browse files Browse the repository at this point in the history
  • Loading branch information
dlopezalvas committed Dec 5, 2023
1 parent 3c32e69 commit 8fb5ac5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 34 deletions.
27 changes: 2 additions & 25 deletions src/api/routes/userChallenges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,11 @@ const upsertChallenge = async (sharedId, user, body) => {

function generateChallengeID(): string {
const nowTimestamp: number = Date.now() - Date.UTC(2023, 0, 1)
return numToAlpha(nowTimestamp)
}

function numToAlpha(num: number): string {
const mapping = '012345679' //We dont use letters to prevent offensive words. We removed the 8 for the same reason.
const alpha: string[] = []
let remainingNum: number = num - 1
while (remainingNum >= 0) {
alpha.push(mapping[remainingNum % 9])
remainingNum = Math.floor(remainingNum / 9) - 1
}
return alpha.reverse().join('')
}

const existChallengeWithSharedId = async (sharedId) => await UserChallengeModel.exists({ sharedId })

const generateUniqueChallengeSharedId = async () => {
let sharedId

do {
sharedId = generateChallengeID()
} while (await existChallengeWithSharedId(sharedId))

return sharedId
return nowTimestamp.toString()
}

const createUserChallenge = async (body, user) => {
const sharedId = await generateUniqueChallengeSharedId()
const sharedId = generateChallengeID()
return await UserChallengeModel.create({ ...body, user, sharedId })
}

Expand Down
10 changes: 1 addition & 9 deletions test/api/userChallenges.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,4 @@ describeApi('User challenges', (request, { authenticated }) => {

})

})

/**
* Si no está logueado nunca entra, asi que siempre testear logueado
*
* - Challenge no existe, lo guardo, el id ya existe, creo otro.
*
*
*/
})

0 comments on commit 8fb5ac5

Please sign in to comment.