Only allow 1 export per minute to debounce button being pressed multple times
This commit is contained in:
@ -2,7 +2,11 @@ import cors from 'cors'
|
||||
import express, { Router } from 'express'
|
||||
import { TaskState } from '../generated/graphql'
|
||||
import { jobStateToTaskState } from '../queue-processor'
|
||||
import { countExportsWithin24Hours, saveExport } from '../services/export'
|
||||
import {
|
||||
countExportsWithin24Hours,
|
||||
countExportsWithinMinute,
|
||||
saveExport,
|
||||
} from '../services/export'
|
||||
import { getClaimsByToken, getTokenByRequest } from '../utils/auth'
|
||||
import { corsConfig } from '../utils/corsConfig'
|
||||
import { queueExportJob } from '../utils/createTask'
|
||||
@ -27,6 +31,17 @@ export function exportRouter() {
|
||||
const userId = claims.uid
|
||||
|
||||
try {
|
||||
const exportsWithinMinute = await countExportsWithinMinute(userId)
|
||||
if (exportsWithinMinute >= 1) {
|
||||
logger.error('User has reached the limit of exports within minute', {
|
||||
userId,
|
||||
exportsWithinMinute,
|
||||
})
|
||||
return res.status(400).send({
|
||||
error: 'EXPORT_LIMIT_REACHED',
|
||||
})
|
||||
}
|
||||
|
||||
const exportsWithin24Hours = await countExportsWithin24Hours(userId)
|
||||
if (exportsWithin24Hours >= 3) {
|
||||
logger.error('User has reached the limit of exports within 24 hours', {
|
||||
|
||||
@ -13,6 +13,16 @@ export const saveExport = async (
|
||||
})
|
||||
}
|
||||
|
||||
export const countExportsWithinMinute = async (
|
||||
userId: string
|
||||
): Promise<number> => {
|
||||
return getRepository(Export).countBy({
|
||||
userId,
|
||||
createdAt: MoreThan(new Date(Date.now() - 60 * 1000)),
|
||||
state: In([TaskState.Pending, TaskState.Running, TaskState.Succeeded]),
|
||||
})
|
||||
}
|
||||
|
||||
export const countExportsWithin24Hours = async (
|
||||
userId: string
|
||||
): Promise<number> => {
|
||||
|
||||
Reference in New Issue
Block a user