Skip to content

Commit

Permalink
Merge pull request #186 from TEAM-SAMSION/main
Browse files Browse the repository at this point in the history
release : 1.1.1 배포
  • Loading branch information
tlarbals824 authored Dec 22, 2023
2 parents 95a465e + 09ad91d commit 897ddd2
Show file tree
Hide file tree
Showing 165 changed files with 3,279 additions and 868 deletions.
2 changes: 1 addition & 1 deletion .deploy/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM amazoncorretto:17
FROM amazoncorretto:17-alpine-jdk
ARG JAR_FILE=/Api-Module/build/libs/Api-Module-0.0.1.jar
COPY ${JAR_FILE} pawith.jar
ENTRYPOINT ["java","-Dcom.mysql.cj.disableAbandonedConnectionCleanup=true","-jar","/pawith.jar"]
2 changes: 1 addition & 1 deletion .deploy/promtail-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ scrape_configs:
- job_name: dev-pawith
docker_sd_configs:
- host: unix:///var/run/docker.sock
refresh_interval: 10s
refresh_interval: 15s
static_configs:
- targets:
- localhost
Expand Down
2 changes: 1 addition & 1 deletion .deploy/promtail-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ scrape_configs:
- job_name: prod-pawith
docker_sd_configs:
- host: unix:///var/run/docker.sock
refresh_interval: 10s
refresh_interval: 15s
static_configs:
- targets:
- localhost
Expand Down
Binary file modified .github/docs/components-Modules.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion .github/workflows/CD-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ jobs:
target: "/home/ubuntu"
strip_components: 2

- name: deploy docker-compose-dev push
- name: deploy docker-compose-prod push
uses: appleboy/scp-action@master
with:
host: ${{ secrets.PROD_HOST }}
Expand Down
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ out/
### application ###
**/src/main/resources/application.properties
**/src/main/resources/application-test.yml
**/src/main/resources/application

### resources static docs ###
**/src/main/resources/static/docs/
Expand All @@ -50,4 +51,7 @@ logs/logback/*
**/.jqwik-database

### firebase ###
**/src/main/resources/firebase/firebase.json
**/src/main/resources/firebase.json

### QueryDSL ###
**/src/main/generated/
1 change: 1 addition & 0 deletions Alarm-Module/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ dependencies {
implementation project(':Common-Module')
testImplementation(testFixtures(project(':Common-Module')))
implementation project(':Domain-Module:User-Module:User-Domain')

}
5 changes: 5 additions & 0 deletions Alarm-Module/src/docs/Alarm-API.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ operation::alarm-controller-test/get-alarms-exist[snippets='http-request,request

operation::alarm-controller-test/get-alarms[snippets='http-request,request-headers,query-parameters,http-response,response-fields']

[[알림-일괄-조회]]
=== 알림 일괄 조회

operation::alarm-controller-test/patch-alarms[snippets='http-request,request-headers,http-response']

[[device-token-저장]]
=== 디바이스 토큰 저장

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.pawith.alarmmodule.controller;

import com.pawith.alarmmodule.service.AlarmService;
import com.pawith.alarmmodule.service.dto.response.AlarmExistenceResponse;
import com.pawith.alarmmodule.service.dto.response.UnReadAlarmResponse;
import com.pawith.alarmmodule.service.dto.response.AlarmInfoResponse;
import com.pawith.commonmodule.response.SliceResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand All @@ -15,13 +16,18 @@ public class AlarmController {
private final AlarmService alarmService;

@GetMapping("/alarms/exist")
public AlarmExistenceResponse getAlarmsExist(){
return alarmService.getAlarmsExist();
public UnReadAlarmResponse getAlarmsExist(){
return alarmService.getUnreadAlarmCount();
}

@GetMapping("/alarms")
public SliceResponse<AlarmInfoResponse> getAlarms(Pageable pageable){
return alarmService.getAlarms(pageable);
}

@PatchMapping("/alarms")
public void patchAlarms(){
alarmService.changeAllAlarmStatusToRead();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.pawith.alarmmodule.entity.vo.AlarmBody;
import com.pawith.commonmodule.domain.BaseEntity;
import com.pawith.commonmodule.enums.AlarmCategory;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
Expand All @@ -22,15 +21,14 @@ public class Alarm extends BaseEntity {
private AlarmBody alarmBody;
private Boolean isRead = Boolean.FALSE;

@Enumerated(EnumType.STRING)
private AlarmCategory alarmCategory;
private String alarmCategory;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "alarm_user_id")
private AlarmUser alarmUser;

@Builder
public Alarm(AlarmBody alarmBody, AlarmCategory alarmCategory, AlarmUser alarmUser) {
public Alarm(AlarmBody alarmBody, String alarmCategory, AlarmUser alarmUser) {
this.alarmBody = alarmBody;
this.alarmCategory = alarmCategory;
this.alarmUser = alarmUser;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.pawith.alarmmodule.entity;

import com.pawith.commonmodule.domain.BaseEntity;
import com.pawith.commonmodule.util.DomainFieldUtils;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Objects;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
Expand All @@ -29,6 +28,9 @@ public AlarmUser(String deviceToken, Long userId) {
}

public void updateDeviceToken(String deviceToken) {
this.deviceToken = Objects.requireNonNull(deviceToken, "deviceToken must be not null");
this.deviceToken = DomainFieldUtils.DomainValidateBuilder.builder(String.class)
.currentValue(this.deviceToken)
.newValue(deviceToken)
.validate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.pawith.alarmmodule.fcm;

import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;
import com.pawith.alarmmodule.exception.AlarmError;
import com.pawith.alarmmodule.exception.FcmException;
import com.pawith.alarmmodule.handler.NotificationHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
@RequiredArgsConstructor
public class FcmSendMessageHandler implements NotificationHandler<FcmSendMessageHandler.NotificationInfo> {
private final FirebaseMessaging firebaseMessaging;
private final ExecutorService executorService = Executors.newFixedThreadPool(10);

@Retryable(
retryFor = FcmException.class,
maxAttempts = 3,
backoff = @Backoff(delay = 1000)
)
public void send(final NotificationInfo notificationInfo) {
final Message message = Message.builder()
.setToken(notificationInfo.deviceToken)
.setNotification(notificationInfo.notification)
.build();
try {
firebaseMessaging.send(message);
} catch (FirebaseMessagingException e) {
throw new FcmException(AlarmError.FCM_SEND_ERROR);
}
}

public void sendAsync(final NotificationInfo notificationInfo){
executorService.submit(() -> {
send(notificationInfo);
});
}

public void sendMultiAsync(Collection<NotificationInfo> notificationInfoList){
notificationInfoList.forEach(this::sendAsync);
}

public record NotificationInfo(String deviceToken, Notification notification){ }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.pawith.alarmmodule.handler;

import java.util.Collection;

public interface NotificationHandler<T> {
void send(T t);

void sendMultiAsync(Collection<T> t);

void sendAsync(T t);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.pawith.alarmmodule.repository;

import com.pawith.alarmmodule.entity.Alarm;
import org.springframework.data.repository.NoRepositoryBean;

import java.util.Collection;

@NoRepositoryBean
public interface AlarmBatchRepository{
void saveAllBatch(Collection<Alarm> entities);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface AlarmRepository extends JpaRepository<Alarm, Long> {

@Query("select count(a) > 0 " +
"from Alarm a " +
"join AlarmUser au on au.userId = :userId and a.alarmUser = au " +
"where a.isRead = false")
Boolean existsByUserId(Long userId);
@Query("""
select count(a)
from Alarm a
join AlarmUser au on au.userId = :userId and a.alarmUser = au
where a.isRead = false
""")
Integer countUnReadAlarm(Long userId);

@Query(
"""
Expand All @@ -23,4 +26,13 @@ public interface AlarmRepository extends JpaRepository<Alarm, Long> {
"""
)
Slice<Alarm> findAllByUserId(Long userId, Pageable pageable);


@Modifying
@Query("""
update Alarm a
set a.isRead = true
where a.isRead = false and a.alarmUser in (select au from AlarmUser au where au.userId = :userId)
""")
void changeAllAlarmStatusToRead(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.pawith.alarmmodule.entity.AlarmUser;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface AlarmUserRepository extends JpaRepository<AlarmUser, Long> {
Optional<AlarmUser> findByUserId(Long userId);
List<AlarmUser> findByUserIdIn(List<Long> userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.pawith.alarmmodule.repository;

import com.pawith.alarmmodule.entity.Alarm;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;

@Repository
@Transactional
@RequiredArgsConstructor
public class AlarmBatchRepositoryImpl implements AlarmBatchRepository{
private final JdbcTemplate jdbcTemplate;

@Override
public void saveAllBatch(Collection<Alarm> entities) {
final String insertSql = """
INSERT INTO alarm (domain_id, message, alarm_category,is_read,alarm_user_id,created_at, updated_at)
VALUES (?, ?, ?, ?, ?,now(),now())
""";

jdbcTemplate.batchUpdate(insertSql, entities, 1000, (ps, alarm) -> {
ps.setLong(1, alarm.getAlarmBody().getDomainId());
ps.setString(2, alarm.getAlarmBody().getMessage());
ps.setString(3, alarm.getAlarmCategory().toString());
ps.setBoolean(4, alarm.getIsRead());
ps.setLong(5, alarm.getAlarmUser().getId());
});
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
package com.pawith.alarmmodule.service;

import com.pawith.alarmmodule.entity.Alarm;
import com.pawith.alarmmodule.repository.AlarmBatchRepository;
import com.pawith.alarmmodule.repository.AlarmRepository;
import com.pawith.alarmmodule.service.dto.response.AlarmExistenceResponse;
import com.pawith.alarmmodule.service.dto.response.AlarmInfoResponse;
import com.pawith.alarmmodule.service.dto.response.UnReadAlarmResponse;
import com.pawith.commonmodule.response.SliceResponse;
import com.pawith.userdomain.utils.UserUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@RequiredArgsConstructor
public class AlarmService {
private final AlarmRepository alarmRepository;
private final AlarmBatchRepository alarmBatchRepository;
private final UserUtils userUtils;

public AlarmExistenceResponse getAlarmsExist(){
public UnReadAlarmResponse getUnreadAlarmCount(){
final Long userId = userUtils.getAccessUser().getId();
final Boolean alarmExist = alarmRepository.existsByUserId(userId);
return new AlarmExistenceResponse(alarmExist);
final Integer unReadAlarmCount = alarmRepository.countUnReadAlarm(userId);
return new UnReadAlarmResponse(unReadAlarmCount);
}

public SliceResponse<AlarmInfoResponse> getAlarms(Pageable pageable){
Expand All @@ -40,4 +44,13 @@ public SliceResponse<AlarmInfoResponse> getAlarms(Pageable pageable){
public void saveAlarm(Alarm alarm){
alarmRepository.save(alarm);
}

public void changeAllAlarmStatusToRead(){
final Long userId = userUtils.getAccessUser().getId();
alarmRepository.changeAllAlarmStatusToRead(userId);
}

public void saveAllAlarm(List<Alarm> alarms){
alarmBatchRepository.saveAllBatch(alarms);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Transactional
@RequiredArgsConstructor
Expand Down Expand Up @@ -39,4 +44,9 @@ public AlarmUser findDeviceTokenByUserId(Long userId) {
return alarmUserRepository.findByUserId(userId)
.orElseThrow(() -> new AlarmException(AlarmError.DEVICE_TOKEN_NOT_FOUND));
}

public Map<Long, AlarmUser> findAlarmUsersByUserIds(List<Long> userIds) {
return alarmUserRepository.findByUserIdIn(userIds).stream()
.collect(Collectors.toMap(AlarmUser::getUserId, Function.identity()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.pawith.alarmmodule.service;

public interface MultiAlarmSendService<T> {
void sendAlarm(T t);
}
Loading

0 comments on commit 897ddd2

Please sign in to comment.