Skip to content

Commit

Permalink
using shorter id
Browse files Browse the repository at this point in the history
  • Loading branch information
dlopezalvas committed Nov 29, 2023
1 parent be63cd7 commit 9773498
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
66 changes: 49 additions & 17 deletions src/api/routes/creatorChallenges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,26 @@ import { CreatorChallengeModel } from '../../models/creatorChallenge'

const router = express.Router()

const upsertChallenge = async (_id, user, body) => {
let challenge = await CreatorChallengeModel.findOne({ _id, user}).exec()
router.post('/share', tryy(tokenAuth), onlyIfAuth, syncHandler(async (req: AuthenticatedRequest, res) => {
const { user, body } = req
res.json(await createCreatorChallenge(body, user))
}))

router.get('/sharedChallenge/:sharedId', (async (req: AuthenticatedRequest, res) => {
const { sharedId } = req.params as any
const challenge = await CreatorChallengeModel.findOne({ sharedId }).exec()
res.json(challenge)
}))

router.put('/share/:sharedId', tryy(tokenAuth), onlyIfAuth, syncHandler(async (req: AuthenticatedRequest, res) => {
const { sharedId } = req.params as any
const { user, body } = req
res.json(await upsertChallenge(sharedId, user, body))
}))


const upsertChallenge = async (sharedId, user, body) => {
let challenge = await CreatorChallengeModel.findOne({ sharedId, user }).exec()
if (!challenge) {
challenge = await createCreatorChallenge(body, user)
} else {
Expand All @@ -15,24 +33,38 @@ const upsertChallenge = async (_id, user, body) => {
return challenge
}

const createCreatorChallenge = async (body, user) => await CreatorChallengeModel.create({ ...body, user })
function generateChallengeID(): string {
const nowTimestamp: number = Date.now() - Date.UTC(2023, 0, 1)
return numToAlpha(nowTimestamp)
}

router.post('/share', tryy(tokenAuth), onlyIfAuth, syncHandler(async (req: AuthenticatedRequest, res) => {
const { user, body } = req
res.json(await createCreatorChallenge(body, user))
}))
function numToAlpha(num: number): string {
const mapping: string = 'abcdefghijklmnopqrstuvwxyz';
const alpha: string[] = [];
let remainingNum: number = num - 1;
while (remainingNum >= 0) {
alpha.push(mapping[remainingNum % 26]);
remainingNum = Math.floor(remainingNum / 26) - 1;
}
return alpha.reverse().join('');
}

router.get('/sharedChallenge/:_id', (async (req: AuthenticatedRequest, res) => {
const { _id } = req.params as any
const challenge = await CreatorChallengeModel.findOne({ _id }).exec()
res.json(challenge)
}))
const existChallengeWithSharedId = async (sharedId) => await CreatorChallengeModel.exists({ sharedId })

router.put('/share/:_id', tryy(tokenAuth), onlyIfAuth, syncHandler(async (req: AuthenticatedRequest, res) => {
const { _id } = req.params as any
const { user, body } = req
res.json(await upsertChallenge(_id, user, body))
}))
const generateUniqueChallengeSharedId = async () => {
let sharedId

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

return sharedId
}

const createCreatorChallenge = async (body, user) => {
const sharedId = await generateUniqueChallengeSharedId()
return await CreatorChallengeModel.create({ ...body, user, sharedId })
}


export default router
6 changes: 4 additions & 2 deletions src/models/creatorChallenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { StaticAnalysis } from './staticAnalysis'
import { User } from './user'

//This is duplicated in pilas react, remember to unificar when making the monorepo :)
const sceneTypes = ["Lita", "Duba", "Toto", "Coty", "Manic", "Chuy", "Yvoty", "Capy", "Custom"] as const //Used for file validity checking
const sceneTypes = ["Lita", "Duba", "Toto", "Coty", "Manic", "Chuy", "Yvoty", "Capy", "Custom"] as const //Used for file validity checking
export type SceneType = typeof sceneTypes[number]

type Cell = string
Expand All @@ -17,7 +17,7 @@ export type Scene = {
maps: SceneMap[]
}

type Assesments = {
type Assesments = {
itWorks?: boolean, // old "debeFelicitar", default true
decomposition?: DecompositionAssessment,
simpleRepetition?: boolean,
Expand All @@ -29,6 +29,8 @@ type DecompositionAssessment = { maxProgramLength: number }

@modelOptions({ schemaOptions: { collection: 'creatorChallenges' }, options: { allowMixed: Severity.ALLOW } })
export class CreatorChallenge {
@prop({ required: true, unique: true })
sharedId: string
@prop({ required: true })
fileVersion: number
@prop({ required: true })
Expand Down

0 comments on commit 9773498

Please sign in to comment.