2020-08-04 16:43:13 +09:00
|
|
|
import cron from 'node-cron';
|
2020-08-12 13:31:09 +09:00
|
|
|
import delay from 'delay';
|
|
|
|
|
2020-09-08 20:05:22 +09:00
|
|
|
import { Users } from '../models';
|
|
|
|
import { deleteUser } from '../functions/users';
|
2020-09-09 00:48:02 +09:00
|
|
|
import { updateScore } from '../functions/update-score';
|
2020-09-10 01:43:36 +09:00
|
|
|
import { send } from './send';
|
2021-01-06 02:24:35 +09:00
|
|
|
import { Not } from 'typeorm';
|
|
|
|
import { AlertMode } from '../types/AlertMode';
|
2020-08-04 16:43:13 +09:00
|
|
|
|
|
|
|
export default (): void => {
|
2020-09-09 00:53:50 +09:00
|
|
|
cron.schedule('0 0 0 * * *', async () => {
|
2021-01-06 02:24:35 +09:00
|
|
|
const users = await Users.find({
|
|
|
|
alertMode: Not<AlertMode>('nothing'),
|
|
|
|
});
|
2020-08-04 16:43:13 +09:00
|
|
|
for (const user of users) {
|
|
|
|
try {
|
2020-09-10 01:43:36 +09:00
|
|
|
await send(user);
|
2020-08-04 16:43:13 +09:00
|
|
|
} catch (e) {
|
|
|
|
if (e.code === 'NO_SUCH_USER' || e.code === 'AUTHENTICATION_FAILED') {
|
|
|
|
// ユーザーが削除されている場合、レコードからも消してとりやめ
|
|
|
|
console.info(`${user.username}@${user.host} is deleted, so delete this user from the system`);
|
|
|
|
await deleteUser(user.username, user.host);
|
|
|
|
} else {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2020-08-12 13:31:09 +09:00
|
|
|
} finally {
|
2020-09-09 00:48:02 +09:00
|
|
|
if (user.alertMode === 'note')
|
|
|
|
await delay(3000);
|
2020-09-10 01:43:36 +09:00
|
|
|
await updateScore(user);
|
2020-08-04 16:43:13 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|