import dbConnect from 'configs/dbConnect' 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 'apps/Notes/models/NoteList' export default withSession(async (req, res) => { const conn = await dbConnect() switch (req.method) { case 'POST': try { const session = await conn.startSession() await session.withTransaction(async () => { const noteList = await NoteList.create({}) const body = JSON.parse(req.body) const { _id, email, verificationKey: key, theme, language } = await User.create({ ...body, noteList }) if (!email) { throw new Error('Something went wrong') } sendMail(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 }) } break default: res.status(400).json({ isLoggedIn: false }) break } })