Skip to content

Instantly share code, notes, and snippets.

View tlcheah2's full-sized avatar
💭
Keep thinking and writing code

Tek Loon tlcheah2

💭
Keep thinking and writing code
View GitHub Profile
@tlcheah2
tlcheah2 / server.js
Created August 10, 2019 16:21
This gist added the "quoteEvent" endpoint
// Import env file
require('dotenv').config();
const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();
const { keepAwake } = require('./src/util/heroku_util');
const { setQuoteEvent } = require('./src/controllers/quoteController');
const { start } = require('./src/consumers/scrapQuoteConsumer');
@tlcheah2
tlcheah2 / quoteController.js
Created August 11, 2019 11:38
Implemented Server-Sent Event to receive real time scraped quote
const rp = require('request-promise');
const $ = require('cheerio');
const fs = require('fs');
const path = require('path');
const quotesFilePath = path.join(process.cwd(), 'public', 'quotes.json');
const url = 'http://wisdomquotes.com/stoic-quotes/';
// Quote Event Req
let quoteEventReq = [];
let quoteEventRes = [];
@tlcheah2
tlcheah2 / twilioService.js
Created August 16, 2019 14:01
This gist is a Twilio Service component. It contains sendWhatsappMessage function.
const twilio = require('twilio');
// Twilio Client
let client;
/**
* Initialize Client Connection with Twilio
*/
const initClient = () => {
@tlcheah2
tlcheah2 / quoteController.js
Last active August 17, 2019 10:50
Added sendQuoteViaWhatsapp controller and whatsappQuote endpoint
exports.sendQuoteViaWhatsapp = async (req, res) => {
console.log('sendQuoteViaWhatsapp');
const { mobileNo } = req.body;
try {
const quote = await this.scrapQuoteOfTheDay();
const msg = await sendWhatsappMessage({ mobileNo, quote });
res.json({ msg });
} catch (err) {
console.error('scrap quote error', err);
res.status(400).json({ error: err.message });
@tlcheah2
tlcheah2 / user.js
Last active September 3, 2019 02:59
Mongoose Model of User
const mongoose = require('mongoose');
// Create a simple User's schema
const userSchema = new mongoose.Schema({
name: { type: String, required: true},
gender: { type: String, required: true},
dob: Date,
loginUsing: String,
});
@tlcheah2
tlcheah2 / jest.config.js
Created September 3, 2019 05:28
Jest MongoDB Preset
module.exports = {
preset: '@shelf/jest-mongodb',
};
@tlcheah2
tlcheah2 / jest-mongodb-config.js
Last active September 3, 2019 06:36
Jest MongoDB config
module.exports = {
mongodbMemoryServerOptions: {
instance: {
dbName: 'jest'
},
binary: {
version: '4.0.2', // Version of MongoDB
skipMD5: true
},
autoStart: false
@tlcheah2
tlcheah2 / user.test.js
Created September 3, 2019 06:14
Test for User Model
const mongoose = require('mongoose');
const UserModel = require('../../src/models/user');
const userData = { name: 'TekLoon', gender: 'Male', dob: new Date(), loginUsing: 'Facebook' };
describe('User Model Test', () => {
// It's just so easy to connect to the MongoDB Memory Server
// By using mongoose.connect
beforeAll(async () => {
await mongoose.connect(global.__MONGO_URI__, { useNewUrlParser: true, useCreateIndex: true }, (err) => {
@tlcheah2
tlcheah2 / flightController.js
Created September 15, 2019 15:32
Business logic for flights
const airRouteModel = require('../models/air_routes');
exports.searchFlight = async (req, res, next) => {
const { from, to } = req.body;
try {
// Return routes with matching origin and destination
const result = await airRouteModel.aggregate([
// Filter air routes based on origin and destination
{
$match: {
@tlcheah2
tlcheah2 / flightRes.json
Created September 15, 2019 15:44
API Response for Flight from Kuala Lumpur, Malaysia to Korea, Incheon Airport
{
"data": [
{
"airline": {
"id": 2417,
"name": "AirAsia X",
"alias": "D7",
"iata": "XAX"
},
"src_airport": "KUL",