Skip to content

Commit 71c4043

Browse files
authored
Merge branch 'main' into fix-1197
2 parents 0bea348 + 2dc55f2 commit 71c4043

File tree

11 files changed

+284
-13
lines changed

11 files changed

+284
-13
lines changed

backend/.env.example

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,13 @@ CLOUDINARY_CLOUD_NAME=
66
CLOUDINARY_API_KEY=
77
CLOUDINARY_API_SECRET=
88

9+
10+
EMAIL_USER=
11+
EMAIL_PASS=
12+
13+
COLLABORATION_PUBLIC_URL=
14+
15+
VITE_SERVER_DOMAIN=https://pjt-blog.onrender.com
16+
917
VITE_SERVER_DOMAIN=https://code-a2z.onrender.com
18+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import collaboration from "../Models/collaboration.model.js"
2+
import Project from "../Models/project.model.js";
3+
import crypto from "crypto";
4+
import User from "../Models/user.model.js";
5+
import transporter from "../config/nodemailer.js";
6+
import localtunnel from "localtunnel";
7+
8+
9+
export const invitationToCollaborate = async(req, res)=>{
10+
const userid = req.user;
11+
const {project_id} = req.body;
12+
13+
try {
14+
const user = await User.findById(userid);
15+
if(!user) return res.status(404).json({error: "User not found!"});
16+
const projectToCollaborate = await Project.findOne({project_id: project_id}).populate("author", "personal_info.email");
17+
if(!projectToCollaborate) return res.status(404).json({error: "Project not found!"});
18+
19+
const authorEmail = projectToCollaborate.author.personal_info.email;
20+
const token = crypto.randomBytes(16).toString('hex');
21+
22+
// const baseUrl = global.publicUrl || `http://localhost:${process.env.PORT || 8000}`;
23+
const baseUrl = process.env.COLLABORATION_PUBLIC_URL;
24+
25+
const acceptLink = `${baseUrl}/api/collaborate/accept/${token}`;
26+
const rejectLink = `${baseUrl}/api/collaborate/reject/${token}`;
27+
console.log(acceptLink, rejectLink);
28+
const mailOptions = {
29+
from: process.env.EMAIL_USER,
30+
to: authorEmail,
31+
subject: "Collaboration Invitation",
32+
html: `<p>Hi,</p>
33+
<p>${user.personal_info.fullname} has requested to collaborate on your project "${projectToCollaborate.title}".</p>
34+
<p><a href="${acceptLink}">Accept Invitation</a> | <a href="${rejectLink}">Reject Invitation</a></p>
35+
<p>Thank you!</p>`
36+
37+
};
38+
await transporter.sendMail(mailOptions, (error, info)=>{
39+
if(error){
40+
console.error("Error sending email:", error);
41+
return res.status(500).json({error: "Failed to send invitation email"});
42+
}
43+
console.log("Email sent:", info.response);
44+
})
45+
const collaborationData = new collaboration({
46+
user_id: userid,
47+
project_id: project_id,
48+
author_id: projectToCollaborate.author,
49+
status: "pending",
50+
token : token
51+
})
52+
await collaborationData.save();
53+
return res.status(200).json({message: "Invitation sent successfully!"});
54+
55+
} catch (error) {
56+
console.log(error);
57+
return res.status(500).json({error: "Internal Server Error"});
58+
}
59+
}
60+
61+
62+
63+
64+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import mongoose from 'mongoose';
2+
import collaborationSchema from '../Schemas/collaboration.schema.js';
3+
4+
const collaboration = mongoose.model('collaborators', collaborationSchema);
5+
6+
export default collaboration;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import express from "express";
2+
3+
import { authenticateUser } from "../../Middlewares/auth.middleware.js";
4+
5+
import {invitationToCollaborate} from "../../Controllers/collaboration.controller.js";
6+
const collaborationRoutes = express.Router();
7+
8+
collaborationRoutes.post("/invite", authenticateUser, invitationToCollaborate);
9+
10+
11+
export default collaborationRoutes;

backend/Routes/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import userRoutes from './api/user.routes.js';
66
import notificationRoutes from './api/notification.routes.js';
77
import subscriberRoutes from './api/subscriber.routes.js';
88
import collectionRoutes from './api/collections.routes.js';
9+
import collaborationRoutes from './api/collaboration.routes.js';
10+
911

1012
const router = express.Router();
1113

@@ -16,4 +18,6 @@ router.use('/project', projectRoutes);
1618
router.use('/notification', notificationRoutes);
1719
router.use('/subscriber', subscriberRoutes);
1820
router.use("/collection", collectionRoutes);
21+
router.use('/collaboration', collaborationRoutes);
22+
1923
export default router;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {Schema} from 'mongoose';
2+
3+
4+
const collaborationSchema = Schema(
5+
{
6+
user_id:{
7+
type: Schema.Types.ObjectId,
8+
ref:'users',
9+
required: true,
10+
},
11+
project_id: {
12+
type: String,
13+
ref:'projects',
14+
required: true,
15+
},
16+
17+
author_id:{
18+
type: Schema.Types.ObjectId,
19+
ref:'users',
20+
required: true,
21+
},
22+
status:{
23+
type: String,
24+
enum:["pending", "accepted", "rejected"],
25+
default:"pending",
26+
required: true
27+
},
28+
token:{
29+
type: String,
30+
required: true
31+
}
32+
},{timestamps: true}
33+
)
34+
35+
export default collaborationSchema;

backend/config/cloudinary.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ cloudinary.config({
99
secure: true
1010
});
1111

12-
export default cloudinary;
12+
export default cloudinary;

backend/config/nodemailer.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import nodemailer from 'nodemailer';
2+
3+
4+
const transporter = nodemailer.createTransport({
5+
host:'smtp.gmail.com',
6+
port: 587,
7+
secure: false, // true for 465, false for other ports
8+
auth :{
9+
user: process.env.EMAIL_USER, // your email address
10+
pass: process.env.EMAIL_PASS // your email password
11+
}
12+
})
13+
14+
export default transporter;

0 commit comments

Comments
 (0)