Skip to content

Commit

Permalink
* [Fix] Fixing Cache service and Models
Browse files Browse the repository at this point in the history
+ [Add] Adding Requests that calls Facebook APIs
  • Loading branch information
mohammadhb committed Apr 13, 2021
1 parent 45eb099 commit 229ab89
Show file tree
Hide file tree
Showing 16 changed files with 229 additions and 47 deletions.
36 changes: 30 additions & 6 deletions controller/message.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
function getAllMessages(request,response){
const {Message,User} = require('../database/mongodb/index').Models;

/*
Gets all the messages that sent along the bot
*/
async function getAllMessages(request,response){

const messages = await new Message().getAll();
return response.status(200).json({
data:messages,
errors:[]
});

}

function getMessage(request,response) {
/*
Gets specific Message
*/
async function getMessage(request,response) {


const id = request.params.id;
const message = await Message.getById(id);
return response.status(200).json({
data:message,
errors:[]
});

}

function deleteMessage(request,response) {
/*
Deletes specific Message
*/
async function deleteMessage(request,response) {


const id = request.params.id;
const message = await Message.softDeleteById(id);
return response.status(200).json({
data:message,
errors:[]
});

}


module.exports = {
getAllMessages,
getMessage,
Expand Down
14 changes: 8 additions & 6 deletions database/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const {Redis} = require('./redis/index.js');
let redisController = null;

//Router Controller
let start = (config)=>{
let redis = async ()=>{

redisController = Redis;
if(redisController) {
return redisController;
}
redisController = new Redis();
await redisController.init();
return redisController;

}

module.exports = {
start,
redis:redisController,

Cacher:redis,
}
27 changes: 22 additions & 5 deletions database/mongodb/models/message.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Schema = mongoose.Schema
const Types = Schema.Types

const modelName = 'Message'
const collectionName = 'messages';
const collection = 'messages';

const schema = new Schema({

Expand All @@ -16,10 +16,6 @@ const schema = new Schema({
type:String,
default:null
},
timestamp:{
type:Date,
default:null
},
recipientId: {
type:String,
default:null
Expand Down Expand Up @@ -121,6 +117,27 @@ schema.methods.getAll = function(){

};

schema.methods.getById = function(id){

const model = this.model(modelName);

let query = {
_id:id
};

const fields = {
__v:0,
_data_lifecycle:0,
};

return model
.find(query)
.populate('User','-__v -_data_lifecycle')
.select(fields)
.exec();

};

const model = mongoose.model(modelName, schema, collection)

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions database/mongodb/models/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ const schema = new Schema({

});

schema.methods.getUserByObjectId = function(id){
schema.methods.getByObjectId = function(id){

return this.model(modelName).findOne({
_id:id
});

}

schema.methods.getUserById = function(id){
schema.methods.getById = function(id){

return this.model(modelName).findOne({
id
Expand Down
9 changes: 3 additions & 6 deletions database/redis/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class Redis {

try{

const status = await this.createClient(0),data = await this.createClient(1),message = await this.createClient(2);
this.addModel('user',new models.user(status,data,message));
const status = await this.createClient(0),data = await this.createClient(1);
this._models['user'] = new models.user(status,data);

}catch(error){

Expand Down Expand Up @@ -72,9 +72,6 @@ class Redis {

}

const _redis = new Redis();
_redis.init();

module.exports = {
Redis:_redis
Redis
};
38 changes: 16 additions & 22 deletions database/redis/models/user.model.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class Model {

constructor(statusClient,dataClient,messageClient){

this.status = null;
this.user = null;
constructor(statusClient,dataClient){

this.statusClient = statusClient;
this.dataClient = dataClient;

this.status = null;
this.user = null;

this.expireTime = 60*60*2;//2h

}
Expand All @@ -26,27 +26,21 @@ class Model {
this.user.birthday = value;
}

get user(user){

const userString = JSON.stringify(user);
return this.dataClient.setAsync(user.id,userString,'EX',this.expireTime);
async setId(value){

if(value){
this.user = await this.dataClient.getAsync(value);
this.status = await this.statusClient.getAsync(value);
}

}

set status(user){

const userString = JSON.stringify(user);
return this.dataClient.setAsync(user.id,userString,'EX',this.expireTime);
// await this.emailVerificationClient.delAsync(key);

}

get status(){
return this.messages;
}
set status(value){
this.status = value;
}
// get status(){
// return this.messages;
// }
// set status(value){
// this.status = value;
// }

}

Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

require('dotenv').config();
const router = require('./router');

try{
Expand Down
7 changes: 7 additions & 0 deletions middlewares/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function updateCache(request,response){

}

module.exports = {
updateCache
}
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "A Simple Messenger Bot",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "",
"dev": "nodemon index.js"
},
Expand All @@ -22,6 +23,7 @@
"express": "^4.17.1",
"jest": "^26.6.3",
"mongoose": "^5.12.3",
"redis": "^3.1.0",
"sequelize": "^6.6.2"
}
}
10 changes: 10 additions & 0 deletions requests/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
config:{
baseURL: "https://graph.facebook.com/v7.0",
headers: {
"Content-Type": "application/json",
},
responseType: "json",
},
access_token:process.env.MESSENGER_BOT_ACCESS_TOKEN
};
62 changes: 62 additions & 0 deletions requests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const axios = require("axios");
const { config, access_token } = require("./config");

const client = axios.create(config);

function markSeen(recipient) {
const path = "/me/messages",
data = {
recipient: { id: recipient },
sender_action: "mark_seen",
},
options = {
params: { access_token },
};

return client.post(path, data, options);
}

function setTyping(recipient, state) {
const path = "/me/messages",
data = {
recipient: { id: recipient },
sender_action: state ? "typing_on" : "typing_off",
},
options = {
params: { access_token },
};

return client.post(path, data, options);
}

function sendMessage(recipient, message, quckReplies) {
const path = "/me/messages",
data = {
recipient: { id: recipient },
message: {
text:message,
quick_replies:quckReplies?quckReplies:undefined,
},
},
options = {
params: { access_token },
};

console.log(data)

return client.post(path, data, options);
}

async function sendMessageWithSeenAndTyping(recipient, message, quckReplies) {
await markSeen(recipient);
await setTyping(recipient,true);
await sendMessage(recipient,message,quckReplies);
await setTyping(recipient,false);
}

module.exports = {
markSeen,
setTyping,
sendMessage,
sendMessageWithSeenAndTyping
};
Empty file removed responser/index.js
Empty file.
Empty file removed responser/message.js
Empty file.
Empty file removed responser/user.js
Empty file.
Loading

0 comments on commit 229ab89

Please sign in to comment.