123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- const express = require("express");
- const bodyParser = require("body-parser");
- const Sequelize = require("sequelize");
- const jwt = require("jsonwebtoken");
- const { Op } = Sequelize;
- const { graphqlHTTP } = require("express-graphql");
- const { buildSchema } = require("graphql");
- const cors = require("cors");
- const expressJwt = require("express-jwt");
- const sha1 = require("sha1");
- // const mailer = require("./smtpGmail");
- const app = express();
- app.use(bodyParser.json());
- app.use(express.static("myproject"));
- // app.use(errorHandler);
- app.use(bodyParser.urlencoded({ extended: false }));
- app.use(cors());
- const sequelize = new Sequelize("emrada2", "root", "Emmanuil2228125%", {
- timezone: "+03:00",
- host: "localhost",
- dialect: "mysql",
- });
- class User extends Sequelize.Model {
- get messages() {
- return (async () => {
- let autorForMessage = await this.getAutor();
- let partnerForMessage = await this.getPartner();
- return [...autorForMessage, ...partnerForMessage];
- })();
- }
- get chat_groups() {
- return (async () => {
- let autorForChatGroup = await this.getAutor();
- let partnerForChatGroup = await this.getPartner();
- return [...autorForChatGroup, ...partnerForChatGroup];
- })();
- }
- }
- User.init(
- {
- email: Sequelize.STRING,
- // password: Sequelize.STRING,
- login: Sequelize.STRING,
- password: {
- type: Sequelize.STRING,
- set(value) {
- this.setDataValue("password", sha1(value));
- },
- },
- },
- { sequelize, modelName: "user" }
- );
- class Message extends Sequelize.Model {
- get user() {
- return this.User();
- }
- }
- Message.init(
- { message: Sequelize.STRING },
- {
- scopes: {
- deleted: {
- where: {
- id: 1
- },
- },
- },
- sequelize,
- modelName: "message",
- }
- );
- class ChatGroup extends Sequelize.Model {}
- ChatGroup.init({}, { sequelize, modelName: "chat_group" });
- User.hasMany(Message, { as: "autorForMessage", foreignKey: "autorId" });
- User.hasMany(Message, { as: "partnerForMessage", foreignKey: "partnerId" });
- Message.belongsTo(User, { as: "autor", sourceKey: "autorId" });
- Message.belongsTo(User, { as: "partner", sourceKey: "partnerId" });
- User.hasMany(ChatGroup, { as: "autorForChatGroup", foreignKey: "autorId" });
- User.hasMany(ChatGroup, { as: "partnerForChatGroup", foreignKey: "partnerId" });
- ChatGroup.belongsTo(User, { as: "autor", sourceKey: "autorId" });
- ChatGroup.belongsTo(User, { as: "partner", sourceKey: "partnerId" });
- const secret = `7.!BMB?Y+Bc2vZE-Hb5YuCT6QvE^FN,JWN6M?_VtFXeC5dLtB!`;
- const authenticate = async ({ login, password }) => {
- const user = await User.findOne({ where: { login, password } });
- if (user) {
- const token = jwt.sign(
- { sub: { id: user.id, email: user.email, login: user.login } },
- secret
- );
- return token;
- }
- };
- var schema = buildSchema(`
- type Query {
- getLogin(login: String, password: String): String
- getUser(email: String): User
- getAllUsers: [User]
- getMessage(id: ID!): Message
- getAllMessages: [Message]
- getAllMessagesOneUser(id: ID!): [Message]
- getChatGroup(id: ID!): ChatGroup
- getAllChatGroups: [ChatGroup]
- getAllChatGroupsOneUser(id: ID!): [ChatGroup]
- }
- type Mutation {
- deleteUser(id: ID!): String
- createUser(email: String, login: String, password: String): User
- changePassword(email: String, password: String): User
- createMessage(message: String, autorId: String, partnerId: String): Message
- changeMessage(id: ID!, message: String): Message
- deleteMessage(id: ID!): String
- createChatGroup(autorId: String, partnerId: String): Message
- deleteChatGroup(id: ID!): String
- }
- type User {
- id: Int
- createdAt: String
- email: String
- login: String
- }
- type Message {
- id: Int
- createdAt: String
- message: String
- autorId: User
- partnerId: User
- }
- type ChatGroup {
- id: Int
- createdAt: String
- autorId: User
- partnerId: User
- }
- `);
- const getUser = async ({ email }) => await User.findOne({ where: { email } });
- const getLogin = async ({ login, password }) => {
- const passwordSha1 = sha1(password);
- const userFind = await User.findOne({
- where: { login, password: passwordSha1 },
- });
- return authenticate(userFind);
- };
- const createUser = async ({ email, login, password }) => {
- const wasUserCreated = await User.findOne({ where: { email } });
- if (!wasUserCreated) {
- const user = { email, login, password };
- const newUser = new User(user);
- await newUser.save();
- return await User.findOne({ where: { login } });
- } else console.error("error");
- };
- const deleteUser = async ({ id }) => {
- const userFind = await User.findByPk(id);
- if (userFind) {
- await User.destroy({ where: { id } });
- return "User deleted";
- } else return "User not find";
- };
- const changePassword = async ({ email, password }) => {
- var userFind = await User.findOne({ where: { email } });
- if (userFind) {
- const a = await User.update(
- { password, login: userFind.login },
- { where: { email } }
- );
- userFind.email = "The password has been change";
- console.log(a);
- return userFind;
- } else {
- userFind = { email: "Email not found" };
- return userFind;
- }
- };
- const getAllUsers = async () => await User.findAll();
- const getAllMessages = async () => {
- const foundAllMessages = await Message.findAll();
- for (var allMessages of foundAllMessages) {
- const autor = await User.findAll({ where: { id: allMessages.autorId } });
- const partner = await User.findAll({
- where: { id: allMessages.partnerId },
- });
- allMessages.autorId = autor[0];
- allMessages.partnerId = partner[0];
- }
- return foundAllMessages;
- };
- const getMessage = async ({ id }) => {
- const message = await Message.findOne({ where: { id } });
- const autor = await User.findOne({ where: { id: message.autorId } });
- const partner = await User.findOne({ where: { id: message.partnerId } });
- message.autorId = autor;
- message.partnerId = partner;
- return message;
- };
- const getAllMessagesOneUser = async ({ id }) => {
- const allMess = await Message.findAll();
- for (var key of allMess) {
- const autor = await User.findAll({ where: { id: key.autorId } });
- const partner = await User.findAll({ where: { id: key.partnerId } });
- const ma = autor[0].id === +id || partner[0].id === +id;
- if (ma === true) {
- const y = await Message.findAll({ where: { autorId: id } });
- const u = await Message.findAll({ where: { partnerId: id } });
- console.log(y);
- if ((y.length && u.length) !== 0) {
- const p = [...y, ...u];
- for (var key of p) {
- const r = await User.findAll({ where: { id: key.autorId } });
- const r1 = await User.findAll({ where: { id: key.partnerId } });
- key.autorId = r[0];
- key.partnerId = r1[0];
- }
- return p;
- } else if (y.length !== 0) {
- const pt = [...y];
- for (var key of pt) {
- const r = await User.findAll({ where: { id: key.partnerId } });
- key.autorId = autor[0];
- key.partnerId = r[0];
- }
- return pt;
- } else {
- const ptu = [...u];
- for (var key of ptu) {
- const r = await User.findAll({ where: { id: key.autorId } });
- key.autorId = r[0];
- key.partnerId = partner[0];
- }
- return ptu;
- }
- }
- }
- };
- const createMessage = async (obj) => {
- try {
- return await Message.create({ ...obj });
- } catch (e) {
- console.log(e);
- }
- };
- const changeMessage = async ({ id, message }) => {
- const putMess = await Message.findByPk(id);
- if (putMess) {
- await Message.update({ message }, { where: { id } });
- return await Message.findByPk(id);
- } else return { id: "Message not find" };
- };
- const deleteMessage = async ({ id }) => {
- // await Message.scope("deleted").findByPk(id);
- const messFind = await Message.findByPk(id);
- if (messFind) {
- await Message.destroy({ where: { id } });
- return "Message deleted";
- } else return "Message not find";
- };
- const getChatGroup = async ({id}) => {
- const chatGroup = await ChatGroup.findByPk(id)
- const autor = await User.findOne({ where: { id: chatGroup.autorId } });
- const partner = await User.findOne({ where: { id: chatGroup.partnerId } });
- chatGroup.autorId = autor;
- chatGroup.partnerId = partner;
- return chatGroup;
- }
- const getAllChatGroups = async () => {
- const foundAllChatGroups = await ChatGroup.findAll();
- for (var allUsers of foundAllChatGroups) {
- const autor = await User.findAll({ where: { id: allUsers.autorId } });
- const partner = await User.findAll({ where: { id: allUsers.partnerId } });
- allUsers.autorId = autor[0];
- allUsers.partnerId = partner[0];
- }
- return foundAllChatGroups;
- };
- const getAllChatGroupsOneUser = async ({ id }) => {
- const foundAllChatGroups = await ChatGroup.findAll();
- for (var allUsers of foundAllChatGroups) {
- const autor = await User.findAll({ where: { id: allUsers.autorId } });
- const partner = await User.findAll({ where: { id: allUsers.partnerId } });
- const ma = autor[0].id === +id || partner[0].id === +id;
- if (ma === true) {
- const y = await ChatGroup.findAll({ where: { autorId: id } });
- const u = await ChatGroup.findAll({ where: { partnerId: id } });
- if ((y.length && u.length) !== 0) {
- const p = [...y, ...u];
- for (var key of p) {
- const r = await User.findAll({ where: { id: key.autorId } });
- const r1 = await User.findAll({ where: { id: key.partnerId } });
- key.autorId = r[0];
- key.partnerId = r1[0];
- }
- return p;
- } else if (y.length !== 0) {
- const pt = [...y];
- for (var key of pt) {
- const r = await User.findAll({ where: { id: key.partnerId } });
- key.autorId = autor[0];
- key.partnerId = r[0];
- }
- return pt;
- } else {
- const ptu = [...u];
- for (var key of ptu) {
- const r = await User.findAll({ where: { id: key.autorId } });
- key.autorId = r[0];
- key.partnerId = partner[0];
- }
- return ptu;
- }
- } else console.log("bye");
- }
- };
- const createChatGroup = async (obj) => {
- try {
- const oneUserId = await ChatGroup.findOne({
- where: { autorId: obj.autorId, partnerId: obj.partnerId },
- });
- const twoUserId = await ChatGroup.findOne({
- where: { autorId: obj.partnerId, partnerId: obj.autorId },
- });
- if (!oneUserId && !twoUserId) {
- var newChatGroup = await ChatGroup.create({ ...obj });
- const autor = await User.findAll({ where: { id: newChatGroup.autorId } });
- const partner = await User.findAll({
- where: { id: newChatGroup.partnerId },
- });
- newChatGroup.autorId = autor[0];
- newChatGroup.partnerId = partner[0];
- return newChatGroup;
- }
- } catch (e) {
- return;
- }
- };
- const deleteChatGroup = async ({ id }) => {
- const messFind = await ChatGroup.findByPk(id);
- if (messFind) {
- await ChatGroup.destroy({ where: { id } });
- return "ChatGroup deleted";
- } else return "ChatGroup not find";
- };
- var root = {
- getUser,
- getLogin,
- createUser,
- deleteUser,
- getAllUsers,
- getMessage,
- changePassword,
- createMessage,
- changeMessage,
- deleteMessage,
- getAllMessages,
- getAllMessagesOneUser,
- getChatGroup,
- getAllChatGroups,
- getAllChatGroupsOneUser,
- createChatGroup,
- deleteChatGroup,
- };
- app.use(
- "/graphql",
- graphqlHTTP({
- schema: schema,
- rootValue: root,
- graphiql: true,
- })
- );
- app.get("/users", async (req, res) => res.send(await User.findAll()));
- app.get("/message", async (req, res) => res.send(await Message.findAll()));
- app.get("/chat-group", async (req, res) => res.send(await ChatGroup.findAll()));
- // function errorHandler(err, req, res, next) {
- // if (typeof err === "string") {
- // return res.status(400).json({ message: err });
- // }
- // if (err.name === "UnauthorizedError") {
- // return res.status(401).json({ message: "Invalid Token" });
- // }
- // return res.status(500).json({ message: err.message });
- // }
- app.post("/users/authenticate", async (req, res, next) => {
- authenticate(req.body)
- .then((user) => {
- user
- ? res.json(user)
- : res
- .status(400)
- .json({ message: "Username or password is incorrect" });
- })
- .catch((err) => next(err));
- });
- // app.use(jwtWare());
- // (async () => {
- // let persone =
- // // await User.findOne({ where: { login: "B" } }) ||
- // (await User.create({ email: "ljkd@gfd.gfd", password: "1", login: "A" }))
- // })()
- // createMessage({message: "Hello 1", autorId: 1, partnerId: 2})
- // createChatGroup({ autorId: 1, partnerId: 2})
- sequelize.sync();
- app.listen(3330, () => console.log("The server started on port 3330"));
|