Skip to content

Commit

Permalink
Merge pull request #207 from Team-Hankki/develop
Browse files Browse the repository at this point in the history
[feat] 디스코드 알람 (#206)
  • Loading branch information
Parkjyun authored Oct 15, 2024
2 parents 49702d1 + 0681d65 commit a8d4774
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 10 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ configurations {

repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}

dependencies {
Expand Down Expand Up @@ -68,6 +69,9 @@ dependencies {
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"

// Discord Webhook
implementation 'com.github.napstr:logback-discord-appender:1.0.0'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.hankki.hankkiserver.domain.user.model.User;
import org.hankki.hankkiserver.domain.user.model.UserInfo;
import org.hankki.hankkiserver.domain.user.model.UserStatus;
import org.hankki.hankkiserver.event.EventPublisher;
import org.hankki.hankkiserver.event.user.CreateUserEvent;
import org.hankki.hankkiserver.external.openfeign.apple.AppleClientSecretGenerator;
import org.hankki.hankkiserver.external.openfeign.apple.AppleOAuthProvider;
import org.hankki.hankkiserver.external.openfeign.dto.SocialInfoDto;
Expand Down Expand Up @@ -48,6 +50,7 @@ public class AuthService {
private final KakaoOAuthProvider kakaoOAuthProvider;
private final AppleOAuthProvider appleOAuthProvider;
private final AppleClientSecretGenerator appleClientSecretGenerator;
private final EventPublisher eventPublisher;

public UserLoginResponse login(final String token, final UserLoginRequest request) {
Platform platform = Platform.getEnumPlatformFromStringPlatform(request.platform());
Expand Down Expand Up @@ -114,6 +117,7 @@ private User loadOrCreateUser(final Optional<User> findUser, final Platform plat
.orElseGet(() -> {
User newUser = createUser(socialInfo.name(), socialInfo.email(), socialInfo.serialId(), platform);
saveUserAndUserInfo(newUser);
eventPublisher.publish(CreateUserEvent.of(newUser.getId(), newUser.getName(), newUser.getPlatform().toString()));
return newUser;
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.hankki.hankkiserver.api.external.service;

import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.event.store.CreateStoreEvent;
import org.hankki.hankkiserver.event.store.DeleteStoreEvent;
import org.hankki.hankkiserver.event.user.CreateUserEvent;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

@Component
@RequiredArgsConstructor
public class DiscordEventListener {

private final DiscordService discordService;

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void sendStoreCreationNotice(CreateStoreEvent event) {
discordService.sendStoreCreationMessage(event.storeName(), event.universityName());
}

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void sendUserCreateNotice(CreateUserEvent event) {
discordService.sendUserCreationMessage(event.userId(), event.userName(), event.platform());
}

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void sendStoreDeleteNotice(DeleteStoreEvent event) {
discordService.sendStoreDeleteMessage(event.name(), event.userId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.hankki.hankkiserver.api.external.service;

import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.external.openfeign.discord.DiscordFeignClient;
import org.hankki.hankkiserver.external.openfeign.discord.dto.DiscordMessage;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class DiscordService {

private final DiscordFeignClient discordFeignClient;

public void sendStoreCreationMessage(String storeName, String universityName) {
discordFeignClient.sendStoreCreationMessage(DiscordMessage.storeCreationMessageOf(storeName, universityName));
}
public void sendUserCreationMessage(Long userId, String userName, String platform) {
discordFeignClient.sendUserCreationMessage(DiscordMessage.userCreationMessageOf(userId, userName, platform));
}
public void sendStoreDeleteMessage(String storeName, Long userId) {
discordFeignClient.sendStoreDeleteMessage(DiscordMessage.storeDeleteMessageOf(storeName, userId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import org.hankki.hankkiserver.api.store.service.HeartCommandService;
import org.hankki.hankkiserver.api.store.service.StoreCommandService;
import org.hankki.hankkiserver.api.store.service.StoreQueryService;
import org.hankki.hankkiserver.api.store.service.command.HeartDeleteCommand;
import org.hankki.hankkiserver.api.store.service.command.HeartPostCommand;
import org.hankki.hankkiserver.api.store.service.command.StorePostCommand;
import org.hankki.hankkiserver.api.store.service.command.StoreValidationCommand;
import org.hankki.hankkiserver.api.store.service.command.*;
import org.hankki.hankkiserver.api.store.service.response.*;
import org.hankki.hankkiserver.auth.UserId;
import org.hankki.hankkiserver.common.code.CommonSuccessCode;
Expand Down Expand Up @@ -94,8 +91,8 @@ public HankkiResponse<StorePostResponse> createStore(@RequestPart(required = fal
}

@DeleteMapping("/stores/{id}")
public HankkiResponse<Void> deleteStore(@PathVariable final Long id) {
storeCommandService.deleteStore(id);
public HankkiResponse<Void> deleteStore(@PathVariable final Long id, @UserId final Long userId) {
storeCommandService.deleteStore(StoreDeleteCommand.of(id, userId));
return HankkiResponse.success(CommonSuccessCode.NO_CONTENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.api.auth.service.UserFinder;
import org.hankki.hankkiserver.api.store.service.command.StoreDeleteCommand;
import org.hankki.hankkiserver.event.store.CreateStoreEvent;
import org.hankki.hankkiserver.api.menu.service.MenuUpdater;
import org.hankki.hankkiserver.api.report.service.ReportUpdater;
import org.hankki.hankkiserver.api.store.service.command.StorePostCommand;
Expand All @@ -18,6 +20,8 @@
import org.hankki.hankkiserver.domain.store.model.StoreImage;
import org.hankki.hankkiserver.domain.university.model.University;
import org.hankki.hankkiserver.domain.universitystore.model.UniversityStore;
import org.hankki.hankkiserver.event.EventPublisher;
import org.hankki.hankkiserver.event.store.DeleteStoreEvent;
import org.hankki.hankkiserver.external.s3.S3Service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -40,20 +44,20 @@ public class StoreCommandService {
private final ReportUpdater reportUpdater;
private final UserFinder userFinder;
private final StoreFinder storeFinder;
private final EventPublisher publisher;

@Transactional(rollbackFor = Exception.class)
public StorePostResponse createStore(final StorePostCommand command) {
if (storeExists(command.latitude(), command.longitude(), command.name(), false)) {
throw new BadRequestException(StoreErrorCode.BAD_STORE_INFO);
}

Store store = storeUpdater.save(command.toEntity());
saveImages(command, store);
menuUpdater.saveAll(getMenus(command, store));

University university = universityFinder.findById(command.universityId());
universityStoreUpdater.save(UniversityStore.create(store, university));
reportUpdater.save(Report.create(userFinder.getUser(command.userId()), store, university));
publisher.publish(CreateStoreEvent.of(store.getName(), university.getName()));

return StorePostResponse.of(store);
}
Expand Down Expand Up @@ -84,7 +88,9 @@ private List<Menu> getMenus(final StorePostCommand command, final Store store) {
}

@Transactional
public void deleteStore(final Long id) {
storeFinder.findByIdWhereDeletedIsFalse(id).softDelete();
public void deleteStore(final StoreDeleteCommand command) {
Store findStore = storeFinder.findByIdWhereDeletedIsFalse(command.storeId());
findStore.softDelete();
publisher.publish(DeleteStoreEvent.of(findStore.getName(), command.userId()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.hankki.hankkiserver.api.store.service.command;

public record StoreDeleteCommand(Long storeId,
Long userId) {
public static StoreDeleteCommand of(Long storeId, Long userId) {
return new StoreDeleteCommand(storeId, userId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.hankki.hankkiserver.event;

public interface EventPublisher {
void publish(Object event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.hankki.hankkiserver.event;

import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class EventPublisherAdapter implements EventPublisher {

private final ApplicationEventPublisher applicationEventPublisher;

@Override
public void publish(Object event) {
applicationEventPublisher.publishEvent(event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.hankki.hankkiserver.event.store;

public record CreateStoreEvent (String storeName,
String universityName) {
public static CreateStoreEvent of(String storeName, String universityName) {
return new CreateStoreEvent(storeName, universityName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.hankki.hankkiserver.event.store;

public record DeleteStoreEvent(String name, Long userId) {
public static DeleteStoreEvent of(String name, Long userId) {
return new DeleteStoreEvent(name, userId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.hankki.hankkiserver.event.user;

public record CreateUserEvent(
Long userId,
String userName,
String platform
) {
public static CreateUserEvent of(Long userId, String userName, String platform) {
return new CreateUserEvent(userId, userName, platform);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.hankki.hankkiserver.external.openfeign.discord;

import org.hankki.hankkiserver.external.openfeign.discord.dto.DiscordMessage;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(name = "discordClient", url = "${discord.webhook.url}")
public interface DiscordFeignClient {
@PostMapping(value = "${discord.webhook.create-store}", produces = MediaType.APPLICATION_JSON_VALUE)
void sendStoreCreationMessage(@RequestBody DiscordMessage message);

@PostMapping(value = "${discord.webhook.create-user}", produces = MediaType.APPLICATION_JSON_VALUE)
void sendUserCreationMessage(@RequestBody DiscordMessage message);

@PostMapping(value = "${discord.webhook.delete-store}", produces = MediaType.APPLICATION_JSON_VALUE)
void sendStoreDeleteMessage(@RequestBody DiscordMessage message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.hankki.hankkiserver.external.openfeign.discord.dto;

public record DiscordMessage(String content) {
public static DiscordMessage storeCreationMessageOf(String storeName, String universityName) {
return new DiscordMessage(universityName+ "에 <" +storeName+ ">가 생성되었습니다.");
}

public static DiscordMessage userCreationMessageOf(Long userId, String userName, String platform) {
return new DiscordMessage( platform + userId + "번째 유저 <" +userName+ ">가 가입했습니다.");
}

public static DiscordMessage storeDeleteMessageOf(String storeName, Long userId) {
return new DiscordMessage( userId + "번 유저에의해 <" + storeName + ">가 삭제되었습니다.");
}
}

0 comments on commit a8d4774

Please sign in to comment.