| 1 | const { sendEmail } = require('../../middelwares/mail'); |
| 2 | const Email = require('../../model/schema/email'); |
| 3 | const User = require('../../model/schema/user'); |
| 4 | const mongoose = require('mongoose'); |
| 5 | |
| 6 | const add = async (req, res) => { |
| 7 | try { |
| 8 | const { sender, recipient, subject, message, startDate, createByContact, createBy, createByLead, html, type } = req.body; |
| 9 | |
| 10 | if (createByContact && !mongoose.Types.ObjectId.isValid(createByContact)) { |
| 11 | res.status(400).json({ error: 'Invalid createByContact value' }); |
| 12 | } |
| 13 | if (createByLead && !mongoose.Types.ObjectId.isValid(createByLead)) { |
| 14 | res.status(400).json({ error: 'Invalid createByLead value' }); |
| 15 | } |
| 16 | |
| 17 | const email = { sender, recipient, subject, message, startDate, createBy, html, type } |
| 18 | |
| 19 | if (createByContact) { |
| 20 | email.createByContact = createByContact; |
| 21 | } |
| 22 | if (createByLead) { |
| 23 | email.createByLead = createByLead; |
| 24 | } |
| 25 | |
| 26 | const user = await User.findById({ _id: email.sender }); |
| 27 | user.emailsent = user.emailsent + 1; |
| 28 | await user.save(); |
| 29 | |
| 30 | const result = new Email(email); |
| 31 | await result.save(); |
| 32 | |
| 33 | res.status(200).json({ result }); |
| 34 | } catch (err) { |
| 35 | console.error('Failed to create :', err); |
| 36 | res.status(400).json({ err, error: 'Failed to create' }); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | const index = async (req, res) => { |
| 41 | try { |
| 42 | const query = req.query |
| 43 | if (query.sender) { |
| 44 | query.sender = new mongoose.Types.ObjectId(query.sender); |
| 45 | } |
| 46 | |
| 47 | let result = await Email.aggregate([ |
| 48 | { $match: query }, |
| 49 | { |
| 50 | $lookup: { |
| 51 | from: 'Leads', |
| 52 | localField: 'createByLead', |
| 53 | foreignField: '_id', |
| 54 | as: 'createByrefLead' |
| 55 | } |
| 56 | }, |
| 57 | { |
| 58 | $lookup: { |
| 59 | from: 'Contacts', |
| 60 | localField: 'createByContact', |
| 61 | foreignField: '_id', |
| 62 | as: 'createByRef' |
| 63 | } |
| 64 | }, |
| 65 | { |
| 66 | $lookup: { |
| 67 | from: 'User', |
| 68 | localField: 'sender', |
| 69 | foreignField: '_id', |
| 70 | as: 'users' |
| 71 | } |
| 72 | }, |
| 73 | { $unwind: { path: '$users', preserveNullAndEmptyArrays: true } }, |
| 74 | { $unwind: { path: '$createByRef', preserveNullAndEmptyArrays: true } }, |
| 75 | { $unwind: { path: '$createByrefLead', preserveNullAndEmptyArrays: true } }, |
| 76 | { $match: { 'users.deleted': false } }, |
| 77 | { |
| 78 | $addFields: { |
| 79 | senderName: { $concat: ['$users.firstName', ' ', '$users.lastName'] }, |
| 80 | deleted: { |
| 81 | $cond: [ |
| 82 | { $eq: ['$createByRef.deleted', false] }, |
| 83 | '$createByRef.deleted', |
| 84 | { $ifNull: ['$createByrefLead.deleted', false] } |
| 85 | ] |
| 86 | }, |
| 87 | createByName: { |
| 88 | $cond: { |
| 89 | if: '$createByRef', |
| 90 | then: { $concat: ['$createByRef.title', ' ', '$createByRef.firstName', ' ', '$createByRef.lastName'] }, |
| 91 | else: { $concat: ['$createByrefLead.leadName'] } |
| 92 | } |
| 93 | }, |
| 94 | } |
| 95 | }, |
| 96 | { |
| 97 | $project: { |
| 98 | createByRef: 0, |
| 99 | createByrefLead: 0, |
| 100 | users: 0 |
| 101 | } |
| 102 | }, |
| 103 | ]) |
| 104 | |
| 105 | |
| 106 | res.status(200).json(result); |
| 107 | } catch (err) { |
| 108 | console.error('Failed :', err); |
| 109 | res.status(400).json({ err, error: 'Failed ' }); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | const view = async (req, res) => { |
| 114 | try { |
| 115 | let result = await Email.findOne({ _id: req.params.id }) |
| 116 | |
| 117 | if (!result) return res.status(404).json({ message: "no Data Found." }) |
| 118 | |
| 119 | let response = await Email.aggregate([ |
| 120 | { $match: { _id: result._id } }, |
| 121 | { |
| 122 | $lookup: { |
| 123 | from: 'Leads', |
| 124 | localField: 'createByLead', |
| 125 | foreignField: '_id', |
| 126 | as: 'createByrefLead' |
| 127 | } |
| 128 | }, |
| 129 | { |
| 130 | $lookup: { |
| 131 | from: 'Contacts', |
| 132 | localField: 'createByContact', |
| 133 | foreignField: '_id', |
| 134 | as: 'createByRef' |
| 135 | } |
| 136 | }, |
| 137 | { |
| 138 | $lookup: { |
| 139 | from: 'User', |
| 140 | localField: 'sender', |
| 141 | foreignField: '_id', |
| 142 | as: 'users' |
| 143 | } |
| 144 | }, |
| 145 | { $unwind: { path: '$users', preserveNullAndEmptyArrays: true } }, |
| 146 | { $unwind: { path: '$createByRef', preserveNullAndEmptyArrays: true } }, |
| 147 | { $unwind: { path: '$createByrefLead', preserveNullAndEmptyArrays: true } }, |
| 148 | { $match: { 'users.deleted': false } }, |
| 149 | { |
| 150 | $addFields: { |
| 151 | senderEmail: '$users.username', |
| 152 | deleted: { |
| 153 | $cond: [ |
| 154 | { $eq: ['$createByRef.deleted', false] }, |
| 155 | '$createByRef.deleted', |
| 156 | { $ifNull: ['$createByrefLead.deleted', false] } |
| 157 | ] |
| 158 | }, |
| 159 | createByName: { |
| 160 | $cond: { |
| 161 | if: '$createByRef', |
| 162 | then: { $concat: ['$createByRef.title', ' ', '$createByRef.firstName', ' ', '$createByRef.lastName'] }, |
| 163 | else: { $concat: ['$createByrefLead.leadName'] } |
| 164 | } |
| 165 | }, |
| 166 | } |
| 167 | }, |
| 168 | { |
| 169 | $project: { |
| 170 | createByRef: 0, |
| 171 | createByrefLead: 0, |
| 172 | users: 0 |
| 173 | } |
| 174 | }, |
| 175 | ]) |
| 176 | |
| 177 | res.status(200).json(response[0]) |
| 178 | } catch (err) { |
| 179 | console.error('Failed :', err); |
| 180 | res.status(400).json({ err, error: 'Failed ' }); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | module.exports = { add, index, view } |