-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[recnet-api] Initialize multi-distributing channels and slack integra…
…tion (#344) ## Description This is the very first PR for kicking off the multi-distributing weekly digest channels and slack integration. I designed the new architecture with a subscription data model that supports the potential scaling on two dimensions: sending types and channels. For more information, please refer to the design doc on [Notion](https://www.notion.so/Multiple-Distributing-Channels-Slack-Integration-61323d4345c547cb869129e117c5d722). Here are the changes included in this PR: 1. Add DB migration file to create the subscription table 2. Integrate Slack API and create a testing API to send direct messages ## Related Issue - #260 - #261 ## Notes <!-- Other thing to say --> ## Test 1. set `SLACK_TOKEN` in your env var (ask me) 2. Run local server 3. Hit `POST /subscriptions/slack/test` with request body ```json { "userId": xxxxx } ``` ## Screenshots (if appropriate): <img width="736" alt="Screenshot 2024-10-27 at 4 34 16 PM" src="https://github.com/user-attachments/assets/33a58cf5-596b-43f1-bb9b-d4c20d28dd85"> ## TODO - [x] Clear `console.log` or `console.error` for debug usage - [ ] Update the documentation `recnet-docs` if needed
- Loading branch information
Showing
21 changed files
with
4,188 additions
and
3,534 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
15 changes: 15 additions & 0 deletions
15
apps/recnet-api/prisma/migrations/20241027001827_add_subscription_table/down.sql
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- DropForeignKey | ||
ALTER TABLE "Subscription" DROP CONSTRAINT "Subscription_userId_fkey"; | ||
|
||
-- AlterTable | ||
ALTER TABLE "User" DROP COLUMN "slackEmail"; | ||
|
||
-- DropTable | ||
DROP TABLE "Subscription"; | ||
|
||
-- DropEnum | ||
DROP TYPE "Channel"; | ||
|
||
-- DropEnum | ||
DROP TYPE "SubscriptionType"; | ||
|
25 changes: 25 additions & 0 deletions
25
apps/recnet-api/prisma/migrations/20241027001827_add_subscription_table/migration.sql
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
-- CreateEnum | ||
CREATE TYPE "Channel" AS ENUM ('EMAIL', 'SLACK'); | ||
|
||
-- CreateEnum | ||
CREATE TYPE "SubscriptionType" AS ENUM ('WEEKLY_DIGEST'); | ||
|
||
-- AlterTable | ||
ALTER TABLE "User" ADD COLUMN "slackEmail" VARCHAR(128); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Subscription" ( | ||
"id" SERIAL NOT NULL, | ||
"userId" VARCHAR(64) NOT NULL, | ||
"type" "SubscriptionType" NOT NULL, | ||
"channel" "Channel" NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
||
CONSTRAINT "Subscription_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Subscription_userId_type_channel_key" ON "Subscription"("userId", "type", "channel"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Subscription" ADD CONSTRAINT "Subscription_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
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
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { HttpStatus, Inject, Injectable } from "@nestjs/common"; | ||
import { ConfigType } from "@nestjs/config"; | ||
import { WebClient } from "@slack/web-api"; | ||
|
||
import { SlackConfig } from "@recnet-api/config/common.config"; | ||
import UserRepository from "@recnet-api/database/repository/user.repository"; | ||
import { RecnetError } from "@recnet-api/utils/error/recnet.error"; | ||
import { ErrorCode } from "@recnet-api/utils/error/recnet.error.const"; | ||
|
||
@Injectable() | ||
export class SlackService { | ||
private readonly client: WebClient; | ||
|
||
constructor( | ||
@Inject(SlackConfig.KEY) | ||
private readonly slackConfig: ConfigType<typeof SlackConfig>, | ||
private readonly userRepository: UserRepository | ||
) { | ||
this.client = new WebClient(this.slackConfig.token); | ||
} | ||
|
||
public async sendDirectMessage( | ||
userId: string, | ||
message: string | ||
): Promise<void> { | ||
const user = await this.userRepository.findUserById(userId); | ||
const email = user.slackEmail || user.email; | ||
|
||
// Get the user's Slack ID | ||
const userResp = await this.client.users.lookupByEmail({ email }); | ||
const slackId = userResp?.user?.id; | ||
if (!slackId) { | ||
throw new RecnetError( | ||
ErrorCode.SLACK_ERROR, | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
`Failed to get Slack ID` | ||
); | ||
} | ||
|
||
// Open a direct message conversation | ||
const conversationResp = await this.client.conversations.open({ | ||
users: slackId, | ||
}); | ||
const conversationId = conversationResp?.channel?.id; | ||
if (!conversationId) { | ||
throw new RecnetError( | ||
ErrorCode.SLACK_ERROR, | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
`Failed to open conversation` | ||
); | ||
} | ||
|
||
// Send the message | ||
await this.client.chat.postMessage({ | ||
channel: conversationId, | ||
text: message, | ||
}); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
apps/recnet-api/src/modules/subscription/subscription.controller.ts
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Body, Controller, HttpStatus, Inject, Post } from "@nestjs/common"; | ||
import { ConfigType } from "@nestjs/config"; | ||
import { | ||
ApiBody, | ||
ApiCreatedResponse, | ||
ApiOperation, | ||
ApiTags, | ||
} from "@nestjs/swagger"; | ||
|
||
import { AppConfig } from "@recnet-api/config/common.config"; | ||
import { RecnetError } from "@recnet-api/utils/error/recnet.error"; | ||
import { ErrorCode } from "@recnet-api/utils/error/recnet.error.const"; | ||
|
||
import { SlackService } from "./slack.service"; | ||
|
||
@ApiTags("subscriptions") | ||
@Controller("subscriptions") | ||
export class SubscriptionController { | ||
constructor( | ||
@Inject(AppConfig.KEY) | ||
private readonly appConfig: ConfigType<typeof AppConfig>, | ||
private readonly slackService: SlackService | ||
) {} | ||
|
||
/* Development only */ | ||
@ApiOperation({ | ||
summary: "Send weekly digest slack to the designated user.", | ||
description: "This endpoint is for development only.", | ||
}) | ||
@ApiCreatedResponse() | ||
@ApiBody({ | ||
schema: { | ||
properties: { | ||
userId: { type: "string" }, | ||
}, | ||
required: ["userId"], | ||
}, | ||
}) | ||
@Post("slack/test") | ||
public async testSendingWeeklyDigest( | ||
@Body("userId") userId: string | ||
): Promise<void> { | ||
if (this.appConfig.nodeEnv === "production") { | ||
throw new RecnetError( | ||
ErrorCode.INTERNAL_SERVER_ERROR, | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"This endpoint is only for development" | ||
); | ||
} | ||
return this.slackService.sendDirectMessage(userId, "Test message"); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
apps/recnet-api/src/modules/subscription/subscription.module.ts
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Module } from "@nestjs/common"; | ||
|
||
import { DbRepositoryModule } from "@recnet-api/database/repository/db.repository.module"; | ||
|
||
import { SlackService } from "./slack.service"; | ||
import { SubscriptionController } from "./subscription.controller"; | ||
|
||
@Module({ | ||
controllers: [SubscriptionController], | ||
providers: [SlackService], | ||
imports: [DbRepositoryModule], | ||
}) | ||
export class SubscriptionModule {} |
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 |
---|---|---|
|
@@ -26,6 +26,11 @@ | |
"env": { | ||
"jest": true | ||
} | ||
}, | ||
{ | ||
"files": ["*.json"], | ||
"parser": "jsonc-eslint-parser", | ||
"rules": {} | ||
} | ||
] | ||
} |
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
Oops, something went wrong.