Skip to content

Commit

Permalink
i had solved the problem check the @todo i have tried to say the prob…
Browse files Browse the repository at this point in the history
…lems see you in the meet at 10pm
  • Loading branch information
mkstamin committed Jan 12, 2021
1 parent 04e2407 commit ad85ef2
Show file tree
Hide file tree
Showing 14 changed files with 1,066 additions and 287 deletions.
1 change: 1 addition & 0 deletions .data/checks/4ng3fy95isww6m29n278.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"4ng3fy95isww6m29n278","userPhone":"01811559011","protocol":"http","url":"google.com","method":"GET","successCodes":[200,201,301],"timeoutSeconds":2,"state":"up","lastChecked":1610454755121}
1 change: 0 additions & 1 deletion .data/checks/nsb4fv9izjmloc40ge5s.json

This file was deleted.

1 change: 1 addition & 0 deletions .data/tokens/il5hp0qq75r23skzwn65.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"phone":"01811559011","id":"il5hp0qq75r23skzwn65","expires":1610456417238}
1 change: 0 additions & 1 deletion .data/tokens/zmukk0udqgc3eyi0zjd3.json

This file was deleted.

1 change: 0 additions & 1 deletion .data/users/01797989254.json

This file was deleted.

1 change: 1 addition & 0 deletions .data/users/01811559011.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"firstName":"Mks","lastName":"Tamin ","phone":"01811559011","password":"f6f0b0edb67abeb44e1f61852484585c802034126dd191bab982777bba0cb433","tosAgreement":true,"checks":["4ng3fy95isww6m29n278"]}
27 changes: 27 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"extends": ["prettier", "airbnb-base"],
"parserOptions": {
"ecmaVersion": 12
},
"env": {
"commonjs": true,
"node": true
},
"rules": {
"no-console": 0,
"indent": 0,
"linebreak-style": 0,
"prettier/prettier": [
"error",
{
"trailingComma": "es5",
"singleQuote": true,
"printWidth": 100,
"tabWidth": 4,
"semi": true,
"endOfLine": "auto"
}
]
},
"plugins": ["prettier"]
}
45 changes: 23 additions & 22 deletions helpers/environments.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
/* eslint-disable operator-linebreak */
// scaffolding
const environments = {};

// staging environment
environments.staging = {
port: 3000,
envName: "staging",
secretKey: "key420",
maxChecks: 5,
twilio: {
fromPhone: "+14435683965",
AccountSid: "AC928722d7bb482ad76b2b909f11241f02",
authToken: "fa64fcb77ef04c45f34a58dbe67ffd15",
},
port: 3000,
envName: 'staging',
secretKey: 'key420',
maxChecks: 5,
twilio: {
fromPhone: '',
AccountSid: '',
authToken: '',
},
};

// production environment
environments.production = {
port: 5000,
envName: "production",
secretKey: "key420",
maxChecks: 5,
twilio: {
fromPhone: "+14435683965",
AccountSid: "AC928722d7bb482ad76b2b909f11241f02",
authToken: "fa64fcb77ef04c45f34a58dbe67ffd15",
},
port: 5000,
envName: 'production',
secretKey: 'key420',
maxChecks: 5,
twilio: {
fromPhone: '',
AccountSid: '',
authToken: '',
},
};

// determine which environment was passed
const currentEnvironment =
typeof process.env.NODE_ENV === "string" ? process.env.NODE_ENV : "staging";
typeof process.env.NODE_ENV === 'string' ? process.env.NODE_ENV : 'staging';

// export corresponding environment object
const environmentToExport =
typeof environments[currentEnvironment] === "object"
? environments[currentEnvironment]
: environments.staging;
typeof environments[currentEnvironment] === 'object'
? environments[currentEnvironment]
: environments.staging;

// export module
module.exports = environmentToExport;
97 changes: 47 additions & 50 deletions helpers/notifications.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,66 @@
/* eslint-disable operator-linebreak */
// dependencies
const https = require("https");
const https = require('https');
// const { user } = require("../routes");
const queryString = require("querystring");
const { twilio } = require("./environments");
const queryString = require('querystring');
const { twilio } = require('./environments');

// scaffolding
const notifications = {};

// send sms to user using twilio api
notifications.sendTwilioSms = (phone, msg, callback) => {
// input validation
const userPhone =
typeof phone === "string" && phone.trim().length === 11
? phone.trim()
: false;
// input validation
const userPhone =
typeof phone === 'string' && phone.trim().length === 11 ? phone.trim() : false;

const userMsg =
typeof msg === "string" &&
msg.trim().length > 0 &&
msg.trim().length <= 1600
? msg.trim()
: false;
const userMsg =
typeof msg === 'string' && msg.trim().length > 0 && msg.trim().length <= 1600
? msg.trim()
: false;

if (userPhone && userMsg) {
// configure the payload
const payload = {
From: twilio.fromPhone,
To: `+88${userPhone}`,
Body: userMsg,
};
if (userPhone && userMsg) {
// configure the payload
const payload = {
From: twilio.fromPhone,
To: `+88${userPhone}`,
Body: userMsg,
};

// stringify the payload
const stringifyPayload = queryString.stringify(payload);
// stringify the payload
const stringifyPayload = queryString.stringify(payload);

// configure the req details
const requestDetails = {
hostname: "api.twilio.com",
method: "POST",
path: `/2010-04-01/Accounts/{twilio.AccountSid}/Messages.json`,
auth: `${twilio.AccountSid}:${twilio.authToken}`,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
};
// configure the req details
const requestDetails = {
hostname: 'api.twilio.com',
method: 'GET',
path: `/2010-04-01/Accounts/${twilio.AccountSid}/Messages.json`, // @TODO: missing template string and $ before {twilio}
auth: `${twilio.AccountSid}:${twilio.authToken}`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
};

// instantiate req
const req = https.request(requestDetails, (res) => {
const status = res.statusCode;
// instantiate req
const req = https.request(requestDetails, (res) => {
const status = res.statusCode;

if (status === 200 || status === 201) {
callback(false);
} else {
callback(`Status code returned was ${status}`);
}
});
if (status === 200 || status === 201) {
callback(false);
} else {
callback(`Status code returned was ${status}`);
}
});

req.on("error", (e) => {
callback(e);
});
req.on('error', (e) => {
callback(e);
});

req.write(stringifyPayload);
req.end();
} else {
callback("Given parameters were missing or invalid!");
}
req.write(stringifyPayload);
req.end();
} else {
callback('Given parameters were missing or invalid!');
}
};

// export the module
Expand Down
17 changes: 7 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
// dependencies
const http = require("http");
const { handleReqRes } = require("./helpers/handleReqRes");
const { sendTwilioSms } = require("./helpers/notifications");
const server = require("./lib/server");
const worker = require("./lib/worker");
const server = require('./lib/server');
const worker = require('./lib/worker');

// scaffolding
const app = {};

app.init = () => {
// start the server
server.init();
// start the worker
worker.init();
// start the server
server.init();

// start the worker
worker.init();
};

app.init();
Expand Down
19 changes: 9 additions & 10 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
// dependencies
const http = require("http");
const { handleReqRes } = require("../helpers/handleReqRes");
const { sendTwilioSms } = require("../helpers/notifications");
const http = require('http');
const { handleReqRes } = require('../helpers/handleReqRes');
// const { sendTwilioSms } = require('../helpers/notifications');

// scaffolding
const server = {};

// configuration
server.config = {
port: 3000,
port: 3000,
};

// create server
server.createServer = () => {
const createServerVariable = http.createServer(server.handleReqRes);
createServerVariable.listen(server.config.port, () => {
console.log(`Server is running on PORT: ${server.config.port}`);
});
const createServerVariable = http.createServer(server.handleReqRes);
createServerVariable.listen(server.config.port, () => {
console.log(`Server is running on PORT: ${server.config.port}`);
});
};

// handle Request Response
server.handleReqRes = handleReqRes;

// start the server
server.init = () => {
server.createServer();
server.createServer();
};

// export
Expand Down
Loading

0 comments on commit ad85ef2

Please sign in to comment.