Skip to content

Commit

Permalink
Merge pull request #73 from depromeet/dev
Browse files Browse the repository at this point in the history
🔨 fix(alarm) : 채팅알람 로직 수정 ( 알림 상태확인 )
  • Loading branch information
ImNM authored May 28, 2022
2 parents 2564211 + 934a48c commit c8c224a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 20 deletions.
16 changes: 15 additions & 1 deletion src/apis/alarm/alarm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { AlarmPaginationShowDto } from './dto/alarmPaginationShow.dto';
import { FcmService } from 'src/fcm/fcm.service';
import { LetterRoomIdDto } from 'src/common/dtos/LetterRoomId.dto';
import { Alarm } from 'src/models/alarm.model';
import { ChatAlarmSubDto } from './dto/chatAlarm.sub.dto';
@Injectable()
export class AlarmService {
constructor(
Expand Down Expand Up @@ -90,6 +91,8 @@ export class AlarmService {
// const redis = await this.pushAlarmQueue.isReady();
// console.log('check', redis);
await this.pushAlarmQueue.add(PUSH_ALARM_TYPE.LETTER, sendPushAlarmObj);

// await this.pushAlarmQueue.add(PUSH_ALARM_TYPE.CHAT, sendPushAlarmObj);
}

// 다른사람이 나한테 번개를 줬을 때
Expand Down Expand Up @@ -126,7 +129,7 @@ export class AlarmService {
}

// 내 질문에 댓글 달렸을 때 ( 내 댓글이면 제외 시켜야함. (이또한 책임을 알람 서비스로 넘김 ))

//TODO : 댓글알림시에는 댓글 알림 id 도 필드에 보내줘야함...
async handleCommentAlarm(
sender: User,
receiver: UserIdDto,
Expand Down Expand Up @@ -209,6 +212,17 @@ export class AlarmService {
return new AlarmPaginationShowDto(alarmList, isLast, lastId);
}

async testChatAlarm() {
const sendPushAlarmObj: ChatAlarmSubDto = {
nickname: '테스트 찬진',
roomId: '626d0c3f3cb6e9dce6cb9ad6',
chatId: '626d0c3f3cb6e9dce6cb9ad6',
sender: '62663d718fc11f0eeb47dba3',
content: 'asdfasdfadsf',
};

await this.pushAlarmQueue.add(PUSH_ALARM_TYPE.CHAT, sendPushAlarmObj);
}
// 안읽은 알림 갯수

///--------------------------------------------- 푸시알림
Expand Down
4 changes: 3 additions & 1 deletion src/apis/alarm/dto/sendPushAlarm.sub.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export class SendPushAlarmSubDto {
@Expose()
// @Transform((value) => value.obj.receivers, { toClassOnly: true })
@Transform(
(value) => value.obj.receivers.map((e: string) => new Types.ObjectId(e)),
(value) =>
value.obj.receivers &&
value.obj.receivers.map((e: string) => new Types.ObjectId(e)),
{
toClassOnly: true,
},
Expand Down
12 changes: 12 additions & 0 deletions src/apis/alarm/noti.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ export class NotiController {
return this.alarmService.getMyAlarms(user.userIdDto, pageLastIdDto);
}

// @ApiOperation({
// summary: 'test',
// })
// @Get('test')
// @ApiResponse({
// status: 200,
// description: '성공 응답만 리턴',
// })
// testChatAlarm() {
// return this.alarmService.testChatAlarm();
// }

@ApiOperation({
summary: '내 알림을 다 보게끔 한다.',
})
Expand Down
48 changes: 35 additions & 13 deletions src/apis/alarm/pushAlarm.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { UserRepository } from 'src/repositories/user.repository';
import { ChatAlarmSubDto } from './dto/chatAlarm.sub.dto';
import { SendPushAlarmPubDto } from './dto/sendPushAlarm.pub.dto';
import { SendPushAlarmSubDto } from './dto/sendPushAlarm.sub.dto';
import { UserFcmInfoDto } from './dto/userFcmInfo.dto';

@Processor(PUSH_ALARM)
export class PushAlarmProcessor {
Expand All @@ -27,7 +28,11 @@ export class PushAlarmProcessor {
console.log(findUserFcmToken);

const TokenArray = findUserFcmToken
.filter((e) => (e.appAlarm && e.FCMToken.length === 0 ? false : true))
.filter((e) => {
const checkAppAlarmOn = e.appAlarm;
const checkFCMTokenValid = e.FCMToken.length === 0 ? false : true;
return checkAppAlarmOn && checkFCMTokenValid;
})
.map((e) => e.FCMToken);
console.log(TokenArray);
if (TokenArray.length) {
Expand Down Expand Up @@ -68,19 +73,36 @@ export class PushAlarmProcessor {
console.log('processor ALARM Comment ', job.data);

const chatAlarmSubDto = plainToInstance(ChatAlarmSubDto, job.data);
const roomNameAndUserAlarmInfoArray =
await this.roomRepository.getUserAlarmInfoInRoom(
new RoomIdDto(chatAlarmSubDto.roomId),
);
const room = await this.roomRepository.getUserAlarmInfoInRoom(
new RoomIdDto(chatAlarmSubDto.roomId),
);

if (!room) {
console.log('chat alarm room does not exist');
return;
}

const userFcmInfoList = room.userList
? (room.userList as unknown as UserFcmInfoDto[])
: [];
const roomNameAndUserAlarmInfoArray = {
userFcmInfoList,
roomName: room.name,
};

const TokenArray = roomNameAndUserAlarmInfoArray.userFcmInfoList
.filter((e) =>
e.appAlarm &&
e.chatAlarm &&
!e._id.equals(chatAlarmSubDto.sender) &&
e.FCMToken.length === 0
? false
: true,
)
.filter((e) => {
const checkPushReciverIsSender = e._id.equals(chatAlarmSubDto.sender);
const checkAppAlarmOn = e.appAlarm;
const checkChatAlarmOn = e.chatAlarm;
const checkFCMTokenValid = e.FCMToken.length === 0 ? false : true;
return (
checkPushReciverIsSender &&
checkChatAlarmOn &&
checkAppAlarmOn &&
checkFCMTokenValid
);
})
.map((e) => e.FCMToken);

const sendPushAlarmObj: SendPushAlarmPubDto = {
Expand Down
8 changes: 3 additions & 5 deletions src/repositories/room.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export class RoomRepository {
return rooms;
}

async getUserAlarmInfoInRoom(roomIdDto: RoomIdDto) {
async getUserAlarmInfoInRoom(roomIdDto: RoomIdDto): Promise<Room | null> {
const room = await this.roomModel
.findOne({
_id: roomIdDto.roomId,
Expand All @@ -259,9 +259,7 @@ export class RoomRepository {
select: userFcmInfoSelect,
})
.lean<Room>({ defaults: true });
const userFcmInfoList = room.userList
? (room.userList as unknown as UserFcmInfoDto[])
: [];
return { userFcmInfoList: userFcmInfoList, roomName: room.name };

return room;
}
}

0 comments on commit c8c224a

Please sign in to comment.