diff --git a/config.js b/config.js index 5d57027..03c42b8 100644 --- a/config.js +++ b/config.js @@ -20,6 +20,10 @@ module.exports = function () { twitterBotUrl: process.env.TWITTER_BOT_STAGING, caseFolderName: 'case_photos_staging', userFolderName: 'user_photos_staging', + twitterBotConsumerKey: process.env.BOT_CONSUMER_KEY_STAGING, + twitterBotConsumerKeySecret: process.env.BOT_CONSUMER_SECRET_STAGING, + twitterBotAccessToken: process.env.BOT_ACCESS_TOKEN_STAGING, + twitterBotAccessTokenSecret: process.env.BOT_ACCESS_TOKEN_SECRET_STAGING, }; case 'production': return { @@ -33,6 +37,10 @@ module.exports = function () { twitterBotUrl: process.env.TWITTER_BOT, caseFolderName: 'case_photos_prod', userFolderName: 'user_photos_prod', + twitterBotConsumerKey: process.env.BOT_CONSUMER_KEY, + twitterBotConsumerKeySecret: process.env.BOT_CONSUMER_SECRET, + twitterBotAccessToken: process.env.BOT_ACCESS_TOKEN, + twitterBotAccessTokenSecret: process.env.BOT_ACCESS_TOKEN_SECRET, }; default: return { @@ -46,6 +54,10 @@ module.exports = function () { twitterBotUrl: process.env.TWITTER_BOT_DEV, caseFolderName: 'case_photos', userFolderName: 'user_photos', + twitterBotConsumerKey: process.env.BOT_CONSUMER_KEY_STAGING, + twitterBotConsumerKeySecret: process.env.BOT_CONSUMER_SECRET_STAGING, + twitterBotAccessToken: process.env.BOT_ACCESS_TOKEN_STAGING, + twitterBotAccessTokenSecret: process.env.BOT_ACCESS_TOKEN_SECRET_STAGING, }; } }; diff --git a/db/models/Case.model.js b/db/models/Case.model.js index ddb03e0..c87848a 100644 --- a/db/models/Case.model.js +++ b/db/models/Case.model.js @@ -10,7 +10,7 @@ caseSchema.pre('save', function (next) { // Generate a description of the case let description = `${this.fullname} ${this.nicknames.length > 0 ? `aka ${this.nicknames.join(',')}` : ''}`; - description += ` who is a ${this.age} year old ${this.gender.toLowerCase()} got missing on ${this.dateLastSeen.toDateString()} at ${this.addressLastSeen.formatted_address}, in ${this.addressLastSeen.state}, ${this.addressLastSeen.country}.`; + description += ` who is a ${this.age ? `${this.age} year old ` : ''} ${this.gender.toLowerCase()} got missing on ${this.dateLastSeen.toDateString()} at ${this.addressLastSeen.formatted_address}, in ${this.addressLastSeen.state}, ${this.addressLastSeen.country}.`; this.description = description; next(); diff --git a/db/schemas/Case.schema.js b/db/schemas/Case.schema.js index d47a5ec..a36e5df 100644 --- a/db/schemas/Case.schema.js +++ b/db/schemas/Case.schema.js @@ -28,7 +28,6 @@ const caseSchema = new Schema( nicknames: [String], age: { type: Number, - required: true, }, gender: { type: String, @@ -37,7 +36,6 @@ const caseSchema = new Schema( }, language: { type: String, - required: true, }, residentialAddress: { location: locationSchema, diff --git a/package.json b/package.json index f1a90cf..086a671 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "main": "server.js", "scripts": { "start": "pm2-runtime start ecosystem.config.js --env production", - "test": "mocha \"./test/**/*.spec.js\" --timeout=20000 --require test/prepare --exit", + "test": "mocha test/exit-mocha.js \"./test/**/*.spec.js\" --timeout=20000 --require test/prepare --exit", "coverage": "nyc npm test && nyc report --reporter=text-lcov | coveralls" }, "author": "George Benjamin", diff --git a/schemas/index.js b/schemas/index.js index f17603f..809010c 100644 --- a/schemas/index.js +++ b/schemas/index.js @@ -93,40 +93,32 @@ module.exports = { fullname: Joi.string() .trim() .min(2) - .required() - .pattern(fullnamePattern, 'Firstname Lastname'), + .required(), nicknames: Joi.array().items(Joi.string()), age: Joi.number() .integer() - .min(1) - .required(), + .min(1), residentialAddress: Joi.object({ location: Joi.object({ type: Joi.string() - .trim() - .required(), + .trim(), coordinates: Joi.array() .items(Joi.number()) - .max(2) - .required(), + .max(2), }), formatted_address: Joi.string() - .trim() - .required(), + .trim().optional(), country: Joi.string() - .trim() - .required(), + .trim(), state: Joi.string() - .trim() - .required(), - }).required(), + .trim(), + }).optional(), gender: Joi.string() .trim() .required() .valid('MALE', 'FEMALE'), language: Joi.string() .trim() - .required() .pattern(namePattern, 'name'), addressLastSeen: Joi.object({ location: Joi.object({ @@ -166,8 +158,7 @@ module.exports = { updateCase: Joi.object({ fullname: Joi.string() .trim() - .min(3) - .pattern(fullnamePattern, 'Firstname Lastname'), + .min(3), nicknames: Joi.array().items(Joi.string()), age: Joi.number() .integer() @@ -181,23 +172,18 @@ module.exports = { residentialAddress: Joi.object({ location: Joi.object({ type: Joi.string() - .trim() - .required(), + .trim(), coordinates: Joi.array() .items(Joi.number()) - .max(2) - .required(), + .max(2), }), formatted_address: Joi.string() - .trim() - .required(), + .trim().optional(), country: Joi.string() - .trim() - .required(), + .trim(), state: Joi.string() - .trim() - .required(), - }), + .trim(), + }).optional(), addressLastSeen: Joi.object({ location: Joi.object({ type: Joi.string() diff --git a/server.js b/server.js index 1d47285..847fdb6 100644 --- a/server.js +++ b/server.js @@ -55,6 +55,7 @@ function gracefulShutdown() { .catch((err) => process.exit(err ? 1 : 0)); }); } + process.on('SIGINT', () => { gracefulShutdown(); }); diff --git a/services/case.service.js b/services/case.service.js index 8c9cd5f..8c3830f 100644 --- a/services/case.service.js +++ b/services/case.service.js @@ -135,6 +135,7 @@ async function updateCase( age, gender, language, + residentialAddress, addressLastSeen, state, country, @@ -153,6 +154,7 @@ async function updateCase( if (age) reportedCase.age = age; if (gender) reportedCase.gender = gender; if (language) reportedCase.language = language; + if (residentialAddress) reportedCase.residentialAddress = residentialAddress; if (addressLastSeen) reportedCase.addressLastSeen = addressLastSeen; if (state) reportedCase.state = state; if (country) reportedCase.country = country; diff --git a/services/twitterbot.js b/services/twitterbot.js index fdc97a7..82985e9 100644 --- a/services/twitterbot.js +++ b/services/twitterbot.js @@ -5,13 +5,19 @@ const Twit = require('twit'); const axios = require('axios'); const { handleError, logger } = require('../utils'); -const { frontEndUrl } = require('../config')(); +const { + frontEndUrl, + twitterBotConsumerKey, + twitterBotConsumerKeySecret, + twitterBotAccessToken, + twitterBotAccessTokenSecret, +} = require('../config')(); const bot = new Twit({ - consumer_key: process.env.BOT_CONSUMER_KEY, - consumer_secret: process.env.BOT_CONSUMER_SECRET, - access_token: process.env.BOT_ACCESS_TOKEN, - access_token_secret: process.env.BOT_ACCESS_TOKEN_SECRET, + consumer_key: twitterBotConsumerKey, + consumer_secret: twitterBotConsumerKeySecret, + access_token: twitterBotAccessToken, + access_token_secret: twitterBotAccessTokenSecret, }); /** @@ -20,10 +26,7 @@ const bot = new Twit({ */ async function tweetNewCase(data) { try { - let image = await axios.get( - data.photoURL, - { responseType: 'arraybuffer' }, - ); + let image = await axios.get(data.photoURL, { responseType: 'arraybuffer' }); let returnedB64 = Buffer.from(image.data).toString('base64'); let result = await bot.post('media/upload', { media_data: returnedB64 }); const mediaIdStr = result.data.media_id_string; @@ -31,7 +34,9 @@ async function tweetNewCase(data) { const meta_params = { media_id: mediaIdStr, alt_text: { text: altText } }; await bot.post('media/metadata/create', meta_params); const params = { - status: `${data.description}. For more info, visit ${frontEndUrl}/cases/${data.slug} #HelpLookFor${data.fullname.split(' ').join('')} #HelpLookForMe`, + status: `${data.description}. For more info, visit ${frontEndUrl}/cases/${ + data.slug + } #HelpLookFor${data.fullname.split(' ').join('')} #HelpLookForMe`, media_ids: [mediaIdStr], }; await bot.post('statuses/update', params); diff --git a/test/exit-mocha.js b/test/exit-mocha.js new file mode 100644 index 0000000..7beeef3 --- /dev/null +++ b/test/exit-mocha.js @@ -0,0 +1,7 @@ +/* eslint-disable prefer-arrow-callback */ +/* eslint-disable no-undef */ + +after('Exit mocha gracefully after finishing all tests execution', function () { + console.log('Ran tests, exiting!'); + process.exit(); +});