diff --git a/.gitignore b/.gitignore index 014d771..a53d4fd 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,7 @@ /logs # Ignore data files -/data \ No newline at end of file +/data + +# Ignore package-lock.json +package-lock.json \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 17c6e2c..0f8adf9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "waifu.it", - "version": "4.8.1", + "version": "4.10.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "waifu.it", - "version": "4.8.0", + "version": "4.10.0", "license": "AGPLv3", "dependencies": { "chalk": "^4.1.2", diff --git a/package.json b/package.json index a77f355..ef2ff77 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "waifu.it", - "version": "4.9.1", + "version": "4.10.0", "description": "Random API Serving Anime stuff", "author": "Aeryk", "private": true, diff --git a/src/app.js b/src/app.js index e14f801..53e14bf 100644 --- a/src/app.js +++ b/src/app.js @@ -2,14 +2,16 @@ import express from 'express'; import bodyParser from 'body-parser'; import cors from 'cors'; -import pkg from '../package.json' assert { type: 'json' }; +import { createRequire } from 'module'; +const require = createRequire(import.meta.url); +const pkg = require('../package.json'); /*** Importing custom error handlers and logger middleware ***/ import { handle404 /*** @params: req, res, next ***/, logErrors /*** @params: err, req, res, next ***/, errorHandler /*** @params: err, req, res, next ***/, } from './middlewares/errors.js'; -import { logIP } from './middlewares/logger.js'; +import { requestLogger } from './middlewares/logger.js'; import routesV3 from './routes/v3/index.js'; import routesV4 from './routes/v4/index.js'; @@ -38,7 +40,7 @@ app.use(express.urlencoded({ extended: true })); * @param {Function} next - Express next middleware function. */ if (process.env.LOGGER === 'true') { - app.use(logIP); + app.use(requestLogger); } /*** Middleware to parse the request body ***/ diff --git a/src/controllers/v3/facts/randomFacts.js b/src/controllers/v3/facts/randomFacts.js index b64a767..14b10fb 100644 --- a/src/controllers/v3/facts/randomFacts.js +++ b/src/controllers/v3/facts/randomFacts.js @@ -2,7 +2,6 @@ import createError from 'http-errors'; import Facts from '../../../models/schemas/Fact.js'; import tagsFilter from '../../../modules/tagsFilter.js'; import lengthFilter from '../../../modules/lengthFilter.js'; -import Stats from '../../../models/schemas/Stat.js'; /** * Gets a random anime fact with optional length and tags filters and updates system statistics. @@ -41,18 +40,10 @@ const getRandomFact = async (req, res, next) => { } // Respond with the random fact - res.status(200).json(result); - - // Update system statistics for facts - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { facts: 1 } }); + return res.status(200).json(result); } catch (error) { - // Update system statistics for failed requests - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; -export default getRandomFact; \ No newline at end of file +export default getRandomFact; diff --git a/src/controllers/v3/gifs/randomAngry.js b/src/controllers/v3/gifs/randomAngry.js index 6156cb9..b99c801 100644 --- a/src/controllers/v3/gifs/randomAngry.js +++ b/src/controllers/v3/gifs/randomAngry.js @@ -1,7 +1,5 @@ - import createError from 'http-errors'; import Angry from '../../../models/schemas/Angry.js'; -import Stats from '../../../models/schemas/Stat.js'; // Get random Anime Angry const getRandomAngry = async (req, res, next) => { @@ -16,14 +14,8 @@ const getRandomAngry = async (req, res, next) => { return next(createError(404, 'Could not find any Angry Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { angry: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomBaka.js b/src/controllers/v3/gifs/randomBaka.js index 746b3b6..f798287 100644 --- a/src/controllers/v3/gifs/randomBaka.js +++ b/src/controllers/v3/gifs/randomBaka.js @@ -1,7 +1,5 @@ - import createError from 'http-errors'; import Baka from '../../../models/schemas/Baka.js'; -import Stats from '../../../models/schemas/Stat.js'; // Get random Anime Baka const getRandomBaka = async (req, res, next) => { @@ -16,14 +14,8 @@ const getRandomBaka = async (req, res, next) => { return next(createError(404, 'Could not find any Baka Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { baka: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomBite.js b/src/controllers/v3/gifs/randomBite.js index ddf9bf1..dce9df8 100644 --- a/src/controllers/v3/gifs/randomBite.js +++ b/src/controllers/v3/gifs/randomBite.js @@ -1,7 +1,5 @@ - import createError from 'http-errors'; import Bite from '../../../models/schemas/Bite.js'; -import Stats from '../../../models/schemas/Stat.js'; // Get random Anime Bite const getRandomBite = async (req, res, next) => { @@ -16,14 +14,8 @@ const getRandomBite = async (req, res, next) => { return next(createError(404, 'Could not find any Bite Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bite: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomBlush.js b/src/controllers/v3/gifs/randomBlush.js index 1a90820..ebf9f30 100644 --- a/src/controllers/v3/gifs/randomBlush.js +++ b/src/controllers/v3/gifs/randomBlush.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Blush from '../../../models/schemas/Blush.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomBlush = async (req, res, next) => { return next(createError(404, 'Could not find any Blush Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { blush: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomBonk.js b/src/controllers/v3/gifs/randomBonk.js index c559823..b89b177 100644 --- a/src/controllers/v3/gifs/randomBonk.js +++ b/src/controllers/v3/gifs/randomBonk.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Bonk from '../../../models/schemas/Bonk.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomBonk = async (req, res, next) => { return next(createError(404, 'Could not find any Bonk Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bonk: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomBored.js b/src/controllers/v3/gifs/randomBored.js index 2caedfb..bb5380a 100644 --- a/src/controllers/v3/gifs/randomBored.js +++ b/src/controllers/v3/gifs/randomBored.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Bored from '../../../models/schemas/Bored.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomBored = async (req, res, next) => { return next(createError(404, 'Could not find any Bored Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bored: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomBully.js b/src/controllers/v3/gifs/randomBully.js index ede6578..cb138f3 100644 --- a/src/controllers/v3/gifs/randomBully.js +++ b/src/controllers/v3/gifs/randomBully.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Bully from '../../../models/schemas/Bully.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomBully = async (req, res, next) => { return next(createError(404, 'Could not find any Bully Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bully: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomBye.js b/src/controllers/v3/gifs/randomBye.js index 873468b..054e3a2 100644 --- a/src/controllers/v3/gifs/randomBye.js +++ b/src/controllers/v3/gifs/randomBye.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Bye from '../../../models/schemas/Bye.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomBye = async (req, res, next) => { return next(createError(404, 'Could not find any Bye Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bye: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomChase.js b/src/controllers/v3/gifs/randomChase.js index a16f38b..ded43b9 100644 --- a/src/controllers/v3/gifs/randomChase.js +++ b/src/controllers/v3/gifs/randomChase.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Chase from '../../../models/schemas/Chase.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomChase = async (req, res, next) => { return next(createError(404, 'Could not find any Chase Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { chase: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomCheer.js b/src/controllers/v3/gifs/randomCheer.js index 21447b8..f5d6852 100644 --- a/src/controllers/v3/gifs/randomCheer.js +++ b/src/controllers/v3/gifs/randomCheer.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Cheer from '../../../models/schemas/Cheer.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomCheer = async (req, res, next) => { return next(createError(404, 'Could not find any Cheer Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { cheer: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomCringe.js b/src/controllers/v3/gifs/randomCringe.js index 3551842..4f439c1 100644 --- a/src/controllers/v3/gifs/randomCringe.js +++ b/src/controllers/v3/gifs/randomCringe.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Cringe from '../../../models/schemas/Cringe.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomCringe = async (req, res, next) => { return next(createError(404, 'Could not find any Cringe Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { cringe: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomCry.js b/src/controllers/v3/gifs/randomCry.js index 80d80e5..596d831 100644 --- a/src/controllers/v3/gifs/randomCry.js +++ b/src/controllers/v3/gifs/randomCry.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Cry from '../../../models/schemas/Cry.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomCry = async (req, res, next) => { return next(createError(404, 'Could not find any Cry Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { cry: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomCuddle.js b/src/controllers/v3/gifs/randomCuddle.js index f5f5314..c1ed804 100644 --- a/src/controllers/v3/gifs/randomCuddle.js +++ b/src/controllers/v3/gifs/randomCuddle.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Cuddle from '../../../models/schemas/Cuddle.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomCuddle = async (req, res, next) => { return next(createError(404, 'Could not find any Cuddle Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { cuddle: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomDab.js b/src/controllers/v3/gifs/randomDab.js index d95f2df..1f1c952 100644 --- a/src/controllers/v3/gifs/randomDab.js +++ b/src/controllers/v3/gifs/randomDab.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Dab from '../../../models/schemas/Dab.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomDab = async (req, res, next) => { return next(createError(404, 'Could not find any Dab Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { dab: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomDance.js b/src/controllers/v3/gifs/randomDance.js index 922e5e3..17c4b92 100644 --- a/src/controllers/v3/gifs/randomDance.js +++ b/src/controllers/v3/gifs/randomDance.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Dance from '../../../models/schemas/Dance.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomDance = async (req, res, next) => { return next(createError(404, 'Could not find any Dance Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { dance: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomDie.js b/src/controllers/v3/gifs/randomDie.js index da8936c..bb06ff3 100644 --- a/src/controllers/v3/gifs/randomDie.js +++ b/src/controllers/v3/gifs/randomDie.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Die from '../../../models/schemas/Die.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomDie = async (req, res, next) => { return next(createError(404, 'Could not find any Die Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { die: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomDisgust.js b/src/controllers/v3/gifs/randomDisgust.js index f52edbe..89b49a7 100644 --- a/src/controllers/v3/gifs/randomDisgust.js +++ b/src/controllers/v3/gifs/randomDisgust.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Disgust from '../../../models/schemas/Disgust.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomDisgust = async (req, res, next) => { return next(createError(404, 'Could not find any Disgust Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { disgust: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomFacepalm.js b/src/controllers/v3/gifs/randomFacepalm.js index 4d1751b..54809b0 100644 --- a/src/controllers/v3/gifs/randomFacepalm.js +++ b/src/controllers/v3/gifs/randomFacepalm.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Facepalm from '../../../models/schemas/Facepalm.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomFacepalm = async (req, res, next) => { return next(createError(404, 'Could not find any Facepalm Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { facepalm: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomFeed.js b/src/controllers/v3/gifs/randomFeed.js index 930e163..f69f327 100644 --- a/src/controllers/v3/gifs/randomFeed.js +++ b/src/controllers/v3/gifs/randomFeed.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Feed from '../../../models/schemas/Feed.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomFeed = async (req, res, next) => { return next(createError(404, 'Could not find any Feed Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { feed: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomGlomp.js b/src/controllers/v3/gifs/randomGlomp.js index 2fd4029..346e7ae 100644 --- a/src/controllers/v3/gifs/randomGlomp.js +++ b/src/controllers/v3/gifs/randomGlomp.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Glomp from '../../../models/schemas/Glomp.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomGlomp = async (req, res, next) => { return next(createError(404, 'Could not find any Glomp Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { glomp: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomHappy.js b/src/controllers/v3/gifs/randomHappy.js index 59de724..d277b27 100644 --- a/src/controllers/v3/gifs/randomHappy.js +++ b/src/controllers/v3/gifs/randomHappy.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Happy from '../../../models/schemas/Happy.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomHappy = async (req, res, next) => { return next(createError(404, 'Could not find any Happy Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { happy: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomHi.js b/src/controllers/v3/gifs/randomHi.js index d2614d7..92c2dd1 100644 --- a/src/controllers/v3/gifs/randomHi.js +++ b/src/controllers/v3/gifs/randomHi.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Hi from '../../../models/schemas/Hi.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomHi = async (req, res, next) => { return next(createError(404, 'Could not find any Hi Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { hi: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomHighfive.js b/src/controllers/v3/gifs/randomHighfive.js index c290116..1e77ae2 100644 --- a/src/controllers/v3/gifs/randomHighfive.js +++ b/src/controllers/v3/gifs/randomHighfive.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Highfive from '../../../models/schemas/Highfive.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomHighfive = async (req, res, next) => { return next(createError(404, 'Could not find any Highfive Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { highfive: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomHold.js b/src/controllers/v3/gifs/randomHold.js index 9640b64..2def44b 100644 --- a/src/controllers/v3/gifs/randomHold.js +++ b/src/controllers/v3/gifs/randomHold.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Hold from '../../../models/schemas/Hold.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomHold = async (req, res, next) => { return next(createError(404, 'Could not find any Hold Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { hold: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomHug.js b/src/controllers/v3/gifs/randomHug.js index 8d3432f..a31c2ca 100644 --- a/src/controllers/v3/gifs/randomHug.js +++ b/src/controllers/v3/gifs/randomHug.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Hug from '../../../models/schemas/Hug.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomHug = async (req, res, next) => { return next(createError(404, 'Could not find any Hug Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { hug: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomKick.js b/src/controllers/v3/gifs/randomKick.js index b24d888..5d84359 100644 --- a/src/controllers/v3/gifs/randomKick.js +++ b/src/controllers/v3/gifs/randomKick.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Kick from '../../../models/schemas/Kick.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomKick = async (req, res, next) => { return next(createError(404, 'Could not find any Kick Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { kick: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomKill.js b/src/controllers/v3/gifs/randomKill.js index 7186102..cb78bd2 100644 --- a/src/controllers/v3/gifs/randomKill.js +++ b/src/controllers/v3/gifs/randomKill.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Kill from '../../../models/schemas/Kill.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomKill = async (req, res, next) => { return next(createError(404, 'Could not find any Kill Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { kill: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomKissu.js b/src/controllers/v3/gifs/randomKissu.js index a13ce66..c62c49b 100644 --- a/src/controllers/v3/gifs/randomKissu.js +++ b/src/controllers/v3/gifs/randomKissu.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Kissu from '../../../models/schemas/Kissu.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomKissu = async (req, res, next) => { return next(createError(404, 'Could not find any Kissu Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { kissu: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomLaugh.js b/src/controllers/v3/gifs/randomLaugh.js index 7450f79..9405927 100644 --- a/src/controllers/v3/gifs/randomLaugh.js +++ b/src/controllers/v3/gifs/randomLaugh.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Laugh from '../../../models/schemas/Laugh.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomLaugh = async (req, res, next) => { return next(createError(404, 'Could not find any Laugh Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { laugh: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomLick.js b/src/controllers/v3/gifs/randomLick.js index df87eb8..9b0b7ff 100644 --- a/src/controllers/v3/gifs/randomLick.js +++ b/src/controllers/v3/gifs/randomLick.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Lick from '../../../models/schemas/Lick.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomLick = async (req, res, next) => { return next(createError(404, 'Could not find any Lick Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { lick: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomLove.js b/src/controllers/v3/gifs/randomLove.js index a0699e5..94df8c6 100644 --- a/src/controllers/v3/gifs/randomLove.js +++ b/src/controllers/v3/gifs/randomLove.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Love from '../../../models/schemas/Love.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomLove = async (req, res, next) => { return next(createError(404, 'Could not find any Love Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { love: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomLurk.js b/src/controllers/v3/gifs/randomLurk.js index 74e36f7..abaa2e0 100644 --- a/src/controllers/v3/gifs/randomLurk.js +++ b/src/controllers/v3/gifs/randomLurk.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Lurk from '../../../models/schemas/Lurk.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomLurk = async (req, res, next) => { return next(createError(404, 'Could not find any Lurk Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { lurk: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomMidfing.js b/src/controllers/v3/gifs/randomMidfing.js index 6290752..93c7fb9 100644 --- a/src/controllers/v3/gifs/randomMidfing.js +++ b/src/controllers/v3/gifs/randomMidfing.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Midfing from '../../../models/schemas/Midfing.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomMidfing = async (req, res, next) => { return next(createError(404, 'Could not find any Midfing Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { midfing: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomNervous.js b/src/controllers/v3/gifs/randomNervous.js index 5a2616b..3ed327f 100644 --- a/src/controllers/v3/gifs/randomNervous.js +++ b/src/controllers/v3/gifs/randomNervous.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Nervous from '../../../models/schemas/Nervous.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomNervous = async (req, res, next) => { return next(createError(404, 'Could not find any Nervous Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { nervous: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomNom.js b/src/controllers/v3/gifs/randomNom.js index 00f9b20..1e2e8c8 100644 --- a/src/controllers/v3/gifs/randomNom.js +++ b/src/controllers/v3/gifs/randomNom.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Nom from '../../../models/schemas/Nom.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomNom = async (req, res, next) => { return next(createError(404, 'Could not find any Nom Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { nom: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomNope.js b/src/controllers/v3/gifs/randomNope.js index 348aa18..5780e5d 100644 --- a/src/controllers/v3/gifs/randomNope.js +++ b/src/controllers/v3/gifs/randomNope.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Nope from '../../../models/schemas/Nope.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomNope = async (req, res, next) => { return next(createError(404, 'Could not find any Nope Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { nope: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomNuzzle.js b/src/controllers/v3/gifs/randomNuzzle.js index 38e1f7b..35958e7 100644 --- a/src/controllers/v3/gifs/randomNuzzle.js +++ b/src/controllers/v3/gifs/randomNuzzle.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Nuzzle from '../../../models/schemas/Nuzzle.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomNuzzle = async (req, res, next) => { return next(createError(404, 'Could not find any Nuzzle Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { nuzzle: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomPanic.js b/src/controllers/v3/gifs/randomPanic.js index abf97e0..616b5cc 100644 --- a/src/controllers/v3/gifs/randomPanic.js +++ b/src/controllers/v3/gifs/randomPanic.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Panic from '../../../models/schemas/Panic.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomPanic = async (req, res, next) => { return next(createError(404, 'Could not find any Panic Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { panic: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomPat.js b/src/controllers/v3/gifs/randomPat.js index bd683d9..2de5c60 100644 --- a/src/controllers/v3/gifs/randomPat.js +++ b/src/controllers/v3/gifs/randomPat.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Pat from '../../../models/schemas/Pat.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomPat = async (req, res, next) => { return next(createError(404, 'Could not find any Pat Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { pat: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomPeck.js b/src/controllers/v3/gifs/randomPeck.js index fb96e43..c0ad060 100644 --- a/src/controllers/v3/gifs/randomPeck.js +++ b/src/controllers/v3/gifs/randomPeck.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Peck from '../../../models/schemas/Peck.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomPeck = async (req, res, next) => { return next(createError(404, 'Could not find any Peck Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { peck: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomPoke.js b/src/controllers/v3/gifs/randomPoke.js index 4b2c428..d4cba75 100644 --- a/src/controllers/v3/gifs/randomPoke.js +++ b/src/controllers/v3/gifs/randomPoke.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Poke from '../../../models/schemas/Poke.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomPoke = async (req, res, next) => { return next(createError(404, 'Could not find any Poke Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { poke: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomPout.js b/src/controllers/v3/gifs/randomPout.js index dc45330..2317601 100644 --- a/src/controllers/v3/gifs/randomPout.js +++ b/src/controllers/v3/gifs/randomPout.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Pout from '../../../models/schemas/Pout.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomPout = async (req, res, next) => { return next(createError(404, 'Could not find any Pout Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { pout: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomPunch.js b/src/controllers/v3/gifs/randomPunch.js index dd35b84..9117e38 100644 --- a/src/controllers/v3/gifs/randomPunch.js +++ b/src/controllers/v3/gifs/randomPunch.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Punch from '../../../models/schemas/Punch.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomPunch = async (req, res, next) => { return next(createError(404, 'Could not find any Punch Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { punch: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomRun.js b/src/controllers/v3/gifs/randomRun.js index 061d36f..065939a 100644 --- a/src/controllers/v3/gifs/randomRun.js +++ b/src/controllers/v3/gifs/randomRun.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Run from '../../../models/schemas/Run.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomRun = async (req, res, next) => { return next(createError(404, 'Could not find any Run Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { run: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomSad.js b/src/controllers/v3/gifs/randomSad.js index 0af254d..d32c954 100644 --- a/src/controllers/v3/gifs/randomSad.js +++ b/src/controllers/v3/gifs/randomSad.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Sad from '../../../models/schemas/Sad.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomSad = async (req, res, next) => { return next(createError(404, 'Could not find any Sad Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { sad: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomShoot.js b/src/controllers/v3/gifs/randomShoot.js index cdc8a1b..c753923 100644 --- a/src/controllers/v3/gifs/randomShoot.js +++ b/src/controllers/v3/gifs/randomShoot.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Shoot from '../../../models/schemas/Shoot.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomShoot = async (req, res, next) => { return next(createError(404, 'Could not find any Shoot Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { shoot: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomShrug.js b/src/controllers/v3/gifs/randomShrug.js index 9755221..274e3d4 100644 --- a/src/controllers/v3/gifs/randomShrug.js +++ b/src/controllers/v3/gifs/randomShrug.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Shrug from '../../../models/schemas/Shrug.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomShrug = async (req, res, next) => { return next(createError(404, 'Could not find any Shrug Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { shrug: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomSip.js b/src/controllers/v3/gifs/randomSip.js index 37fcea4..81fac61 100644 --- a/src/controllers/v3/gifs/randomSip.js +++ b/src/controllers/v3/gifs/randomSip.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Sip from '../../../models/schemas/Sip.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomSip = async (req, res, next) => { return next(createError(404, 'Could not find any Sip Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { sip: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomSlap.js b/src/controllers/v3/gifs/randomSlap.js index 89fb3e5..faa6383 100644 --- a/src/controllers/v3/gifs/randomSlap.js +++ b/src/controllers/v3/gifs/randomSlap.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Slap from '../../../models/schemas/Slap.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomSlap = async (req, res, next) => { return next(createError(404, 'Could not find any Slap Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { slap: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomSleepy.js b/src/controllers/v3/gifs/randomSleepy.js index a408099..ad98366 100644 --- a/src/controllers/v3/gifs/randomSleepy.js +++ b/src/controllers/v3/gifs/randomSleepy.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Sleepy from '../../../models/schemas/Sleepy.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomSleepy = async (req, res, next) => { return next(createError(404, 'Could not find any Sleepy Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { sleepy: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomSmile.js b/src/controllers/v3/gifs/randomSmile.js index 54983e2..4ee72b7 100644 --- a/src/controllers/v3/gifs/randomSmile.js +++ b/src/controllers/v3/gifs/randomSmile.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Smile from '../../../models/schemas/Smile.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomSmile = async (req, res, next) => { return next(createError(404, 'Could not find any Smile Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { smile: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomSmug.js b/src/controllers/v3/gifs/randomSmug.js index 8861730..97d2b0a 100644 --- a/src/controllers/v3/gifs/randomSmug.js +++ b/src/controllers/v3/gifs/randomSmug.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Smug from '../../../models/schemas/Smug.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomSmug = async (req, res, next) => { return next(createError(404, 'Could not find any Smug Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { smug: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomStab.js b/src/controllers/v3/gifs/randomStab.js index 3b0ce94..07d1da4 100644 --- a/src/controllers/v3/gifs/randomStab.js +++ b/src/controllers/v3/gifs/randomStab.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Stab from '../../../models/schemas/Stab.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomStab = async (req, res, next) => { return next(createError(404, 'Could not find any Stab Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { stab: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomStare.js b/src/controllers/v3/gifs/randomStare.js index b1c2979..41de723 100644 --- a/src/controllers/v3/gifs/randomStare.js +++ b/src/controllers/v3/gifs/randomStare.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Stare from '../../../models/schemas/Stare.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomStare = async (req, res, next) => { return next(createError(404, 'Could not find any Stare Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { stare: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomSuicide.js b/src/controllers/v3/gifs/randomSuicide.js index f3d355c..ad1c88b 100644 --- a/src/controllers/v3/gifs/randomSuicide.js +++ b/src/controllers/v3/gifs/randomSuicide.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Suicide from '../../../models/schemas/Suicide.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomSuicide = async (req, res, next) => { return next(createError(404, 'Could not find any Suicide Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { suicide: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomTease.js b/src/controllers/v3/gifs/randomTease.js index 5698856..17f389c 100644 --- a/src/controllers/v3/gifs/randomTease.js +++ b/src/controllers/v3/gifs/randomTease.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Tease from '../../../models/schemas/Tease.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomTease = async (req, res, next) => { return next(createError(404, 'Could not find any Tease Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { tease: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomThink.js b/src/controllers/v3/gifs/randomThink.js index 0afad57..99140a0 100644 --- a/src/controllers/v3/gifs/randomThink.js +++ b/src/controllers/v3/gifs/randomThink.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Think from '../../../models/schemas/Think.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomThink = async (req, res, next) => { return next(createError(404, 'Could not find any Think Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { think: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomThumbsup.js b/src/controllers/v3/gifs/randomThumbsup.js index 6a2d1bd..b00c8f4 100644 --- a/src/controllers/v3/gifs/randomThumbsup.js +++ b/src/controllers/v3/gifs/randomThumbsup.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Thumbsup from '../../../models/schemas/Thumbsup.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomThumbsup = async (req, res, next) => { return next(createError(404, 'Could not find any Thumbsup Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { thumbsup: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomTickle.js b/src/controllers/v3/gifs/randomTickle.js index 06e4bd2..ed373ff 100644 --- a/src/controllers/v3/gifs/randomTickle.js +++ b/src/controllers/v3/gifs/randomTickle.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Tickle from '../../../models/schemas/Tickle.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomTickle = async (req, res, next) => { return next(createError(404, 'Could not find any Tickle Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { tickle: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomTriggered.js b/src/controllers/v3/gifs/randomTriggered.js index bfbc35e..b9da9f7 100644 --- a/src/controllers/v3/gifs/randomTriggered.js +++ b/src/controllers/v3/gifs/randomTriggered.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Triggered from '../../../models/schemas/Triggered.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomTriggered = async (req, res, next) => { return next(createError(404, 'Could not find any Triggered Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { triggered: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomWag.js b/src/controllers/v3/gifs/randomWag.js index 88ad360..e152ca1 100644 --- a/src/controllers/v3/gifs/randomWag.js +++ b/src/controllers/v3/gifs/randomWag.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Wag from '../../../models/schemas/Wag.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomWag = async (req, res, next) => { return next(createError(404, 'Could not find any Wag Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { wag: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomWave.js b/src/controllers/v3/gifs/randomWave.js index 195dfb1..57a55e7 100644 --- a/src/controllers/v3/gifs/randomWave.js +++ b/src/controllers/v3/gifs/randomWave.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Wave from '../../../models/schemas/Wave.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomWave = async (req, res, next) => { return next(createError(404, 'Could not find any Wave Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { wave: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomWink.js b/src/controllers/v3/gifs/randomWink.js index fb03a57..7fc1ebd 100644 --- a/src/controllers/v3/gifs/randomWink.js +++ b/src/controllers/v3/gifs/randomWink.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Wink from '../../../models/schemas/Wink.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomWink = async (req, res, next) => { return next(createError(404, 'Could not find any Wink Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { wink: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/gifs/randomYes.js b/src/controllers/v3/gifs/randomYes.js index 1408489..02f26ab 100644 --- a/src/controllers/v3/gifs/randomYes.js +++ b/src/controllers/v3/gifs/randomYes.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Yesh from '../../../models/schemas/Yesh.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,14 +15,8 @@ const getRandomYes = async (req, res, next) => { return next(createError(404, 'Could not find any Yes Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { yes: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/quotes/randomQuotes.js b/src/controllers/v3/quotes/randomQuotes.js index 9ae2cce..a194216 100644 --- a/src/controllers/v3/quotes/randomQuotes.js +++ b/src/controllers/v3/quotes/randomQuotes.js @@ -29,13 +29,12 @@ const getRandomQuote = async (req, res, next) => { } // Respond with the random quote - res.status(200).json(result); + return res.status(200).json(result); // Update system statistics for quotes - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { quotes: 1 } }); } catch (error) { // Update system statistics for failed requests - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); + return next(error); } }; diff --git a/src/controllers/v3/utils/listTags.js b/src/controllers/v3/utils/listTags.js index f7b15c1..e09680c 100644 --- a/src/controllers/v3/utils/listTags.js +++ b/src/controllers/v3/utils/listTags.js @@ -41,16 +41,9 @@ const listTags = async (req, res, next) => { res.json(results); // Update system statistics for tags - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { tags: 1 } } - ); } catch (error) { // Update system statistics for failed requests - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); + return next(error); } }; diff --git a/src/controllers/v3/utils/owoify.js b/src/controllers/v3/utils/owoify.js index beb1fa0..1a0aaa5 100644 --- a/src/controllers/v3/utils/owoify.js +++ b/src/controllers/v3/utils/owoify.js @@ -24,16 +24,9 @@ const getOwofiyText = async (req, res, next) => { }); // Update system statistics for Owofied texts - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { owoify: 1 } } - ); } catch (error) { // Update system statistics for failed requests - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); + return next(error); } }; diff --git a/src/controllers/v3/utils/randomPassword.js b/src/controllers/v3/utils/randomPassword.js index fe65922..9552194 100644 --- a/src/controllers/v3/utils/randomPassword.js +++ b/src/controllers/v3/utils/randomPassword.js @@ -27,16 +27,9 @@ const getRandomPassword = async (req, res, next) => { }); // Update system statistics for generated passwords - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { password: 1 } } - ); } catch (error) { // Update system statistics for failed requests - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); + return next(error); } }; diff --git a/src/controllers/v3/utils/uvuify.js b/src/controllers/v3/utils/uvuify.js index e22dee0..e17b2b6 100644 --- a/src/controllers/v3/utils/uvuify.js +++ b/src/controllers/v3/utils/uvuify.js @@ -13,16 +13,7 @@ const getOwofiyText = async (req, res, next) => { res.status(200).json({ text: uwuify(text), }); - - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { uwuify: 1 } } - ); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v3/utils/uwuify.js b/src/controllers/v3/utils/uwuify.js index 02b2f2e..dd95abf 100644 --- a/src/controllers/v3/utils/uwuify.js +++ b/src/controllers/v3/utils/uwuify.js @@ -13,16 +13,7 @@ const getOwofiyText = async (req, res, next) => { res.status(200).json({ text: uvuify(text), }); - - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { uvuify: 1 } } - ); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); next(error); } }; diff --git a/src/controllers/v3/waifus/randomWaifus.js b/src/controllers/v3/waifus/randomWaifus.js index c6d3554..4de8c05 100644 --- a/src/controllers/v3/waifus/randomWaifus.js +++ b/src/controllers/v3/waifus/randomWaifus.js @@ -10,11 +10,8 @@ const getRandomWaifu = async (req, res, next) => { { $project: { __v: 0 } }, ]); - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { waifus: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); next(error); } }; diff --git a/src/controllers/v4/images/husbando.js b/src/controllers/v4/images/husbando.js index b771035..81454ba 100644 --- a/src/controllers/v4/images/husbando.js +++ b/src/controllers/v4/images/husbando.js @@ -61,16 +61,15 @@ const getHusbando = async (req, res, next) => { return next(createError(404, 'Could not find any matching husbando')); } - res.status(200).json(result); + return res.status(200).json(result); // Update system statistics for husbandos - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { husbandos: 1 } }); } catch (error) { /** * Update system statistics for failed requests. * @type {Object} */ - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); + next(error); } }; diff --git a/src/controllers/v4/images/waifu.js b/src/controllers/v4/images/waifu.js index 785cc49..3d15297 100644 --- a/src/controllers/v4/images/waifu.js +++ b/src/controllers/v4/images/waifu.js @@ -87,16 +87,15 @@ const getWaifu = async (req, res, next) => { * @property {Number} statistics.upvote - Number of upvotes for the waifu. * @property {Number} statistics.downvote - Number of downvotes for the waifu. */ - res.status(200).json(result); + return res.status(200).json(result); // Update system statistics for waifus - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { waifus: 1 } }); } catch (error) { /** * Update system statistics for failed requests * @type {Object} */ - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); + next(error); } }; diff --git a/src/controllers/v4/interactions/angry.js b/src/controllers/v4/interactions/angry.js index 35e8051..83ae590 100644 --- a/src/controllers/v4/interactions/angry.js +++ b/src/controllers/v4/interactions/angry.js @@ -15,13 +15,10 @@ const getRandomAngry = async (req, res, next) => { return next(createError(404, 'Could not find any Angry Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { angry: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; -export default getRandomAngry; \ No newline at end of file +export default getRandomAngry; diff --git a/src/controllers/v4/interactions/baka.js b/src/controllers/v4/interactions/baka.js index 1afa018..08d3a1c 100644 --- a/src/controllers/v4/interactions/baka.js +++ b/src/controllers/v4/interactions/baka.js @@ -15,11 +15,8 @@ const getRandomBaka = async (req, res, next) => { return next(createError(404, 'Could not find any Baka Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { baka: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/bite.js b/src/controllers/v4/interactions/bite.js index 591e677..7cedeef 100644 --- a/src/controllers/v4/interactions/bite.js +++ b/src/controllers/v4/interactions/bite.js @@ -15,11 +15,8 @@ const getRandomBite = async (req, res, next) => { return next(createError(404, 'Could not find any Bite Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bite: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/blush.js b/src/controllers/v4/interactions/blush.js index 2c698c8..ebf9f30 100644 --- a/src/controllers/v4/interactions/blush.js +++ b/src/controllers/v4/interactions/blush.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Blush from '../../../models/schemas/Blush.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,17 +15,10 @@ const getRandomBlush = async (req, res, next) => { return next(createError(404, 'Could not find any Blush Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { blush: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; export default getRandomBlush; - diff --git a/src/controllers/v4/interactions/bonk.js b/src/controllers/v4/interactions/bonk.js index bcf4786..b89b177 100644 --- a/src/controllers/v4/interactions/bonk.js +++ b/src/controllers/v4/interactions/bonk.js @@ -1,4 +1,3 @@ - import createError from 'http-errors'; import Bonk from '../../../models/schemas/Bonk.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -16,17 +15,10 @@ const getRandomBonk = async (req, res, next) => { return next(createError(404, 'Could not find any Bonk Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bonk: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } - }; export default getRandomBonk; diff --git a/src/controllers/v4/interactions/bored.js b/src/controllers/v4/interactions/bored.js index 3879136..bb5380a 100644 --- a/src/controllers/v4/interactions/bored.js +++ b/src/controllers/v4/interactions/bored.js @@ -15,11 +15,8 @@ const getRandomBored = async (req, res, next) => { return next(createError(404, 'Could not find any Bored Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bored: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/bully.js b/src/controllers/v4/interactions/bully.js index d7ea12b..cb138f3 100644 --- a/src/controllers/v4/interactions/bully.js +++ b/src/controllers/v4/interactions/bully.js @@ -15,11 +15,8 @@ const getRandomBully = async (req, res, next) => { return next(createError(404, 'Could not find any Bully Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bully: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/bye.js b/src/controllers/v4/interactions/bye.js index 3a6ff17..054e3a2 100644 --- a/src/controllers/v4/interactions/bye.js +++ b/src/controllers/v4/interactions/bye.js @@ -15,11 +15,8 @@ const getRandomBye = async (req, res, next) => { return next(createError(404, 'Could not find any Bye Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bye: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/chase.js b/src/controllers/v4/interactions/chase.js index f2dfeea..ded43b9 100644 --- a/src/controllers/v4/interactions/chase.js +++ b/src/controllers/v4/interactions/chase.js @@ -15,11 +15,8 @@ const getRandomChase = async (req, res, next) => { return next(createError(404, 'Could not find any Chase Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { chase: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/cheer.js b/src/controllers/v4/interactions/cheer.js index 54c4f55..f5d6852 100644 --- a/src/controllers/v4/interactions/cheer.js +++ b/src/controllers/v4/interactions/cheer.js @@ -15,11 +15,8 @@ const getRandomCheer = async (req, res, next) => { return next(createError(404, 'Could not find any Cheer Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { cheer: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/cringe.js b/src/controllers/v4/interactions/cringe.js index badd781..4f439c1 100644 --- a/src/controllers/v4/interactions/cringe.js +++ b/src/controllers/v4/interactions/cringe.js @@ -1,5 +1,3 @@ - - import createError from 'http-errors'; import Cringe from '../../../models/schemas/Cringe.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -17,14 +15,8 @@ const getRandomCringe = async (req, res, next) => { return next(createError(404, 'Could not find any Cringe Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { cringe: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v4/interactions/cry.js b/src/controllers/v4/interactions/cry.js index 00c4861..596d831 100644 --- a/src/controllers/v4/interactions/cry.js +++ b/src/controllers/v4/interactions/cry.js @@ -15,11 +15,8 @@ const getRandomCry = async (req, res, next) => { return next(createError(404, 'Could not find any Cry Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { cry: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/cuddle.js b/src/controllers/v4/interactions/cuddle.js index 36bdf52..c1ed804 100644 --- a/src/controllers/v4/interactions/cuddle.js +++ b/src/controllers/v4/interactions/cuddle.js @@ -15,11 +15,8 @@ const getRandomCuddle = async (req, res, next) => { return next(createError(404, 'Could not find any Cuddle Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { cuddle: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/dab.js b/src/controllers/v4/interactions/dab.js index 88f3315..1f1c952 100644 --- a/src/controllers/v4/interactions/dab.js +++ b/src/controllers/v4/interactions/dab.js @@ -15,11 +15,8 @@ const getRandomDab = async (req, res, next) => { return next(createError(404, 'Could not find any Dab Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { dab: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/dance.js b/src/controllers/v4/interactions/dance.js index 615a043..17c4b92 100644 --- a/src/controllers/v4/interactions/dance.js +++ b/src/controllers/v4/interactions/dance.js @@ -1,6 +1,3 @@ - - - import createError from 'http-errors'; import Dance from '../../../models/schemas/Dance.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -18,14 +15,8 @@ const getRandomDance = async (req, res, next) => { return next(createError(404, 'Could not find any Dance Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { dance: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v4/interactions/die.js b/src/controllers/v4/interactions/die.js index e194b3e..bb06ff3 100644 --- a/src/controllers/v4/interactions/die.js +++ b/src/controllers/v4/interactions/die.js @@ -1,6 +1,3 @@ - - - import createError from 'http-errors'; import Die from '../../../models/schemas/Die.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -18,14 +15,8 @@ const getRandomDie = async (req, res, next) => { return next(createError(404, 'Could not find any Die Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { die: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v4/interactions/disgust.js b/src/controllers/v4/interactions/disgust.js index 6c83bb1..89b49a7 100644 --- a/src/controllers/v4/interactions/disgust.js +++ b/src/controllers/v4/interactions/disgust.js @@ -1,6 +1,3 @@ - - - import createError from 'http-errors'; import Disgust from '../../../models/schemas/Disgust.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -18,14 +15,8 @@ const getRandomDisgust = async (req, res, next) => { return next(createError(404, 'Could not find any Disgust Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { disgust: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v4/interactions/facepalm.js b/src/controllers/v4/interactions/facepalm.js index 8149def..54809b0 100644 --- a/src/controllers/v4/interactions/facepalm.js +++ b/src/controllers/v4/interactions/facepalm.js @@ -15,11 +15,8 @@ const getRandomFacepalm = async (req, res, next) => { return next(createError(404, 'Could not find any Facepalm Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { facepalm: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/feed.js b/src/controllers/v4/interactions/feed.js index 6145e04..f69f327 100644 --- a/src/controllers/v4/interactions/feed.js +++ b/src/controllers/v4/interactions/feed.js @@ -15,11 +15,8 @@ const getRandomFeed = async (req, res, next) => { return next(createError(404, 'Could not find any Feed Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { feed: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/glomp.js b/src/controllers/v4/interactions/glomp.js index 3f2e32b..346e7ae 100644 --- a/src/controllers/v4/interactions/glomp.js +++ b/src/controllers/v4/interactions/glomp.js @@ -15,11 +15,8 @@ const getRandomGlomp = async (req, res, next) => { return next(createError(404, 'Could not find any Glomp Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { glomp: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/happy.js b/src/controllers/v4/interactions/happy.js index 4379c45..d277b27 100644 --- a/src/controllers/v4/interactions/happy.js +++ b/src/controllers/v4/interactions/happy.js @@ -15,11 +15,8 @@ const getRandomHappy = async (req, res, next) => { return next(createError(404, 'Could not find any Happy Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { happy: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/hi.js b/src/controllers/v4/interactions/hi.js index 08ffb0f..92c2dd1 100644 --- a/src/controllers/v4/interactions/hi.js +++ b/src/controllers/v4/interactions/hi.js @@ -15,11 +15,8 @@ const getRandomHi = async (req, res, next) => { return next(createError(404, 'Could not find any Hi Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { hi: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/highfive.js b/src/controllers/v4/interactions/highfive.js index 2f19fea..1e77ae2 100644 --- a/src/controllers/v4/interactions/highfive.js +++ b/src/controllers/v4/interactions/highfive.js @@ -1,6 +1,3 @@ - - - import createError from 'http-errors'; import Highfive from '../../../models/schemas/Highfive.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -18,14 +15,8 @@ const getRandomHighfive = async (req, res, next) => { return next(createError(404, 'Could not find any Highfive Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { highfive: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v4/interactions/hold.js b/src/controllers/v4/interactions/hold.js index 0dddf0d..2def44b 100644 --- a/src/controllers/v4/interactions/hold.js +++ b/src/controllers/v4/interactions/hold.js @@ -1,6 +1,3 @@ - - - import createError from 'http-errors'; import Hold from '../../../models/schemas/Hold.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -18,14 +15,8 @@ const getRandomHold = async (req, res, next) => { return next(createError(404, 'Could not find any Hold Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { hold: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v4/interactions/hug.js b/src/controllers/v4/interactions/hug.js index f619681..a31c2ca 100644 --- a/src/controllers/v4/interactions/hug.js +++ b/src/controllers/v4/interactions/hug.js @@ -15,11 +15,8 @@ const getRandomHug = async (req, res, next) => { return next(createError(404, 'Could not find any Hug Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { hug: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/kick.js b/src/controllers/v4/interactions/kick.js index c07b92e..5d84359 100644 --- a/src/controllers/v4/interactions/kick.js +++ b/src/controllers/v4/interactions/kick.js @@ -15,11 +15,8 @@ const getRandomKick = async (req, res, next) => { return next(createError(404, 'Could not find any Kick Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { kick: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/kill.js b/src/controllers/v4/interactions/kill.js index 8e40895..cb78bd2 100644 --- a/src/controllers/v4/interactions/kill.js +++ b/src/controllers/v4/interactions/kill.js @@ -15,11 +15,8 @@ const getRandomKill = async (req, res, next) => { return next(createError(404, 'Could not find any Kill Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { kill: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/kiss.js b/src/controllers/v4/interactions/kiss.js index d2e1b2a..1fc17ba 100644 --- a/src/controllers/v4/interactions/kiss.js +++ b/src/controllers/v4/interactions/kiss.js @@ -15,11 +15,8 @@ const getRandomKiss = async (req, res, next) => { return next(createError(404, 'Could not find any Kiss Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { kissu: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/laugh.js b/src/controllers/v4/interactions/laugh.js index f34b771..9405927 100644 --- a/src/controllers/v4/interactions/laugh.js +++ b/src/controllers/v4/interactions/laugh.js @@ -15,11 +15,8 @@ const getRandomLaugh = async (req, res, next) => { return next(createError(404, 'Could not find any Laugh Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { laugh: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/lick.js b/src/controllers/v4/interactions/lick.js index 9c50d21..9b0b7ff 100644 --- a/src/controllers/v4/interactions/lick.js +++ b/src/controllers/v4/interactions/lick.js @@ -15,11 +15,8 @@ const getRandomLick = async (req, res, next) => { return next(createError(404, 'Could not find any Lick Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { lick: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/love.js b/src/controllers/v4/interactions/love.js index bf22105..94df8c6 100644 --- a/src/controllers/v4/interactions/love.js +++ b/src/controllers/v4/interactions/love.js @@ -15,11 +15,8 @@ const getRandomLove = async (req, res, next) => { return next(createError(404, 'Could not find any Love Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { love: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/lurk.js b/src/controllers/v4/interactions/lurk.js index b0ae8b5..abaa2e0 100644 --- a/src/controllers/v4/interactions/lurk.js +++ b/src/controllers/v4/interactions/lurk.js @@ -15,11 +15,8 @@ const getRandomLurk = async (req, res, next) => { return next(createError(404, 'Could not find any Lurk Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { lurk: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/midfing.js b/src/controllers/v4/interactions/midfing.js index 2f1afa0..93c7fb9 100644 --- a/src/controllers/v4/interactions/midfing.js +++ b/src/controllers/v4/interactions/midfing.js @@ -15,11 +15,8 @@ const getRandomMidfing = async (req, res, next) => { return next(createError(404, 'Could not find any Midfing Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { midfing: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/nervous.js b/src/controllers/v4/interactions/nervous.js index c3f7b6e..3ed327f 100644 --- a/src/controllers/v4/interactions/nervous.js +++ b/src/controllers/v4/interactions/nervous.js @@ -15,11 +15,8 @@ const getRandomNervous = async (req, res, next) => { return next(createError(404, 'Could not find any Nervous Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { nervous: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/nom.js b/src/controllers/v4/interactions/nom.js index 47f04e2..1e2e8c8 100644 --- a/src/controllers/v4/interactions/nom.js +++ b/src/controllers/v4/interactions/nom.js @@ -15,11 +15,8 @@ const getRandomNom = async (req, res, next) => { return next(createError(404, 'Could not find any Nom Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { nom: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/nope.js b/src/controllers/v4/interactions/nope.js index 1079cfa..5780e5d 100644 --- a/src/controllers/v4/interactions/nope.js +++ b/src/controllers/v4/interactions/nope.js @@ -15,11 +15,8 @@ const getRandomNope = async (req, res, next) => { return next(createError(404, 'Could not find any Nope Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { nope: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/nuzzle.js b/src/controllers/v4/interactions/nuzzle.js index 85b583f..35958e7 100644 --- a/src/controllers/v4/interactions/nuzzle.js +++ b/src/controllers/v4/interactions/nuzzle.js @@ -1,6 +1,3 @@ - - - import createError from 'http-errors'; import Nuzzle from '../../../models/schemas/Nuzzle.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -18,14 +15,8 @@ const getRandomNuzzle = async (req, res, next) => { return next(createError(404, 'Could not find any Nuzzle Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { nuzzle: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v4/interactions/panic.js b/src/controllers/v4/interactions/panic.js index dc9a13e..616b5cc 100644 --- a/src/controllers/v4/interactions/panic.js +++ b/src/controllers/v4/interactions/panic.js @@ -15,11 +15,8 @@ const getRandomPanic = async (req, res, next) => { return next(createError(404, 'Could not find any Panic Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { panic: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/pat.js b/src/controllers/v4/interactions/pat.js index 3295744..2de5c60 100644 --- a/src/controllers/v4/interactions/pat.js +++ b/src/controllers/v4/interactions/pat.js @@ -15,11 +15,8 @@ const getRandomPat = async (req, res, next) => { return next(createError(404, 'Could not find any Pat Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { pat: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/peck.js b/src/controllers/v4/interactions/peck.js index 133b904..c0ad060 100644 --- a/src/controllers/v4/interactions/peck.js +++ b/src/controllers/v4/interactions/peck.js @@ -15,11 +15,8 @@ const getRandomPeck = async (req, res, next) => { return next(createError(404, 'Could not find any Peck Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { peck: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/poke.js b/src/controllers/v4/interactions/poke.js index 94e4c9d..d4cba75 100644 --- a/src/controllers/v4/interactions/poke.js +++ b/src/controllers/v4/interactions/poke.js @@ -15,11 +15,8 @@ const getRandomPoke = async (req, res, next) => { return next(createError(404, 'Could not find any Poke Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { poke: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/pout.js b/src/controllers/v4/interactions/pout.js index 995232b..2317601 100644 --- a/src/controllers/v4/interactions/pout.js +++ b/src/controllers/v4/interactions/pout.js @@ -15,11 +15,8 @@ const getRandomPout = async (req, res, next) => { return next(createError(404, 'Could not find any Pout Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { pout: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/punch.js b/src/controllers/v4/interactions/punch.js index f9f1d93..9117e38 100644 --- a/src/controllers/v4/interactions/punch.js +++ b/src/controllers/v4/interactions/punch.js @@ -15,11 +15,8 @@ const getRandomPunch = async (req, res, next) => { return next(createError(404, 'Could not find any Punch Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { punch: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/run.js b/src/controllers/v4/interactions/run.js index 3005f7a..065939a 100644 --- a/src/controllers/v4/interactions/run.js +++ b/src/controllers/v4/interactions/run.js @@ -15,11 +15,8 @@ const getRandomRun = async (req, res, next) => { return next(createError(404, 'Could not find any Run Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { run: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/sad.js b/src/controllers/v4/interactions/sad.js index 9b2c660..d32c954 100644 --- a/src/controllers/v4/interactions/sad.js +++ b/src/controllers/v4/interactions/sad.js @@ -15,11 +15,8 @@ const getRandomSad = async (req, res, next) => { return next(createError(404, 'Could not find any Sad Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { sad: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/shoot.js b/src/controllers/v4/interactions/shoot.js index de561ba..c753923 100644 --- a/src/controllers/v4/interactions/shoot.js +++ b/src/controllers/v4/interactions/shoot.js @@ -15,11 +15,8 @@ const getRandomShoot = async (req, res, next) => { return next(createError(404, 'Could not find any Shoot Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { shoot: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/shrug.js b/src/controllers/v4/interactions/shrug.js index 10dcf62..274e3d4 100644 --- a/src/controllers/v4/interactions/shrug.js +++ b/src/controllers/v4/interactions/shrug.js @@ -1,6 +1,3 @@ - - - import createError from 'http-errors'; import Shrug from '../../../models/schemas/Shrug.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -18,14 +15,8 @@ const getRandomShrug = async (req, res, next) => { return next(createError(404, 'Could not find any Shrug Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { shrug: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v4/interactions/sip.js b/src/controllers/v4/interactions/sip.js index 9f41a60..81fac61 100644 --- a/src/controllers/v4/interactions/sip.js +++ b/src/controllers/v4/interactions/sip.js @@ -15,11 +15,8 @@ const getRandomSip = async (req, res, next) => { return next(createError(404, 'Could not find any Sip Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { sip: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/slap.js b/src/controllers/v4/interactions/slap.js index b9a51a2..faa6383 100644 --- a/src/controllers/v4/interactions/slap.js +++ b/src/controllers/v4/interactions/slap.js @@ -15,11 +15,8 @@ const getRandomSlap = async (req, res, next) => { return next(createError(404, 'Could not find any Slap Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { slap: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/sleepy.js b/src/controllers/v4/interactions/sleepy.js index b321317..ad98366 100644 --- a/src/controllers/v4/interactions/sleepy.js +++ b/src/controllers/v4/interactions/sleepy.js @@ -15,11 +15,8 @@ const getRandomSleepy = async (req, res, next) => { return next(createError(404, 'Could not find any Sleepy Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { sleepy: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/smile.js b/src/controllers/v4/interactions/smile.js index 885057a..4ee72b7 100644 --- a/src/controllers/v4/interactions/smile.js +++ b/src/controllers/v4/interactions/smile.js @@ -15,11 +15,8 @@ const getRandomSmile = async (req, res, next) => { return next(createError(404, 'Could not find any Smile Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { smile: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/smug.js b/src/controllers/v4/interactions/smug.js index 0f6a1f8..97d2b0a 100644 --- a/src/controllers/v4/interactions/smug.js +++ b/src/controllers/v4/interactions/smug.js @@ -15,11 +15,8 @@ const getRandomSmug = async (req, res, next) => { return next(createError(404, 'Could not find any Smug Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { smug: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/stab.js b/src/controllers/v4/interactions/stab.js index 742b2a1..07d1da4 100644 --- a/src/controllers/v4/interactions/stab.js +++ b/src/controllers/v4/interactions/stab.js @@ -15,11 +15,8 @@ const getRandomStab = async (req, res, next) => { return next(createError(404, 'Could not find any Stab Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { stab: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/stare.js b/src/controllers/v4/interactions/stare.js index 6cd83f1..41de723 100644 --- a/src/controllers/v4/interactions/stare.js +++ b/src/controllers/v4/interactions/stare.js @@ -15,11 +15,8 @@ const getRandomStare = async (req, res, next) => { return next(createError(404, 'Could not find any Stare Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { stare: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/suicide.js b/src/controllers/v4/interactions/suicide.js index 5ba6d1f..ad1c88b 100644 --- a/src/controllers/v4/interactions/suicide.js +++ b/src/controllers/v4/interactions/suicide.js @@ -1,7 +1,3 @@ - - - - import createError from 'http-errors'; import Suicide from '../../../models/schemas/Suicide.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -19,14 +15,8 @@ const getRandomSuicide = async (req, res, next) => { return next(createError(404, 'Could not find any Suicide Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { suicide: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v4/interactions/tease.js b/src/controllers/v4/interactions/tease.js index 8939709..17f389c 100644 --- a/src/controllers/v4/interactions/tease.js +++ b/src/controllers/v4/interactions/tease.js @@ -15,11 +15,8 @@ const getRandomTease = async (req, res, next) => { return next(createError(404, 'Could not find any Tease Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { tease: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/think.js b/src/controllers/v4/interactions/think.js index 5af4973..99140a0 100644 --- a/src/controllers/v4/interactions/think.js +++ b/src/controllers/v4/interactions/think.js @@ -15,11 +15,8 @@ const getRandomThink = async (req, res, next) => { return next(createError(404, 'Could not find any Think Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { think: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/thumbsup.js b/src/controllers/v4/interactions/thumbsup.js index 9131cf8..b00c8f4 100644 --- a/src/controllers/v4/interactions/thumbsup.js +++ b/src/controllers/v4/interactions/thumbsup.js @@ -15,11 +15,8 @@ const getRandomThumbsup = async (req, res, next) => { return next(createError(404, 'Could not find any Thumbsup Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { thumbsup: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/tickle.js b/src/controllers/v4/interactions/tickle.js index b7fee1e..ed373ff 100644 --- a/src/controllers/v4/interactions/tickle.js +++ b/src/controllers/v4/interactions/tickle.js @@ -15,11 +15,8 @@ const getRandomTickle = async (req, res, next) => { return next(createError(404, 'Could not find any Tickle Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { tickle: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/triggered.js b/src/controllers/v4/interactions/triggered.js index c287eee..b9da9f7 100644 --- a/src/controllers/v4/interactions/triggered.js +++ b/src/controllers/v4/interactions/triggered.js @@ -1,6 +1,3 @@ - - - import createError from 'http-errors'; import Triggered from '../../../models/schemas/Triggered.js'; import Stats from '../../../models/schemas/Stat.js'; @@ -18,14 +15,8 @@ const getRandomTriggered = async (req, res, next) => { return next(createError(404, 'Could not find any Triggered Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { triggered: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate( - { _id: 'systemstats' }, - { $inc: { failed_requests: 1 } } - ); return next(error); } }; diff --git a/src/controllers/v4/interactions/wag.js b/src/controllers/v4/interactions/wag.js index 6e32232..e152ca1 100644 --- a/src/controllers/v4/interactions/wag.js +++ b/src/controllers/v4/interactions/wag.js @@ -15,11 +15,8 @@ const getRandomWag = async (req, res, next) => { return next(createError(404, 'Could not find any Wag Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { wag: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/wave.js b/src/controllers/v4/interactions/wave.js index fb8bd0f..57a55e7 100644 --- a/src/controllers/v4/interactions/wave.js +++ b/src/controllers/v4/interactions/wave.js @@ -15,11 +15,8 @@ const getRandomWave = async (req, res, next) => { return next(createError(404, 'Could not find any Wave Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { wave: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/wink.js b/src/controllers/v4/interactions/wink.js index a33087e..7fc1ebd 100644 --- a/src/controllers/v4/interactions/wink.js +++ b/src/controllers/v4/interactions/wink.js @@ -15,11 +15,8 @@ const getRandomWink = async (req, res, next) => { return next(createError(404, 'Could not find any Wink Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { wink: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/interactions/yes.js b/src/controllers/v4/interactions/yes.js index 82e2047..02f26ab 100644 --- a/src/controllers/v4/interactions/yes.js +++ b/src/controllers/v4/interactions/yes.js @@ -15,11 +15,8 @@ const getRandomYes = async (req, res, next) => { return next(createError(404, 'Could not find any Yes Gif')); } - res.status(200).json(result); - - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { yes: 1 } }); + return res.status(200).json(result); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; diff --git a/src/controllers/v4/internal/membership.js b/src/controllers/v4/internal/membership.js index 8478321..33cb9d5 100644 --- a/src/controllers/v4/internal/membership.js +++ b/src/controllers/v4/internal/membership.js @@ -47,7 +47,7 @@ const getMembership = async (req, res, next) => { return next(createError(404, 'No membership data found')); } - res.status(200).json(result); + return res.status(200).json(result); } catch (error) { return next(error); } diff --git a/src/controllers/v4/internal/notification.js b/src/controllers/v4/internal/notification.js new file mode 100644 index 0000000..9fd3678 --- /dev/null +++ b/src/controllers/v4/internal/notification.js @@ -0,0 +1,137 @@ +import Notification from '../../../models/schemas/Notification.js'; +import UserNotification from '../../../models/schemas/UserNotification.js'; +import NotificationStatus from '../../../models/schemas/NotificationStatus.js'; + +/** + * Fetches all notifications (user-specific and global), sorted by latest date. + * + * @param {Object} req - Express request object. + * @param {Object} res - Express response object. + */ +const retrieveNotifications = async (req, res) => { + try { + const userId = req.headers.uid; // Assuming user ID is available in req.headers + + // Fetch user-specific notifications (excluding deleted ones) + const userNotifications = await UserNotification.find({ userId, deleted: false }).lean(); + + // Fetch global notifications + const globalNotifications = await Notification.find().lean(); + + // Fetch the user's read & deleted notifications status + const notificationStatus = await NotificationStatus.findOne({ _id: userId }).lean(); + const readNotifications = notificationStatus?.read || []; + const deletedNotifications = notificationStatus?.deleted || []; // Ensure correct field spelling + + // Filter out globally deleted notifications + const filteredGlobalNotifications = globalNotifications + .filter(notif => !deletedNotifications.includes(notif._id)) + .map(notif => ({ + ...notif, + read: readNotifications.includes(notif._id), // True if read, false otherwise + })); + + // Combine both types of notifications + const allNotifications = [...userNotifications, ...filteredGlobalNotifications]; + + // Sort notifications by timestamp (latest first) + allNotifications.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp)); + + return res.status(200).json({ notifications: allNotifications }); + } catch (error) { + console.error('Error fetching notifications:', error); + return res.status(500).json({ message: 'Internal Server Error' }); + } +}; + +/** + * Marks a notification as read for the user. + * + * @param {Object} req - Express request object. + * @param {Object} res - Express response object. + */ +const markNotificationAsRead = async (req, res) => { + try { + const { id } = req.body; // isGlobal: true for global notifications + const userId = req.headers.uid; + + if (!id) { + return res.status(400).json({ message: 'Notification ID is required' }); + } + + const isGlobal = id.startsWith('G'); + + if (isGlobal) { + if (!userId) { + return res.status(400).json({ message: 'User ID is required' }); + } + // Check if the global notification exists + const notificationExists = await Notification.exists({ _id: { $eq: id } }); + if (!notificationExists) { + return res.status(404).json({ message: 'Global notification not found' }); + } + + // Update read status for global notifications in NotificationStatus + await NotificationStatus.findOneAndUpdate( + { _id: userId }, + { $addToSet: { read: id } }, // Add without duplicates + { upsert: true, new: true }, + ); + } else { + // Update read status for user-specific notifications + await UserNotification.findOneAndUpdate({ _id: { $eq: id } }, { read: true }); + } + + return res.status(200).json({ message: 'Notification marked as read' }); + } catch (error) { + console.error('Error marking notification as read:', error); + return res.status(500).json({ message: 'Internal Server Error' }); + } +}; + +/** + * Marks a notification as deleted for the user. + * + * @param {Object} req - Express request object. + * @param {Object} res - Express response object. + */ +const markNotificationAsDeleted = async (req, res) => { + try { + const { id } = req.body; // isGlobal: true for global notifications + const userId = req.headers.uid; + + if (!id) { + return res.status(400).json({ message: 'Notification ID is required' }); + } + + const isGlobal = id.startsWith('G'); + + if (isGlobal) { + if (!userId) { + return res.status(400).json({ message: 'User ID is required' }); + } + // Check if the global notification exists + const notificationExists = await Notification.exists({ _id: { $eq: id } }); + if (!notificationExists) { + return res.status(404).json({ message: 'Global notification not found' }); + } + + // Update deleted status for global notifications in NotificationStatus + await NotificationStatus.findOneAndUpdate( + { _id: userId }, + { $addToSet: { deleted: id } }, // Add without duplicates + { upsert: true, new: true }, + ); + } else { + // Update deleted status for user-specific notifications + await UserNotification.findOneAndUpdate({ _id: { $eq: id } }, { deleted: true }); + } + + return res.status(200).json({ message: 'Notification marked as deleted' }); + } catch (error) { + console.error('Error marking notification as deleted:', error); + return res.status(500).json({ message: 'Internal Server Error' }); + } +}; + +export { retrieveNotifications, markNotificationAsRead, markNotificationAsDeleted }; diff --git a/src/controllers/v4/internal/pages.js b/src/controllers/v4/internal/pages.js new file mode 100644 index 0000000..6a93af6 --- /dev/null +++ b/src/controllers/v4/internal/pages.js @@ -0,0 +1,248 @@ +import System from '../../../models/schemas/System.js'; + +/** + * Fetches the status of a specific page (availability & maintenance status). + */ +export const getPageStatus = async (req, res) => { + try { + const { id } = req.params; + const key = req.headers.key; + + // Check for valid access key in headers + if (!key || key !== process.env.ACCESS_KEY) { + return res.status(401).json({ + message: 'Unauthorized', + }); + } + + const system = await System.findOne({ 'pages._id': id }, { 'pages.$': 1 }); + + if (!system || !system.pages.length) { + return res.status(404).json({ message: 'Page not found' }); + } + + const page = system.pages[0]; + + return res.status(200).json({ + _id: page._id, + available: page.available, + maintenance: page.maintenance, + }); + } catch (error) { + console.error('Error fetching page status:', error); + return res.status(500).json({ message: 'Internal Server Error' }); + } +}; + +/** + * Retrieves metadata, including permissions and maintenance info. + */ +export const getPageMeta = async (req, res) => { + try { + const { id } = req.params; + const key = req.headers.key; + + // Check for valid access key in headers + if (!key || key !== process.env.ACCESS_KEY) { + return res.status(401).json({ + message: 'Unauthorized', + }); + } + + const system = await System.findOne({ 'pages._id': id }, { 'pages.$': 1 }); + + if (!system || !system.pages.length) { + return res.status(404).json({ message: 'Page not found' }); + } + + const page = system.pages[0]; + + return res.status(200).json({ + _id: page._id, + type: page.type, + maintenance: page.maintenance, + permission: page.permission, + }); + } catch (error) { + console.error('Error fetching page metadata:', error); + return res.status(500).json({ message: 'Internal Server Error' }); + } +}; + +/** + * Checks if the user has permission to access the page. + */ +export const checkPageAccess = async (req, res) => { + try { + const { id } = req.params; + const { role } = req.query; // User role should be passed as a query param + const key = req.headers.key; + + // Check for valid access key in headers + if (!key || key !== process.env.ACCESS_KEY) { + return res.status(401).json({ + message: 'Unauthorized', + }); + } + + const system = await System.findOne({ 'pages._id': id }, { 'pages.$': 1 }); + + if (!system || !system.pages.length) { + return res.status(404).json({ message: 'Page not found' }); + } + + const page = system.pages[0]; + + if (!role) { + return res.status(200).json({ _id: page._id, permissions: page.permission }); + } + + const hasAccess = page.permission.roles.includes('guest') || page.permission.roles.includes(role); + + return res.status(200).json({ _id: page._id, access: hasAccess }); + } catch (error) { + console.error('Error checking page access:', error); + return res.status(500).json({ message: 'Internal Server Error' }); + } +}; + +/** + * Returns general page details, including type, status, and permissions. + */ +export const getPageInfo = async (req, res) => { + try { + const { id } = req.params; + const key = req.headers.key; + + // Check for valid access key in headers + if (!key || key !== process.env.ACCESS_KEY) { + return res.status(401).json({ + message: 'Unauthorized', + }); + } + + const system = await System.findOne({ 'pages._id': id }, { 'pages.$': 1 }); + + if (!system || !system.pages.length) { + return res.status(404).json({ message: 'Page not found' }); + } + + const page = system.pages[0]; + + return res.status(200).json(page); + } catch (error) { + console.error('Error fetching page info:', error); + return res.status(500).json({ message: 'Internal Server Error' }); + } +}; + +/** + * Updates page details (availability, type, maintenance, or permissions). + */ +export const updatePage = async (req, res) => { + try { + const { id } = req.params; + const { available, type, maintenance, permission } = req.body; + const key = req.headers.key; + + // Check for valid access key in headers + if (!key || key !== process.env.ACCESS_KEY) { + return res.status(401).json({ message: 'Unauthorized' }); + } + + // Validate input + if (type && !['production', 'alpha', 'beta'].includes(type)) { + return res.status(400).json({ message: 'Invalid page type' }); + } + + const updateFields = {}; + if (available !== undefined) updateFields['pages.$.available'] = available; + if (type) updateFields['pages.$.type'] = type; + + // Merge `maintenance` fields instead of replacing + if (maintenance) { + Object.keys(maintenance).forEach(key => { + updateFields[`pages.$.maintenance.${key}`] = maintenance[key]; + }); + } + + // Merge `permission` fields instead of replacing + if (permission) { + Object.keys(permission).forEach(key => { + updateFields[`pages.$.permission.${key}`] = permission[key]; + }); + } + + if (Object.keys(updateFields).length === 0) { + return res.status(400).json({ message: 'No valid fields to update' }); + } + + // Update the document without projection to avoid MongoDB error + await System.findOneAndUpdate({ 'pages._id': id }, { $set: updateFields }); + + // Fetch the updated page separately + const updatedSystem = await System.findOne({ 'pages._id': id }, { pages: { $elemMatch: { _id: id } } }); + + if (!updatedSystem || !updatedSystem.pages.length) { + return res.status(404).json({ message: 'Page not found' }); + } + + return res.status(200).json({ + message: 'Page updated successfully', + page: updatedSystem.pages[0], + }); + } catch (error) { + console.error('Error updating page:', error); + return res.status(500).json({ message: 'Internal Server Error' }); + } +}; + +/** + * Middleware to add page details + */ +export const addPage = async (req, res, next) => { + try { + const { name, available, type, maintenance, permission } = req.body; + const key = req.headers.key; + + // Check for valid access key in headers + if (!key || key !== process.env.ACCESS_KEY) { + return res.status(401).json({ message: 'Unauthorized' }); + } + + // Validate input + if (!name) { + return res.status(400).json({ message: 'Page name is required' }); + } + + if (type && !['production', 'alpha', 'beta'].includes(type)) { + return res.status(400).json({ message: 'Invalid page type' }); + } + + const newPage = { + _id: name, + available: available ?? true, // Default to true if not provided + type: type || 'production', + maintenance: maintenance || {}, + permission: permission || {}, + }; + + const updatedSystem = await System.findOneAndUpdate( + {}, + { $push: { pages: newPage } }, + { new: true, projection: { pages: 1 } }, + ); + + if (!updatedSystem) { + return res.status(500).json({ message: 'Failed to add page' }); + } + + return res.status(201).json({ + message: 'Page added successfully', + page: newPage, + }); + } catch (error) { + console.error('Error adding page:', error); + return res.status(500).json({ message: 'Internal Server Error' }); + } +}; diff --git a/src/controllers/v4/internal/rewards.js b/src/controllers/v4/internal/rewards.js new file mode 100644 index 0000000..808fd6c --- /dev/null +++ b/src/controllers/v4/internal/rewards.js @@ -0,0 +1,182 @@ +import System from '../../../models/schemas/System.js'; +import User from '../../../models/schemas/User.js'; + +/** + * Fetches rewards/rewards from the system. + * Supports: + * - All rewards + * - Filtering by type (coupon, giftcard, voucher) + * - Fetching a specific reward by its ID/code + */ +export const getRewards = async (req, res) => { + try { + const key = req.headers.key; + const { id } = req.params; + const { type } = req.query; + + // Authorization check + if (!key || key !== process.env.ACCESS_KEY) { + return res.status(401).json({ message: 'Unauthorized' }); + } + + // Fetch only the rewards field + const system = await System.findById('system').select('rewards'); + + if (!system || !Array.isArray(system.rewards)) { + return res.status(404).json({ message: 'No rewards found' }); + } + + // If an ID is provided, return that specific reward + if (id) { + const reward = system.rewards.find(r => r._id === id.toUpperCase()); + if (!reward) { + return res.status(404).json({ message: 'Reward not found' }); + } + return res.status(200).json(reward); + } + + // Otherwise, return all (optionally filtered by type) + let rewards = system.rewards.filter(r => r.isActive); + if (type) { + rewards = rewards.filter(r => r.type === type); + } + + return res.status(200).json({ + count: rewards.length, + rewards, + }); + } catch (error) { + console.error('Error fetching rewards:', error); + return res.status(500).json({ message: 'Internal Server Error' }); + } +}; + +/** + * Creates a new reward (giftcard, coupon, voucher) and adds it to the system. + */ +export const createReward = async (req, res) => { + try { + const key = req.headers.key; + + // Authorization check + if (!key || key !== process.env.ACCESS_KEY) { + return res.status(401).json({ message: 'Unauthorized' }); + } + + const { id, type, description, discount, appliesTo, usage, validFrom, validUntil, isActive = true } = req.body; + + if (!id || !type || !discount || !discount.type || discount.value === undefined) { + return res.status(400).json({ message: 'Missing required reward fields' }); + } + + const system = await System.findById('system'); + if (!system) { + return res.status(404).json({ message: 'System not found' }); + } + + // Check for existing reward with same ID + const exists = system.rewards.find(r => r._id === id); + if (exists) { + return res.status(409).json({ message: 'Reward with this ID already exists' }); + } + + const newReward = { + _id: id, + type, + description, + discount, + appliesTo: appliesTo || {}, + usage: usage || {}, + validFrom, + validUntil, + isActive, + }; + + system.rewards.push(newReward); + await system.save(); + + return res.status(201).json({ + message: 'Reward created successfully', + reward: newReward, + }); + } catch (error) { + console.error('Error creating reward:', error); + return res.status(500).json({ message: 'Internal Server Error' }); + } +}; + +/** + * Redeems a reward for a user if not already claimed. + */ +export const redeemReward = async (req, res) => { + try { + const key = req.headers.key; + + // Authorization check + if (!key || key !== process.env.ACCESS_KEY) { + return res.status(401).json({ message: 'Unauthorized' }); + } + + const { code } = req.body; + const userId = req.headers.id; // Adjust based on how you auth + + if (!code || !userId) { + return res.status(400).json({ message: 'Missing code or user ID' }); + } + + // Find the system document (assuming you have only one system doc) + const system = await System.findById('system').select('rewards'); + if (!system) { + return res.status(500).json({ message: 'System configuration not found for rewards' }); + } + + // Find the reward by _id (code) + const reward = system.rewards.find(r => r._id.toLowerCase() === code.toLowerCase() && r.isActive); + + if (!reward) { + return res.status(404).json({ message: 'Invalid or inactive reward code' }); + } + + // Check valid date range if set + const now = new Date(); + if (reward.validFrom && now < reward.validFrom) { + return res.status(400).json({ message: 'Reward is not yet valid' }); + } + if (reward.validUntil && now > reward.validUntil) { + return res.status(400).json({ message: 'Reward has expired' }); + } + + // Get user and check status_history + const user = await User.findById(userId); + if (!user) { + return res.status(404).json({ message: 'User not found' }); + } + + const alreadyClaimed = user.status_history?.some(status => status.value === code.toUpperCase()); + if (alreadyClaimed) { + return res.status(409).json({ message: 'Reward already claimed' }); + } + + // Add reward to user's history + user.status_history.push({ + _id: user.status_history.length + 1, + value: code.toUpperCase(), + timestamp: new Date(), + reason: 'Self Reward redeemed', + }); + + await user.save(); + + return res.status(200).json({ + message: 'Reward redeemed successfully', + reward: { + name: reward.name, + type: reward.type, + value: reward.value, + }, + }); + } catch (err) { + console.error('Error redeeming reward:', err); + return res.status(500).json({ message: 'Internal Server Error' }); + } +}; diff --git a/src/controllers/v4/internal/stats.js b/src/controllers/v4/internal/stats.js index 0397357..410ee24 100644 --- a/src/controllers/v4/internal/stats.js +++ b/src/controllers/v4/internal/stats.js @@ -1,33 +1,190 @@ import createError from 'http-errors'; -import Stats from '../../../models/schemas/Stat.js'; - -// Get Internal Status or statistics -const getStats = async (req, res, next) => { - const key = req.headers.key; - // Check for valid access key in headers - if (!key || key !== process.env.ACCESS_KEY) { - return res.status(401).json({ - message: 'Unauthorized', - }); +import Stat from '../../../models/schemas/Stat.js'; +import User from '../../../models/schemas/User.js'; + +// Helper function to parse the "top" query param (e.g., ?top=1, ?top=2-6) +const parseTopParam = top => { + if (!top) return [0, 10]; // Default: Top 10 + const range = top.split('-').map(n => parseInt(n, 10)); + return range.length === 2 ? range : [0, range[0]]; +}; + +// Get Most Popular Endpoints (Overall) +const getPopularEndpoints = async (req, res, next) => { + try { + const { top } = req.query; + const [start, end] = parseTopParam(top); + const key = req.headers.key; + + // Check for valid access key in headers + if (!key || key !== process.env.ACCESS_KEY) { + return res.status(401).json({ message: 'Unauthorized' }); + } + + // ✅ Use .lean() to get a plain object + const stats = await Stat.findOne({ _id: 'system' }, { endpoints: 1 }).lean(); + + if (!stats || !stats.endpoints || Object.keys(stats.endpoints).length === 0) { + return next(createError(404, 'No endpoint data found.')); + } + + const sortedEndpoints = Object.entries(stats.endpoints) + .sort((a, b) => b[1] - a[1]) // Sort by request count + .slice(start, end); // Apply range filter + + res.status(200).json(Object.fromEntries(sortedEndpoints)); + } catch (error) { + return next(error); + } +}; + +// Get Top Endpoints for Today +const getTopEndpointsToday = async (req, res, next) => { + try { + const today = new Date().toISOString().split('T')[0]; // YYYY-MM-DD format + const { top } = req.query; + const [start, end] = parseTopParam(top); + const key = req.headers.key; + + // Check for valid access key in headers + if (!key || key !== process.env.ACCESS_KEY) { + return res.status(401).json({ message: 'Unauthorized' }); + } + + // ✅ Use .lean() to get a plain object + const stats = await Stat.findOne({ _id: 'system' }, { daily: 1 }).lean(); + + // ✅ Correct way to check if today's data exists + if (!stats || !stats.daily || !stats.daily[today]) { + return next(createError(404, 'No endpoint data for today.')); + } + + const dailyData = stats.daily[today]; // ✅ Correct access + + if (!dailyData.endpoints || Object.keys(dailyData.endpoints).length === 0) { + return next(createError(404, 'No endpoint data for today.')); + } + + // ✅ Sort and filter the endpoints + const sortedEndpoints = Object.entries(dailyData.endpoints) + .sort((a, b) => b[1] - a[1]) + .slice(start, end); // Apply range filter + + res.status(200).json(Object.fromEntries(sortedEndpoints)); + } catch (error) { + return next(error); } +}; + +// Get Monthly Request Data (Last 5 Months) +const getMonthlyRequests = async (req, res, next) => { try { - const [result] = await Stats.aggregate([ - // Select a random document from the results - { $sample: { size: 1 } }, - { $project: { __v: 0, _id: 0 } }, - ]); + const now = new Date(); + const last5Months = []; + const key = req.headers.key; + + // Check for valid access key in headers + if (!key || key !== process.env.ACCESS_KEY) { + return res.status(401).json({ message: 'Unauthorized' }); + } - if (!result) { - return next(createError(404, 'Could not find any Stats')); + for (let i = 4; i >= 0; i--) { + // Reverse order to ensure oldest month is at the top + const date = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth() - i, 1)); + const monthKey = date.toISOString().slice(0, 7); // YYYY-MM format + const monthName = date.toLocaleString('en-US', { month: 'short' }).toLowerCase(); // "mar" + last5Months.push({ monthKey, monthName, year: date.getUTCFullYear() }); } - res.status(200).json(result); + const stats = await Stat.findOne({ _id: 'system' }, { daily: 1 }).lean(); + + if (!stats || !stats.daily || Object.keys(stats.daily).length === 0) { + console.log('❌ No daily data found.'); + return next(createError(404, 'No daily data found.')); + } + + const monthlyData = last5Months.reduce((acc, { monthKey, monthName, year }) => { + const datesInMonth = Object.keys(stats.daily).filter(date => date.startsWith(monthKey)); + const usage = datesInMonth.reduce((sum, date) => { + const dailyRequests = stats.daily[date]?.total_requests || 0; + return sum + dailyRequests; + }, 0); + + acc[monthName] = { usage, year }; + return acc; + }, {}); + + res.status(200).json(monthlyData); + } catch (error) { + console.error('🔥 Error in getMonthlyRequests:', error); + return next(error); + } +}; + +// Middleware to Get Users by Type (Quota) +const getUsersStat = async (req, res, next) => { + try { + const { type, top } = req.query; + const [start, end] = parseTopParam(top); + const key = req.headers.key; + + if (!key || key !== process.env.ACCESS_KEY) { + return res.status(401).json({ message: 'Unauthorized' }); + } + + // ✅ Validate Type + if (!['quota', 'requests'].includes(type)) { + return next(createError(400, 'Invalid type.')); + } + + let users; + + switch (type) { + case 'quota': + users = await User.aggregate([ + { + $project: { + username: { $ifNull: ['$username', 'Anonymous'] }, + req_quota: 1, + }, + }, + { $sort: { req_quota: -1 } }, + { $skip: start }, + { $limit: end - start }, + ]); + + break; + + case 'requests': + // ✅ Fetch all users with request statistics + users = await User.find({}, { username: 1, 'statistics.requests': 1 }).lean(); + + // ✅ Process and sum up request counts + users = users.map(user => { + const totalRequests = Object.values(user.statistics?.requests || {}).reduce((sum, count) => sum + count, 0); + return { + _id: user._id, + username: user.username || 'Anonymous', + count: totalRequests, + }; + }); + + // ✅ Sort users by total requests (descending) + users.sort((a, b) => b.count - a.count); + + // ✅ Apply pagination + users = users.slice(start, end); + break; + } + + if (!users.length) { + return next(createError(404, `No users found with ${type === 'quota' ? 'request quotas' : 'requests'}.`)); + } - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { stats: 1 } }); + res.status(200).json(users); } catch (error) { - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); return next(error); } }; -export { getStats }; +export { getPopularEndpoints, getTopEndpointsToday, getMonthlyRequests, getUsersStat }; diff --git a/src/controllers/v4/internal/user.js b/src/controllers/v4/internal/user.js index 8ae45f4..fbed70a 100644 --- a/src/controllers/v4/internal/user.js +++ b/src/controllers/v4/internal/user.js @@ -1,5 +1,6 @@ import crypto from 'crypto'; import Users from '../../../models/schemas/User.js'; +import UserNotification from '../../../models/schemas/UserNotification.js'; import generateToken from '../../../modules/generateToken.js'; /** @@ -46,7 +47,11 @@ const processUserAction = async (req, res, next) => { } const userId = req.params.id; - const { action, amount, reason, executor, expiry } = req.body; // Extract fields from the request body + const { action, amount, reason, value, executor, expiry, old_token, new_token, isForced } = req.body; // Extract fields from the request body + + if (!action) { + return res.status(400).json({ message: 'Action is required in body' }); // Action is required + } try { // Fetch user by ID @@ -70,7 +75,7 @@ const processUserAction = async (req, res, next) => { _id: user.status_history.length + 1, timestamp: new Date(), reason: reason || 'Quota added', - value: `+${amount} quota`, + value: value || `+${amount} quota`, executor: executor || 'system', }); @@ -91,7 +96,7 @@ const processUserAction = async (req, res, next) => { _id: user.status_history.length + 1, timestamp: new Date(), reason: reason || 'Quota removed', - value: `-${amount} quota`, + value: value || `-${amount} quota`, executor: executor || 'system', }); @@ -154,6 +159,24 @@ const processUserAction = async (req, res, next) => { updatedUser = await user.save(); break; + case 'addtokenhistory': + if (!old_token && !new_token) { + return res.status(400).json({ message: 'Old and new tokens are required' }); + } + // Update status history + user.token_history.push({ + _id: user.token_history.length + 1, + timestamp: new Date(), + reason: reason || 'Self Regenerated', + executor: executor || 'Self', + isForced: isForced || false, + old_token, + new_token, + }); + + updatedUser = await user.save(); + break; + default: return res.status(400).json({ message: `Invalid action: ${action}` }); } @@ -185,7 +208,7 @@ const processUserAction = async (req, res, next) => { const processUserSessionAndUpdate = async (req, res, next) => { try { const { headers, body } = req; - const { token, id, email, 'access-token': access_token } = body; + const { token, id, email, username, 'access-token': access_token } = body; const { key } = headers; // Validate access key @@ -214,12 +237,21 @@ const processUserSessionAndUpdate = async (req, res, next) => { const newUser = { _id: id, email, + username, token: generatedToken, access_token, password: crypto.randomBytes(22).toString('base64'), // Generate a random password }; + const newUserNotification = { + _id: `U${(await UserNotification.countDocuments()) + 1}`, + userId: id, + type: 'success', + message: `🎉 Welcome abroad!`, + expiry: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), + }; await Users.create(newUser); + await UserNotification.create(newUserNotification); return res.status(201).json({ message: 'User created successfully', diff --git a/src/controllers/v4/textUtilities/fact.js b/src/controllers/v4/textUtilities/fact.js index 1892ddd..f677c87 100644 --- a/src/controllers/v4/textUtilities/fact.js +++ b/src/controllers/v4/textUtilities/fact.js @@ -71,16 +71,15 @@ const getFact = async (req, res, next) => { * @property {Array} tags - Array of tags associated with the fact. * @property {Number} length - The length of the fact. */ - res.status(200).json(result); + return res.status(200).json(result); // Update system statistics for facts - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { facts: 1 } }); } catch (error) { /** * Update system statistics for failed requests * @type {Object} */ - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); + return next(error); } }; diff --git a/src/controllers/v4/textUtilities/listTags.js b/src/controllers/v4/textUtilities/listTags.js index 1bac190..2e1954a 100644 --- a/src/controllers/v4/textUtilities/listTags.js +++ b/src/controllers/v4/textUtilities/listTags.js @@ -67,13 +67,12 @@ const listTags = async (req, res, next) => { res.json(results); // Update system statistics for tags - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { tags: 1 } }); } catch (error) { /** * Update system statistics for failed requests * @type {Object} */ - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); + return next(error); } }; diff --git a/src/controllers/v4/textUtilities/owoify.js b/src/controllers/v4/textUtilities/owoify.js index 639096f..fabbd65 100644 --- a/src/controllers/v4/textUtilities/owoify.js +++ b/src/controllers/v4/textUtilities/owoify.js @@ -46,13 +46,12 @@ const getOwoifyText = async (req, res, next) => { * Update system statistics for Owofied texts * @type {Object} */ - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { owoify: 1 } }); } catch (error) { /** * Update system statistics for failed requests * @type {Object} */ - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); + return next(error); } }; diff --git a/src/controllers/v4/textUtilities/password.js b/src/controllers/v4/textUtilities/password.js index c04e539..9e6cfc3 100644 --- a/src/controllers/v4/textUtilities/password.js +++ b/src/controllers/v4/textUtilities/password.js @@ -48,13 +48,12 @@ const getPassword = async (req, res, next) => { }); // Update system statistics for generated passwords - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { password: 1 } }); } catch (error) { /** * Update system statistics for failed requests * @type {Object} */ - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); + return next(error); } }; diff --git a/src/controllers/v4/textUtilities/quote.js b/src/controllers/v4/textUtilities/quote.js index ba21b8f..3df3dc3 100644 --- a/src/controllers/v4/textUtilities/quote.js +++ b/src/controllers/v4/textUtilities/quote.js @@ -54,19 +54,18 @@ const getQuote = async (req, res, next) => { * Respond with the random quote * @type {Object} */ - res.status(200).json(result); + return res.status(200).json(result); /** * Update system statistics for quotes * @type {Object} */ - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { quotes: 1 } }); } catch (error) { /** * Update system statistics for failed requests * @type {Object} */ - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); + return next(error); } }; diff --git a/src/controllers/v4/textUtilities/uvuify.js b/src/controllers/v4/textUtilities/uvuify.js index 0b5121e..926543b 100644 --- a/src/controllers/v4/textUtilities/uvuify.js +++ b/src/controllers/v4/textUtilities/uvuify.js @@ -25,10 +25,9 @@ const getUvuifyText = async (req, res, next) => { }); // Increment the UvUify counter in the stats - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { uvuify: 1 } }); } catch (error) { // Increment failed requests counter in the stats and pass the error to the next middleware - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); + return next(error); } }; diff --git a/src/controllers/v4/textUtilities/uwuify.js b/src/controllers/v4/textUtilities/uwuify.js index 3ead9e2..2c5b7c0 100644 --- a/src/controllers/v4/textUtilities/uwuify.js +++ b/src/controllers/v4/textUtilities/uwuify.js @@ -25,10 +25,9 @@ const getUwuifyText = async (req, res, next) => { }); // Increment the UwUify counter in the stats - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { uwuify: 1 } }); } catch (error) { // Increment failed requests counter in the stats and pass the error to the next middleware - await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); + next(error); } }; diff --git a/src/middlewares/authorize.js b/src/middlewares/authorize.js index 11c69d5..018c064 100644 --- a/src/middlewares/authorize.js +++ b/src/middlewares/authorize.js @@ -145,8 +145,10 @@ const authorize = requiredRole => async (req, res, next) => { * @returns {string} - The extracted endpoint. */ const getEndpointFromUrl = url => { - const urlSegments = url.split('/'); - return urlSegments[urlSegments.length - 1]; // Last segment is assumed to be the endpoint + const urlSegments = url.split('?')[0].split('/').filter(Boolean); // Remove query & empty segments + const versionIndex = urlSegments.findIndex(segment => /^v\d+$/.test(segment)); // Find "vX" (e.g., v3, v4, v5) + + return versionIndex !== -1 && versionIndex + 1 < urlSegments.length ? urlSegments[versionIndex + 1] : null; }; /** @@ -187,7 +189,7 @@ const isEndpointEnabledInStats = async endpoint => { * @throws {Error} Throws an error if there is an issue with updating statistics. */ const incrementSystemStats = async stats => { - await Stats.findByIdAndUpdate({ _id: 'systemstats' }, { $inc: stats }); + await Stats.findByIdAndUpdate({ _id: 'system' }, { $inc: stats }); }; /** diff --git a/src/middlewares/logger.js b/src/middlewares/logger.js index 37cc5a2..385a66c 100644 --- a/src/middlewares/logger.js +++ b/src/middlewares/logger.js @@ -1,25 +1,139 @@ +import Stats from '../models/schemas/Stat.js'; import requestIp from 'request-ip'; import { Webhook } from 'discord-webhook-node'; const hook = new Webhook(process.env.DISCORD_WEBHOOK_URL); -export const logIP = (req, res, next) => { - const auth = req.headers.authorization || 'Null Auth'; +const validEndpoints = new Set([ + 'husbando', + 'waifu', + 'angry', + 'baka', + 'bite', + 'blush', + 'bonk', + 'bored', + 'bully', + 'bye', + 'chase', + 'cheer', + 'cringe', + 'cry', + 'cuddle', + 'dab', + 'dance', + 'die', + 'disgust', + 'facepalm', + 'feed', + 'glomp', + 'happy', + 'hi', + 'highfive', + 'hold', + 'hug', + 'kick', + 'kill', + 'kiss', + 'laugh', + 'lick', + 'love', + 'lurk', + 'midfing', + 'nervous', + 'nom', + 'nope', + 'nuzzle', + 'panic', + 'pat', + 'peck', + 'poke', + 'pout', + 'punch', + 'run', + 'sad', + 'shoot', + 'shrug', + 'sip', + 'slap', + 'sleepy', + 'smile', + 'smug', + 'stab', + 'stare', + 'suicide', + 'tease', + 'think', + 'thumbsup', + 'tickle', + 'triggered', + 'wag', + 'wave', + 'wink', + 'yes', + 'membership', + 'notification', + 'pages', + 'stats', + 'user', + 'fact', + 'listTags', + 'owoify', + 'password', + 'quote', + 'uvuify', + 'uwuify', +]); - const log = `${new Date()} - STATUS=${res.statusCode} - METHOD=${req.method} - IP=${req.ip} | ${requestIp.getClientIp( - req, - )} - URL=${req.originalUrl} - ${auth}\n`; +export const requestLogger = async (req, res, next) => { + try { + const auth = req.headers.authorization || 'Null Auth'; + const log = `${new Date()} - STATUS=${res.statusCode} - METHOD=${req.method} - IP=${ + req.ip + } | ${requestIp.getClientIp(req)} - URL=${req.originalUrl} - ${auth}\n`; + const now = new Date(); + const dateKey = now.toISOString().split('T')[0]; // "YYYY-MM-DD" + const pathSegments = req.path.split('/').filter(Boolean); // Remove empty segments + const endpoint = pathSegments[2]; - const IMAGE_URL = 'https://homepages.cae.wisc.edu/~ece533/images/airplane.png'; - hook.setUsername('API Logger'); - hook.setAvatar(IMAGE_URL); - hook.send(`\`${log}\``); + const IMAGE_URL = 'https://i.imgur.com/c55SNmu.png'; + hook.setUsername('API Logger'); + hook.setAvatar(IMAGE_URL); + hook.send(`\`${log}\``); + console.log(log); - // fs.appendFile('./logs/ip-logs.log', log, (err) => { - // if (err) throw err; - // }); + // Ignore invalid endpoints + if (!validEndpoints.has(endpoint)) return next(); - console.log(log); + res.on('finish', async () => { + const failedStatusCodes = new Set([400, 401, 402, 404, 502]); + const isSuccess = res.statusCode >= 200 && res.statusCode < 400; + const isFailure = failedStatusCodes.has(res.statusCode); - next(); + const update = { + $inc: { + total_requests: 1, + [`daily.${dateKey}.total_requests`]: 1, + [`daily.${dateKey}.${isSuccess ? 'success_requests' : 'failed_requests'}`]: 1, + [`daily.${dateKey}.endpoints.${endpoint}`]: 1, + + // Increment the all-time count for this endpoint + [`endpoints.${endpoint}`]: 1, + }, + }; + // Increment failed_requests only for specific failure codes + if (isFailure) { + update.$inc.failed_requests = 1; + } + + // console.log(update); + + await Stats.findOneAndUpdate({ _id: 'system' }, update, { upsert: true }); + }); + + next(); + } catch (error) { + console.error('Request logging failed:', error); + next(); + } }; diff --git a/src/models/schemas/Notification.js b/src/models/schemas/Notification.js new file mode 100644 index 0000000..62f1f70 --- /dev/null +++ b/src/models/schemas/Notification.js @@ -0,0 +1,23 @@ +import mongoose from 'mongoose'; + +/** + * Represents the schema for the User model. + * @class UserSchema + */ +const NotificationSchema = new mongoose.Schema({ + _id: String, + userId: { type: String, required: true, index: true }, // Index for fast lookup + type: { type: String, required: true, enum: ['info', 'warning', 'error', 'success'] }, // Predefined types for efficiency + message: { type: String, required: true }, + timestamp: { type: Date, default: Date.now }, + expiry: Date, // Auto-delete expired notifications +}); + +/** + * User model for interacting with the 'Users' collection in MongoDB. + * @class User + * @type {mongoose.Model} + */ +const Notification = mongoose.model('Notification', NotificationSchema); + +export default Notification; diff --git a/src/models/schemas/NotificationStatus.js b/src/models/schemas/NotificationStatus.js new file mode 100644 index 0000000..227034d --- /dev/null +++ b/src/models/schemas/NotificationStatus.js @@ -0,0 +1,20 @@ +import mongoose from 'mongoose'; + +/** + * Represents the schema for the User model. + * @class UserSchema + */ +const NotificationStatusSchema = new mongoose.Schema({ + _id: { type: String, required: true }, + read: { type: [String], default: [] }, // Auto-delete expired notifications + deleted: { type: [String], default: [] }, +}); + +/** + * User model for interacting with the 'Users' collection in MongoDB. + * @class User + * @type {mongoose.Model} + */ +const NotificationStatus = mongoose.model('NotificationStatus', NotificationStatusSchema); + +export default NotificationStatus; diff --git a/src/models/schemas/Stat.js b/src/models/schemas/Stat.js index 4b284db..87bcb33 100644 --- a/src/models/schemas/Stat.js +++ b/src/models/schemas/Stat.js @@ -3,104 +3,35 @@ const { Schema, model } = mongoose; const StatSchema = new Schema({ _id: { type: String, required: true, default: 'system' }, - dashboard: { - isEnabled: { type: Boolean, default: true }, - }, - registrations: { - isEnabled: { type: Boolean, default: true }, - }, - login: { - isEnabled: { type: Boolean, default: true }, - }, - tokenReset: { - isEnabled: { type: Boolean, default: true }, - }, - quote: { - isEnabled: { type: Boolean, default: true }, - }, total_requests: { type: Number, default: 0 }, endpoints_requests: { type: Number, default: 0 }, failed_requests: { type: Number, default: 0 }, success_requests: { type: Number, default: 0 }, banned_requests: { type: Number, default: 0 }, daily_requests: { type: Number, default: 0 }, - stats: { type: Number, default: 0 }, - run: { type: Number, default: 0 }, - sad: { type: Number, default: 0 }, - shoot: { type: Number, default: 0 }, - shrug: { type: Number, default: 0 }, - sip: { type: Number, default: 0 }, - slap: { type: Number, default: 0 }, - sleepy: { type: Number, default: 0 }, - smile: { type: Number, default: 0 }, - smug: { type: Number, default: 0 }, - stab: { type: Number, default: 0 }, - stare: { type: Number, default: 0 }, - suicide: { type: Number, default: 0 }, - tags: { type: Number, default: 0 }, - tease: { type: Number, default: 0 }, - think: { type: Number, default: 0 }, - thumbsup: { type: Number, default: 0 }, - tickle: { type: Number, default: 0 }, - triggered: { type: Number, default: 0 }, - users: { type: Number, default: 0 }, - wag: { type: Number, default: 0 }, - waifus: { type: Number, default: 0 }, - wave: { type: Number, default: 0 }, - wink: { type: Number, default: 0 }, - yesh: { type: Number, default: 0 }, - angry: { type: Number, default: 0 }, - baka: { type: Number, default: 0 }, - bite: { type: Number, default: 0 }, - blush: { type: Number, default: 0 }, - bonk: { type: Number, default: 0 }, - bored: { type: Number, default: 0 }, - bully: { type: Number, default: 0 }, - bye: { type: Number, default: 0 }, - chase: { type: Number, default: 0 }, - owoify: { type: Number, default: 0 }, - uwuify: { type: Number, default: 0 }, - uvuify: { type: Number, default: 0 }, - cheer: { type: Number, default: 0 }, - client: { type: Number, default: 0 }, - cringe: { type: Number, default: 0 }, - cry: { type: Number, default: 0 }, - cuddle: { type: Number, default: 0 }, - dab: { type: Number, default: 0 }, - dance: { type: Number, default: 0 }, - die: { type: Number, default: 0 }, - disgust: { type: Number, default: 0 }, - facepalm: { type: Number, default: 0 }, - facts: { type: Number, default: 0 }, - feed: { type: Number, default: 0 }, - glomp: { type: Number, default: 0 }, - happy: { type: Number, default: 0 }, - hi: { type: Number, default: 0 }, - highfive: { type: Number, default: 0 }, - hold: { type: Number, default: 0 }, - hug: { type: Number, default: 0 }, - kick: { type: Number, default: 0 }, - kill: { type: Number, default: 0 }, - kissu: { type: Number, default: 0 }, - laugh: { type: Number, default: 0 }, - lick: { type: Number, default: 0 }, - love: { type: Number, default: 0 }, - lurk: { type: Number, default: 0 }, - midfing: { type: Number, default: 0 }, - nervouse: { type: Number, default: 0 }, - nom: { type: Number, default: 0 }, - nope: { type: Number, default: 0 }, - nuzzle: { type: Number, default: 0 }, - panic: { type: Number, default: 0 }, - pat: { type: Number, default: 0 }, - peck: { type: Number, default: 0 }, - poke: { type: Number, default: 0 }, - pout: { type: Number, default: 0 }, - punch: { type: Number, default: 0 }, - quotes: { type: Number, default: 0 }, - password: { type: Number, default: 0 }, + endpoints: { + type: Map, + of: { type: Number, default: 0 }, + default: {}, + }, + daily: { + type: Map, + of: { + total_requests: { type: Number, default: 0 }, + failed_requests: { type: Number, default: 0 }, + success_requests: { type: Number, default: 0 }, + endpoints: { + type: Map, + of: { type: Number, default: 0 }, // Key: endpoint name (e.g., "login"), Value: request count + }, + }, + default: {}, + }, }); +StatSchema.index({ 'endpoints.$**': 1 }); // Indexes all endpoint names dynamically +StatSchema.index({ 'daily.$**': 1 }); // Indexes all daily stats dynamically + const Stat = model('Stat', StatSchema); export default Stat; diff --git a/src/models/schemas/System.js b/src/models/schemas/System.js index bbdde3d..1b6ed5f 100644 --- a/src/models/schemas/System.js +++ b/src/models/schemas/System.js @@ -1,6 +1,26 @@ import mongoose from 'mongoose'; const { Schema, model } = mongoose; +const roles = [ + 'developer', + 'super_admin', + 'admin', + 'database_moderator', + 'discord_moderator', + 'moderator', + 'community_manager', + 'support', + 'contributor', + 'tester', + 'beta_tester', + 'alpha_tester', + 'translator', + 'sponsor', + 'member', + 'user', + 'guest', +]; + const SystemSchema = new Schema({ _id: String, membership: { @@ -22,6 +42,75 @@ const SystemSchema = new Schema({ }, ], }, + pages: [ + { + _id: { type: String, required: true, unique: true }, + available: { type: Boolean, default: true }, + type: { type: String, enum: ['production', 'alpha', 'beta'], required: true }, + maintenance: { + status: { type: Boolean, default: false }, + message: { type: String, default: 'Page is under maintenance. Please try again later.' }, + }, + permission: { + roles: [ + { + type: String, + enum: roles, + required: true, + default: ['user'], + }, + ], + }, + content: { + available: { type: Boolean, default: true }, + service: { + available: { type: Boolean, default: true }, + message: String, + }, + maintenance: { + status: { type: Boolean, default: false }, + message: { type: String, default: 'Page is under maintenance. Please try again later.' }, + }, + permission: { + roles: [ + { + type: String, + enum: roles, + required: true, + default: ['user'], + }, + ], + }, + }, + }, + ], + rewards: [ + { + _id: { type: String, required: true, unique: true }, // Unique code like "SUMMER2025" + type: { type: String, enum: ['giftcard', 'coupon', 'voucher'], required: true }, + description: String, + discount: { + type: { type: String, enum: ['percentage', 'fixed'], required: true }, + value: { type: Number, required: true }, // e.g., 20 for 20% or $20 + }, + appliesTo: { + plans: [String], // Array of plan IDs it can be applied to, if any + roles: [String], // Optionally apply by role + }, + usage: { + maxRedemptions: { type: Number, default: 1 }, // How many times this code can be used globally + usedBy: [ + { + userId: String, + redeemedAt: Date, + }, + ], + }, + validFrom: Date, + validUntil: Date, + isActive: { type: Boolean, default: true }, + }, + ], }); export default model('System', SystemSchema); diff --git a/src/models/schemas/User.js b/src/models/schemas/User.js index 8a57bbf..c9b15d6 100644 --- a/src/models/schemas/User.js +++ b/src/models/schemas/User.js @@ -11,6 +11,12 @@ const UserSchema = new mongoose.Schema({ */ _id: { type: String }, + /** + * User's username. + * @type {string} + * + */ + username: { type: String }, /** * User's email address. * @type {string} @@ -37,6 +43,77 @@ const UserSchema = new mongoose.Schema({ */ token: { type: String }, + /** + * Array to store the history of token resets, including timestamps, reason, + * executor, and old/new token values. + * @type {Array<{ + * timestamp: Date, + * reason: string, + * isForced: boolean, + * executor: string, + * old_token: string, + * new_token: string, + * expiry?: Date + * }>} + */ + token_history: [ + { + /** + * Unique identifier for the token reset entry. + * @type {string} + * @required + */ + _id: { type: String, required: true }, + + /** + * Timestamp of the token reset. + * @type {Date} + * @default Date.now + */ + timestamp: { type: Date, default: Date.now }, + + /** + * Optional expiry time if token is meant to be invalidated after a period. + * @type {Date} + */ + expiry: { type: Date }, + + /** + * Reason for the token reset (e.g., "suspicious activity", "user request"). + * @type {string} + */ + reason: { type: String, default: 'Self Regenerated' }, + + /** + * Indicates whether the token reset was forced (e.g., by an admin). + * @type {boolean} + * @default false + */ + isForced: { type: Boolean, default: false }, + + /** + * Information about the staff member or system who performed the reset. + * @type {string} + * @required + */ + executor: { type: String, required: true, default: 'Self' }, + + /** + * The token before the reset. + * @type {string} + * @required + */ + old_token: { type: String, required: true }, + + /** + * The token after the reset. + * @type {string} + * @required + */ + new_token: { type: String, required: true }, + }, + ], + /** * Flag indicating whether the user is banned. * @type {boolean} diff --git a/src/models/schemas/UserNotification.js b/src/models/schemas/UserNotification.js new file mode 100644 index 0000000..952864d --- /dev/null +++ b/src/models/schemas/UserNotification.js @@ -0,0 +1,25 @@ +import mongoose from 'mongoose'; + +/** + * Represents the schema for the User model. + * @class UserSchema + */ +const UserNotificationSchema = new mongoose.Schema({ + _id: String, + userId: { type: String, required: true, index: true }, // Index for fast lookup + type: { type: String, required: true, enum: ['info', 'warning', 'error', 'success'] }, // Predefined types for efficiency + message: { type: String, required: true }, + timestamp: { type: Date, default: Date.now }, + expiry: Date, // Auto-delete expired notifications + read: { type: Boolean, default: false }, + deleted: { type: Boolean, default: false }, +}); + +/** + * User model for interacting with the 'Users' collection in MongoDB. + * @class User + * @type {mongoose.Model} + */ +const UserNotification = mongoose.model('UserNotification', UserNotificationSchema); + +export default UserNotification; diff --git a/src/routes/v4/index.js b/src/routes/v4/index.js index 6cd08ba..fba0bd7 100644 --- a/src/routes/v4/index.js +++ b/src/routes/v4/index.js @@ -1224,6 +1224,53 @@ import membershipRoutes from './internal/membership.js'; */ router.use('/membership', membershipRoutes); +import notificationRoutes from './internal/notifications.js'; + +/** + * @api {use} Mount Membership Routes + * @apiDescription Mount the membership-related routes for handling interactions. + * @apiName UseMembershipRoutes + * @apiGroup Routes + * + * @apiSuccess {Object} routes Membership-related routes mounted on the parent router. + * + * @function createMembershipRoutes + * @description Creates and returns a set of routes for handling interactions related to Membership. + * @returns {Object} Membership-related routes. + */ +router.use('/notifications', notificationRoutes); + +import pageRoutes from './internal/pages.js'; + +/** + * @api {use} Mount Pages Routes + * @apiDescription Mount the pages-related routes for handling page interactions. + * @apiName UsePagesRoutes + * @apiGroup Routes + * + * @apiSuccess {Object} routes Pages-related routes mounted on the parent router. + * + * @function createPagesRoutes + * @description Creates and returns a set of routes for handling interactions related to pages. + * @returns {Object} Pages-related routes. + */ +router.use('/pages', pageRoutes); + +import rewardsRoutes from './internal/rewards.js'; +/** + * @api {use} Mount Rewards Routes + * @apiDescription Mount the rewards-related routes for handling interactions. + * @apiName UseRewardsRoutes + * @apiGroup Routes + * + * @apiSuccess {Object} routes Rewards-related routes mounted on the parent router. + * + * @function createRewardsRoutes + * @description Creates and returns a set of routes for handling interactions related to rewards. + * @returns {Object} Rewards-related routes. + */ +router.use('/rewards', rewardsRoutes); + /** * Exporting the router for use in other parts of the application. * @exports {Router} router - Express Router instance with mounted routes. diff --git a/src/routes/v4/internal/notifications.js b/src/routes/v4/internal/notifications.js new file mode 100644 index 0000000..cb31fb1 --- /dev/null +++ b/src/routes/v4/internal/notifications.js @@ -0,0 +1,76 @@ +import { Router } from 'express'; +import { + retrieveNotifications, + markNotificationAsRead, + markNotificationAsDeleted, +} from '../../../controllers/v4/internal/notification.js'; +import createRateLimiter from '../../../middlewares/rateLimit.js'; + +const router = Router(); + +router + .route('/') + /** + * @api {get} v4/membership Get Membership Details + * @apiDescription Retrieve membership details, including plans and features. + * @apiName getMembership + * @apiGroup Membership + * @apiPermission user + * + * @apiHeader {String} Authorization System access token. + * + * @apiParam {String} [q] Optional query parameter to filter results. + * @apiParamExample {json} Request Example: + * GET /membership?q=plans&features + * + * @apiSuccess {Object} membership Membership object. + * @apiSuccessExample {json} Success Response: + * { + * "membership": { + * "plans": [...], + * "features": [...] + * } + * } + * + * @apiError (Unauthorized 401) Unauthorized Only authenticated users can access the data. + * @apiError (Forbidden 403) Forbidden Only authorized users can access the data. + * @apiError (Too Many Requests 429) TooManyRequests The client has exceeded the allowed number of requests within the time window. + * @apiError (Bad Request 400) BadRequest Invalid query parameter(s) provided. + * @apiError (Internal Server Error 500) InternalServerError An error occurred while processing the request. + */ + .get(createRateLimiter(), retrieveNotifications); +router + .route('/read') + /** + * @api {patch} v4/notifications/read Mark Notification as Read + * @apiDescription Marks a notification as read. + * @apiName markNotificationAsRead + * @apiGroup Notifications + * @apiPermission user + * + * @apiHeader {String} Authorization System access token. + * + * @apiParam {String} notificationId Notification ID. + * + * @apiSuccess {String} message Success message. + */ + .patch(markNotificationAsRead); + +router + .route('/delete') + /** + * @api {delete} v4/notifications/delete Delete Notification + * @apiDescription Deletes a notification. + * @apiName markNotificationAsDeleted + * @apiGroup Notifications + * @apiPermission user + * + * @apiHeader {String} Authorization System access token. + * + * @apiParam {String} notificationId Notification ID. + * + * @apiSuccess {String} message Success message. + */ + .delete(markNotificationAsDeleted); +// Export the router +export default router; diff --git a/src/routes/v4/internal/pages.js b/src/routes/v4/internal/pages.js new file mode 100644 index 0000000..81f6a78 --- /dev/null +++ b/src/routes/v4/internal/pages.js @@ -0,0 +1,148 @@ +import { Router } from 'express'; +import createRateLimiter from '../../../middlewares/rateLimit.js'; +import { + getPageStatus, + getPageMeta, + checkPageAccess, + getPageInfo, + updatePage, + addPage, +} from '../../../controllers/v4/internal/pages.js'; + +const router = Router(); + +router + .route('/') + /** + * @api {post} v4/pages Add Page + * @apiDescription Adds a new page to the system. + * @apiName addPage + * @apiGroup PageManagement + * @apiPermission internal + * + * @apiHeader {String} Key Internal access token. + * @apiBody {String} name Page name. + * @apiBody {Boolean} [available] Page availability status. + * @apiBody {String="production","alpha","beta"} [type] Page type. + * @apiBody {Object} [maintenance] Maintenance info. + * @apiBody {Object} [permission] Role-based access. + * + * @apiSuccess {Object} page Newly created page details. + * + * @apiError (401) Unauthorized Invalid or missing access key. + * @apiError (400) BadRequest Missing or invalid required fields. + * @apiError (500) InternalServerError Unexpected error. + */ + .post(addPage); + +router + .route('/:id/status') + /** + * @api {get} v4/pages/:id/status Get Page Status + * @apiDescription Fetches the availability and maintenance status of a specific page. + * @apiName getPageStatus + * @apiGroup PageManagement + * @apiPermission internal + * + * @apiHeader {String} Key Internal access token. + * @apiParam {String} id Page's unique identifier. + * + * @apiSuccess {String} _id Page ID. + * @apiSuccess {Boolean} available Page availability status. + * @apiSuccess {Object} maintenance Page maintenance status and message. + * + * @apiError (401) Unauthorized Invalid or missing access key. + * @apiError (404) NotFound Page not found. + * @apiError (500) InternalServerError Unexpected error. + */ + .get(getPageStatus); + +router + .route('/:id/meta') + /** + * @api {get} v4/pages/:id/meta Get Page Metadata + * @apiDescription Fetches metadata, including type, permissions, and maintenance info. + * @apiName getPageMeta + * @apiGroup PageManagement + * @apiPermission internal + * + * @apiHeader {String} Key Internal access token. + * @apiParam {String} id Page's unique identifier. + * + * @apiSuccess {String} _id Page ID. + * @apiSuccess {String} type Page type (production, alpha, beta). + * @apiSuccess {Object} maintenance Maintenance info. + * @apiSuccess {Object} permission Role-based access. + * + * @apiError (401) Unauthorized Invalid or missing access key. + * @apiError (404) NotFound Page not found. + * @apiError (500) InternalServerError Unexpected error. + */ + .get(getPageMeta); + +router + .route('/:id/access') + /** + * @api {get} v4/pages/:id/access Check Page Access + * @apiDescription Checks if a user role has access to the page. + * @apiName checkPageAccess + * @apiGroup PageManagement + * @apiPermission internal + * + * @apiHeader {String} Key Internal access token. + * @apiParam {String} id Page's unique identifier. + * @apiQuery {String} role User role to check access. + * + * @apiSuccess {Boolean} access True if role has access, false otherwise. + * + * @apiError (401) Unauthorized Invalid or missing access key. + * @apiError (400) BadRequest Role query parameter is required. + * @apiError (404) NotFound Page not found. + * @apiError (500) InternalServerError Unexpected error. + */ + .get(checkPageAccess); + +router + .route('/:id') + /** + * @api {get} v4/pages/:id Get Page Info + * @apiDescription Returns general page details, including type, status, and permissions. + * @apiName getPageInfo + * @apiGroup PageManagement + * @apiPermission internal + * + * @apiHeader {String} Key Internal access token. + * @apiParam {String} id Page's unique identifier. + * + * @apiSuccess {Object} page Full page details. + * + * @apiError (401) Unauthorized Invalid or missing access key. + * @apiError (404) NotFound Page not found. + * @apiError (500) InternalServerError Unexpected error. + */ + .get(getPageInfo) + /** + * @api {patch} v4/pages/:id Update Page + * @apiDescription Updates page details (availability, type, maintenance, or permissions). + * @apiName updatePage + * @apiGroup PageManagement + * @apiPermission internal + * + * @apiHeader {String} Key Internal access token. + * @apiParam {String} id Page's unique identifier. + * @apiBody {Boolean} [available] Page availability status. + * @apiBody {String="production","alpha","beta"} [type] Page type. + * @apiBody {Object} [maintenance] Maintenance info. + * @apiBody {Object} [permission] Role-based access. + * + * @apiSuccess {Object} page Updated page details. + * + * @apiError (401) Unauthorized Invalid or missing access key. + * @apiError (400) BadRequest No valid fields to update. + * @apiError (404) NotFound Page not found. + * @apiError (500) InternalServerError Unexpected error. + */ + .patch(updatePage); + +// Export the router +export default router; diff --git a/src/routes/v4/internal/rewards.js b/src/routes/v4/internal/rewards.js new file mode 100644 index 0000000..d5f0120 --- /dev/null +++ b/src/routes/v4/internal/rewards.js @@ -0,0 +1,88 @@ +import { Router } from 'express'; +import { createReward, getRewards } from '../../../controllers/v4/internal/rewards.js'; +import createRateLimiter from '../../../middlewares/rateLimit.js'; + +const router = Router(); + +router + .route('/') + /** + * @api {get} v4/rewards Get All Rewards + * @apiDescription Fetch all available rewards (coupons, vouchers, giftcards). + * @apiName getRewards + * @apiGroup Rewards + * @apiPermission internal + * + * @apiHeader {String} Key Internal access token + * + * @apiSuccess {Array} rewards List of available rewards. + * @apiError (Unauthorized 401) Unauthorized Invalid or missing access key. + * @apiError (Internal Server Error 500) InternalServerError An error occurred while fetching rewards. + */ + .get(createRateLimiter(), getRewards) + /** + * @api {post} v4/rewards Create a New Reward + * @apiDescription Create a new reward entry for use as a giftcard, coupon, or voucher. + * @apiName createReward + * @apiGroup Rewards + * @apiPermission admin + * + * @apiHeader {String} Key Internal access token + * + * @apiBody {String} _id Unique ID of the reward. + * @apiBody {String} type Reward type (coupon, giftcard, voucher). + * @apiBody {Object} discount Discount details. + * @apiBody {Object} appliesTo (optional) Role/plan filters. + * @apiBody {Object} usage (optional) Max usage/conditions. + * @apiBody {Date} validFrom (optional) Validity start. + * @apiBody {Date} validUntil (optional) Validity end. + * @apiBody {Boolean} isActive Whether reward is currently active. + * + * @apiSuccess {Object} reward Created reward object. + * @apiError (Bad Request 400) BadRequest Missing or invalid fields. + * @apiError (Conflict 409) Conflict Reward with this ID already exists. + * @apiError (Unauthorized 401) Unauthorized Invalid or missing access key. + * @apiError (Internal Server Error 500) InternalServerError An error occurred while creating reward. + */ + .post(createRateLimiter(), createReward); + +router + .route('/:id') + /** + * @api {get} v4/rewards/:id Get Specific Reward + * @apiDescription Fetch a specific reward by its unique ID. + * @apiName getSingleReward + * @apiGroup Rewards + * @apiPermission internal + * + * @apiHeader {String} Key Internal access token + * + * @apiParam {String} id Reward's unique identifier. + * + * @apiSuccess {Object} reward Reward details. + * @apiError (Not Found 404) NotFound Reward with the given ID does not exist. + * @apiError (Unauthorized 401) Unauthorized Invalid or missing access key. + * @apiError (Internal Server Error 500) InternalServerError An error occurred while fetching reward. + */ + .get(createRateLimiter(), getRewards); + +router.route('/redeem'); +/** + * @api {post} v4/rewards/redeem Redeem a Reward + * @apiDescription Redeem a reward using its unique ID. + * @apiName redeemReward + * @apiGroup Rewards + * @apiPermission user + * + * @apiHeader {String} Key Internal access token + * + * @apiBody {String} id Unique ID of the reward to redeem. + * + * @apiSuccess {Object} result Redemption result. + * @apiError (Bad Request 400) BadRequest Missing or invalid fields. + * @apiError (Not Found 404) NotFound Reward with the given ID does not exist. + * @apiError (Unauthorized 401) Unauthorized Invalid or missing access key. + * @apiError (Internal Server Error 500) InternalServerError An error occurred while redeeming reward. + */ + +export default router; diff --git a/src/routes/v4/internal/stats.js b/src/routes/v4/internal/stats.js index 025f9f6..2c7faf2 100644 --- a/src/routes/v4/internal/stats.js +++ b/src/routes/v4/internal/stats.js @@ -1,5 +1,10 @@ import { Router } from 'express'; -import { getStats } from '../../../controllers/v4/internal/stats.js'; +import { + getPopularEndpoints, + getTopEndpointsToday, + getMonthlyRequests, + getUsersStat, +} from '../../../controllers/v4/internal/stats.js'; import createRateLimiter from '../../../middlewares/rateLimit.js'; const router = Router(); @@ -7,8 +12,8 @@ const router = Router(); router .route('/') /** - * @api {post} v4/stats Get Statistics - * @apiDescription Get statistics about the system usage. + * @api {get} v4/stats Get Statistics + * @apiDescription Get overall system statistics. * @apiName getStats * @apiGroup Statistics * @apiPermission user @@ -22,7 +27,73 @@ router * @apiError (Too Many Requests 429) TooManyRequests The client has exceeded the allowed number of requests within the time window. * @apiError (Internal Server Error 500) InternalServerError An error occurred while processing the request. */ - .get(createRateLimiter(), getStats); + .get(createRateLimiter(), getPopularEndpoints); + +router + .route('/popular') + /** + * @api {get} v4/stats/popular Get Most Popular Endpoints + * @apiDescription Retrieve the most requested API endpoints. + * @apiName getPopularEndpoints + * @apiGroup Statistics + * @apiPermission user + * + * @apiHeader {String} Authorization System access token. + * + * @apiQuery {String} [top] Range for top endpoints (e.g., `?top=5`, `?top=2-10`). + * + * @apiSuccess {Object} popular_endpoints List of most requested API endpoints. + */ + .get(createRateLimiter(), getPopularEndpoints); + +router + .route('/today') + /** + * @api {get} v4/stats/today Get Top Endpoints for Today + * @apiDescription Retrieve today's most requested API endpoints. + * @apiName getTopEndpointsToday + * @apiGroup Statistics + * @apiPermission user + * + * @apiHeader {String} Authorization System access token. + * + * @apiQuery {String} [top] Range for top endpoints (e.g., `?top=5`, `?top=2-10`). + * + * @apiSuccess {Object} top_endpoints_today List of today's most requested API endpoints. + */ + .get(createRateLimiter(), getTopEndpointsToday); + +router + .route('/monthly') + /** + * @api {get} v4/stats/monthly Get Monthly Request Data + * @apiDescription Retrieve system request statistics for the last 5 months. + * @apiName getMonthlyRequests + * @apiGroup Statistics + * @apiPermission user + * + * @apiHeader {String} Authorization System access token. + * + * @apiSuccess {Object} last_5_months Request count per month for the last 5 months. + */ + .get(createRateLimiter(), getMonthlyRequests); + +router + .route('/users') + /** + * @api {get} v4/users Get User Statistics + * @apiDescription Retrieve user statistics, including request quotas. + * @apiName getUsersStat + * @apiGroup Users + * @apiPermission user + * + * @apiHeader {String} Authorization System access token. + * + * @apiSuccess {Object[]} users List of users with request quotas. + * @apiSuccess {String} users.username The username (or "Anonymous" if missing). + * @apiSuccess {Number} users.req_quota The request quota of the user. + */ + .get(createRateLimiter(), getUsersStat); // Export the router export default router;