Last active
September 28, 2017 02:05
-
-
Save akexorcist/fe028f9be2c435c110b2532d036eab21 to your computer and use it in GitHub Desktop.
NBTC Auto Fetch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose'); | |
var deviceSchema = mongoose.Schema({ | |
j_id: String, | |
j_cert_no: String, | |
j_trade_name: String, | |
j_model_code: String | |
}) | |
module.exports.DeviceModel = mongoose.model('DeviceModel', deviceSchema, 'device_list') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios') | |
const _ = require('async-lodash') | |
const mongoose = require('mongoose') | |
const Promise = require('promise') | |
const cron = require('node-cron') | |
const { DeviceModel } = require('./schema/DeviceSchema') | |
const COLLECTION_NAME = 'device_list' | |
const KEY_ID = 'j_id' | |
const KEY_CERT_NUMBER = 'j_cert_no' | |
const KEY_TRADE_NAME = 'j_trade_name' | |
const KEY_MODEL_CODE = 'j_model_code' | |
const KEY_IS_NEW_DEVICE = 'is_new_device' | |
const MONGO_USER = '...' | |
const MONGO_PASS = '...' | |
const MONGO_AUTH_SOURCE = 'admin' | |
const MONGO_URL = '...' | |
const URL_NEXTZY_SLACK = '...' | |
const URL_DROIDSANS_SLACK = '...' | |
const CHANNEL_NEXTZY_SLACK = '...' | |
const CHANNEL_DROIDSANS_SLACK = '...' | |
console.log('NBTC Auto Fetch Running') | |
const mongooseOptions = { | |
useMongoClient: true | |
} | |
var db = mongoose.connect(MONGO_URL, mongooseOptions).collection(COLLECTION_NAME) | |
var api = axios.create({ | |
baseURL: 'http://apps.nbtc.go.th', | |
responseType: 'text' | |
}) | |
var schedule = cron.schedule('* 6 * * *', () => { | |
console.log('Fetch device list') | |
fetchDeviceList() | |
schedule.start() | |
}, true) | |
schedule.start() | |
const forceUpdateDeviceList = () => { | |
api.get('/type_approval/gen_json.php') | |
.then(response => { | |
return convertJsonToDevices(response) | |
}).then(devices => { | |
return saveAllNewDeviceToDatabase(devices) | |
}).then(result => { | |
saveAllNewDeviceSuccessful() | |
}) | |
} | |
const fetchDeviceList = () => { | |
api.get('/type_approval/gen_json.php') | |
.then(response => { | |
return convertJsonToDevices(response) | |
}).then(devices => { | |
return getExistDeviceFromDatabase(devices) | |
}).then(compareDevices => { | |
return filterByNewDevice(compareDevices) | |
}).then(devices => { | |
return saveAllNewDeviceToDatabase(devices) | |
}).then(devices => { | |
return convertToDeviceName(devices) | |
}).then(devices => { | |
return notifyNewDevice(devices) | |
}).then(result => { | |
saveAllNewDeviceSuccessful() | |
}).catch(error => { | |
console.log(error) | |
}) | |
} | |
const convertJsonToDevices = response => { | |
return new Promise((resolve, reject) => { | |
resolve(JSON.parse(response.data.toString().trim()).data) | |
}) | |
} | |
const getExistDeviceFromDatabase = devices => { | |
return new Promise((resolve, reject) => { | |
DeviceModel.find({}, KEY_CERT_NUMBER, (err, existDevices) => { | |
if (err) { | |
reject(err) | |
} else { | |
var compareDevices = { | |
fetchDevices: devices, | |
existDevices: existDevices | |
} | |
resolve(compareDevices) | |
} | |
}) | |
}) | |
} | |
const filterByNewDevice = (compareDevices) => { | |
var fetchDevices = compareDevices.fetchDevices | |
var existDevices = compareDevices.existDevices | |
return _.filter(fetchDevices, device => { | |
for (var existDevice of existDevices) { | |
if (existDevice[KEY_CERT_NUMBER] == device[KEY_CERT_NUMBER]) { | |
return false | |
} | |
} | |
return true | |
}) | |
} | |
const convertToDeviceName = devices => { | |
return _.flatMap(devices, device => { | |
return device[KEY_TRADE_NAME].trim() + ' ' + device[KEY_MODEL_CODE].trim() | |
}) | |
} | |
const notifyNewDevice = devices => { | |
if (devices && _.isArray(devices) && !_.isEmpty(devices)) { | |
for (var device of devices) { | |
console.log('• ' + device) | |
} | |
sendUpdateToNextzySlack(devices) | |
sendUpdateToDroidsansSlack(devices) | |
console.log('Notify new device successful') | |
} else { | |
console.log('Don\'t need to notify') | |
} | |
} | |
const sendUpdateToNextzySlack = devices => { | |
sendNewDeviceViaSlackMessage(devices, URL_NEXTZY_SLACK, CHANNEL_NEXTZY_SLACK) | |
} | |
const sendUpdateToDroidsansSlack = devices => { | |
sendNewDeviceViaSlackMessage(devices, URL_DROIDSANS_SLACK, CHANNEL_DROIDSANS_SLACK) | |
} | |
const sendNewDeviceViaSlackMessage = (devices, slackUrl, slackChannel) => { | |
var newDeviceMessage = devices.join('\n') | |
axios({ | |
method: 'post', | |
url: slackUrl, | |
responseType: 'json', | |
data: { | |
text: newDeviceMessage + '\nMore information : http://apps.nbtc.go.th/type_approval/', | |
username: 'NBTC Reporter', | |
channel: slackChannel, | |
icon_emoji: ':satellite_antenna:' | |
} | |
}).then(response => { | |
console.log('Hook successful') | |
}) | |
} | |
const saveAllNewDeviceToDatabase = devices => { | |
return _.forEach(devices, device => { | |
saveNewDevice(device) | |
}) | |
} | |
const saveNewDevice = device => { | |
device[KEY_TRADE_NAME] = device[KEY_TRADE_NAME].trim() | |
device[KEY_MODEL_CODE] = device[KEY_MODEL_CODE].trim() | |
return Promise.resolve(db.insert(device)) | |
} | |
const saveAllNewDeviceSuccessful = () => { | |
console.log('Saved all devices') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment