1
- import { Injectable , Logger } from '@nestjs/common' ;
2
- import { Motivation } from '@src/modules/motivation/entities/motivation.entity' ;
3
- import * as dayjs from 'dayjs' ;
4
- import { CategoryType } from '@src/modules/motivation/movitation.type' ;
5
- import { Cron , CronExpression } from '@nestjs/schedule' ;
6
- import { SlackInteractiveService } from '@src/modules/slack/service/slack.interactive.service' ;
7
- import { InjectRepository } from '@nestjs/typeorm' ;
8
- import { Repository } from 'typeorm' ;
9
- import { UserService } from '@src/modules/user/user.service' ;
10
- import { HolidayService } from '@src/modules/holiday/holiday.service' ;
11
- import { OnlineDatabaseInterfaceService } from '@lib/online-database-interface' ;
12
- import { MotivationModel } from '@lib/online-database-interface/online-database-interface.type' ;
13
- import { CategoryWeight } from '@src/modules/motivation/category.weight' ;
14
- import { ConfigService } from '@nestjs/config' ;
15
- import { getRandomNumber } from '@src/modules/common/utils' ;
1
+ import { Injectable , Logger } from "@nestjs/common" ;
2
+ import { Motivation } from "@src/modules/motivation/entities/motivation.entity" ;
3
+ import * as dayjs from "dayjs" ;
4
+ import { CategoryType } from "@src/modules/motivation/movitation.type" ;
5
+ import { Cron , CronExpression } from "@nestjs/schedule" ;
6
+ import { SlackInteractiveService } from "@src/modules/slack/service/slack.interactive.service" ;
7
+ import { InjectRepository } from "@nestjs/typeorm" ;
8
+ import { Repository } from "typeorm" ;
9
+ import { UserService } from "@src/modules/user/user.service" ;
10
+ import { HolidayService } from "@src/modules/holiday/holiday.service" ;
11
+ import { OnlineDatabaseInterfaceService } from "@lib/online-database-interface" ;
12
+ import { MotivationModel } from "@lib/online-database-interface/online-database-interface.type" ;
13
+ import { CategoryWeight } from "@src/modules/motivation/category.weight" ;
14
+ import { ConfigService } from "@nestjs/config" ;
15
+ import { getRandomNumber } from "@src/modules/common/utils" ;
16
+ import { User } from "@src/modules/user/entities/user.entity" ;
16
17
17
18
@Injectable ( )
18
19
export class MotivationService {
19
20
private readonly logger : Logger = new Logger ( this . constructor . name ) ;
21
+
20
22
constructor (
21
23
@InjectRepository ( Motivation ) private motivationRepository : Repository < Motivation > ,
22
24
private readonly userService : UserService ,
23
25
private readonly holidayService : HolidayService ,
24
26
private readonly configService : ConfigService ,
25
27
private readonly slackInteractiveService : SlackInteractiveService ,
26
- private readonly onlineDatabaseService : OnlineDatabaseInterfaceService ,
27
- ) { }
28
+ private readonly onlineDatabaseService : OnlineDatabaseInterfaceService
29
+ ) {
30
+ }
28
31
29
32
/**
30
33
*
31
34
* @private
32
35
*/
33
36
@Cron ( CronExpression . EVERY_DAY_AT_MIDNIGHT , {
34
- timeZone : ' Asia/Seoul' ,
37
+ timeZone : " Asia/Seoul"
35
38
} )
36
39
private async createConfirmMotivation ( ) {
37
40
try {
38
41
const modelList = await this . onlineDatabaseService . searchConfirmMotivation ( ) ;
39
42
40
43
if ( modelList . length === 0 ) {
41
- this . logger . log ( ' 승인된 추천 글귀가 없습니다.' ) ;
44
+ this . logger . log ( " 승인된 추천 글귀가 없습니다." ) ;
42
45
return ;
43
46
}
44
47
45
48
const makeEntityList = ( resultList : MotivationModel [ ] ) => {
46
49
return resultList . map ( ( { contents, category } ) => {
47
50
return this . motivationRepository . create ( {
48
51
contents : contents ,
49
- category : CategoryType [ category ] ,
52
+ category : CategoryType [ category ]
50
53
} ) ;
51
54
} ) ;
52
55
} ;
@@ -57,7 +60,7 @@ export class MotivationService {
57
60
await this . onlineDatabaseService . updateMotivationRecord ( modelList ) ;
58
61
} catch ( e ) {
59
62
if ( e instanceof Error ) {
60
- this . logger . error ( ' 추천 글귀 추가 과정 중 문제가 발생했습니다.' ) ;
63
+ this . logger . error ( " 추천 글귀 추가 과정 중 문제가 발생했습니다." ) ;
61
64
throw e ;
62
65
}
63
66
}
@@ -68,23 +71,23 @@ export class MotivationService {
68
71
* 지정한 공휴일의 경우 발송하지 않습니다.
69
72
* @private
70
73
*/
71
- @Cron ( ' */10 * * * 1-5' , {
72
- timeZone : ' Asia/Seoul' ,
74
+ @Cron ( " */10 * * * 1-5" , {
75
+ timeZone : " Asia/Seoul"
73
76
} )
74
77
private async sendMotivation ( ) : Promise < void > {
75
78
try {
76
- if ( this . configService . get < string > ( ' APP_ENV' ) !== ' prod' ) {
77
- this . logger . debug ( ' 운영환경이 아니기 때문에 종료합니다.' ) ;
79
+ if ( this . configService . get < string > ( " APP_ENV" ) !== " prod" ) {
80
+ this . logger . debug ( " 운영환경이 아니기 때문에 종료합니다." ) ;
78
81
return ;
79
82
}
80
83
81
- const holiday = await this . holidayService . findOne ( dayjs ( ) . format ( ' YYYYMMDD' ) ) ;
84
+ const holiday = await this . holidayService . findOne ( dayjs ( ) . format ( " YYYYMMDD" ) ) ;
82
85
if ( holiday ) {
83
- this . logger . log ( ' 공휴일이기 때문에 메시지 발송을 종료합니다.' ) ;
86
+ this . logger . log ( " 공휴일이기 때문에 메시지 발송을 종료합니다." ) ;
84
87
return ;
85
88
}
86
89
87
- const time = dayjs ( ) . format ( ' HH:mm' ) ;
90
+ const time = dayjs ( ) . format ( " HH:mm" ) ;
88
91
this . logger . log ( `메시지 수신 대상자를 조회합니다. (${ time } )` ) ;
89
92
const userList = await this . userService . findSubscriberOnPushTime ( time ) ;
90
93
@@ -102,12 +105,16 @@ export class MotivationService {
102
105
await this . slackInteractiveService . postMessage (
103
106
user . channelId ,
104
107
`${ user . name } . 오늘의 메시지가 도착했어요. 오늘 하루도 힘내세요!
105
- >>>${ motivation . contents } ` ,
108
+ >>>${ motivation . contents } `
106
109
) ;
110
+ const now = dayjs ( ) ;
111
+ if ( now . year ( ) === 2024 && now . month ( ) === 10 && now . date ( ) === 13 && ! user . jerry ) {
112
+ await this . slackInteractiveService . postMessage ( user . channelId , await this . getServiceClosedMessage ( user ) ) ;
113
+ }
107
114
}
108
115
this . logger . log ( `${ userList . length } 명에게 메시지 전송 완료. (${ time } )` ) ;
109
116
} catch ( e ) {
110
- if ( e instanceof Error ) this . logger . error ( ' 글귀 발송 과정 중 문제가 발생했습니다.' ) ;
117
+ if ( e instanceof Error ) this . logger . error ( " 글귀 발송 과정 중 문제가 발생했습니다." ) ;
111
118
throw e ;
112
119
}
113
120
}
@@ -123,33 +130,18 @@ export class MotivationService {
123
130
private async getMotivation ( motivationList : Motivation [ ] , category : CategoryType , isModernText : boolean ) {
124
131
const filteredMotivationList = motivationList . filter ( ( item ) => item . category === category ) ;
125
132
if ( isModernText ) {
126
- filteredMotivationList . push ( ...filteredMotivationList . filter ( ( item ) => item . category === CategoryType [ '기타' ] ) ) ;
133
+ filteredMotivationList . push ( ...filteredMotivationList . filter ( ( item ) => item . category === CategoryType [ "기타" ] ) ) ;
127
134
}
128
135
return filteredMotivationList [ Math . floor ( getRandomNumber ( ) * filteredMotivationList . length ) ] ;
129
136
}
130
137
131
- // 이벤트 종료
132
- // /**
133
- // * 1주년 메시지 발송
134
- // * 2023-01-13 11:00 기준 구독자에게 전체 발송
135
- // private async sendEventMessage() {
136
- // const now = dayjs();
137
- // if (now.year() === 2023 && now.month() === 0 && now.date() === 13 && now.hour() === 11 && now.minute() === 0) {
138
- // const message = await this.notionService.searchEasterEgg(process.env.FIRST_YEAR_MESSAGE);
139
- //
140
- // const userList = await this.userRepository.find({
141
- // where: { isSubscribe: true },
142
- // });
143
- // userList.map(async (user) => {
144
- // let newMessage = message.replace(/\${name}/gi, user.name);
145
- // newMessage = newMessage.replace(/\${josa}/gi, isEndWithConsonant(user.name) ? '을' : '를');
146
- // try {
147
- // await this.slackInteractiveService.postMessage(user.channelId, newMessage);
148
- // } catch (e) {
149
- // this.logger.error(`${user.name} 오류`);
150
- // throw e;
151
- // }
152
- // });
153
- // }
154
- // }*/
138
+ /**
139
+ * 서비스 종료 메시지 발송
140
+ * 2024-11-13 기준 구독자에게 전체 발송
141
+ * */
142
+ private async getServiceClosedMessage ( user : User ) {
143
+ await this . userService . updateJerry ( user . id ) ;
144
+ const message = await this . onlineDatabaseService . searchEasterEgg ( "서비스 종료" ) ;
145
+ return message . replace ( / \$ { name} / gi, user . name ) ;
146
+ }
155
147
}
0 commit comments