Browse Source

done message notification

unknown 2 năm trước cách đây
mục cha
commit
d638186670
7 tập tin đã thay đổi với 46 bổ sung4 xóa
  1. 2 0
      app.js
  2. 26 0
      controllers/chats.js
  3. 8 3
      controllers/messages.js
  4. 8 0
      model/schemas/chat.js
  5. BIN
      notifications/recive.mp3
  6. BIN
      notifications/send.mp3
  7. 2 1
      routes/chats.js

+ 2 - 0
app.js

@@ -12,8 +12,10 @@ const userRoute = require('./routes/user');
 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';
 

+ 26 - 0
controllers/chats.js

@@ -153,6 +153,27 @@ const muteChat = async (req, res, next) => {
 	}
 };
 
+const seenChat = async (req, res, next) => {
+	try {
+		const id = req.body.id;
+		const userId = req.user.id;
+		const isChat = await ChatModel.getByField(id, userId);
+		const isMessage = await MessageModel.getList(userId, {});
+		if (isChat && isMessage) {
+			const { _id } = isChat;
+			const { total } = isMessage;
+			await ChatModel.update(_id, { seen: total });
+			return res.status(200).json({
+				status: 'success',
+				code: 200,
+				data: {},
+			});
+		}
+	} catch (e) {
+		next(e);
+	}
+};
+
 const chatById = async (req, res, next) => {
 	try {
 		const userId = req.user.id;
@@ -166,6 +187,8 @@ const chatById = async (req, res, next) => {
 				avatarUrl,
 				color,
 				mute,
+				seen,
+				total,
 				number,
 				_id,
 				companionId,
@@ -184,6 +207,8 @@ const chatById = async (req, res, next) => {
 					color,
 					online,
 					mute,
+					seen,
+					total,
 					number,
 					_id,
 					companionId,
@@ -203,5 +228,6 @@ module.exports = {
 	listChats,
 	startChat,
 	muteChat,
+	seenChat,
 	chatById,
 };

+ 8 - 3
controllers/messages.js

@@ -39,10 +39,11 @@ const sentMessage = async (req, res, next) => {
 		const user = req.user;
 		const userId = user.id;
 		const companion = await UserModel.findById(id);
-		const isUser = await ChatModel.getByField(id, userId);
-		const isCompanion = await ChatModel.getByField(userId, id);
+		const isChat = await ChatModel.getByField(id, userId);
+		const isCompanionChat = await ChatModel.getByField(userId, id);
 		const { name, lastName, avatarUrl, color, number } = user;
-		if (companion && isUser && isCompanion) {
+
+		if (companion && isChat && isCompanionChat) {
 			const newMessage = await MessageModel.add({
 				message,
 				name,
@@ -63,6 +64,10 @@ const sentMessage = async (req, res, next) => {
 				companionId: userId,
 				owner: id,
 			});
+			const { total } = await MessageModel.getList(userId, {});
+			await ChatModel.update(isChat._id, { total });
+			const { total: Total } = await MessageModel.getList(id, {});
+			await ChatModel.update(isCompanionChat._id, { total: Total });
 			return res.status(201).json({
 				status: 'success',
 				code: 201,

+ 8 - 0
model/schemas/chat.js

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

BIN
notifications/recive.mp3


BIN
notifications/send.mp3


+ 2 - 1
routes/chats.js

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