Skip to content

Commit

Permalink
Code moved to lib, index created
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagoGouvea committed Oct 28, 2017
1 parent 53d4094 commit 8544d77
Show file tree
Hide file tree
Showing 20 changed files with 383 additions and 61 deletions.
25 changes: 25 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"extends": ["standard", "standard-react"],
"plugins": [
"react",
"react-native"
],
"ecmaFeatures": {
"jsx": true
},
"rules": {
"indent": [
"error",
4
],
"semi": [
2,
"always"
],
"quotes": 0,
"react/jsx-indent": [2, 4],
"react/jsx-indent-props": [2, 4],
"react/prop-types": [0]
}
}

29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# Commenting this out is preferred by some people, see
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules

# Users Environment Variables
.lock-wscript
.idea
284 changes: 239 additions & 45 deletions .idea/workspace.xml

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
amAuth: require('./lib/am-auth'),
amBootstrap: require('./lib/am-bootstrap'),
amController: require('./lib/am-controller'),
amError: require('./lib/am-error'),
amInvite: require('./lib/am-invite'),
amLeadManager: require('./lib/am-leadManager'),
amMailing: require('./lib/am-mailing'),
amModel: require('./lib/am-model'),
amRdStation: require('./lib/am-rdstation'),
amRouter: require('./lib/am-router'),
amSoftDelete: require('./lib/am-softDelete'),
amSystem: require('./lib/am-system'),
checkVersion: require('./lib/check-version'),
dateTime: require('./lib/dateTime'),
util: require('./lib/util')
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 9 additions & 11 deletions am-invite.js → lib/am-invite.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ var restful = require('node-restful');
var CronJob = require('cron').CronJob;
var mongoose = restful.mongoose;

var config = require('../../config/config');
var AMMailing = require('./am-mailing');

class AMInvite {
static setup (config) {
// Config
AMInvite.config = config;
AMInvite.model = mongoose.model('invitefriend');
// Initialize queue
AMInvite.queue = [];
Expand All @@ -23,18 +23,14 @@ class AMInvite {

static createQueue () {
AMInvite.model.find({inviteSent: {$exists: false}, error: {$exists: false} })
.then(result => {
.then(result =>{
console.log(result);
AMInvite.sendEmailInQueue(result);
}).catch(err => console.log(err));
})
}

static updateModel (where, attribute) {
return new Promise((resolve, reject) => {
AMInvite.model.update(where, attribute)
.then(response => resolve(response))
.catch(error => reject(error));
});
return AMInvite.model.update(where, attribute);
}

static async sendEmailInQueue (queue) {
Expand All @@ -44,17 +40,17 @@ class AMInvite {
const MESSAGE = 'Baixe agora o app Emagreça Já';
const HTML = `<h1>Emagreça já</h1> <p>venha conferir nosso app</p> <a href="http://localhost:3000/api/invitation/${person._id}">Emagreça já</a>`;
try {
let success = await AMMailing.sendEmail(config.mail.from, person.email, 'convite', MESSAGE, HTML);
let success = await AMMailing.sendEmail(AMInvite.config.mail.from, person.email, 'convite', MESSAGE, HTML);
/* console.log('----------------------------------------');
console.log(success);
console.log('----------------------------------------'); */

await AMInvite.updateModel({email: person.email, inviteSent: {$exists: false}}, {inviteSent: new Date()});
console.log('email sent to: ', person.email);
console.log('emails left to be sent: ', queue.length - (i + 1));
} catch (err) {
console.log('AMInvite.sendEmailInQueue', err.message)

if (err.message === 'No recipients defined') {
await AMInvite.updateModel({email: person.email}, {error: err.message});
} else {
Expand All @@ -65,4 +61,6 @@ class AMInvite {
console.log('All emails were sent');
}
}
AMInvite.config =null;

module.exports = AMInvite;
File renamed without changes.
1 change: 0 additions & 1 deletion am-mailing.js → lib/am-mailing.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ var mongoose = restful.mongoose;
class AMMailing {
static setup (config) {
// Config
console.log(config.smtp);
AMMailing.smtpconfig = config.smtp;
// AMMailing.message = config.mail.message;
// Initialize queue
Expand Down
2 changes: 1 addition & 1 deletion am-model.js → lib/am-model.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const mongoose = require('mongoose');
const AMCache = require('../libNpm/lib/amCache');
const AMCache = require('@app-masters/mongoose-it').amCache;

class AMModel {
constructor (modelName, cacheAll) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 32 additions & 0 deletions lib/am-system.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const mongoose = require('mongoose');

module.exports = function () {
let result = {};

result.env = {"NODE_ENV":process.env.NODE_ENV};
result.config = global.config; //!!!!!! @todo Remove passwords
result.node = {};
result.mongoose = {};
result.mongo = {};

// Mongoose
status.mongoose.readyState = mongoose.connection.readyState;

// Mongo
mongoose.connection.db.listCollections().toArray((err, names) => {
let collections = [];
names.map(col => {
let name = col.name;
// console.log(name);
mongoose.connection.db.collection(col.name).count().then(count => {
// console.log('count',count);
let obj = {collection: name, records: count};
collections.push(obj);
if (collections.length == names.length) {
status.mongo.collections = collections;
res.send(status);
}
});
});
});
};
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions mongooseUtil.js → lib/util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const restful = require('node-restful');
const mongoose = restful.mongoose;

class mongooseUtil {
class util {
// Save detail records from array, setting the masterKey on each record
static saveDetailRecords (masterRecord, masterKey, detailRecords, modelName, masterAttribute) {
return new Promise((fulfill, reject) => {
Expand Down Expand Up @@ -127,7 +127,7 @@ class mongooseUtil {
obj = {};

// It is a object id?
if (mongooseUtil.isValidObjectId(oldObj)) {
if (util.isValidObjectId(oldObj)) {
// Then, it's in fact the keyAttribute value of the object
obj[keyAttribute] = new mongoose.Types.ObjectId(oldObj);
// console.log('was object it');
Expand Down Expand Up @@ -218,4 +218,4 @@ class mongooseUtil {

}

module.exports = mongooseUtil;
module.exports = util;
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@app-masters/node-lib",
"version": "1.0.2",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/app-masters/node-lib.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/app-masters/node-lib/issues"
},
"homepage": "https://github.com/app-masters/node-lib#readme",
"devDependencies": {
"@app-masters/mongoose-it": "^1.0.2",
"cron": "^1.3.0",
"i": "^0.3.6",
"mustache": "^2.3.0",
"nodemailer": "^4.3.1",
"npm": "^5.5.1",
"rdstation-node-client": "^1.2.0"
}
}

0 comments on commit 8544d77

Please sign in to comment.