Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.6 kB
1
Indexable
Never
import connectToMongo from 'src/db'
import { scratchmessages } from 'src/models/ScratchMessage'
import { PythonShell } from 'python-shell'

export default async function handler(request, res) {
  connectToMongo()
  const { campaignName, linkedinemail, message ,emailmessage} = request.body
  if (request.method === 'POST') {
    try {
      const user = JSON.parse(request.headers['user-data'])
      const userId = user._id

      const existingMessage = await scratchmessages.findOne({
        user: userId,
        campaignName: campaignName,
        linkedinemail: linkedinemail
      })

      if (existingMessage) {
        // Update the existing document with the new message
        existingMessage.message = message
        existingMessage.emailmessage = emailmessage
        
        await existingMessage.save()
        res.status(200).json({ message: 'Existing message updated' })
      } 
      else {
        // Create a new document if it doesn't exist
        const otpData = new scratchmessages({
          user: userId,
          campaignName: campaignName,
          linkedinemail: linkedinemail,
          message: message,
          emailmessage: emailmessage
        })

        console.log('mess', otpData)
        const savedAccount = await otpData.save()
        console.log('mssv', savedAccount)
        res.status(201).json({ message: 'New OTP Added' })
      }
    } catch (error) {
      res.status(500).json({ success: false, error })
    }
  } else {
    res.status(405).json({ error: 'Method Not Allowed' })
  }
}
Leave a Comment