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

feat: the ability to do modify services via running a cron #219

Merged
merged 1 commit into from
Sep 18, 2022
Merged
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
15 changes: 15 additions & 0 deletions src/apps/api/v1/cache/cache.queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,18 @@ export async function deleteAllCachesOfAUser(user_id) {
});
});
}

/**
* It deletes all the keys in Redis that match the pattern "user-id-.-request-download-user-data".
* @returns a promise that resolves to the value of the redis.get() call.
*/
export async function clearDownloadUserDataRequestCounts() {
redis.keys('*', function (err, keys) {
if (err) return null;
keys.forEach((key) => {
if (key.match(/user-id-.-request-download-user-data/)) {
redis.del(key);
}
});
});
}
14 changes: 14 additions & 0 deletions src/apps/api/v1/users/users.queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import db from '../../../../database/db.js';
import logger from '../../../../utils/logger.js';
import { red } from '../../../../utils/rainbow-log.js';
import { pick, omit } from 'lodash-es';
import dayjs from 'dayjs';

/**
* Get all users from the database.
Expand Down Expand Up @@ -186,3 +187,16 @@ export function restoreUserData(user_id) {
export function postRestoreUser(user_id) {
return db.update({ deleted: false }).from('users').where({ id: user_id }).returning('*');
}

/**
* Get all users whose birthday is today.
* @returns An array of objects.
*/
export function getAllUsersWhoseBirthdayIsToday() {
const TODAY = dayjs().format('YYYY-MM-DD');
return db
.select('*')
.from('users')
.leftJoin('user_details', 'users.id', 'user_details.user_id')
.andWhere({ birth_date: TODAY });
}
4 changes: 4 additions & 0 deletions src/bin/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Chad from '../utils/chad.js';
import pkg from '../utils/pkg.js';
import EmailService from '../services/email.service.js';
import latestChangelog from '../utils/latest-changelog.js';
import CronsServices from '../services/cron.services.js';

app.listen(port, () => {
logger.warn(`Server is on ${env} mode!`);
Expand Down Expand Up @@ -109,3 +110,6 @@ app.listen(port, () => {
Chad.flex(e.message, e.stack);
}
})();

// ------------------------------ crons ------------------------------
CronsServices.start();
1 change: 0 additions & 1 deletion src/services/cron.service.js

This file was deleted.

22 changes: 22 additions & 0 deletions src/services/cron.services.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import resetDownloadUserDataRequestCron from './crons/reset-download-user-data.cron.js';
import sendHappyBirthdayEmailCron from './crons/happy-birthday.cron.js';

import Logger from '../utils/logger.js';
import cron from 'node-cron';

export default class CronsServices {
static async start() {
try {
Logger.info(`Crons services was started!`);

// everyday at mid night
cron.schedule('0 0 * * *', resetDownloadUserDataRequestCron).start();

// everyday at mid night
cron.schedule('0 0 * * *', sendHappyBirthdayEmailCron).start();
} catch (e) {
Logger.error(e.message);
Chad.flex(e.message, e);
}
}
}
29 changes: 29 additions & 0 deletions src/services/crons/happy-birthday.cron.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Logger from '../../utils/logger.js';

import EmailServices from '../../services/email.service.js';
import * as UsersServices from '../../apps/api/v1/users/users.queries.js';

export default async function sendHappyBirthdayEmailCron() {
try {
Logger.info('sendHappyBirthdayEmailCron() cron has started!');

const users = await UsersServices.getAllUsersWhoseBirthdayIsToday();

await Promise.all(
users.map((user) => {
return EmailServices.send({
to: user.email,
subject: 'Happy Birthday',
template: 'happy-birthday',
data: {
username: user.username,
},
});
}),
);

Logger.info('sendHappyBirthdayEmailCron() cron has finished!');
} catch (e) {
Logger.error(e.message);
}
}
14 changes: 14 additions & 0 deletions src/services/crons/reset-download-user-data.cron.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Logger from '../../utils/logger.js';
import * as CachesQueries from '../../apps/api/v1/cache/cache.queries.js';

export default async function resetDownloadUserDataRequestCron() {
try {
Logger.info('resetDownloadUserDataRequestCron() cron has started!');

await CachesQueries.clearDownloadUserDataRequestCounts();

Logger.info('resetDownloadUserDataRequestCron() cron has finished!');
} catch (e) {
Logger.error(e.message);
}
}
3 changes: 2 additions & 1 deletion src/services/email.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import nodemailer from 'nodemailer';
import { email } from '../config/env.js';
import Chad from '../utils/chad.js';
import logger from '../utils/logger.js';
import { red } from '../utils/rainbow-log.js';
import Template from './emails/template.js';

// use https://ethereal.email/ for testing purposes
Expand Down Expand Up @@ -50,6 +49,8 @@ export default class EmailService {

if (!sent) throw new Error('Something went wrong while sending email!');

logger.info(`${template} email was sent to ${to}!`);

return sent;
} catch (e) {
logger.error(e);
Expand Down