Skip to content

Commit

Permalink
fix(server): solved message controller types
Browse files Browse the repository at this point in the history
fixes #112
  • Loading branch information
roman-ojha committed Jul 1, 2022
1 parent a8d24a4 commit 4eab149
Show file tree
Hide file tree
Showing 7 changed files with 1,655 additions and 876 deletions.
28 changes: 14 additions & 14 deletions client/src/interface/userDocument.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export type UserDocumentMessages = {
lastMessageOn: Date;
messageToId: string;
roomID: string;
message: [{ senderId: string; content: string; date: Date }];
// roomID: string;
message: { senderId: string; content: string; date: Date }[];
};

export type UserDocumentPostsComments = {
Expand All @@ -22,17 +22,17 @@ export type UserDocumentPosts = {
};
likes: {
No: number;
by: [
{
user: string;
}
];
by:
| {
user: string;
}[]
| [];
};
comments: {
No: number;
by: [UserDocumentPostsComments];
by: UserDocumentPostsComments[] | [];
};
date: Date;
date?: Date;
};

export interface RootUserNewPosts {
Expand Down Expand Up @@ -98,15 +98,15 @@ export default interface UserDocument {
birthday: UserDocumentBirthday;
date: Date;
gender: string;
messages: [UserDocumentMessages];
messages: UserDocumentMessages[];
followersNo: number;
followers: [UserDocumentFollower];
followers: UserDocumentFollower[];
followingNo: number;
following: [UserDocumentFollowing];
following: UserDocumentFollowing[];
friendsNo: number;
friends: [UserDocumentFriends];
friends: UserDocumentFriends[];
postNo: number;
posts: [UserDocumentPosts];
posts: UserDocumentPosts[];
stories: UserDocumentStories;
tokens: [
{
Expand Down
174 changes: 87 additions & 87 deletions controllers/message.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,90 +6,90 @@ import { UserDocumentMessages } from "../interface/userDocument.js";
import SchemaMethodInstance from "../interface/userSchemaMethods.js";

export default {
createMessage: async (req: Request, res: Response): Promise<object> => {
try {
const rootUser = req.rootUser;
const receiverUser = req.body.receiver;
if (!req.body.receiver) {
return res.status(401).json({ error: "receiver Doesn't exist" });
}
const receiverExist = await userDetail.findOne(
{
// searching that user to message if it exist
userID: receiverUser,
},
{
email: 1,
userID: 1,
name: 1,
}
);
if (!receiverExist) {
return res.status(400).json({ error: "User doesn't exist" });
}
const messageExist = await userDetail.findOne(
{
userID: rootUser.userID,
messages: {
$elemMatch: {
messageTo: receiverUser,
},
},
},
{
email: 1,
userID: 1,
name: 1,
}
);
if (!messageExist) {
// if initialize message doesn't exist then we have to create a field for both user
const resSaveRootMsg = await userDetail.updateOne(
// creating and saving message to rootUser
{
userID: rootUser.userID,
},
{
$push: {
messages: {
messageTo: receiverUser,
receiverPicture: receiverExist.picture,
message: [],
},
},
}
);
const resSaverReciverMsg = await userDetail.updateOne(
// creating and saving message to rootUser
{
userID: receiverUser,
},
{
$push: {
messages: {
messageTo: rootUser.userID,
receiverPicture: rootUser.picture,
message: [],
},
},
}
);
if (resSaverReciverMsg && resSaveRootMsg) {
return res.status(200).json({ message: "message created" });
} else {
return res.status(500).json({ error: "server error" });
}
} else {
return res
.status(200)
.json({ message: "Message already been created" });
}
} catch (err) {
return res
.status(500)
.json({ error: "Server Error!!, Please Try again later" });
}
},
// createMessage: async (req: Request, res: Response): Promise<object> => {
// try {
// const rootUser = req.rootUser;
// const receiverUser = req.body.receiver;
// if (!req.body.receiver) {
// return res.status(401).json({ error: "receiver Doesn't exist" });
// }
// const receiverExist = await userDetail.findOne(
// {
// // searching that user to message if it exist
// userID: receiverUser,
// },
// {
// email: 1,
// userID: 1,
// name: 1,
// }
// );
// if (!receiverExist) {
// return res.status(400).json({ error: "User doesn't exist" });
// }
// const messageExist = await userDetail.findOne(
// {
// userID: rootUser.userID,
// messages: {
// $elemMatch: {
// messageTo: receiverUser,
// },
// },
// },
// {
// email: 1,
// userID: 1,
// name: 1,
// }
// );
// if (!messageExist) {
// // if initialize message doesn't exist then we have to create a field for both user
// const resSaveRootMsg = await userDetail.updateOne(
// // creating and saving message to rootUser
// {
// userID: rootUser.userID,
// },
// {
// $push: {
// messages: {
// messageToId: receiverUser,
// lastMessageOn: new Date(),
// message: [],
// },
// },
// }
// );
// const resSaverReciverMsg = await userDetail.updateOne(
// // creating and saving message to rootUser
// {
// userID: receiverUser,
// },
// {
// $push: {
// messages: {
// messageToId: rootUser.userID,
// receiverPicture: rootUser.picture,
// message: [],
// },
// },
// }
// );
// if (resSaverReciverMsg && resSaveRootMsg) {
// return res.status(200).json({ message: "message created" });
// } else {
// return res.status(500).json({ error: "server error" });
// }
// } else {
// return res
// .status(200)
// .json({ message: "Message already been created" });
// }
// } catch (err) {
// return res
// .status(500)
// .json({ error: "Server Error!!, Please Try again later" });
// }
// },
getSingleUserMessage: async (
req: Request,
res: Response
Expand Down Expand Up @@ -151,7 +151,7 @@ export default {
$push: {
messages: {
messageToId: messageToId,
// roomID: roomID,
lastMessageOn: new Date(),
message: [],
},
},
Expand All @@ -166,7 +166,7 @@ export default {
$push: {
messages: {
messageToId: rootUser.id,
// roomID: roomID,
lastMessageOn: new Date(),
message: [],
},
},
Expand Down Expand Up @@ -273,7 +273,7 @@ export default {
const newObj = {
lastMessageOn: obj.lastMessageOn,
messageToId: obj.messageToId,
roomID: obj.roomID,
// roomID: obj.roomID,
message: obj.message,
receiverPicture: newUser[0].picture,
messageToUserId: newUser[0].userID,
Expand Down
3 changes: 2 additions & 1 deletion controllers/storage.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import storage from "../db/userStorageConnection.js";
import ResponseObject from "../interface/responseObject";
import { Request, Response } from "express";
import constants from "../constants/index.js";
import { UserDocumentPosts } from "../interface/userDocument.js";
const bucket = storage.bucket();

export default {
Expand Down Expand Up @@ -89,7 +90,7 @@ export default {
)}?alt=media&token=${picToken}`;
const postID = crypto.randomBytes(16).toString("hex");
const today = new Date();
const userPostDetail = {
const userPostDetail = <UserDocumentPosts>{
id: postID,
caption: caption,
picture: {
Expand Down
19 changes: 9 additions & 10 deletions interface/userDocument.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Document } from "mongoose";
import { Interface } from "readline";

export type UserDocumentMessages = {
lastMessageOn: Date;
messageToId: string;
roomID: string;
message: [{ senderId: string; content: string; date: Date }];
// roomID: string;
message: { senderId: string; content: string; date: Date }[] | [];
};

export type UserDocumentPostsComments = {
Expand All @@ -25,17 +24,17 @@ export type UserDocumentPosts = {
};
likes: {
No: number;
by: [
{
user: string;
}
];
by:
| {
user: string;
}[]
| [];
};
comments: {
No: number;
by: [UserDocumentPostsComments];
by: UserDocumentPostsComments[] | [];
};
date: Date;
date?: Date;
};

export interface RootUserNewPosts {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"yarn": "1.x"
},
"dependencies": {
"@types/redis": "^4.0.11",
"axios": "^0.27.2",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
Expand Down Expand Up @@ -63,6 +62,7 @@
"vercel": "^24.2.4"
},
"devDependencies": {
"@types/redis": "^4.0.11",
"@types/bcryptjs": "^2.4.2",
"@types/cookie-parser": "^1.4.3",
"@types/express": "^4.17.13",
Expand Down
10 changes: 5 additions & 5 deletions routes/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import messageController from "../controllers/message.controller.js";
const messageRoute = express.Router();

// this route had implemented into "/u/getMessage" route when message doesn't exist
messageRoute.post(
"/u/createMessage",
authenticate,
messageController.createMessage
);
// messageRoute.post(
// "/u/createMessage",
// authenticate,
// messageController.createMessage
// );

messageRoute.post(
"/u/getMessage",
Expand Down
Loading

0 comments on commit 4eab149

Please sign in to comment.