-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from jkuznik/Task-42-Day-SMS-Limit
Task 42 day sms limit
- Loading branch information
Showing
4 changed files
with
117 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 92 additions & 14 deletions
106
src/main/java/pl/ateam/disasteralerts/disasteralert/SMSService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,123 @@ | ||
package pl.ateam.disasteralerts.disasteralert; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Table; | ||
import lombok.*; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import pl.ateam.disasteralerts.disasteralert.dto.AlertAddDTO; | ||
import pl.ateam.disasteralerts.user.dto.UserDTO; | ||
import com.twilio.Twilio; | ||
import com.twilio.rest.api.v2010.account.Message; | ||
import com.twilio.type.PhoneNumber; | ||
import pl.ateam.disasteralerts.util.EntityAudit; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
class SMSService implements AlertListener { | ||
|
||
private final SMSLimitService smsLimitService; | ||
|
||
public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID"); | ||
public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN"); | ||
public static final String MY_PHONE_NUMBER = System.getenv("MY_PHONE_NUMBER"); | ||
public static final String TWILIO_PHONE_NUMBER = System.getenv("TWILIO_PHONE_NUMBER"); | ||
|
||
@Override | ||
public void addedAlert(AlertAddDTO alertAddDTO, Set<UserDTO> interestedUsers) { | ||
interestedUsers.forEach(userDTO -> { | ||
sendSMS(alertAddDTO.description(), userDTO.phoneNumber()); | ||
}); | ||
} | ||
|
||
public void sendSMS(String alertDescription, String phoneNumber) { | ||
LocalDateTime today = LocalDateTime.now(); | ||
|
||
if(smsLimitService.isLimitReached(today)){ | ||
Twilio.init(ACCOUNT_SID, AUTH_TOKEN); | ||
|
||
Message message = Message | ||
.creator( | ||
new PhoneNumber(phoneNumber), | ||
new PhoneNumber(TWILIO_PHONE_NUMBER), | ||
alertDescription | ||
) | ||
.create(); | ||
|
||
System.out.println(message.getSid()); | ||
|
||
smsLimitService.increaseLimit(today); | ||
} else { | ||
throw new RuntimeException("Day SMS limit reached"); | ||
} | ||
} | ||
} | ||
|
||
@Builder | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Entity | ||
@Table(name = "sms_limits") | ||
class SMSLimit extends EntityAudit { | ||
|
||
@Column(nullable = false) | ||
private int limitCounter; | ||
|
||
// TODO: docelowo powinna zadziałać powyższa część kodu, poniżej do testu podaję swój nr | ||
// sendSMS(alertAddDTO.description(), MY_PHONE_NUMBER); | ||
public void increaseCounter(){ | ||
limitCounter++; | ||
} | ||
} | ||
|
||
public static void sendSMS(String alertDescription, String phoneNumber) { | ||
Twilio.init(ACCOUNT_SID, AUTH_TOKEN); | ||
@Repository | ||
interface SMSLimitRepository extends JpaRepository<SMSLimit, UUID> { | ||
@Query(value = "SELECT * FROM sms_limits WHERE CAST(create_date AS date) = CAST(:date AS date)", nativeQuery = true) | ||
Optional<SMSLimit> findByExactDay(@Param("date") LocalDateTime date); | ||
} | ||
|
||
Message message = Message | ||
.creator( | ||
new PhoneNumber(phoneNumber), | ||
new PhoneNumber(TWILIO_PHONE_NUMBER), | ||
alertDescription | ||
) | ||
.create(); | ||
@RequiredArgsConstructor | ||
@Service | ||
class SMSLimitService { | ||
|
||
private final SMSLimitRepository smsLimitRepository; | ||
|
||
@Transactional | ||
boolean isLimitReached(LocalDateTime date) { | ||
boolean result = false; | ||
|
||
Optional<SMSLimit> byExactDay = smsLimitRepository.findByExactDay(date); | ||
if(byExactDay.isPresent()){ | ||
result = byExactDay.get().getLimitCounter() < Integer.parseInt(System.getenv("DAY_SMS_LIMIT")); //TODO: ktoś z Was już próbował podpowiedzianego rozwiązania z sterowaniem konfiguracji aplikacji z poziomu application.properties? tutaj by mi się to przydało | ||
} else { | ||
createLimiter(); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
@Transactional | ||
void increaseLimit(LocalDateTime date) { | ||
SMSLimit smsLimit = smsLimitRepository.findByExactDay(date).orElseThrow( | ||
() -> new RuntimeException("Something go wrong with increase " + date + " SMS limit") | ||
); | ||
|
||
smsLimit.increaseCounter(); | ||
} | ||
|
||
System.out.println(message.getSid()); | ||
@Transactional | ||
private void createLimiter() { | ||
smsLimitRepository.save(SMSLimit.builder() | ||
.limitCounter(0) | ||
.build()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters