Merge branch 'feature/SUPPORT-9416_listen_kafka' into develop

This commit is contained in:
adel.ka 2025-09-22 15:08:31 +03:00
commit 9378464f6a
15 changed files with 232 additions and 182 deletions

View file

@ -6,6 +6,8 @@ import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import ru.micord.ervu.account_applications.security.context.SecurityContext;
import ru.micord.ervu.account_applications.security.model.UserSession;
import ru.micord.ervu.account_applications.service.AccountFetchService;
import ru.micord.ervu.account_applications.service.UserApplicationListService;
@ -24,16 +26,14 @@ public class UserApplicationListRpcService extends Behavior {
private UserApplicationListService applicationListService;
@Autowired
private AccountFetchService accountService;
@Autowired
private SecurityContext securityContext;
@RpcCall
public void saveError(long appNumber, String errorMsg) {
UserSession userSession = securityContext.getUserSession();
LOGGER.error("error for application = {}, message: {}", appNumber, errorMsg);
applicationListService.saveError(appNumber, errorMsg);
}
@RpcCall
public void saveAcceptedStatus(long appNumber) {
applicationListService.saveAcceptedStatus(appNumber);
applicationListService.saveError(appNumber, errorMsg, userSession.name(), userSession.userId());
}
@RpcCall

View file

@ -1,58 +1,35 @@
package ru.micord.ervu.account_applications.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PutMapping;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import ru.micord.ervu.account_applications.security.service.EncryptionService;
import ru.micord.ervu.account_applications.model.StatusResponse;
import ru.micord.ervu.account_applications.service.UserApplicationListService;
import ru.micord.ervu.account_applications.websocket.dto.ProcessErrorMsg;
import ru.micord.ervu.account_applications.websocket.dto.ProcessResponseDto;
/**
* @author gulnaz
*/
@RestController
public class UserApplicationController {
private static final Logger LOGGER = LoggerFactory.getLogger(UserApplicationController.class);
private final UserApplicationListService applicationService;
private final EncryptionService encryptionService;
public UserApplicationController(UserApplicationListService applicationService,
EncryptionService encryptionService) {
public UserApplicationController(UserApplicationListService applicationService) {
this.applicationService = applicationService;
this.encryptionService = encryptionService;
}
@PutMapping(value = "/status", consumes = MediaType.APPLICATION_JSON_VALUE)
public Long updateStatus(@RequestBody ProcessResponseDto data) {
Long appNumber = data.body().applicationNumber();
switch (data.className()) {
case UPDATE -> {
LOGGER.info("update by appNumber = {}", appNumber);
String tempPass = data.body().tempPass();
if (StringUtils.hasText(tempPass)) {
String encryptedPassword = encryptionService.encrypt(tempPass);
applicationService.savePassword(appNumber, encryptedPassword);
}
else {
applicationService.saveAcceptedStatus(appNumber);
}
}
case PROCESS_ERROR -> {
ProcessErrorMsg errorMsg = data.body().msg();
String msg = errorMsg == null ? "unknown error" : errorMsg.message();
LOGGER.error("error by appNumber = {}, message: {}", appNumber, msg);
applicationService.saveError(appNumber, msg);
}
}
return appNumber;
@PostMapping("/status/batch")
public List<StatusResponse> getBatchStatuses(@RequestBody List<Long> appNumbers) {
Map<Long, String> statuses = applicationService.getStatusesBatch(appNumbers);
return appNumbers.stream()
.map(appNumber -> new StatusResponse(
appNumber,
statuses.get(appNumber)
))
.collect(Collectors.toList());
}
}

View file

@ -2,6 +2,9 @@ package ru.micord.ervu.account_applications.dao;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.jooq.DSLContext;
import org.springframework.stereotype.Repository;
@ -23,6 +26,22 @@ public class UserApplicationListDao {
this.dslContext = dslContext;
}
public Map<Long, String> getStatusesBatch(List<Long> appNumbers) {
if (appNumbers == null || appNumbers.isEmpty()) {
return Map.of();
}
return dslContext.select(USER_APPLICATION_LIST.NUMBER_APP, USER_APPLICATION_LIST.APPLICATION_STATUS)
.from(USER_APPLICATION_LIST)
.where(USER_APPLICATION_LIST.NUMBER_APP.in(appNumbers))
.fetch()
.stream()
.collect(Collectors.toMap(
record -> record.get(USER_APPLICATION_LIST.NUMBER_APP),
record -> record.get(USER_APPLICATION_LIST.APPLICATION_STATUS)
));
}
public void savePassword(Long appNumber, String encodedPass) {
dslContext.update(USER_APPLICATION_LIST)
.set(USER_APPLICATION_LIST.APPLICATION_STATUS, ACCEPTED.name())

View file

@ -0,0 +1,69 @@
package ru.micord.ervu.account_applications.kafka;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import ru.micord.ervu.account_applications.kafka.model.DeclarationStatus;
import ru.micord.ervu.account_applications.security.model.jwt.UserClaims;
import ru.micord.ervu.account_applications.security.service.EncryptionService;
import ru.micord.ervu.account_applications.security.service.JwtTokenService;
import ru.micord.ervu.account_applications.service.UserApplicationListService;
/**
* @author Adel Kalimullin
*/
@Component
public class DeclarationStatusListener {
private static final Logger LOGGER = LoggerFactory.getLogger(DeclarationStatusListener.class);
private final ObjectMapper mapper;
private final UserApplicationListService applicationService;
private final EncryptionService encryptionService;
private final JwtTokenService jwtTokenService;
public DeclarationStatusListener(ObjectMapper mapper,
UserApplicationListService applicationService, EncryptionService encryptionService,
JwtTokenService jwtTokenService) {
this.mapper = mapper;
this.applicationService = applicationService;
this.encryptionService = encryptionService;
this.jwtTokenService = jwtTokenService;
}
@KafkaListener(id = "${kafka.application.status.group.id}",
topics = "${kafka.application.status}")
public void listenKafkaDomain(String kafkaMessage, @Header("authorization") String authHeader) {
try {
String token = authHeader.replace("Bearer ", "");
UserClaims userClaims = jwtTokenService.getUserClaims(token);
String name = userClaims.name();
String userId = userClaims.userId();
DeclarationStatus declarationStatus = mapper.readValue(kafkaMessage, DeclarationStatus.class);
Long applicationNumber = declarationStatus.applicationNumber();
if (declarationStatus.status()) {
LOGGER.info("update by appNumber = {}", applicationNumber);
String tempPass = declarationStatus.password();
if (StringUtils.hasText(tempPass)) {
String encryptedPassword = encryptionService.encrypt(tempPass);
applicationService.savePassword(applicationNumber, encryptedPassword, name, userId);
}
else {
applicationService.saveAcceptedStatus(applicationNumber, name, userId);
}
}
else {
String errorMsg = declarationStatus.errorMsg();
LOGGER.error("error by appNumber = {}, message: {}", applicationNumber, errorMsg);
applicationService.saveError(applicationNumber, errorMsg, name, userId);
}
}
catch (JsonProcessingException e) {
LOGGER.error("Failed to deserialize message: {}", kafkaMessage, e);
}
}
}

View file

@ -0,0 +1,14 @@
package ru.micord.ervu.account_applications.kafka.model;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* @author Adel Kalimullin
*/
public record DeclarationStatus(
@JsonProperty("applicationNumber") Long applicationNumber,
@JsonProperty("password") String password,
@JsonProperty("status") boolean status,
@JsonProperty("userName") String userName,
@JsonProperty("description") String errorMsg
) {}

View file

@ -0,0 +1,6 @@
package ru.micord.ervu.account_applications.model;
/**
* @author Adel Kalimullin
*/
public record StatusResponse(Long appNumber, String status) {}

View file

@ -3,12 +3,12 @@ package ru.micord.ervu.account_applications.service;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.micord.ervu.account_applications.component.dao.AuditDao;
import ru.micord.ervu.account_applications.dao.UserApplicationListDao;
import ru.micord.ervu.account_applications.security.context.SecurityContext;
import ru.micord.ervu.account_applications.security.model.UserSession;
import utils.DateTimeUtil;
import static ru.micord.ervu.account_applications.enums.ApplicationStatus.ACCEPTED;
@ -22,38 +22,40 @@ public class UserApplicationListService {
private final UserApplicationListDao dao;
private final AuditDao auditDao;
private final SecurityContext securityContext;
public UserApplicationListService(UserApplicationListDao dao, AuditDao auditDao, SecurityContext securityContext) {
public UserApplicationListService(UserApplicationListDao dao, AuditDao auditDao) {
this.dao = dao;
this.auditDao = auditDao;
this.securityContext = securityContext;
}
public void savePassword(Long appNumber, String encodedPass) {
public Map<Long, String> getStatusesBatch(List<Long> appNumbers) {
return dao.getStatusesBatch(appNumbers);
}
@Transactional
public void savePassword(Long appNumber, String encodedPass, String name, String userId) {
dao.savePassword(appNumber, encodedPass);
saveAuditStatusByAppNumber(appNumber, ACCEPTED.name());
saveAuditStatusByAppNumber(appNumber, ACCEPTED.name(), name, userId);
}
public boolean userExists(String login){
return dao.userExists(login);
}
public void saveAcceptedStatus(long appNumber) {
@Transactional
public void saveAcceptedStatus(long appNumber, String name, String userId) {
dao.saveAcceptedStatus(appNumber);
saveAuditStatusByAppNumber(appNumber, ACCEPTED.name());
saveAuditStatusByAppNumber(appNumber, ACCEPTED.name(), name, userId);
}
public void saveError(long appNumber, String errorMsg) {
@Transactional
public void saveError(long appNumber, String errorMsg, String name, String userId) {
dao.saveError(appNumber, errorMsg);
saveAuditStatusByAppNumber(appNumber, AGREED.name());
saveAuditStatusByAppNumber(appNumber, AGREED.name(), name, userId);
}
private void saveAuditStatusByAppNumber(long appNumber, String status) {
private void saveAuditStatusByAppNumber(long appNumber, String status, String name, String userId) {
List<Long> appIds = auditDao.selectAppListIdsByAppNumber(appNumber);
UserSession userSession = securityContext.getUserSession();
String name = userSession.name();
String userId = userSession.userId();
appIds.forEach(id -> {
auditDao.insert(id, name, userId, status, Timestamp.valueOf(
DateTimeUtil.dateToLocalDateTimeUtc(new Date())));

View file

@ -1,10 +0,0 @@
package ru.micord.ervu.account_applications.websocket.dto;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
/**
* @author gulnaz
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public record ProcessErrorMsg(String message) {
}

View file

@ -1,13 +0,0 @@
package ru.micord.ervu.account_applications.websocket.dto;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* @author gulnaz
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public record ProcessResponseBody(String type, Long applicationNumber, String userName,
@JsonProperty("value") String tempPass,
String secretLink, ProcessErrorMsg msg) {
}

View file

@ -1,11 +0,0 @@
package ru.micord.ervu.account_applications.websocket.dto;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import ru.micord.ervu.account_applications.websocket.enums.ClassName;
/**
* @author gulnaz
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public record ProcessResponseDto(String forUser, ClassName className, ProcessResponseBody body) {
}

View file

@ -1,16 +0,0 @@
package ru.micord.ervu.account_applications.websocket.enums;
import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* @author gulnaz
*/
public enum ClassName {
@JsonProperty("update")
UPDATE,
@JsonProperty("processError")
PROCESS_ERROR,
@JsonEnumDefaultValue
SKIP
}

View file

@ -7,7 +7,7 @@ import {
TextField,
Visible
} from "@webbpm/base-package";
import {HttpClient, HttpErrorResponse, HttpResponse} from "@angular/common/http";
import {HttpClient} from "@angular/common/http";
import {FormField} from "../field/FormField";
import {AuthorizationService} from "../../../modules/app/service/authorization.service";
import {ApplicationKind} from "../enum/ApplicationKind";
@ -202,10 +202,12 @@ export class UserManagementService extends Behavior {
this.httpClient.post(url, request).toPromise()
.then((response: ProcessResponse) => {
let code = response.code;
if (code !== '200') {
this.saveError(appNumber, response.msg);
}
else {
this.statusUpdateService.trackDeclaration(appNumber);
}
})
.catch(reason => {
console.error("Error while executing request:", reason.toString());

View file

@ -1,9 +1,6 @@
import {Injectable, OnDestroy} from "@angular/core";
import {Injectable} from "@angular/core";
import {Subject} from "rxjs";
import {HttpClient} from "@angular/common/http";
import {WebsocketService} from "../websocket/websocket.service";
import {StatusUpdateService} from "./status-update.service";
import {ErvuPermission} from "../enum/ErvuPermission";
export interface UserSession {
userId: string,
@ -14,14 +11,13 @@ export interface UserSession {
}
@Injectable({providedIn: 'root'})
export class AuthorizationService implements OnDestroy {
export class AuthorizationService{
private session: UserSession;
public onSessionUpdate: Subject<UserSession> = new Subject<UserSession>();
constructor(protected httpClient: HttpClient, protected websocketService: WebsocketService,
protected statusUpdateService: StatusUpdateService) {}
constructor(protected httpClient: HttpClient) {}
public getCurrentSession(): Promise<any> {
if (this.session) return new Promise(resolve => resolve(this.session));
@ -30,18 +26,6 @@ export class AuthorizationService implements OnDestroy {
.then((session: UserSession) => {
this.session = session;
this.onSessionUpdate.next(session);
if (this.hasPermission(ErvuPermission.APPROVER)) {
this.websocketService.subscribe(({data}) => {
let parsedObj = JSON.parse(data);
if (parsedObj && parsedObj.body && parsedObj.body.applicationNumber) {
if (parsedObj.className === 'update' || parsedObj.className === 'processError') {
this.statusUpdateService.update(parsedObj);
}
}
});
}
return session;
})
}
@ -77,8 +61,4 @@ export class AuthorizationService implements OnDestroy {
getPermissions(): string[] {
return this.isAuthorized() ? this.session.permissions : null;
}
ngOnDestroy(): void {
this.websocketService.unsubscribe();
}
}

View file

@ -2,6 +2,11 @@ import {Injectable} from "@angular/core";
import {BehaviorSubject} from "rxjs";
import {HttpClient} from "@angular/common/http";
export interface StatusResponse {
appNumber: number;
status: string;
}
enum ApplicationStatus {
AGREED = 'Согласована',
ACCEPTED = 'Исполнена'
@ -14,19 +19,82 @@ export class StatusUpdateService {
}
public statusMessage = new BehaviorSubject<any>(null);
private pendingDeclarations = new Map<number, number>();
private pollingInterval: any;
private readonly MAX_ATTEMPTS = 12;
public update(responseObj: any): void {
this.httpClient.put<number>('status', responseObj)
.toPromise()
.then(appNumber => this.publishStatus(appNumber, responseObj.className === 'update'))
.catch(err => console.log('failed to update application status', err));
public trackDeclaration(appNumber: number): void {
if (!this.pendingDeclarations.has(appNumber)) {
this.pendingDeclarations.set(appNumber, 0);
this.startPolling();
}
}
public publishStatus(appNumber: number, accepted: boolean) {
this.statusMessage.next(
{
appNumber: appNumber,
status: accepted ? ApplicationStatus.ACCEPTED : ApplicationStatus.AGREED
this.statusMessage.next({
appNumber: appNumber,
status: accepted
? ApplicationStatus.ACCEPTED
: ApplicationStatus.AGREED
});
}
private startPolling(): void {
if (this.pendingDeclarations.size === 0 || this.pollingInterval) {
return;
}
this.pollingInterval = setInterval(() => {
this.checkPendingStatuses();
}, 5000);
}
private stopPolling(): void {
if (this.pollingInterval) {
clearInterval(this.pollingInterval);
this.pollingInterval = null;
}
}
private checkPendingStatuses(): void {
const appNumbers = Array.from(this.pendingDeclarations.keys());
if (appNumbers.length === 0) {
this.stopPolling();
return;
}
this.httpClient.post<StatusResponse[]>('status/batch', appNumbers)
.toPromise()
.then(responses => {
responses.forEach(response => {
const attemptCount = (this.pendingDeclarations.get(response.appNumber) || 0) + 1;
this.pendingDeclarations.set(response.appNumber, attemptCount);
if (response.status !== 'SENT') {
this.pendingDeclarations.delete(response.appNumber);
this.publishStatus(response.appNumber, response.status === 'ACCEPTED');
}
else if (attemptCount >= this.MAX_ATTEMPTS) {
this.pendingDeclarations.delete(response.appNumber);
console.warn(`Max attempts exceeded for declaration ${response.appNumber}`);
}
});
if (this.pendingDeclarations.size === 0) {
this.stopPolling();
}
})
.catch(err => {
console.error('Failed to check statuses', err);
appNumbers.forEach(appNumber => {
const attemptCount = (this.pendingDeclarations.get(appNumber) || 0) + 1;
this.pendingDeclarations.set(appNumber, attemptCount);
if (attemptCount >= this.MAX_ATTEMPTS) {
this.pendingDeclarations.delete(appNumber);
console.warn(`Max attempts exceeded for declaration ${appNumber} due to errors`);
}
});
});
}
}
}

View file

@ -1,37 +0,0 @@
import {Injectable} from "@angular/core";
@Injectable({providedIn: 'root'})
export class WebsocketService {
private initialData;
public subscribe(fn: Function): void {
let property = Object.getOwnPropertyDescriptor(MessageEvent.prototype, "data");
const data = property.get;
this.initialData = data;
// wrapper that replaces getter
function lookAtMessage() {
let socket = this.currentTarget instanceof WebSocket;
if (!socket || !this.currentTarget.url.endsWith('notifier.message.send.push')) {
return data.call(this);
}
let msg = data.call(this);
Object.defineProperty(this, "data", { value: msg }); //anti-loop
fn({ data: msg, socket: this.currentTarget, event: this });
return msg;
}
property.get = lookAtMessage;
Object.defineProperty(MessageEvent.prototype, "data", property);
}
public unsubscribe(): void {
let property = Object.getOwnPropertyDescriptor(MessageEvent.prototype, "data");
if (this.initialData) {
property.get = this.initialData;
Object.defineProperty(MessageEvent.prototype, "data", property);
}
}
}