Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added links #1

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion backend/controllers/apiController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const asyncHandler = require('express-async-handler')

// Models
const Joke = require('../models/joke');

exports.get_joke = asyncHandler(async (req, res, next) => {
Expand Down
26 changes: 24 additions & 2 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ const mongoose = require('mongoose')
const requireAuth = require('./middlewares/requireAuth');
const dotenv = require('dotenv')
const createError = require('http-errors');
const bodyParser = require('body-parser')
const cron = require('node-cron');
const { sendEmail } = require('./tasks');
const Joke = require('./models/joke');
const User = require('./models/user')

dotenv.config()

// DB
Expand All @@ -29,8 +35,8 @@ const app = express()
// Middleware
app.use(cors())
app.use(morgan('common'))
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

// URLs
app.use('/', indexRouter)
Expand All @@ -41,6 +47,22 @@ app.use((req, res, next) => {
createError(404);
});

// cronjobs
cron.schedule('* * * * *', async () => {
let joke = await Joke.aggregate([{ $sample: { size: 1}}])
joke = joke.length > 0 ? joke[0] : null
if (joke) {
const users = await User.find({})
for (let user of users) {
sendEmail({
to: user.email,
subject: 'Cringomba! Your joke of the day',
body: joke.text
})
}
}
});

const port = process.env.PORT || 8000
app.listen(port, () => {
console.log('Server is listening on ' + port)
Expand Down
Loading
Loading