Repository navigation
Expand file tree
/
Copy pathTransactionService.java
More file actions
104 lines (84 loc) · 4.31 KB
/
Copy pathTransactionService.java
File metadata and controls
104 lines (84 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.portfolio.merchantapi.transaction;
import com.portfolio.merchantapi.common.exception.DuplicateIdempotencyKeyException;
import com.portfolio.merchantapi.common.exception.InvalidIdempotencyKeyException;
import com.portfolio.merchantapi.common.exception.MerchantNotFoundException;
import com.portfolio.merchantapi.merchant.MerchantService;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
@Service
@RequiredArgsConstructor
public class TransactionService {
private final TransactionRepository transactionRepository;
private final IdempotencyRecordRepository idempotencyRecordRepository;
private final MerchantService merchantService;
@Transactional
public TransactionResponse createTransaction(TransactionRequest request, String idempotencyKey) {
validateIdempotencyKey(idempotencyKey);
if (!merchantService.existsById(request.merchantId())) {
throw new MerchantNotFoundException(request.merchantId());
}
IdempotencyRecord idempotencyRecord = createIdempotencyRecord(idempotencyKey);
MerchantTransaction transaction = new MerchantTransaction();
transaction.setTransactionAmount(request.transactionAmount());
transaction.setTransactionType(request.transactionType());
transaction.setTransactionDate(LocalDateTime.now());
transaction.setMerchantId(request.merchantId());
transaction.setConnectionMode(request.connectionMode());
transaction.setTransactionStatus("New");
MerchantTransaction savedTransaction = transactionRepository.save(transaction);
idempotencyRecord.setTransactionId(savedTransaction.getId());
idempotencyRecordRepository.save(idempotencyRecord);
return TransactionResponse.from(savedTransaction);
}
public List<TransactionResponse> findTransactionsByMerchant(Long merchantId, String connectionMode) {
if (!merchantService.existsById(merchantId)) {
throw new MerchantNotFoundException(merchantId);
}
return findMerchantTransactions(merchantId, connectionMode).stream()
.map(TransactionResponse::from)
.toList();
}
public List<TransactionResponse> findTransactions(String connectionMode) {
List<MerchantTransaction> transactions = isConnectionMode(connectionMode)
? transactionRepository.findByConnectionModeIgnoreCase(connectionMode)
: transactionRepository.findAll();
return transactions.stream()
.map(TransactionResponse::from)
.toList();
}
private List<MerchantTransaction> findMerchantTransactions(Long merchantId, String connectionMode) {
if (isConnectionMode(connectionMode)) {
return transactionRepository.findByMerchantIdAndConnectionModeIgnoreCase(merchantId, connectionMode);
}
return transactionRepository.findByMerchantId(merchantId);
}
private boolean isConnectionMode(String connectionMode) {
return connectionMode != null
&& (connectionMode.equalsIgnoreCase("Live") || connectionMode.equalsIgnoreCase("Test"));
}
private IdempotencyRecord createIdempotencyRecord(String idempotencyKey) {
if (idempotencyRecordRepository.existsById(idempotencyKey)) {
throw new DuplicateIdempotencyKeyException(idempotencyKey);
}
IdempotencyRecord record = new IdempotencyRecord();
record.setIdempotencyKey(idempotencyKey);
record.setCreatedAt(LocalDateTime.now());
try {
return idempotencyRecordRepository.saveAndFlush(record);
} catch (DataIntegrityViolationException exception) {
throw new DuplicateIdempotencyKeyException(idempotencyKey);
}
}
private void validateIdempotencyKey(String idempotencyKey) {
if (idempotencyKey == null || idempotencyKey.isBlank()) {
throw new InvalidIdempotencyKeyException("Idempotency-Key header is required");
}
if (idempotencyKey.length() > 128) {
throw new InvalidIdempotencyKeyException("Idempotency-Key must be 128 characters or less");
}
}
}