Skip to content

Commit

Permalink
Merge pull request #143 from Modagbul/feat/mission-alarm
Browse files Browse the repository at this point in the history
Feat/mission alarm
  • Loading branch information
seungueonn authored Dec 13, 2023
2 parents 87dc516 + 7b9274e commit 01aa765
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package com.moing.backend.domain.mission.application.service;

import com.moing.backend.domain.history.application.dto.response.MemberIdAndToken;
import com.moing.backend.domain.history.domain.entity.AlarmType;
import com.moing.backend.domain.history.domain.entity.PagePath;
import com.moing.backend.domain.mission.domain.entity.Mission;
import com.moing.backend.domain.missionArchive.domain.service.MissionArchiveScheduleQueryService;
import com.moing.backend.domain.teamMember.domain.service.TeamMemberGetService;
import com.moing.backend.global.config.fcm.dto.event.MultiFcmEvent;
import com.moing.backend.global.config.fcm.dto.request.MultiRequest;
import com.moing.backend.global.config.fcm.service.FcmService;
import lombok.RequiredArgsConstructor;
import net.minidev.json.JSONObject;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;
import java.util.Random;

import static com.moing.backend.global.config.fcm.constant.RemindMissionTitle.*;
Expand All @@ -16,22 +26,26 @@
@RequiredArgsConstructor
public class MissionRemindAlarmUseCase {

private final FcmService fcmService;

public void sendRemindMissionAlarm(Mission mission) {
private final MissionArchiveScheduleQueryService missionArchiveScheduleQueryService;
private final ApplicationEventPublisher eventPublisher;


public Boolean sendRemindMissionAlarm() {

Random random = new Random(System.currentTimeMillis());



String title = getTitle(random.nextInt(4));
String message = getMessage(random.nextInt(4));

MultiRequest multiRequest = new MultiRequest();
String REMIND_NAME = "미션 리마인드";

Optional<List<MemberIdAndToken>> remainMissionPeople = missionArchiveScheduleQueryService.getRemainMissionPeople();

eventPublisher.publishEvent(new MultiFcmEvent(title, message, remainMissionPeople, remainMissionPeople,
"",REMIND_NAME, AlarmType.REMIND, PagePath.MISSION_ALL_PTAH.getValue()));
return true;
}


public String getTitle(int num) {
switch (num) {
case 0:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import com.moing.backend.domain.mission.application.dto.req.MissionReq;
import com.moing.backend.domain.mission.application.dto.res.MissionCreateRes;
import com.moing.backend.domain.mission.application.dto.res.MissionReadRes;
import com.moing.backend.domain.mission.application.service.MissionCreateUseCase;
import com.moing.backend.domain.mission.application.service.MissionDeleteUseCase;
import com.moing.backend.domain.mission.application.service.MissionReadUseCase;
import com.moing.backend.domain.mission.application.service.MissionUpdateUseCase;
import com.moing.backend.domain.mission.application.service.*;
import com.moing.backend.global.config.security.dto.User;
import com.moing.backend.global.response.SuccessResponse;
import lombok.AllArgsConstructor;
Expand All @@ -28,6 +25,8 @@ public class MissionController {
private final MissionUpdateUseCase missionUpdateUseCase;
private final MissionDeleteUseCase missionDeleteUseCase;

private final MissionRemindAlarmUseCase missionRemindAlarmUseCase;

/**
* 미션 조회
* [GET] {teamId}/missions/{missionId}
Expand Down Expand Up @@ -103,6 +102,11 @@ public ResponseEntity<SuccessResponse<Boolean>> isLeader(@AuthenticationPrincipa
return ResponseEntity.ok(SuccessResponse.create(RECOMMEND_MISSION_SUCCESS.getMessage(), this.missionCreateUseCase.getIsLeader(user.getSocialId(),teamId)));
}

// @PostMapping("/remind")
// public ResponseEntity<SuccessResponse<Boolean>> remindAlarm(@AuthenticationPrincipal User user,@PathVariable Long teamId) {
// return ResponseEntity.ok(SuccessResponse.create(RECOMMEND_MISSION_SUCCESS.getMessage(), this.missionRemindAlarmUseCase.sendRemindMissionAlarm()));
// }




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,18 @@ public Long deleteArchive(String userSocialId, Long missionId,Long count) {
Mission mission = missionQueryService.findMissionById(missionId);
Team team = mission.getTeam();

// 사진 제출 했다면, s3 삭제 로직
if (mission.getWay() == MissionWay.PHOTO && missionArchiveQueryService.isDone(memberId, missionId)) {
MissionArchive deleteArchive = missionArchiveQueryService.findOneMyArchive(memberId, missionId,count).get(0);
MissionState missionState = missionStateQueryService.findMissionState(member, mission);

LocalDateTime createdDate = deleteArchive.getCreatedDate();
LocalDateTime today = LocalDateTime.now();

// 반복미션이면서 오늘 이전에 한 인증은 인증 취소할 수 없도록
if (mission.getType().equals(MissionType.REPEAT) && createdDate.toLocalDate().isBefore(today.toLocalDate())) {
throw new NoAccessMissionArchiveException();
}

MissionArchive deleteArchive = missionArchiveQueryService.findOneMyArchive(memberId, missionId,count).get(0);
missionArchiveDeleteService.deleteMissionArchive(deleteArchive);

MissionState missionState = missionStateQueryService.findMissionState(member, mission);
missionStateDeleteService.deleteMissionState(missionState);

return deleteArchive.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,4 @@ public interface MissionArchiveCustomRepository {

Boolean findMyArchivesToday(Long memberId,Long missionId);

Optional<List<String>> findPeopleRemainMission();


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import com.moing.backend.domain.mission.application.dto.res.FinishMissionBoardRes;
import com.moing.backend.domain.mission.application.dto.res.RepeatMissionBoardRes;
import com.moing.backend.domain.mission.application.dto.res.SingleMissionBoardRes;
import com.moing.backend.domain.mission.domain.entity.QMission;
import com.moing.backend.domain.mission.domain.entity.constant.MissionStatus;
import com.moing.backend.domain.mission.domain.entity.constant.MissionType;
import com.moing.backend.domain.mission.domain.entity.constant.MissionWay;
import com.moing.backend.domain.missionArchive.application.dto.res.MissionArchivePhotoRes;
import com.moing.backend.domain.missionArchive.domain.entity.MissionArchive;
import com.moing.backend.domain.missionArchive.domain.entity.MissionArchiveStatus;
import com.moing.backend.domain.missionArchive.domain.entity.QMissionArchive;
import com.moing.backend.domain.team.domain.entity.QTeam;
import com.moing.backend.domain.teamMember.domain.entity.QTeamMember;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.Order;
Expand All @@ -18,6 +22,7 @@
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import feign.Param;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -342,34 +347,11 @@ public Boolean findMyArchivesToday(Long memberId, Long missionId) {
}


public Optional<List<String>> findPeopleRemainMission() {
return Optional.ofNullable(queryFactory
.select(teamMember.member.nickName).distinct()
.from(teamMember)
.join(teamMember.team, team)
.join(team.missions, mission)
.join(mission.missionStateList,missionState)
.on(
mission.eq(missionState.mission),
teamMember.member.eq(missionState.member),
mission.number.loe(missionState.count()))
// .on(missionState.isNull())
.where(
mission.status.eq(MissionStatus.END).or(mission.status.eq(MissionStatus.SUCCESS)).not()
// missionState.isNull())
).fetch());
// .where(
// // active 한 미션 필터링
// (((mission.status.eq(MissionStatus.ONGOING).or(mission.status.eq(MissionStatus.WAIT))).and(mission.type.eq(MissionType.ONCE)))
// .or((mission.status.eq(MissionStatus.ONGOING)).and(mission.type.eq(MissionType.REPEAT)))),

// // 인증하지 않은값
// missionState.id.isNull()


// ).fetch());
}

private BooleanExpression createRepeatTypeConditionByArchive() {
LocalDate now = LocalDate.now();
DayOfWeek firstDayOfWeek = DayOfWeek.MONDAY;
Expand Down Expand Up @@ -405,5 +387,4 @@ private BooleanExpression createRepeatTypeConditionByState() {




}
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package com.moing.backend.domain.missionArchive.domain.repository;

import com.moing.backend.domain.history.application.dto.response.MemberIdAndToken;
import com.moing.backend.domain.mission.domain.entity.constant.MissionStatus;
import com.moing.backend.domain.missionArchive.domain.entity.MissionArchive;
import com.moing.backend.domain.missionArchive.domain.entity.MissionArchiveStatus;
import com.querydsl.core.Tuple;
import feign.Param;
import org.hibernate.annotations.NamedNativeQuery;
import org.hibernate.annotations.Parameter;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.persistence.ColumnResult;
import javax.persistence.ConstructorResult;
import javax.persistence.SqlResultSetMapping;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository
Expand All @@ -28,4 +35,47 @@ public interface MissionArchiveRepository extends JpaRepository<MissionArchive,
@Query("select m from MissionArchive as m where m.mission.id IN :missionIds and m.member.memberId =:memberId")
Optional<List<MissionArchive>> findRepeatMissionArchivesByMission (@Param("memberId") Long memberId, @Param("missionIds") List <Long> missionIds);


// @Query(value = "SELECT distinct tmSub.fcm_token, tmSub.member_id" +
// "FROM ( " +
// " SELECT distinct COALESCE(tm.member_id, 0) AS member_id, t.team_id, me.fcm_token " +
// " FROM mission m " +
// " LEFT JOIN team t ON m.team_id = t.team_id " +
// " LEFT JOIN team_member tm ON t.team_id = tm.team_id AND tm.is_deleted = 'False' " +
// " LEFT JOIN member me on tm.member_id = me.member_id " +
// " ) tmSub " +
// " LEFT JOIN mission m ON NOT (m.status = 'END' OR m.status = 'SUCCESS') and m.team_id = tmSub.team_id " +
// " LEFT JOIN mission_archive ms ON m.mission_id = ms.mission_id and ms.member_id = tmSub.member_id " +
// "GROUP BY tmSub.member_id, m.mission_id, m.number " +
// "having COUNT(ms.mission_archive_id) < m.number", nativeQuery = true)
// Optional<List<MemberIdAndToken>> findHavingRemainMissions();


@Query(value = "SELECT distinct COALESCE(tmSub.fcm_token,'undef') as fcmToken, tmSub.member_id as memberId " +
"FROM (SELECT distinct COALESCE(tm.member_id, 0) AS member_id, t.team_id, me.fcm_token " +
"FROM mission m " +
"LEFT JOIN team t ON m.team_id = t.team_id " +
"LEFT JOIN team_member tm ON t.team_id = tm.team_id AND tm.is_deleted = 'False' " +
"LEFT JOIN member me on tm.member_id = me.member_id) tmSub " +
"LEFT JOIN mission m ON NOT (m.status = 'END' OR m.status = 'SUCCESS') and m.team_id = tmSub.team_id " +
"LEFT JOIN mission_archive ms ON m.mission_id = ms.mission_id and ms.member_id = tmSub.member_id " +
"GROUP BY tmSub.member_id, m.mission_id, m.number " +
"HAVING COUNT(ms.mission_archive_id) < m.number", nativeQuery = true
)
Optional<List<Map<String, Long>>> findHavingRemainMissions();


// @Query(value = "SELECT distinct tmSub.fcm_token as fcmToken, tmSub.member_id as memberId " +
// "FROM (SELECT distinct COALESCE(tm.member_id, 0) AS member_id, t.team_id, me.fcm_token " +
// "FROM mission m " +
// "LEFT JOIN team t ON m.team_id = t.team_id " +
// "LEFT JOIN team_member tm ON t.team_id = tm.team_id AND tm.is_deleted = 'False' " +
// "LEFT JOIN member me on tm.member_id = me.member_id) tmSub " +
// "LEFT JOIN mission m ON NOT (m.status = 'END' OR m.status = 'SUCCESS') and m.team_id = tmSub.team_id " +
// "LEFT JOIN mission_archive ms ON m.mission_id = ms.mission_id and ms.member_id = tmSub.member_id " +
// "GROUP BY tmSub.member_id, m.mission_id, m.number " +
// "HAVING COUNT(ms.mission_archive_id) < m.number", nativeQuery = true
// )
// Optional<List<MemberIdAndToken>> findHavingRemainMissions();

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.moing.backend.domain.missionArchive.domain.service;

import com.moing.backend.domain.history.application.dto.response.MemberIdAndToken;
import com.moing.backend.domain.member.domain.entity.Member;
import com.moing.backend.domain.missionArchive.application.dto.res.MissionArchiveRes;
import com.moing.backend.domain.missionArchive.domain.repository.MissionArchiveRepository;
import com.moing.backend.global.annotation.DomainService;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@DomainService
@Transactional
Expand All @@ -17,8 +22,39 @@ public class MissionArchiveScheduleQueryService {

private final MissionArchiveRepository missionArchiveRepository;

public List<String> getRemainMissionPeople() {
return missionArchiveRepository.findPeopleRemainMission().orElseThrow();
public Optional<List<MemberIdAndToken>> getRemainMissionPeople() {
List<Map<String, Long>> maps = missionArchiveRepository.findHavingRemainMissions().orElseThrow();

return Optional.ofNullable(maps.stream()
.map(map -> {
Object memberIdObject = map.get("memberId");
Long memberId = ((BigInteger) memberIdObject).longValue();

Object fcmObject = map.get("fcmToken");
String fcmToken = fcmObject.toString();

return MemberIdAndToken.builder()
.fcmToken(fcmToken)
.memberId(memberId)
.build();
})
.collect(Collectors.toList()));

// return null;

// return maps.stream()
// .map(map -> {
// String fcmToken = Optional.ofNullable(map.get("fcmToken")).map(Object::toString).orElse("undef");
// Long memberId = map.get("memberId");
//
// return MemberIdAndToken.builder()
// .fcmToken(fcmToken)
// .memberId(memberId)
// .build();
//
// })
// .collect(Collectors.toList());

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.moing.backend.domain.mission.domain.entity.Mission;
import com.moing.backend.domain.mission.domain.entity.constant.MissionStatus;
import com.moing.backend.domain.mission.domain.service.MissionQueryService;
import com.moing.backend.domain.mission.domain.service.MissionSaveService;
import com.moing.backend.domain.missionState.domain.entity.MissionState;
import com.moing.backend.domain.missionState.domain.service.MissionStateDeleteService;
import com.moing.backend.domain.missionState.domain.service.MissionStateQueryService;
Expand Down Expand Up @@ -33,7 +34,6 @@ public class MissionStateScheduleUseCase {
private final MissionStateQueryService missionStateQueryService;
private final MissionStateDeleteService missionStateDeleteService;


private final TeamScoreLogicUseCase teamScoreLogicUseCase;

/**
Expand Down Expand Up @@ -68,7 +68,6 @@ public void singleMissionEndRoutine() {
teamScoreLogicUseCase.updateTeamScore(mission.getId());
});


}

/**
Expand Down
Loading

0 comments on commit 01aa765

Please sign in to comment.