Skip to content

Commit

Permalink
feat: add /voidhours command
Browse files Browse the repository at this point in the history
  • Loading branch information
rutmanz committed Sep 11, 2023
1 parent 97ded93 commit 5105269
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { handleLogCommand, handleLogModal, handleLogShortcut } from "./log";
import { handleLogoutCommand } from "./logout";
import { handleRejectButton, handleRejectModal } from "./reject";
import { handleOpenSettingsModal, handleSettingsSave } from "./settings";
import { handleVoidCommand } from "./void";

export function register_listeners(app:App) {
// Commands and Shortcuts
app.command('/log', handleLogCommand)
app.command('/graph', handleGraphCommand)
app.command('/clearlogin', handleLogoutCommand)
app.command("/voidtime", handleVoidCommand)
app.shortcut('log_hours', handleLogShortcut)

// Buttons
Expand Down
82 changes: 82 additions & 0 deletions src/handlers/void.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import type { SlackCommandMiddlewareArgs, AllMiddlewareArgs } from "@slack/bolt";
import { voidHours } from "../utils/drive";
import { certs } from "../utils/data";

Check warning on line 3 in src/handlers/void.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

src/handlers/void.ts#L3

[@typescript-eslint/no-unused-vars] 'certs' is defined but never used.
import { WebClient } from "@slack/web-api";
import { readFileSync } from 'fs';

Check warning on line 5 in src/handlers/void.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

src/handlers/void.ts#L5

[@typescript-eslint/no-unused-vars] 'readFileSync' is defined but never used.

async function isManager(client:WebClient, user_id:string) {
const conversations = await client.conversations.members({channel:"C05R9UGJDM5"}) //TODO: Put in constants file
if (!conversations.ok) {
console.warn(conversations.error)
return {
ok:false,
isManager: null
}
}
return {
ok: true,
isManager: conversations.members!.some((member) => member == user_id)
}
}

export async function handleVoidCommand({ command, logger, ack, respond, client }: SlackCommandMiddlewareArgs & AllMiddlewareArgs) {
await ack()
const isInvokerManager = await isManager(client, command.user_id)
if (!isInvokerManager.ok) {
await respond({response_type:"ephemeral", text:"Something went wrong!"})
return
}
if (!isInvokerManager.isManager) {
await respond({response_type:"ephemeral", text: "Must be a manager to run this command"})
return
}

try {
const target_match = command.text.match(/<@([\w\d]+)\|.+>/)
if (target_match == null) {
await respond({response_type:"ephemeral", text:`Please provide the user in the form of a mention (like <@${command.user_id}>)`})
return
}
const target_id = target_match![1]
const target = await client.users.info({user:target_id})

if (!target.ok) {
await respond({response_type:"ephemeral", text:`Could not find user <@${target_id}>`})
return
}
const isTargetManager = await isManager(client, target.user!.id!)
if (isTargetManager.isManager) {
await respond({response_type: "ephemeral", text:"Cannot void hours for other managers!"})
return
}
const name = target.user!.real_name!
const status = await voidHours(name)
switch (status) {
case 200:
await respond({response_type:"ephemeral", text: `Successfully voided hours for ${name}`})
console.log(`${command.user_name} has voided hours for ${name}`)
client.chat.postMessage({channel:target.user!.id!, text:"Your current time sheet session has been voided by a manager. You will need to sign in again when you resume work"})
break;
case 422:
await respond({response_type:"ephemeral", text: `${name} is not logged in`})
break;
default:
await respond({response_type:"ephemeral", text: `Could not void hours for ${name}: ${status}`})
}
} catch (e) {
logger.error(e)
await respond({response_type:"ephemeral", text: `Could not parse arguments: ${e}`})
return
}
// switch (status) {
// case 200:
// await respond({response_type:"ephemeral", text: `Successfully cleared login, you are no longer signed in`})
// break;
// case 422:
// await respond({response_type:"ephemeral", text: `You are not logged in`})
// break;
// default:
// await respond({response_type:"ephemeral", text: `Could not void hours: ${status}`})
// }

}

0 comments on commit 5105269

Please sign in to comment.