Files
cal.diy/packages/app-store/slackmessaging/lib/showLinksMessage.ts
T
c03ff0a543 Fixes slack verification and timeout errors (#2972)
* Fixing slack create event

* Fixes verify working 90% of the time :S

* Verify V2

* Fixing verify - not needed on interaction payload

* Fixing verifcation + response

* Fix verifcation

* DM user error/success

* Remove random 200

* Timeout return

* Adding Zod schema verification

* Added validations, cleanup

* Type fixes

* Update event.ts

Co-authored-by: Peer Richelsen <[email protected]>
Co-authored-by: zomars <[email protected]>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2022-06-11 22:30:52 +01:00

50 lines
1.4 KiB
TypeScript

import { Prisma } from "@prisma/client";
import { WebClient } from "@slack/web-api";
import { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { WhereCredsEqualsId } from "./WhereCredsEqualsID";
import slackVerify from "./slackVerify";
import { NoUserMessage } from "./views";
import ShowLinks from "./views/ShowLinks";
export default async function showLinksMessage(req: NextApiRequest, res: NextApiResponse) {
const body = req.body;
await slackVerify(req, res);
const data = await prisma.credential.findFirst({
...WhereCredsEqualsId(body.user_id),
include: {
user: {
select: {
username: true,
eventTypes: {
where: {
hidden: false,
},
select: {
slug: true,
title: true,
},
},
},
},
},
});
if (!data) return res.status(200).json(NoUserMessage);
const slackCredentials = data?.key; // Only one slack credential for user
const access_token = (slackCredentials as Prisma.JsonObject)?.access_token as string;
const slackClient = new WebClient(access_token);
const blocks = JSON.parse(ShowLinks(data.user?.eventTypes, data.user?.username ?? "")).blocks;
slackClient.chat.postMessage({
channel: body.channel_id,
text: `${data.user?.username}'s Cal.com Links`,
blocks,
});
return res.status(200).end();
}