Skip to content

Commit

Permalink
call once a day
Browse files Browse the repository at this point in the history
  • Loading branch information
L04DB4L4NC3R committed Apr 29, 2020
1 parent fadaeb2 commit bea36e0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 42 deletions.
36 changes: 20 additions & 16 deletions app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// express server
import express from "express";
// import VoiceResponse = require("twilio").twiml.VoiceResponse;

// database
import mongoose from "mongoose";
Expand All @@ -17,6 +16,10 @@ import {
const dotenv = require("dotenv");
dotenv.config();

// Func to call people daily
import { InfoAPICronFunc } from "./src/pkg/outbound/cron";

import { MongoRepo } from "./src/pkg/user/mongodb";

// initializing express server
const app : express.Application = express();
Expand All @@ -36,16 +39,6 @@ app.get("/ping", (req, res, next) => {
// application endpoint handlers
app.use(`${process.env.API_VERSION}/user`, UsersRouter);

/*
app.post("/voice", (req, res, next) => {
const twiml = new VoiceResponse();
twiml.say({ voice: "alice" }, "Hello, World!");
response.type('"text/xml"');
response.send(twiml.toString());
});
*/

// mongoDB connection promise
mongoose.connect(<string>process.env.DB_URI, {
useNewUrlParser: true,
Expand Down Expand Up @@ -77,9 +70,20 @@ app.listen(port, () => {
console.info(
`Serving on port ${port}`
);
/*
setInterval(() => {
// Function to be repeated HERE
}, 1000 * 2);
*/

let userRepo = new MongoRepo();
setInterval(async () => {
console.log("Calling subscribers");
userRepo.ShowAllPhoneNumbers(0, 0)
.then(nums => {
let numArray = [];
for(let n of nums) {
numArray.push(n.phoneNumber)
}
InfoAPICronFunc(numArray)
.then(console.log)
.catch(console.error)
})
.catch(console.error)
}, 1000 * 60 * 60 * 24);
});
35 changes: 15 additions & 20 deletions src/pkg/outbound/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,23 @@ import {
// https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/cron
// or npm i cron @types/cron
// TODO: to be repeated daily
function InfoAPICronFunc() {
return new Promise((resolve, reject) => {
export function InfoAPICronFunc(nums: string[]): any {
return new Promise((resolve, reject) => {
let ob = new Outbound();

Promise.all([ob.fetchSubscribers(), ob.fetchInfoAPI()])
.then((data: any) => {
// because max twiml size is 4000
ob.callAll(data[0].slice(0, 3900), data[1])
.then((returnedData) => {
resolve(returnedData);
})
.catch(error => {
console.error(error);
reject(error);
})
}).catch(error => {
console.error(error);
reject(error);
})
})
ob.fetchInfoAPI()
.then((response: any) => {
let str = "";
for(let news of response) {
str += ". " + news.description;
}
// 4000 is max twiml size
let truncate = str.slice(0, 3900);
ob.callAll(nums, truncate)
.then(resolve)
.catch(reject)
}).catch(reject)
})
}

// for testing
export function Probe(): any {
return new Promise((resolve, reject) => {
Expand Down
1 change: 1 addition & 0 deletions src/pkg/outbound/on_demand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "express";
import { MongoRepo } from "../user/mongodb";
import request from "request";

const client = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);

let userRepo = new MongoRepo();
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/user/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class MongoRepo implements Repository {
})
}
public ShowAllRequests(skip: number, limit: number) {
return UserModel.find({}, {_id: 0, requests: 1, password: 0})
return UserModel.find({}, {_id: 0, requests: 1})
.skip(skip)
.limit(limit)
.exec()
Expand All @@ -122,7 +122,7 @@ export class MongoRepo implements Repository {
})
}
public ShowAllPhoneNumbers(skip: number, limit: number) {
return UserModel.find({subscribed: true}, {_id: 0, phoneNumber: 1, password: 0})
return UserModel.find({subscribed: true}, {_id: 0, phoneNumber: 1})
.skip(skip)
.limit(limit)
.exec()
Expand Down
26 changes: 22 additions & 4 deletions test/call_probe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { Probe } from "../src/pkg/outbound/cron";
import { InfoAPICronFunc } from "../src/pkg/outbound/cron";
import mongoose from "mongoose";
//
// dotenv
const dotenv = require("dotenv");
dotenv.config("../.env");

// mongoDB connection promise
mongoose.connect(<string>process.env.DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
}).then(() => {
console.log("Connected to MongoDB")
InfoAPICronFunc([])
.then(console.log)
.catch(console.error)
}).catch((err: any) => {
console.error(err);
process.exit(1);
})

Probe()
.then(console.log)
.catch(console.error)

0 comments on commit bea36e0

Please sign in to comment.