-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve messages and midnight task scheduling
- Loading branch information
Showing
5 changed files
with
48 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,41 @@ | ||
import { Prisma } from '@prisma/client' | ||
import schedule from 'node-schedule' | ||
import logger from '~lib/logger' | ||
import prisma from '~lib/prisma' | ||
import { emitCluckChange } from '~lib/sockets' | ||
import { slack_client } from '~slack' | ||
import responses from '~slack/blocks/responses' | ||
|
||
export function setupAutoLogout() { | ||
schedule.scheduleJob('Midnight Logout', '0 0 * * *', async (date) => { | ||
const loggedIn = await prisma.hourLog.findMany({ | ||
where: { | ||
state: 'pending', | ||
type: 'lab' | ||
}, | ||
select: { time_in: true, Member: { select: { slack_id: true } } } | ||
}) | ||
for (const log of loggedIn) { | ||
try { | ||
const slack_id = log.Member.slack_id | ||
if (slack_id) { | ||
await slack_client.chat.postMessage({ | ||
...responses.autoSignoutDM({ slack_id, time_in: log.time_in }), | ||
channel: log.Member.slack_id | ||
}) | ||
} | ||
} catch (e) { | ||
logger.warn(e) | ||
export async function logoutAll() { | ||
const loggedIn = await prisma.hourLog.findMany({ | ||
where: { | ||
state: 'pending', | ||
type: 'lab' | ||
}, | ||
select: { time_in: true, Member: { select: { slack_id: true, email: true } } } | ||
}) | ||
for (const log of loggedIn) { | ||
try { | ||
const slack_id = log.Member.slack_id | ||
emitCluckChange({ email: log.Member.email, logging_in: false }) | ||
if (slack_id) { | ||
await slack_client.chat.postMessage({ | ||
...responses.autoSignoutDM({ slack_id, time_in: log.time_in }), | ||
channel: slack_id | ||
}) | ||
} | ||
} catch (e) { | ||
logger.warn(e) | ||
} | ||
} | ||
await prisma.hourLog.updateMany({ | ||
where: { | ||
state: 'pending', | ||
type: 'lab' | ||
}, | ||
data: { | ||
state: 'cancelled', | ||
time_out: new Date(), | ||
duration: new Prisma.Decimal(0) | ||
} | ||
await prisma.hourLog.updateMany({ | ||
where: { | ||
state: 'pending', | ||
type: 'lab' | ||
}, | ||
data: { | ||
state: 'cancelled', | ||
time_out: new Date(), | ||
duration: new Prisma.Decimal(0) | ||
} | ||
}) | ||
}) | ||
} |