Skip to content

Commit 08c6bc8

Browse files
authored
Merge pull request #63 from TP-RENTPLACE/feature/add-postmark-api
Feature/add postmark api
2 parents d2354b7 + bd40a36 commit 08c6bc8

9 files changed

Lines changed: 126 additions & 12 deletions

File tree

rentplace/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ dependencies {
4242
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.12.6")
4343
runtimeOnly("io.jsonwebtoken:jjwt-impl:0.12.6")
4444
implementation("io.jsonwebtoken:jjwt-api:0.12.6")
45+
implementation("com.postmarkapp:postmark:1.11.1")
4546

4647
compileOnly 'org.projectlombok:lombok'
4748
runtimeOnly 'org.postgresql:postgresql'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package kattsyn.dev.rentplace.configs;
2+
3+
import jakarta.validation.constraints.NotBlank;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
6+
@ConfigurationProperties(prefix = "email.retry")
7+
public record EmailRetryProperties(
8+
@NotBlank Integer maxAttempts,
9+
@NotBlank Integer delayMs
10+
) {
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package kattsyn.dev.rentplace.exceptions;
2+
3+
public class EmailException extends RuntimeException {
4+
public EmailException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package kattsyn.dev.rentplace.exceptions;
2+
3+
public class EmailServiceUnavailableException extends RuntimeException {
4+
public EmailServiceUnavailableException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package kattsyn.dev.rentplace.services.impl;
2+
3+
import kattsyn.dev.rentplace.configs.EmailRetryProperties;
4+
import kattsyn.dev.rentplace.exceptions.EmailException;
5+
import kattsyn.dev.rentplace.exceptions.EmailServiceUnavailableException;
6+
import kattsyn.dev.rentplace.services.EmailService;
7+
import lombok.RequiredArgsConstructor;
8+
import lombok.extern.slf4j.Slf4j;
9+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
10+
import org.springframework.context.annotation.Primary;
11+
import org.springframework.stereotype.Service;
12+
13+
@Service
14+
@RequiredArgsConstructor
15+
@Slf4j
16+
@Primary
17+
@EnableConfigurationProperties(EmailRetryProperties.class)
18+
public class CompositeEmailService implements EmailService {
19+
20+
private final PostmarkEmailServiceImpl postmarkEmailServiceImpl;
21+
private final EmailServiceImpl emailServiceImpl;
22+
private final EmailRetryProperties retryProperties;
23+
24+
@Override
25+
public void sendVerificationCode(String email, String code) {
26+
try {
27+
postmarkEmailServiceImpl.sendVerificationCode(email, code);
28+
} catch (EmailException postmarkException) {
29+
log.warn("Postmark failed, trying JavaMail. Reason: {}", postmarkException.getMessage());
30+
try {
31+
emailServiceImpl.sendVerificationCode(email, code);
32+
} catch (EmailException javaMailEx) {
33+
log.error("All email services failed: {}", javaMailEx.getMessage());
34+
throw new EmailServiceUnavailableException("All email providers failed");
35+
}
36+
}
37+
}
38+
}
Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package kattsyn.dev.rentplace.services.impl;
22

3+
import kattsyn.dev.rentplace.exceptions.EmailException;
34
import kattsyn.dev.rentplace.services.EmailService;
45
import lombok.RequiredArgsConstructor;
56
import lombok.extern.slf4j.Slf4j;
@@ -16,17 +17,21 @@ public class EmailServiceImpl implements EmailService {
1617

1718
@Override
1819
public void sendVerificationCode(String email, String code) {
19-
log.info("Sending verification code to " + email);
20-
SimpleMailMessage message = new SimpleMailMessage();
20+
try {
21+
log.info("Sending verification code to {}", email);
22+
SimpleMailMessage message = new SimpleMailMessage();
2123

22-
message.setTo(email);
23-
message.setSubject("Код подтверждения");
24-
String text = String.format(
25-
"Здравствуйте!\n\nВаш код подтверждения для регистрации: %s\n\nС уважением,\nКоманда rentplace",
26-
code
27-
);
28-
message.setText(text);
29-
mailSender.send(message);
30-
log.info("Verification code should be sent");
24+
message.setTo(email);
25+
message.setSubject("Код подтверждения");
26+
String text = String.format(
27+
"Здравствуйте!\n\nВаш код подтверждения для регистрации: %s\n\nС уважением,\nКоманда rentplace",
28+
code
29+
);
30+
message.setText(text);
31+
mailSender.send(message);
32+
log.info("Verification code should be sent");
33+
} catch (Exception e) {
34+
throw new EmailException("JavaMail error: " + e.getMessage());
35+
}
3136
}
3237
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package kattsyn.dev.rentplace.services.impl;
2+
3+
import com.postmarkapp.postmark.Postmark;
4+
import com.postmarkapp.postmark.client.ApiClient;
5+
import com.postmarkapp.postmark.client.exception.PostmarkException;
6+
import kattsyn.dev.rentplace.exceptions.EmailException;
7+
import kattsyn.dev.rentplace.services.EmailService;
8+
import lombok.extern.slf4j.Slf4j;
9+
import org.springframework.stereotype.Service;
10+
11+
import java.io.IOException;
12+
13+
import com.postmarkapp.postmark.client.data.model.message.Message;
14+
15+
@Service
16+
@Slf4j
17+
public class PostmarkEmailServiceImpl implements EmailService {
18+
19+
private final ApiClient postmarkClient;
20+
21+
public PostmarkEmailServiceImpl() {
22+
String serverToken = "f68fd941-1f63-4e1e-a348-c8232ea2ded6";
23+
this.postmarkClient = Postmark.getApiClient(serverToken);
24+
}
25+
26+
@Override
27+
public void sendVerificationCode(String email, String code) {
28+
log.info("Sending verification code to {}", email);
29+
try {
30+
Message message = new Message("support@rentplace.online", email, "Код подтверждения", String.format(
31+
"Здравствуйте!\n\nВаш код подтверждения для регистрации: %s\n\nС уважением,\nКоманда rentplace",
32+
code));
33+
postmarkClient.deliverMessage(message);
34+
log.info("Verification code sent successfully");
35+
36+
} catch (PostmarkException | IOException e) {
37+
throw new EmailException("Postmark error: " + e.getMessage());
38+
}
39+
}
40+
}
41+

rentplace/src/main/java/kattsyn/dev/rentplace/services/impl/UserServiceImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public UserDTO findById(Long id) {
8181
@Override
8282
public UserDTO createWithImage(UserCreateEditDTO userCreateEditDTO) {
8383
User user = userMapper.fromUserCreateEditDTO(userCreateEditDTO);
84+
user.setUserStatus(UserStatus.STATUS_ACTIVE);
8485
user = userRepository.save(user);
8586
if (userCreateEditDTO.getFile() != null && !userCreateEditDTO.getFile().isEmpty()) {
8687
return uploadImage(userCreateEditDTO.getFile(), user);

rentplace/src/main/resources/application.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ openrouter:
6161
key: ${OPENROUTER_API_KEY}
6262
model: ${OPENROUTER_API_MODEL}
6363
default-system-prompt: ${OPENROUTER_API_SYSTEM_PROMPT}
64-
64+
email:
65+
retry:
66+
delay-ms: 1000
67+
max-attempts: 2
6568
api:
6669
path: api/v1
6770
upload:

0 commit comments

Comments
 (0)