Browse Source

done notificatioN

unknown 2 years ago
parent
commit
ba2b3eb402
6 changed files with 43 additions and 9 deletions
  1. 3 3
      app.js
  2. 27 3
      controllers/chats.js
  3. 1 1
      controllers/messages.js
  4. 2 1
      controllers/user.js
  5. 8 0
      model/schemas/chat.js
  6. 2 1
      routes/chats.js

+ 3 - 3
app.js

@@ -14,9 +14,6 @@ const app = express();
 const FOLDER_IMAGES = process.env.DIR_IMAGES;
 const FOLDER_NOTIFICATIONS = process.env.DIR_NOTIFICATIONS;
 
-app.use(express.static(path.join(__dirname, FOLDER_IMAGES)));
-app.use(express.static(path.join(__dirname, FOLDER_NOTIFICATIONS)));
-
 const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short';
 
 app.use(helmet());
@@ -24,6 +21,9 @@ app.use(logger(formatsLogger));
 app.use(cors());
 app.use(express.json());
 
+app.use(express.static(path.join(__dirname, FOLDER_IMAGES)));
+app.use(express.static(path.join(__dirname, FOLDER_NOTIFICATIONS)));
+
 app.use('/api/chats', apiLimiter, chatsRouter);
 app.use('/api/messages', apiLimiter, messagesRouter);
 app.use('/api/contacts', apiLimiter, contactsRouter);

+ 27 - 3
controllers/chats.js

@@ -158,11 +158,30 @@ const seenChat = async (req, res, next) => {
 		const id = req.body.id;
 		const userId = req.user.id;
 		const isChat = await ChatModel.getByField(id, userId);
+		const isCompanionChat = await ChatModel.getByField(userId, id);
 		const isMessage = await MessageModel.getList(userId, {});
-		if (isChat && isMessage) {
-			const { _id } = isChat;
+		if (isChat && isMessage && isCompanionChat) {
 			const { total } = isMessage;
-			await ChatModel.update(_id, { seen: total });
+			await ChatModel.update(isChat._id, { seen: total, watched: false });
+			await ChatModel.update(isCompanionChat._id, { watched: true });
+			return res.status(200).json({
+				status: 'success',
+				code: 200,
+				data: {},
+			});
+		}
+	} catch (e) {
+		next(e);
+	}
+};
+
+const typingChat = async (req, res, next) => {
+	try {
+		const { id, typing } = req.body;
+		const userId = req.user.id;
+		const isCompanionChat = await ChatModel.getByField(userId, id);
+		if (isCompanionChat) {
+			await ChatModel.update(isCompanionChat._id, { typing });
 			return res.status(200).json({
 				status: 'success',
 				code: 200,
@@ -189,6 +208,8 @@ const chatById = async (req, res, next) => {
 				mute,
 				seen,
 				total,
+				watched,
+				typing,
 				number,
 				_id,
 				companionId,
@@ -209,6 +230,8 @@ const chatById = async (req, res, next) => {
 					mute,
 					seen,
 					total,
+					watched,
+					typing,
 					number,
 					_id,
 					companionId,
@@ -229,5 +252,6 @@ module.exports = {
 	startChat,
 	muteChat,
 	seenChat,
+	typingChat,
 	chatById,
 };

+ 1 - 1
controllers/messages.js

@@ -65,7 +65,7 @@ const sentMessage = async (req, res, next) => {
 				owner: id,
 			});
 			const { total } = await MessageModel.getList(userId, {});
-			await ChatModel.update(isChat._id, { total });
+			await ChatModel.update(isChat._id, { total, seen: total });
 			const { total: Total } = await MessageModel.getList(id, {});
 			await ChatModel.update(isCompanionChat._id, { total: Total });
 			return res.status(201).json({

+ 2 - 1
controllers/user.js

@@ -114,10 +114,11 @@ const getCurrent = async (req, res, next) => {
 			});
 		const id = req.user.id;
 		await UserModel.updateUser(id, { online: true });
+		const updatedUser = await UserModel.findById(id);
 		return res.status(200).json({
 			status: 'success',
 			code: 200,
-			data: user,
+			data: updatedUser,
 		});
 	} catch (e) {
 		next(e);

+ 8 - 0
model/schemas/chat.js

@@ -42,6 +42,14 @@ const chatSchema = new Schema(
 			type: Number,
 			default: 0,
 		},
+		watched: {
+			type: Boolean,
+			default: false,
+		},
+		typing: {
+			type: Boolean,
+			default: false,
+		},
 		number: {
 			type: String,
 			min: 8,

+ 2 - 1
routes/chats.js

@@ -9,6 +9,7 @@ router
 	.get('/:companionId', guard, controllers.chatById)
 	.post('/', guard, validation.startChat, controllers.startChat)
 	.post('/mute/', guard, controllers.muteChat)
-	.post('/seen/', guard, controllers.seenChat);
+	.post('/seen/', guard, controllers.seenChat)
+	.post('/typing/', guard, controllers.typingChat);
 
 module.exports = router;