diff --git a/src/controllers/developers/developers.js b/src/controllers/developers/developers.js deleted file mode 100644 index b2784e32..00000000 --- a/src/controllers/developers/developers.js +++ /dev/null @@ -1,92 +0,0 @@ -import { hash } from 'bcrypt'; -import { v4 as uuid } from 'uuid'; -<<<<<<<< HEAD:src/controllers/developers.ts -import { isProduction, CLIENT_TEST, isTest } from '../config'; -import { developerSchema } from '../models/Developer'; -import { createDbConnection, handleCloseConnection } from '../services/database'; -import { MiddleWare } from '../types'; -import { sendNewDeveloper } from './email'; -======== -import { isProduction, CLIENT_TEST, isTest } from '../../config'; -import { developerSchema } from '../../models/Developer'; -import { createDbConnection, handleCloseConnection } from '../../services/database'; -import { sendNewDeveloper } from '../email'; ->>>>>>>> 53662e8 (chore: move developers controller method to developers folder #627):src/controllers/developers/developers.js - -const TEST_EMAIL = 'developer@example.com'; - -/* Creates a new apiKey to be associated with a developer */ -const generateApiKey = uuid; - -/* Creates a new Developer in the database */ -export const postDeveloper: MiddleWare = async (req, res, next) => { - const connection = createDbConnection(); - const Developer = connection.model('Developer', developerSchema); - - try { - const { body: data } = req; - const { email, password, name } = data; - - const developers = await Developer.find({ email }); - if (developers.length && email !== TEST_EMAIL) { - throw new Error('This email is already used.'); - } - - if ((isProduction || CLIENT_TEST) && email === TEST_EMAIL) { - throw new Error('This email is already used.'); - } - - const apiKey = generateApiKey(); - const hashedPassword = await hash(password, 10); - const developer = new Developer({ - name, - email, - apiKey, - password: hashedPassword, - }); - await developer.save(); - if (!isTest) { - try { - await sendNewDeveloper({ to: email, apiKey, name }); - } catch (err: any) { - console.log(err?.response?.body?.errors); - } - } - await handleCloseConnection(connection); - return res.send({ - message: `Success email sent to ${email}`, - apiKey, - }); - } catch (err: any) { - await handleCloseConnection(connection); - if (!isTest) { - console.trace(err); - } - return next(err); - } -}; - -export const getDeveloper: MiddleWare = async (req, res, next) => { - const connection = createDbConnection(); - const Developer = connection.model('Developer', developerSchema); - const { id } = req.params; - try { - const developer = await Developer.findById(id); - - if (!developer) { - throw new Error("Developer doesn't exist"); - } - - await handleCloseConnection(connection); - return res.send({ - message: 'Success', - developer, - }); - } catch (err) { - await handleCloseConnection(connection); - if (!isTest) { - console.trace(err); - } - return next(err); - } -}; diff --git a/src/controllers/developers/index.js b/src/controllers/developers/index.js deleted file mode 100644 index 1839871a..00000000 --- a/src/controllers/developers/index.js +++ /dev/null @@ -1,3 +0,0 @@ -import { getDeveloper, postDeveloper } from './developers'; - -export { getDeveloper, postDeveloper }; diff --git a/src/controllers/developers/utils.js b/src/controllers/developers/utils.js deleted file mode 100644 index a7a99348..00000000 --- a/src/controllers/developers/utils.js +++ /dev/null @@ -1,64 +0,0 @@ -import { developerSchema } from '../../models/Developer'; -import { createDbConnection } from '../../services/database'; -import { PROD_LIMIT, isTest } from '../../config'; - -export const FALLBACK_API_KEY = 'fallback_api_key'; - -const determineLimit = (apiLimit) => (isTest ? apiLimit || PROD_LIMIT : PROD_LIMIT); - -export const handleRequest = (req) => { - const { apiLimit } = req.query; - const apiToken = req.headers['X-API-Key'] || req.headers['x-api-key']; - - return { apiLimit, apiToken }; -}; - -const isSameDate = (first, second) => ( - first.getFullYear() === second.getFullYear() - && first.getMonth() === second.getMonth() - && first.getDate() === second.getDate() -); - -/* Increments usage count and updates usage date */ -const handleDeveloperUsage = async (developer) => { - const updatedDeveloper = developer; - const isNewDay = !isSameDate(updatedDeveloper.usage.date, new Date()); - updatedDeveloper.usage.date = Date.now(); - - if (isNewDay) { - updatedDeveloper.usage.count = 0; - } else { - updatedDeveloper.usage.count += 1; - } - - return updatedDeveloper.save(); -}; - -/* Finds a developer with provided information */ -export const findDeveloper = async (apiKey) => { - console.time('Finding developer account'); - const connection = createDbConnection(); - const Developer = connection.model('Developer', developerSchema); - const developer = await Developer.find({ apiKey }); - if (!developer) { - throw new Error('Invalid API Key. Check your API Key and try again'); - } - console.timeEnd('Finding developer account'); - return developer; -}; - -export const checkDeveloperAPILimit = async (apiLimit, apiKey, next) => { - const developer = await findDeveloper(apiKey); - console.log(developer); - - if (developer) { - console.log(determineLimit(apiLimit)); - if (developer.usage.count >= determineLimit(apiLimit)) { - throw new Error('You have exceeded your API limit. Please upgrade your plan.', 403); - } - await handleDeveloperUsage(developer); - return next(); - } - - throw new Error('Invalid API Key. Check your API Key and try again', 401); -}; diff --git a/src/routers/router.ts b/src/routers/router.ts index 01ce9ee6..8d56a81a 100644 --- a/src/routers/router.ts +++ b/src/routers/router.ts @@ -3,7 +3,7 @@ import rateLimit from 'express-rate-limit'; import { MiddleWare } from '../types'; import { getWords, getWord } from '../controllers/words'; import { getExamples, getExample } from '../controllers/examples'; -import { getDeveloper, postDeveloper } from '../controllers/developers'; +import { getDeveloper, postDeveloper } from '../controllers/developers/developers'; import { getStats } from '../controllers/stats'; import validId from '../middleware/validId'; import validateDeveloperBody from '../middleware/validateDeveloperBody';