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

[recnet-api] Update slack message template #359

Merged
merged 8 commits into from
Nov 18, 2024
Merged

Conversation

swh00tw
Copy link
Collaborator

@swh00tw swh00tw commented Nov 17, 2024

Description

Beautify slack weekly digest template

Related Issue

Notes

Misc: I think we should add email and slack testing API endpoints back to ease the development, but I am not sure where to add it, maybe create subscription.controller.ts again? @joannechen1223

Test

You will need to add back the POST /subsriptions/slack/test endpoint to test. I will provide example code below in the comment to show how I use mock data to test it.

Screenshots (if appropriate):

The data in the screenshot were generated by generateMock().

Screenshot 2024-11-16 at 11 43 14 PM

TODO

  • Clear console.log or console.error for debug usage
  • Update the documentation recnet-docs if needed

Copy link

vercel bot commented Nov 17, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
recnet ⬜️ Ignored (Inspect) Visit Preview Nov 18, 2024 1:46am
recnet-docs ⬜️ Ignored (Inspect) Visit Preview Nov 18, 2024 1:46am

@swh00tw
Copy link
Collaborator Author

swh00tw commented Nov 17, 2024

Example subscription.controller.ts for testing endpoint.

import { generateMock } from "@anatine/zod-mock";
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 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";

import { getLatestCutOff } from "@recnet/recnet-date-fns";

import { announcementSchema, recSchema } from "@recnet/recnet-api-model";

import { WeeklyDigestContent } from "./subscription.type";

import { SlackService } from "../slack/slack.service";

@ApiTags("subscriptions")
@Controller("subscriptions")
export class SubscriptionController {
  constructor(
    @Inject(AppConfig.KEY)
    private readonly appConfig: ConfigType<typeof AppConfig>,
    private readonly slackService: SlackService,
    private readonly userRepository: UserRepository
  ) {}

  /* 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"
      );
    }

    function getMockWeeklyDigestData(): WeeklyDigestContent {
      const getMockRec = (title = 1) =>
        generateMock(recSchema, {
          stringMap: {
            photoUrl: () => "https://avatar.iran.liara.run/public",
            title: () => `Paper Title ${title}`,
          },
        });
      const announcement = generateMock(announcementSchema, {
        stringMap: {
          content: () => "This is a test announcement!",
        },
      });
      return {
        recs: [getMockRec(), getMockRec(2), getMockRec(3), getMockRec()],
        numUnusedInviteCodes: 3,
        latestAnnouncement: {
          ...announcement,
          startAt: new Date(announcement.startAt),
          endAt: new Date(announcement.endAt),
        },
      };
    }

    const cutoff = getLatestCutOff();
    const user = await this.userRepository.findUserById(userId);
    const content = getMockWeeklyDigestData();

    this.slackService.sendWeeklyDigest(user, content, cutoff);
  }
}

@@ -97,10 +97,12 @@ export class SlackTransporter {
);
}

const notificationText = `📬 Your RecNet weekly digest has arrived!`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This SlackTransporter class is designed to send messages for general purposes. I suggest putting this text into the template to create a better logic boundary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I forget to extract this part.

@joannechen1223
Copy link
Collaborator

I'll add the test subscription API back once this PR is merged.

@swh00tw swh00tw merged commit ac328f3 into dev Nov 18, 2024
4 checks passed
joannechen1223 added a commit that referenced this pull request Nov 19, 2024
## RecNet auto-release action
This is an auto-generated PR by recnet-release-action 🤖
Please make sure to test your changes in staging before merging. 
## Related Issues
- #261
- #64
- #356
- #355
## Related PRs
- #359
- #357
- #358
## Staging links
recnet-web:
[https://vercel.live/link/recnet-git-dev-recnet-542617e7.vercel.app](https://vercel.live/link/recnet-git-dev-recnet-542617e7.vercel.app)
recnet-api:
[https://dev-api.recnet.io/api](https://dev-api.recnet.io/api)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants