Skip to content

Commit

Permalink
#17 move direct db calls into util
Browse files Browse the repository at this point in the history
  • Loading branch information
blms committed Jul 28, 2020
1 parent 7fbc9a1 commit f8ccb24
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 83 deletions.
85 changes: 2 additions & 83 deletions src/pages/api/document/index.js
Original file line number Diff line number Diff line change
@@ -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;
83 changes: 83 additions & 0 deletions src/utils/dbUtil.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit f8ccb24

Please sign in to comment.