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

Void command #1

Merged
merged 3 commits into from
Sep 11, 2023
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
2 changes: 2 additions & 0 deletions secrets/consts.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export const hours_sheet_id = 'googlesheetid'
export const slack_admin_id = "USLACKUSER"
// The ID of a slack channel to celebrate new certs in
export const slack_celebration_channel = "CSLACKCHANNEL"
// The ID of a slack channel that is restricted to managers, for use with /voidhours
export const slack_voider_channel = 'CSLACKCHANNEL'
// The API endpoint of a CLUCK server
export const cluck_baseurl = 'http://localhost:4000'
// The Souper Secret CLUCK Api Key
Expand Down
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
83 changes: 83 additions & 0 deletions src/handlers/void.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import type { SlackCommandMiddlewareArgs, AllMiddlewareArgs } from "@slack/bolt";
import { voidHours } from "../utils/drive";
import { WebClient } from "@slack/web-api";
import { slack_voider_channel } from "../../secrets/consts";


async function isVoider(client:WebClient, user_id:string) {
const conversations = await client.conversations.members({channel:slack_voider_channel})
if (!conversations.ok) {
console.warn(conversations.error)
return {
ok:false,
isVoider: null
}
}
return {
ok: true,
isVoider: conversations.members!.some((member) => member == user_id)
}
}

export async function handleVoidCommand({ command, logger, ack, respond, client }: SlackCommandMiddlewareArgs & AllMiddlewareArgs) {
await ack()
const invokerPermissions = await isVoider(client, command.user_id)
if (!invokerPermissions.ok) {
await respond({response_type:"ephemeral", text:"Something went wrong!"})
return
}
if (!invokerPermissions.isVoider) {
await respond({response_type:"ephemeral", text: "Must be a copresident 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 isVoider(client, target.user!.id!)
if (isTargetManager.isVoider) {
await respond({response_type: "ephemeral", text:"Cannot void hours for other voiders!"})
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 the leadership ⛵. You will need to sign in again when you resume work"})
client.chat.postMessage({channel:slack_voider_channel, text:`<@${command.user_id}> has voided hours for <@${target_id}>`})
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}`})
// }

}
Loading