From f8ccb24bdbd810ec8cb83cb065e79fc9a6f702c2 Mon Sep 17 00:00:00 2001 From: Ben Silverman Date: Tue, 28 Jul 2020 14:58:22 -0400 Subject: [PATCH] #17 move direct db calls into util --- src/pages/api/document/index.js | 85 +-------------------------------- src/utils/dbUtil.js | 83 ++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 83 deletions(-) create mode 100644 src/utils/dbUtil.js diff --git a/src/pages/api/document/index.js b/src/pages/api/document/index.js index b1a367e7..00f99f6d 100644 --- a/src/pages/api/document/index.js +++ b/src/pages/api/document/index.js @@ -1,90 +1,9 @@ import nc from 'next-connect'; -import { ObjectID } from 'mongodb'; -import jwt from 'next-auth/jwt'; import middleware from '../../../middlewares/middleware'; - -const secret = process.env.AUTH_SECRET; +import postDocument from '../../../utils/dbUtil'; const handler = nc() .use(middleware) - .post( - async (req, res) => { - const token = await jwt.getJwt({ req, secret }); - if (token && token.exp > 0) { - const dateCreated = new Date(Date.now()); - const { - title, - slug, - groups, - resourceType, - authors, - publisher, - publicationDate, - bookTitle, - edition, - url, - accessed, - rightsStatus, - location, - state, - text, - uploadContentType, - editors, - volume, - issue, - pageNumbers, - publication, - series, - sesiesNumber, - notes, - } = req.body; - const metadata = { - title, - slug, - groups, - resourceType, - authors, - publisher, - publicationDate, - bookTitle, - edition, - url, - accessed, - rightsStatus, - location, - state, - text, - uploadContentType, - editors, - volume, - issue, - pageNumbers, - publication, - series, - sesiesNumber, - notes, - }; - Object.keys(metadata).forEach((key) => { - if (metadata[key] === undefined) { - delete metadata[key]; - } - }); - await req.db - .collection('documents') - .insert( - { - owner: ObjectID(token.user.id), - createdAt: dateCreated, - updatedAt: dateCreated, - ...metadata, - }, - (err, doc) => { - if (err) throw err; - res.status(200).json(doc); - }, - ); - } else res.status(403).json({ error: '403 Invalid or expired token' }); - }, - ); + .post(postDocument); export default handler; diff --git a/src/utils/dbUtil.js b/src/utils/dbUtil.js new file mode 100644 index 00000000..386ca481 --- /dev/null +++ b/src/utils/dbUtil.js @@ -0,0 +1,83 @@ +import { ObjectID } from 'mongodb'; +import jwt from 'next-auth/jwt'; + +const secret = process.env.AUTH_SECRET; + +const postDocument = async (req, res) => { + const token = await jwt.getJwt({ req, secret }); + if (token && token.exp > 0) { + const dateCreated = new Date(Date.now()); + const { + title, + slug, + groups, + resourceType, + authors, + publisher, + publicationDate, + bookTitle, + edition, + url, + accessed, + rightsStatus, + location, + state, + text, + uploadContentType, + editors, + volume, + issue, + pageNumbers, + publication, + series, + sesiesNumber, + notes, + } = req.body; + const metadata = { + title, + slug, + groups, + resourceType, + authors, + publisher, + publicationDate, + bookTitle, + edition, + url, + accessed, + rightsStatus, + location, + state, + text, + uploadContentType, + editors, + volume, + issue, + pageNumbers, + publication, + series, + sesiesNumber, + notes, + }; + Object.keys(metadata).forEach((key) => { + if (metadata[key] === undefined) { + delete metadata[key]; + } + }); + await req.db + .collection('documents') + .insert( + { + owner: ObjectID(token.user.id), + createdAt: dateCreated, + updatedAt: dateCreated, + ...metadata, + }, + (err, doc) => { + if (err) throw err; + res.status(200).json(doc); + }, + ); + } else res.status(403).json({ error: '403 Invalid or expired token' }); +}; +export default postDocument;