Skip to content

Commit 904ae3b

Browse files
committed
[feat] 스몰톡 생성 시 알림 추가
1 parent 0951853 commit 904ae3b

File tree

8 files changed

+88
-4
lines changed

8 files changed

+88
-4
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { ChallengeHelper } from 'src/domain/challenge/application/helper/Challenge.Helper';
3+
import { Challenge } from 'src/domain/challenge/domain/entity/Challenge';
4+
@Injectable()
5+
export class ChallengeApi {
6+
constructor(private readonly challengeHelper: ChallengeHelper) {}
7+
8+
public async requestChallengeById(challengeId: number): Promise<Challenge> {
9+
return this.challengeHelper.giveChallengeById(challengeId);
10+
}
11+
}

src/domain/smalltalk/infrastructure/User.Api.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,12 @@ export class UserApi {
9393
// 검증 x
9494
return this.affiliationHelper.giveAffiliationAndUserById(affiliationId);
9595
}
96+
97+
public async requestUserChallengeAndAffiliationAndUserAndFirebaseTokenByChallengeId(
98+
challengeId: number,
99+
) {
100+
return this.userChallengeHelper.giveUserChallengeAndAffiliationAndUserAndFirebaseTokenByChallengeId(
101+
challengeId,
102+
);
103+
}
96104
}

src/domain/smalltalk/service/SmallTalk.Service.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ import { SmallTalkErrorCode } from '../../../global/exception/smalltalk/SmallTal
1010
import { getTodayDateString } from '../util/date';
1111
import { SmallTalk } from '../domain/entity/SmallTalk';
1212
import { MutexAlgorithm } from '../../../global/decorator/mutex';
13+
import { AlarmService } from 'src/global/alarm/Alarm.Service';
14+
import { ChallengeApi } from '../infrastructure/Challenge.Api';
1315

1416
@Injectable()
1517
export 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) {

src/domain/smalltalk/service/SmallTalkComment.Service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export class SmallTalkCommentService {
2727
affiliationData.getId(),
2828
agoraComment,
2929
);
30+
// smallTalkId를 통해 challengeId를 구함.
31+
32+
// challengeId를 통해 userChallenge -> affliliation -> user -> firebaseToken을 가져온다.
33+
// 생성한 자신을 제외한, 같은 소속의 유저들에게 알림을 보냄 -> 템플릿 댓글과 같은 로직 수행
3034
}
3135

3236
public async bringSmallTalkCommentRead(

src/domain/smalltalk/smalltalk.module.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ import { SmallTalkDao } from './domain/repository/dao/SmallTalk.Dao';
1313
import { SmallTalkCommentDao } from './domain/repository/dao/SmallTalkComment.Dao';
1414
import { UserApi } from './infrastructure/User.Api';
1515
import { SmallTalkVerifyService } from 'src/global/exception/smalltalk/SmallTalkVerify.Service';
16+
import { AlarmService } from 'src/global/alarm/Alarm.Service';
17+
import { ChallengeApi } from './infrastructure/Challenge.Api';
18+
import { ChallengeModule } from '../challenge/challenge.module';
1619

1720
@Module({
18-
imports: [TypeOrmModule.forFeature([SmallTalk, SmallTalkComment]), UserModule],
21+
imports: [TypeOrmModule.forFeature([SmallTalk, SmallTalkComment]), UserModule, ChallengeModule],
1922
providers: [
2023
{ provide: 'smallTalkImpl', useClass: SmallTalkDao },
2124
{ provide: 'smallTalkCommentImpl', useClass: SmallTalkCommentDao },
@@ -24,7 +27,9 @@ import { SmallTalkVerifyService } from 'src/global/exception/smalltalk/SmallTalk
2427
SmallTalkHelper,
2528
SmallTalkCommentHelper,
2629
UserApi,
30+
ChallengeApi,
2731
SmallTalkVerifyService,
32+
AlarmService,
2833
],
2934
controllers: [SmallTalkController, SmallTalkCommentController],
3035
exports: [SmallTalkHelper, SmallTalkCommentHelper],

src/domain/user/domain/repository/UserChallenge.Repository.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,7 @@ export interface UserChallengeRepository extends Repository<UserChallenge> {
5050
findUserChallengeByChallengeId(challengeId: number): Promise<UserChallenge[]>;
5151
updateUserChallengeDeposit(challengeDeposit: ChallengeDeposit[]): Promise<void>;
5252
findUserChallengeById(userChallengeId: number): Promise<UserChallenge>;
53+
findUserChallengeAndAffiliationAndUserAndFirebaseTokenByChallengeId(
54+
challengeId: number,
55+
): Promise<UserChallenge[]>;
5356
}

src/domain/user/domain/repository/dao/UserChallenge.Dao.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,18 @@ export class UserChallengeDao extends Repository<UserChallenge> implements UserC
237237
})
238238
.getOne();
239239
}
240+
241+
async findUserChallengeAndAffiliationAndUserAndFirebaseTokenByChallengeId(
242+
challengeId: number,
243+
): Promise<UserChallenge[]> {
244+
return this.dataSource
245+
.createQueryBuilder()
246+
.select('uc')
247+
.from(UserChallenge, 'uc')
248+
.innerJoinAndSelect('uc.affiliation', 'a', 'a.affiliation_id = uc.affiliation_id')
249+
.innerJoinAndSelect('a.user', 'u', 'u.user_id = a.user_id')
250+
.leftJoinAndSelect('u.firebaseTokens', 'ft', 'ft.user_id = u.user_id')
251+
.where('uc.challenge_id = :challengeId', { challengeId })
252+
.getMany();
253+
}
240254
}

src/domain/user/helper/UserChallenge.Helper.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,10 @@ export class UserChallengeHelper {
185185
async executeInsertWritonerChallenge(affiliation: Affiliation) {
186186
return this.writoner.execute(affiliation);
187187
}
188+
189+
async giveUserChallengeAndAffiliationAndUserAndFirebaseTokenByChallengeId(challengeId: number) {
190+
return this.userChallengeRepository.findUserChallengeAndAffiliationAndUserAndFirebaseTokenByChallengeId(
191+
challengeId,
192+
);
193+
}
188194
}

0 commit comments

Comments
 (0)