Skip to content

Commit

Permalink
Merge pull request #20 from mju-likelion/feature/slackbot
Browse files Browse the repository at this point in the history
방생성시 슬랙 알림 기능 추가
  • Loading branch information
kMinsAlgorithm authored Sep 10, 2023
2 parents db1076f + 30c1889 commit 8e27cd6
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 5 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
"@nestjs/mapped-types": "*",
"@nestjs/passport": "^10.0.0",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/schedule": "^3.0.3",
"@prisma/client": "4.16.1",
"@slack/web-api": "^6.9.0",
"bcrypt": "^5.1.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"date-fns": "^2.30.0",
"lodash": "^4.17.21",
"nanoid": "^3.3.6",
"node-cron": "^3.0.2",
"passport": "^0.6.0",
"passport-jwt": "^4.0.1",
"passport-local": "^1.0.0",
Expand Down
3 changes: 2 additions & 1 deletion src/api/rooms/rooms.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Module } from '@nestjs/common';

import { PrismaModule } from '@/prisma/prisma.module';
import { SlackbotModule } from '@/slackbot/slackbot.module';

import { RoomsService } from './rooms.service';
import { RoomsController } from './rooms.controller';

@Module({
imports: [PrismaModule],
imports: [PrismaModule, SlackbotModule],
controllers: [RoomsController],
providers: [RoomsService],
exports: [RoomsService],
Expand Down
8 changes: 6 additions & 2 deletions src/api/rooms/rooms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import uniq from 'lodash/uniq';
import { customAlphabet } from 'nanoid';

import { PrismaService } from '@/prisma/prisma.service';
import { SlackbotService } from '@/slackbot/slackbot.service';

import { CreateRoomDto } from './dto/create-room.dto';

const nanoid = customAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', 6);

@Injectable()
export class RoomsService {
constructor(private readonly prismaService: PrismaService) {}
constructor(
private readonly slackbotService: SlackbotService,
private readonly prismaService: PrismaService
) {}

private convertStringToDate(stringDate: string): Date {
return new Date(stringDate);
Expand Down Expand Up @@ -57,7 +61,7 @@ export class RoomsService {
*/
async create(createRoomDto: CreateRoomDto): Promise<Room> {
this.validateDates(createRoomDto);

this.slackbotService.sendSlackCreateRoomNotific();
return this.prismaService.room.create({
data: {
code: nanoid(),
Expand Down
5 changes: 4 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import { PrismaModule } from '@/prisma/prisma.module';
import { HealthModule } from '@/health/health.module';

import { AuthModule } from './api/auth/auth.module';
import { SlackbotModule } from './slackbot/slackbot.module';
import authConfig from './config/authConfig';
import slackbotConfig from './config/slackbotConfig';

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [authConfig],
load: [authConfig, slackbotConfig],
}),
PrismaModule,
RoomsModule,
UsersModule,
AuthModule,
HealthModule,
SlackbotModule,
],
controllers: [],
providers: [],
Expand Down
7 changes: 7 additions & 0 deletions src/config/slackbotConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { registerAs } from '@nestjs/config';

export default registerAs('slackbot', () => ({
slackbotTk: process.env.SLACKBOT_TOKEN,
slackbotUserTk: process.env.SLACK_USER_TOKEN,
slackbotReportChannel: process.env.SLACKBOT_REPORT_CHANNEL,
}));
13 changes: 13 additions & 0 deletions src/slackbot/slackbot.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common';
import { ScheduleModule } from '@nestjs/schedule';

import { PrismaModule } from '@/prisma/prisma.module';

import { SlackbotService } from './slackbot.service';

@Module({
imports: [PrismaModule, ScheduleModule.forRoot()],
providers: [SlackbotService],
exports: [SlackbotService],
})
export class SlackbotModule {}
61 changes: 61 additions & 0 deletions src/slackbot/slackbot.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Inject, Injectable } from '@nestjs/common';
import { ConfigType } from '@nestjs/config';
import { WebClient } from '@slack/web-api';
import slackbotConfig from 'src/config/slackbotConfig';
import * as cron from 'node-cron';

import { PrismaService } from '@/prisma/prisma.service';

@Injectable()
export class SlackbotService {
constructor(
@Inject(slackbotConfig.KEY)
readonly config: ConfigType<typeof slackbotConfig>,
private readonly prismaService: PrismaService
) {
// 한국 시간에 맞춰서 매일 자정에 sendDailyRoomCount 함수를 실행합니다.
cron.schedule('0 0 * * *', this.sendDailyRoomCount.bind(this), {
timezone: 'Asia/Seoul',
});
}
private async getTodayRoomCount(): Promise<number> {
// 오늘 날짜의 시작과 끝 시간을 구합니다.
const startOfDay = new Date();
startOfDay.setHours(0, 0, 0, 0);

const endOfDay = new Date();
endOfDay.setHours(23, 59, 59, 999);

// Prisma를 사용하여 해당 범위에 있는 레코드의 개수를 검색합니다.
const count = await this.prismaService.room.count({
where: {
createdAt: {
gte: startOfDay,
lte: endOfDay,
},
},
});

return count;
}

async sendSlackCreateRoomNotific() {
const client = new WebClient(this.config.slackbotTk);

await client.chat.postMessage({
channel: this.config.slackbotReportChannel,
text: `방금 새로운 약속방이 생성되었습니다!`,
});
return;
}

async sendDailyRoomCount() {
const client = new WebClient(this.config.slackbotTk);
const dailyRoomCount = await this.getTodayRoomCount();

await client.chat.postMessage({
channel: this.config.slackbotReportChannel,
text: `오늘 하루 총 방 생성 개수는 ${dailyRoomCount}개입니다.`,
});
}
}
Loading

0 comments on commit 8e27cd6

Please sign in to comment.