-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserService.java
More file actions
56 lines (46 loc) · 2.02 KB
/
UserService.java
File metadata and controls
56 lines (46 loc) · 2.02 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
package com.example.spring.app.user;
import com.example.spring.core.keycloakClient.UserResource;
import com.example.spring.core.userQuota.UserQuotaModel;
import com.example.spring.core.userQuota.UserQuotaRepository;
import com.example.spring.common.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static com.example.spring.core.userQuota.UserQuotaUtil.getRemainingSearchesBasedOnUserTier;
@EnableScheduling
@Service
public class UserService {
@Autowired
private UserResource userResource;
@Autowired
private UserQuotaRepository userQuotaRepository;
@Scheduled(fixedRate = 1000 * 60 * 15) // 15 minutes
@Transactional
public void assignQuotaToEmptyUsers() {
try {
List<UserDTO> users = userResource.getUsers();
for (UserDTO user : users) {
Optional<UserQuotaModel> userQuota = userQuotaRepository.findByUserId(user.getId());
if (userQuota.isEmpty()) {
UserQuotaModel newUserQuota = new UserQuotaModel();
newUserQuota.setUserId(user.getId());
Integer quotaAllocated = getRemainingSearchesBasedOnUserTier(user);
newUserQuota.setQuotaAllocated(quotaAllocated);
newUserQuota.setQuotaUsed(0);
userQuotaRepository.save(newUserQuota);
LogUtil.info("Assigned new quota to user", Map.of(
"userId", user.getId(),
"quotaAllocated", quotaAllocated
));
}
}
} catch (Exception e) {
LogUtil.error("Error while assigning quota to empty users:", e);
}
}
}