@@ -10,12 +10,16 @@ import { SmallTalkErrorCode } from '../../../global/exception/smalltalk/SmallTal
1010import { getTodayDateString } from '../util/date' ;
1111import { SmallTalk } from '../domain/entity/SmallTalk' ;
1212import { MutexAlgorithm } from '../../../global/decorator/mutex' ;
13+ import { AlarmService } from 'src/global/alarm/Alarm.Service' ;
14+ import { ChallengeApi } from '../infrastructure/Challenge.Api' ;
1315
1416@Injectable ( )
1517export class SmallTalkService {
1618 constructor (
1719 private readonly smallTalkHelper : SmallTalkHelper ,
1820 private readonly userApi : UserApi ,
21+ private readonly challengeApi : ChallengeApi ,
22+ private readonly alarmService : AlarmService ,
1923 ) { }
2024
2125 public async checkSmallTalk ( challengeId : number , date : string ) : Promise < SmallTalkResult > {
@@ -33,17 +37,46 @@ export class SmallTalkService {
3337 question : string ,
3438 ) : Promise < void > {
3539 await this . validateSmallTalkCount ( challengeId , getTodayDateString ( ) ) ;
36- const userChallengeData =
37- await this . userApi . requestUserChallengeAndAffiliationByChallengeIdWithUserIdAndOrganization (
40+ const [ userChallengeData , challenge ] = await Promise . all ( [
41+ this . userApi . requestUserChallengeAndAffiliationByChallengeIdWithUserIdAndOrganization (
3842 challengeId ,
3943 userId ,
4044 organization ,
41- ) ;
45+ ) ,
46+ this . challengeApi . requestChallengeById ( challengeId ) ,
47+ ] ) ;
4248 await this . smallTalkHelper . executeInsertSmallTalk (
4349 challengeId ,
4450 userChallengeData . getId ( ) ,
4551 question ,
4652 ) ;
53+ const userChallenges =
54+ await this . userApi . requestUserChallengeAndAffiliationAndUserAndFirebaseTokenByChallengeId (
55+ challengeId ,
56+ ) ;
57+ const userFirebaseTokenGroups = userChallenges
58+ . filter ( ( userChallenge ) => userChallenge . getId ( ) !== userChallengeData . getId ( ) )
59+ . map ( ( userChallenge ) => {
60+ const user = userChallenge . affiliation . user ;
61+ return {
62+ userId : user . userId ,
63+ nickName : userChallenge . affiliation . getNickname ( ) ,
64+ firebaseTokens : user . firebaseTokens ?? [ ] ,
65+ } ;
66+ } )
67+ . filter ( ( entry ) => entry . firebaseTokens . length > 0 ) ;
68+ await Promise . all (
69+ userFirebaseTokenGroups . map ( ( group ) => {
70+ const engineValues = group . firebaseTokens . map ( ( token ) => token . engineValue ) ;
71+ return this . alarmService . sendPushAlarm (
72+ group . userId ,
73+ engineValues ,
74+ `🚨${ organization } ${ challenge . getName ( ) } 챌린지 스몰톡 생성🚨$` ,
75+ `${ userChallengeData . getAffiliation ( ) . getNickname ( ) } 님이 스몰톡을 생성했습니다. 확인해보세요😃` ,
76+ 'https://your-url.com' ,
77+ ) ;
78+ } ) ,
79+ ) ;
4780 }
4881
4982 private async validateSmallTalkCount ( challengeId : number , date : string ) {
0 commit comments