Skip to content

Validation #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions server/api/points/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,41 @@ import { Middleware } from 'koa'
import Router from 'koa-router'

import PointModel, {
PointDocument,
PointDocument,
// PointType,
} from '../../models/points'

import {
Props,
} from '@types'


import { validTopDays, validUserDays, validGetUser } from '../../utils/points/validators'

export default (props: Props): Middleware => {
const router = new Router()

const {

} = props

// TODO: validate input

router.get('/:id', async ctx => {
if (!validGetUser(ctx.params)) return ctx.throw(400)

const points: PointDocument[] | null = await PointModel.find({ userID: ctx.params.id })

if (points?.length > 0) ctx.body = points
else ctx.throw(404)
})

router.get('/user/:id/:days', async ctx => {
if (!validUserDays(ctx.params)) return ctx.throw(400)


const date = new Date(Date.now() - Number(ctx.params.days) * 24 * 60 * 60 * 1000)

const points: PointDocument[] | null = await PointModel.find({
userID: ctx.params.id,
createdAt: { $gte: date.toISOString() },
Expand All @@ -39,16 +47,16 @@ export default (props: Props): Middleware => {
})

router.get('/top/:days', async ctx => {
const date = new Date(Date.now() - Number(ctx.params.days) * 24 * 60 * 60 * 1000)
if (!validTopDays(ctx.params)) return ctx.throw(400)

/* TODO: Type all of this */
const date = new Date(Date.now() - Number(ctx.params.days) * 24 * 60 * 60 * 1000)

const { results } = await PointModel.mapReduce({
map: "function () { emit(this.userID, this.amount) }",
reduce: "function (_, values) { return Array.sum(values) }",

query: {
createdAt: {
createdAt: {
$gte: date.toISOString(),
},
},
Expand Down
22 changes: 22 additions & 0 deletions server/utils/points/validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const validGetUser = (params): boolean => {
if (!Object.keys(params).includes('id')) return false

return true
}

const validUserDays = (params): boolean => {
if (!Object.keys(params).includes('days')) return false
if (typeof params.days !== 'string') return false

return true
}

const validTopDays = (params): boolean => {
if (!Object.keys(params).includes('days')) return false
if (typeof params.days !== 'string') return false

return true
}


export { validGetUser, validUserDays, validTopDays }