Browse Source

finished pin with contact and chat

unknown 1 year ago
parent
commit
b01fed0733
8 changed files with 52 additions and 15 deletions
  1. 0 14
      bin/server.js
  2. 16 0
      controllers/chats.js
  3. 16 0
      controllers/contacts.js
  4. 4 0
      model/schemas/chat.js
  5. 4 0
      model/schemas/contact.js
  6. 1 0
      routes/chats.js
  7. 2 1
      routes/contacts.js
  8. 9 0
      validation/contact.js

+ 0 - 14
bin/server.js

@@ -2,20 +2,6 @@ const app = require('../app');
 const db = require('../model/db');
 const createFolderIsExist = require('../helpers/create-directory');
 const PORT = process.env.PORT || 3000;
-const APP_NAME = process.env.APP_NAME;
-// const http = require('http');
-
-// setInterval(() => {
-// 	http
-// 		.get(`http://${APP_NAME}.herokuapp.com`, function (res) {
-// 			res.on('data', function (_chunk) {
-// 				console.log('HEROKU RESPONSE: PINGED EVERY 25 MINUTES');
-// 			});
-// 		})
-// 		.on('error', function (_err) {
-// 			console.log('HEROKU RESPONSE: NOT PINGED');
-// 		});
-// }, 25 * 60 * 1000);
 
 db.then(() => {
 	app.listen(PORT, async () => {

+ 16 - 0
controllers/chats.js

@@ -219,6 +219,21 @@ const seenChat = async (req, res, next) => {
 	}
 };
 
+const pinChat = async (req, res, next) => {
+	try {
+		const { id, pinned } = req.body;
+		const userId = req.user.id;
+		await ChatModel.update(id, userId, { pinned });
+		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;
@@ -263,6 +278,7 @@ module.exports = {
 	muteChat,
 	sortChat,
 	seenChat,
+	pinChat,
 	typingChat,
 	getChatById,
 };

+ 16 - 0
controllers/contacts.js

@@ -77,6 +77,21 @@ const addContact = async (req, res, next) => {
 	}
 };
 
+const pinContact = async (req, res, next) => {
+	try {
+		const { id, pinned } = req.body;
+		const userId = req.user.id;
+		await ContactModel.update(id, userId, { pinned });
+		return res.status(200).json({
+			status: 'success',
+			code: 200,
+			data: {},
+		});
+	} catch (e) {
+		next(e);
+	}
+};
+
 const updateContact = async (req, res, next) => {
 	try {
 		const { id, _id, name, lastName, companionId } = req.body;
@@ -125,5 +140,6 @@ module.exports = {
 	getContactById,
 	addContact,
 	removeContact,
+	pinContact,
 	updateContact,
 };

+ 4 - 0
model/schemas/chat.js

@@ -83,6 +83,10 @@ const chatSchema = new Schema(
 			type: String,
 			default: false,
 		},
+		pinned: {
+			type: Boolean,
+			default: false,
+		},
 		owner: {
 			type: SchemaTypes.ObjectId,
 			ref: 'user',

+ 4 - 0
model/schemas/contact.js

@@ -34,6 +34,10 @@ const contactSchema = new Schema(
 			type: String,
 			default: null,
 		},
+		pinned: {
+			type: Boolean,
+			default: false,
+		},
 		owner: {
 			type: SchemaTypes.ObjectId,
 			ref: 'user',

+ 1 - 0
routes/chats.js

@@ -12,6 +12,7 @@ router
 	.patch('/sort/', guard, controllers.sortChat)
 	.patch('/seen/', guard, controllers.seenChat)
 	.patch('/typing/', guard, validation.typingChat, controllers.typingChat)
+	.patch('/pin/', guard, controllers.pinChat)
 	.delete('/both/:id', guard, controllers.removeChatForBoth);
 
 module.exports = router;

+ 2 - 1
routes/contacts.js

@@ -6,7 +6,8 @@ const validation = require('../validation/contact');
 router
 	.get('/', guard, controllers.listContacts)
 	.post('/', guard, validation.createContact, controllers.addContact)
-	.patch('/', guard, validation.updateContact, controllers.updateContact);
+	.patch('/', guard, validation.updateContact, controllers.updateContact)
+	.patch('/pin/', guard, validation.updatePin, controllers.pinContact);
 
 router
 	.get('/:number', guard, controllers.getContactById)

+ 9 - 0
validation/contact.js

@@ -14,6 +14,11 @@ const schemaUpdateContact = Joi.object({
 	companionId: Joi.string().min(1).required(),
 });
 
+const schemaUpdatePin = Joi.object({
+	id: Joi.string().required(),
+	pinned: Joi.boolean().required(),
+});
+
 module.exports.createContact = (req, _res, next) => {
 	return validate(schemaCreateContact, req.body, next);
 };
@@ -21,3 +26,7 @@ module.exports.createContact = (req, _res, next) => {
 module.exports.updateContact = (req, _res, next) => {
 	return validate(schemaUpdateContact, req.body, next);
 };
+
+module.exports.updatePin = (req, _res, next) => {
+	return validate(schemaUpdatePin, req.body, next);
+};