güncelleme
This commit is contained in:
parent
ab3acdd046
commit
73a824eb24
2
.env
Normal file
2
.env
Normal file
@ -0,0 +1,2 @@
|
||||
JWT_TOKEN_SECRET="iueamsuialmieu a iemly üaieie4u"
|
||||
CRYPTO_KEY="1234"
|
||||
2
app.js
2
app.js
@ -1,5 +1,7 @@
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
const dotenv = require('dotenv');
|
||||
dotenv.config();
|
||||
|
||||
// DB
|
||||
const sequelize = require('./data/db');
|
||||
|
||||
18
controllers/check.js
Normal file
18
controllers/check.js
Normal file
@ -0,0 +1,18 @@
|
||||
const result_check = require("../middlewares/result_check");
|
||||
const Members = require("../models/members")
|
||||
|
||||
exports.isMembersToChat = async function (chatId, accountId) {
|
||||
try {
|
||||
const result = await Members.findOne({
|
||||
where: {
|
||||
chatId: chatId,
|
||||
accountId: accountId
|
||||
}
|
||||
}).then(result_check);
|
||||
console.log(chatId, accountId, result);
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
62
controllers/check/account.js
Normal file
62
controllers/check/account.js
Normal file
@ -0,0 +1,62 @@
|
||||
const jwt = require('jsonwebtoken');
|
||||
const Accounts = require("../../models/accounts")
|
||||
|
||||
exports.isAccountElbab = async function (elbab_id) {
|
||||
try {
|
||||
const result = await Accounts.findOne({
|
||||
where: {
|
||||
elbab_id: elbab_id
|
||||
}
|
||||
});
|
||||
if (result) {
|
||||
return { Success: true, Data: result };
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
exports.registerAccountForElbab = async function (data) {
|
||||
try {
|
||||
const isComplete = await Accounts.create({
|
||||
elbab_id: data.id,
|
||||
name: data.name,
|
||||
surname: data.surname,
|
||||
username: data.username,
|
||||
email: data.email
|
||||
});
|
||||
if (isComplete) {
|
||||
const jwtGenerate = await jwt.sign({
|
||||
id: isComplete.id,
|
||||
elbab_id: isComplete.elbab_id,
|
||||
name: isComplete.name,
|
||||
surname: isComplete.surname,
|
||||
username: isComplete.username,
|
||||
email: isComplete.email
|
||||
}, process.env.JWT_TOKEN_SECRET);
|
||||
return (jwtGenerate) ? { Token: jwtGenerate, Success: true } : isComplete;
|
||||
} else {
|
||||
return false;
|
||||
};
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
exports.loginAccountForElbab = async function (data) {
|
||||
try {
|
||||
const jwtGenerate = await jwt.sign({
|
||||
id: data.id,
|
||||
elbab_id: data.elbab_id,
|
||||
name: data.name,
|
||||
surname: data.surname,
|
||||
username: data.username,
|
||||
email: data.email
|
||||
}, process.env.JWT_TOKEN_SECRET);
|
||||
return (jwtGenerate) ? { Token: jwtGenerate, Success: true } : isComplete;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
16
controllers/post/account.js
Normal file
16
controllers/post/account.js
Normal file
@ -0,0 +1,16 @@
|
||||
const result_check = require("../../middlewares/result_check");
|
||||
const Accounts = require("../../models/accounts");
|
||||
|
||||
exports.destroy = async function (aid) {
|
||||
try {
|
||||
const isComplete = await Accounts.destroy({
|
||||
where: {
|
||||
id: aid
|
||||
}
|
||||
}).then(result_check);
|
||||
return isComplete;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@ -1,19 +1,85 @@
|
||||
const result_check = require("../../middlewares/result_check");
|
||||
const Chats = require("../../models/chats");
|
||||
const Members = require("../../models/members");
|
||||
const Messages = require("../../models/messages");
|
||||
const { isMembersToChat } = require("../check");
|
||||
|
||||
exports.create = async function (title, type, aid) {
|
||||
try {
|
||||
let isComplete = false;
|
||||
const isCompleteChat = await Chats.build({
|
||||
title: title,
|
||||
type: type
|
||||
});
|
||||
const resultChat = await isCompleteChat.save();
|
||||
|
||||
if (resultChat) {
|
||||
const isCompleteMember = await Members.create({
|
||||
chatId: resultChat.id,
|
||||
accountId: aid
|
||||
}).then(result_check);
|
||||
isComplete = resultChat && isCompleteMember;
|
||||
};
|
||||
return isComplete;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
exports.join = async function (chatId, accountId) {
|
||||
try {
|
||||
const isComplete = await Chats.create({
|
||||
const isMemberAlright = await isMembersToChat(chatId, accountId);
|
||||
if (isMemberAlright == true) {
|
||||
return true;
|
||||
};
|
||||
|
||||
const isComplete = await Members.create({
|
||||
chatId: chatId,
|
||||
accountId: accountId
|
||||
}).then(res => {
|
||||
if (res) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}).then(result_check);
|
||||
return isComplete;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return false;
|
||||
};
|
||||
};
|
||||
|
||||
exports.leave = async function (chatId, accountId) {
|
||||
try {
|
||||
const isMemberAlright = await isMembersToChat(chatId, accountId);
|
||||
if (isMemberAlright == false) {
|
||||
return false;
|
||||
};
|
||||
|
||||
const isComplete = await Members.destroy({
|
||||
where: {
|
||||
chatId: chatId,
|
||||
accountId: accountId
|
||||
}
|
||||
}).then(result_check);
|
||||
return isComplete;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
exports.clear = async function (cid) {
|
||||
try {
|
||||
const isMemberAlright = await isMembersToChat(chatId, accountId);
|
||||
if (isMemberAlright == false) {
|
||||
return false;
|
||||
};
|
||||
|
||||
const isComplete = await Messages.destroy({
|
||||
where: {
|
||||
chatId: cid
|
||||
}
|
||||
}).then(result_check);
|
||||
return isComplete;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@ -1,21 +1,72 @@
|
||||
const Messages = require("../../models/messages")
|
||||
const result_check = require("../../middlewares/result_check");
|
||||
const Messages = require("../../models/messages");
|
||||
const { isMembersToChat } = require("../check");
|
||||
|
||||
exports.send = async function (text, chatId, accountId, time) {
|
||||
try {
|
||||
const isMemberAlright = await isMembersToChat(chatId, accountId);
|
||||
console.log(isMemberAlright);
|
||||
console.log(text, chatId, accountId, time);
|
||||
if (isMemberAlright == false) {
|
||||
return false;
|
||||
};
|
||||
|
||||
const isComplete = await Messages.create({
|
||||
text: text,
|
||||
chatId: chatId,
|
||||
accountId: accountId,
|
||||
createdAt: time
|
||||
}).then(res => {
|
||||
if (res) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
});
|
||||
}).then(result_check);
|
||||
return isComplete;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return false;
|
||||
};
|
||||
};
|
||||
|
||||
exports.destroy = async function (messageId, accountId) {
|
||||
try {
|
||||
const isComplete = await Messages.destroy({
|
||||
where: {
|
||||
id: messageId,
|
||||
accountId: accountId
|
||||
}
|
||||
}).then(result_check);
|
||||
return isComplete;
|
||||
} catch (error) {
|
||||
error.log(error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
exports.destroyAll = async function (cid, aid) {
|
||||
try {
|
||||
const isComplete = await Messages.destroy({
|
||||
where: {
|
||||
accountId: aid,
|
||||
chatId: cid
|
||||
}
|
||||
}).then(result_check);
|
||||
return isComplete;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
exports.edit = async function (text, messageId, time) {
|
||||
try {
|
||||
const isComplete = await Messages.update({
|
||||
text: text,
|
||||
updatedAt: time
|
||||
}, {
|
||||
where: {
|
||||
id: messageId
|
||||
}
|
||||
}).then(result_check);
|
||||
return isComplete;
|
||||
} catch (error) {
|
||||
error.log(error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@ -1,3 +0,0 @@
|
||||
module.exports = async function (data) {
|
||||
|
||||
};
|
||||
7
middlewares/result_check.js
Normal file
7
middlewares/result_check.js
Normal file
@ -0,0 +1,7 @@
|
||||
module.exports = res => {
|
||||
if (res) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
16
middlewares/token.js
Normal file
16
middlewares/token.js
Normal file
@ -0,0 +1,16 @@
|
||||
exports.isVerification = async function (req, res, next) {
|
||||
try {
|
||||
const token = req.headers.authorization;
|
||||
if (token && req.url != '/account') {
|
||||
console.log(token);
|
||||
next();
|
||||
|
||||
} else {
|
||||
res.send(false);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.send(false);
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
const express = require('express');
|
||||
const { chats } = require('../controllers/get/chats');
|
||||
const { messages } = require('../controllers/get/messages');
|
||||
const { isVerification } = require('../middlewares/token');
|
||||
const { registerAccountForElbab, loginAccountForElbab } = require('../controllers/check/account');
|
||||
const router = express();
|
||||
|
||||
//CHATS
|
||||
@ -8,9 +10,25 @@ router.get('/chats', async (req, res) => {
|
||||
res.json(await chats());
|
||||
});
|
||||
|
||||
router.get('/chats/ismember', async (req, res) => {
|
||||
// isMember Chat
|
||||
const cid = req.query.cid; // Chat ID
|
||||
const aid = req.query.aid; // Account ID
|
||||
|
||||
const result = await isMembersToChat(cid, aid);
|
||||
res.send(result);
|
||||
})
|
||||
|
||||
//MESSAGES
|
||||
router.get('/messages', async (req, res) => {
|
||||
res.json(await messages(req.query.chatId));
|
||||
});
|
||||
|
||||
// ACCOUNT
|
||||
router.get('/account', isVerification, async (req, res) => {
|
||||
const token = req.headers.authorization;
|
||||
|
||||
res.send("");
|
||||
})
|
||||
|
||||
module.exports = router;
|
||||
109
routers/post.js
109
routers/post.js
@ -1,23 +1,106 @@
|
||||
const express = require('express');
|
||||
const { send } = require('../controllers/post/messages');
|
||||
|
||||
const ControllerMessage = require('../controllers/post/messages');
|
||||
// { send, destroy, edit, destroyAll }
|
||||
|
||||
const ControllerChats = require('../controllers/post/chats');
|
||||
// { leave, join, create, clear }
|
||||
|
||||
const ControllerAccount = require('../controllers/post/account');
|
||||
const { isMembersToChat } = require('../controllers/check');
|
||||
const { isVerification } = require('../middlewares/token');
|
||||
// { destroy }
|
||||
|
||||
const router = express();
|
||||
|
||||
// CHATS
|
||||
router.post('/chats/join', (req, res) => {
|
||||
|
||||
// CHATS
|
||||
router.post('/chats/create', async (req, res) => {
|
||||
const title = req.body.title; // Chat TITLE
|
||||
const type = 1; // Chat TYPE
|
||||
const aid = req.body.aid; // Account ID
|
||||
|
||||
const result = await ControllerChats.create(title, type, aid);
|
||||
res.send(result);
|
||||
});
|
||||
|
||||
// MESSAGE
|
||||
router.post('/message/send', async (req, res) => {
|
||||
const message = req.body.msg;
|
||||
const chatId = req.body.cid;
|
||||
const account = req.body.aid;
|
||||
const time = req.body.time;
|
||||
router.post('/chats/join', async (req, res) => {
|
||||
// Join Chat
|
||||
const cid = req.body.cid; // Chat ID
|
||||
const aid = req.body.aid; // Account ID
|
||||
|
||||
console.log(message, account, chatId, time);
|
||||
|
||||
const result = await send(message, chatId, account, time);
|
||||
const result = await ControllerChats.join(cid, aid);
|
||||
res.send(result);
|
||||
})
|
||||
});
|
||||
|
||||
router.post('/chats/leave', async (req, res) => {
|
||||
// Leave Chat
|
||||
const cid = req.body.cid; // Chat ID
|
||||
const aid = req.body.aid; // Account ID
|
||||
|
||||
const result = await ControllerChats.leave(cid, aid);
|
||||
res.send(result);
|
||||
});
|
||||
|
||||
router.post('/chats/clear', async (req, res) => {
|
||||
// Clear Chat
|
||||
const cid = req.body.cid // Chat ID
|
||||
|
||||
const result = await ControllerChats.clear(cid);
|
||||
res.send(result);
|
||||
});
|
||||
|
||||
// MESSAGES
|
||||
router.post('/message/send', isVerification, async (req, res) => {
|
||||
// Send Message
|
||||
const msg = req.body.msg; // Message TEXT
|
||||
const cid = req.body.cid; // Chat ID
|
||||
const aid = req.body.aid; // Account ID
|
||||
const time = req.body.time; // Message Created Time
|
||||
|
||||
const result = await ControllerMessage.send(msg, cid, aid, time);
|
||||
res.send(result);
|
||||
});
|
||||
|
||||
router.post('/message/destroy', async (req, res) => {
|
||||
// Destroy Message
|
||||
const mid = req.body.mid; // Message ID
|
||||
const aid = req.body.aid; // Account ID
|
||||
|
||||
const result = await ControllerMessage.destroy(mid, aid);
|
||||
res.send(result);
|
||||
});
|
||||
|
||||
router.post('/message/destroyall', async (req, res) => {
|
||||
// Destroy All Message Then Chat
|
||||
const cid = req.body.cid; // Chat ID
|
||||
const aid = req.body.aid; // Account ID
|
||||
|
||||
const result = await ControllerMessage.destroyAll(cid, aid);
|
||||
res.send(result);
|
||||
});
|
||||
|
||||
router.post('/message/edit', async (req, res) => {
|
||||
// Edit Message
|
||||
const mid = req.body.mid; // Message ID
|
||||
const msg = req.body.msg; // Message TEXT
|
||||
const time = req.body.time; // Message Update Time
|
||||
|
||||
const result = await ControllerMessage.edit(msg, mid, time);
|
||||
res.send(result);
|
||||
});
|
||||
|
||||
// ACCOUNTS
|
||||
router.post('/account/clear', async (req, res) => {
|
||||
// Clear Account
|
||||
});
|
||||
|
||||
router.post('/account/destroy', async (req, res) => {
|
||||
// Destroy Account
|
||||
const aid = req.body.aid;
|
||||
|
||||
const result = await ControllerAccount.destroy(aid);
|
||||
res.send(result);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Loading…
Reference in New Issue
Block a user