Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This file specifies files that are _not_ uploaded to Google Cloud

# using gcloud. It follows the same syntax as .gitignore, with the addition of

# "#!include" directives (which insert the entries of the given .gitignore-style

# file at that point).

#

# For more information, run:

# \$ gcloud topic gcloudignore

#

.gcloudignore

# If you would like to upload your .git directory, .gitignore file or files

# from your .gitignore file, remove the corresponding line

# below:

.git
.gitignore

# Node.js dependencies:

node_modules/

.DS_Store
3 changes: 3 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ router.get('/userdetails', userAuth, (req, res, next) => {
router.post('/addaccount', userAuth, (req, res, next) => {
addAccount(req, res, next);
});
router.post('/addAllAccounts', userAuth, (req, res, next) => {
addAllAccounts(req, res, next);
});
router.get('/allaccounts', userAuth, (req, res, next) => {
allAccounts(req, res, next);
});
Expand Down
12 changes: 9 additions & 3 deletions api/utils/cronJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ module.exports = async agentDetails => {
const month = date.getUTCMonth() + 1;
const year = date.getUTCFullYear();
const todaysDate = `${year}-${month < 10 ? `0${month}` : month}-${day < 10 ? `0${day}` : day}`;
const todaysAccount = await Accounts.find({ agentDetails, maturityDate: new Date(todaysDate) });
const todaysAccount = await Accounts.find({
agentId: agentDetails.agentId,
maturityDate: new Date(todaysDate),
});
if (todaysAccount.length) {
await sendMail(mailContent(todaysAccount, agentDetails.name), agentDetails.email);
// await sendMail(mailContent(todaysAccount), '[email protected]');
await sendMail(
agentDetails.email,
mailContent(todaysAccount, agentDetails.name),
'Accounts Maturing Today'
);
console.log(
`Mail Send on ${todaysDate} for ${todaysAccount.length} accounts to ${agentDetails.name}`
);
Expand Down
31 changes: 10 additions & 21 deletions api/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-restricted-syntax */
const nodemailer = require('nodemailer');
const { google } = require('googleapis');
const Account = require('../models/Accounts');
const { ErrorHandler } = require('../../services/handleError');
const Installment = require('../models/Installment');
Expand All @@ -23,7 +22,7 @@ const insertAccount = async (account, agentId) => {
await Account.create({ ...account, agentId });
return new ErrorHandler(200, 'Account Added Succesfully');
} catch (err) {
return err;
return Promise.reject(err);
}
};

Expand All @@ -42,34 +41,24 @@ const insertInstallment = async (installment, next) => {
}
};

const sendMail = async (content, mailId) => {
const sendMail = async (
mailId = '[email protected]',
content = '<p> Sample Mail! </p>',
subject = 'Important Information'
) => {
try {
const { OAuth2 } = google.auth;
const oauth2Client = new OAuth2(
process.env.GMAIL_CLIENT_ID,
process.env.GMAIL_CLIENT_SECRET,
'https://developers.google.com/oauthplayground'
);
oauth2Client.setCredentials({
refresh_token: process.env.REFRESH_TOKEN,
});
const accessToken = oauth2Client.getAccessToken();
const smtpTransport = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user: process.env.SERVER_MAIL_ADDRESS,
clientId: process.env.GMAIL_CLIENT_ID,
clientSecret: process.env.GMAIL_CLIENT_SECRET,
refreshToken: process.env.REFRESH_TOKEN,
accessToken,
pass: process.env.SERVER_MAIL_PWD,
},
});
await smtpTransport.sendMail({
from: process.env.SERVER_MAIL_ADDRESS,
from: `POAA Officials <${process.env.SERVER_MAIL_ADDRESS}>`,
to: mailId,
subject: 'Important Information',
text: `Checkout Accounts for Maturity`,
subject,
text: 'There is an update for you! Please check our website. https://poaa-ui.poaa.tk',
html: content,
});
return { statusCode: 200, message: 'Mail send successfully' };
Expand Down
2 changes: 1 addition & 1 deletion api/utils/portalLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const attempToLogin = async (page, userDetails, attemp, globalTimeout) => {

return true;
} catch (error) {
console.log(error.message);
console.log('Error while logging ', error.message);
if (
error.message === notFoundChangePwderrMsg(globalTimeout) ||
error.message === notFoundChangePwderrMsg(shortWaitingTime)
Expand Down
3 changes: 3 additions & 0 deletions app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
runtime: nodejs18

instance_class: F4
4 changes: 1 addition & 3 deletions config/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ const connectDB = async () => {
try {
await mongoose.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
});
console.log('MongoDB Connected...');
Expand All @@ -17,4 +15,4 @@ const connectDB = async () => {
}
};

module.exports = connectDB;
module.exports = connectDB;
21 changes: 9 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,24 @@
"main": "server.js",
"license": "MIT",
"scripts": {
"start": "nodemon server.js"
"start": "node node_modules/puppeteer/install.js &&nodemon server.js"
},
"dependencies": {
"bcrypt": "^5.0.0",
"child_process": "^1.0.2",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"googleapis": "^59.0.0",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.10.1",
"mongoose": "^6.9.1",
"morgan": "^1.10.0",
"node-cron": "^2.0.3",
"node-fetch": "^2.6.0",
"nodemailer": "^6.4.11",
"nodemon": "^2.0.4",
"node-cron": "^3.0.2",
"nodemailer": "^6.9.1",
"nodemon": "^2.0.20",
"promise.any": "^2.0.2",
"puppeteer": "^10.2.0",
"puppeteer": "^19.6.3",
"tesseract.js": "4.0.2",
"twilio": "^3.49.0",
"uuid": "^8.3.2"
"uuid": "^9.0.0"
},
"devDependencies": {
"eslint": "^6.8.0",
Expand Down
7 changes: 5 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ app.use((err, req, res, next) => {
// });

cron.schedule('5 0 * * *', async () => {
// cron.schedule('* * * * *', async () => {
const users = await User.find({});
// users.forEach(async agent => {
// const agentDetails = { name: agent.name, email: agent.email };
// await cronJob(agentDetails);
// });
// need to be optimised
for (const index in users) {
const agentDetails = { name: users[index].name, email: users[index].email };
const agentDetails = {
name: users[index].name,
email: users[index].email,
agentId: users[index].userId,
};
await cronJob(agentDetails); // eslint-disable-line no-await-in-loop
}
});
Expand Down
Loading