aboutsummaryrefslogtreecommitdiffstats
path: root/pages/api/register.js
diff options
context:
space:
mode:
authorGravatar piotrruss <mail@pruss.it> 2021-09-09 23:23:53 +0200
committerGravatar piotrruss <mail@pruss.it> 2021-09-09 23:23:53 +0200
commita863c77ea12a4c7060b9c496e5df81bbdcca2374 (patch)
tree8d38ebc21928fa973235d8829bf1c6f7d65028df /pages/api/register.js
parent569bdb8c5d7538fa0ea8a99ff2f8376f7cbfa51a (diff)
downloadmy_apps-a863c77ea12a4c7060b9c496e5df81bbdcca2374.tar.gz
my_apps-a863c77ea12a4c7060b9c496e5df81bbdcca2374.tar.bz2
my_apps-a863c77ea12a4c7060b9c496e5df81bbdcca2374.zip
add db transactions, move db models
Diffstat (limited to 'pages/api/register.js')
-rw-r--r--pages/api/register.js26
1 files changed, 16 insertions, 10 deletions
diff --git a/pages/api/register.js b/pages/api/register.js
index 036cede..ba72e53 100644
--- a/pages/api/register.js
+++ b/pages/api/register.js
@@ -3,25 +3,31 @@ import sendMail from 'configs/sendMail'
import withSession from 'hocs/withSession'
import { subject, text, html } from 'helpers/email'
import User from 'models/User'
-import NoteList from 'models/NoteList'
+import NoteList from 'apps/Notes/models/NoteList'
export default withSession(async (req, res) => {
- await dbConnect()
+ const conn = await dbConnect()
switch (req.method) {
case 'POST':
try {
- const noteList = await NoteList.create({})
+ const session = await conn.startSession()
- const { _id, email, verificationKey: key, theme, language } = await User.create({ ...req.body, noteList })
- if (!email) { throw new Error('Something went wrong') }
+ await session.withTransaction(async () => {
+ const noteList = await NoteList.create({})
+ const { _id, email, verificationKey: key, theme, language } = await User.create({ ...req.body, noteList })
- sendMail(req.body.email, subject(language), text(language, key), html(language, key))
+ if (!email) { throw new Error('Something went wrong') }
- const user = { _id, email, noteList, theme, language, isVerified: false, isLoggedIn: true }
- req.session.set('user', user)
- await req.session.save()
- res.status(201).json(user)
+ sendMail(req.body.email, subject(language), text(language, key), html(language, key))
+
+ const user = { _id, email, noteList, theme, language, isVerified: false, isLoggedIn: true }
+ req.session.set('user', user)
+ await req.session.save()
+
+ session.endSession()
+ res.status(201).json(user)
+ })
} catch (error) {
res.status(400).json({ isLoggedIn: false })
}