Commit cad617f0 authored by AnasJeg's avatar AnasJeg

notification&chartResponse

parent 02a706d3
package bmci.esign.backendend.controllers;
import bmci.esign.backendend.dto.AuthorityDto;
import bmci.esign.backendend.models.Authority;
import bmci.esign.backendend.models.ResponseModelStandard;
import bmci.esign.backendend.services.AuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.List;
......@@ -22,6 +20,14 @@ public class AuthorityController {
public ResponseModelStandard<List<AuthorityDto>> getAll(){
return new ResponseModelStandard<>("000", authorityService.getAll());
}
@PostMapping("/create")
public ResponseModelStandard<Authority> createAuthority(@RequestBody Authority authority) {
try {
Authority response= authorityService.addAuthority(authority);
return new ResponseModelStandard<>("000", response);
}catch (Exception e){
return new ResponseModelStandard<>("099");
}
}
}
......@@ -114,7 +114,7 @@ public class DemandeController {
@GetMapping("/chart")
public ResponseModelStandard<List<DashboardChart>> chartSignDemandes(@RequestParam(name = "email") String email){
try {
List<DashboardChart> dashboardCharts =demandeService.loadSignaturesChart(email);
List<DashboardChart> dashboardCharts =demandeService.loadReceivedChart(email);
if(dashboardCharts == null){
return new ResponseModelStandard<>("099","Missing data");
}
......
package bmci.esign.backendend.controllers;
import bmci.esign.backendend.dto.NotificationDto;
import bmci.esign.backendend.dto.request.UserEmailRequest;
import bmci.esign.backendend.models.Notification;
import bmci.esign.backendend.models.ResponseModelStandard;
import bmci.esign.backendend.models.enums.ENotification;
import bmci.esign.backendend.services.NotificationService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/notifications")
public class NotificationController {
private final NotificationService notificationService;
public NotificationController(NotificationService notificationService) {
this.notificationService = notificationService;
}
@PostMapping("/create")
public ResponseModelStandard<Notification> createNotification(@RequestBody Notification notification) {
try {
Notification response= notificationService.createNotification(notification);
return new ResponseModelStandard<>("000", response);
}catch (Exception e){
return new ResponseModelStandard<>("099");
}
}
@PostMapping("/user/active")
public ResponseModelStandard<List<NotificationDto>> loadActiveNotificationsByUserEmail(@RequestBody UserEmailRequest userEmailRequest) {
try {
List<NotificationDto> notifications= notificationService.loadActiveNotificationsByUserEmail(userEmailRequest.getEmail());
return new ResponseModelStandard<>("000", notifications);
}catch (Exception e){
return new ResponseModelStandard<>("099");
}
}
@PostMapping("/update/status")
public ResponseModelStandard<NotificationDto> updateStatus(@RequestParam Long id) {
try {
notificationService.changeStatusByDmId(id, null,ENotification.VIEW);
return new ResponseModelStandard<>("000");
}catch (Exception e){
return new ResponseModelStandard<>("099");
}
}
}
package bmci.esign.backendend.dto;
import bmci.esign.backendend.models.enums.ENotification;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class NotificationDto {
private Long id;
private String title;
private String message;
private ENotification status;
}
package bmci.esign.backendend.dto.request;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class UserEmailRequest {
private String email;
}
package bmci.esign.backendend.dto.response;
public interface NotificationsResponse {
Long getId();
String getTitle();
String getMessage();
String getStatus();
}
package bmci.esign.backendend.dto.services;
import bmci.esign.backendend.dto.response.DashboardChart;
import org.springframework.data.domain.Page;
import java.util.Collection;
......@@ -17,5 +18,6 @@ public interface IMapClassWithDto<E,D> {
List<E> convertListToListEntity(Collection<D> dtoList, Class<E> outCLass);
Page<D> convertResponseToPageDto(Page<E> entityList, Class<D> outCLass);
List<DashboardChart> mappingData(List<DashboardChart> chartList);
}
package bmci.esign.backendend.dto.services;
import bmci.esign.backendend.dto.response.DashboardChart;
import bmci.esign.backendend.models.enums.EStatut;
import org.modelmapper.Conditions;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
......@@ -12,6 +14,7 @@ import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
......@@ -73,4 +76,24 @@ public class MapClassWithDto<E, D> implements IMapClassWithDto<E, D> {
return new PageImpl<>(dtoList, pageable, entityPage.getTotalElements());
}
public List<DashboardChart> mappingData(List<DashboardChart> chartList){
Map<String, Integer> map=chartList.stream().collect(Collectors.toMap(DashboardChart::getStatut, DashboardChart::getCount));
for(EStatut eStatut: EStatut.values()){
if(!map.containsKey(eStatut.name())){
map.put(eStatut.name(), 0);
}
}
return map.entrySet().stream().map(entry -> new DashboardChart() {
@Override
public int getCount() {
return entry.getValue();
}
@Override
public String getStatut() {
return entry.getKey();
}
}).collect(Collectors.toList());
}
}
......@@ -3,8 +3,9 @@ package bmci.esign.backendend.models;
import bmci.esign.backendend.models.enums.EDemande;
import bmci.esign.backendend.models.enums.EStatut;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
......@@ -12,8 +13,7 @@ import java.util.List;
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Data
@Entity
public class Demande extends AuditorEntity implements Serializable {
......@@ -48,23 +48,5 @@ public class Demande extends AuditorEntity implements Serializable {
@OneToMany(mappedBy = "demande")
private List<Document> documents;
@Override
public String toString() {
return "Demande{" +
"confidentiality='" + confidentiality + '\'' +
", priority='" + priority + '\'' +
", objetMail='" + objetMail + '\'' +
", message='" + message + '\'' +
", statut=" + statut +
", edemande=" + edemande +
", user=" + user +
", typeDocument=" + typeDocument +
", expirationDate=" + expirationDate +
", destinataires=" + destinataires +
", documents=" + documents +
", id=" + id +
", creationDate=" + creationDate +
", modificationDate=" + modificationDate +
'}';
}
}
package bmci.esign.backendend.models;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import bmci.esign.backendend.models.enums.ENotification;
import lombok.*;
import javax.persistence.*;
import java.io.Serializable;
@Data
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Notification extends AuditorEntity implements Serializable {
private String title;
private String message;
@ManyToOne
@JoinColumn(name = "destinataire_id")
private Destinataire destinataire;
@ManyToOne
@JoinColumn(name = "demande_id")
private Demande demande;
@Enumerated(EnumType.STRING)
@Column(length = 50)
private ENotification status;
@Override
public String toString() {
return "Notification{" +
"title='" + title + '\'' +
", message='" + message + '\'' +
", destinataire_name=" + destinataire.getName() +
", demande_id=" + demande.getId() +
", status=" + status +
", id=" + id +
", creationDate=" + creationDate +
", modificationDate=" + modificationDate +
'}';
}
}
package bmci.esign.backendend.models.enums;
public enum ENotification {
WAITING("En attente"),
VIEW("Vue");
private final String displayName;
ENotification(String displayName) {
this.displayName = displayName;
}
public String getDisplayName() {
return displayName;
}
public static ENotification fromString(String text) {
for (ENotification statut : ENotification.values()) {
if (statut.displayName.equalsIgnoreCase(text)) {
return statut;
}
}
throw new IllegalArgumentException("No constant with text " + text + " found");
}
}
......@@ -17,3 +17,19 @@ public interface DemandeRepository extends JpaRepository<Demande, Long>, JpaSpec
@Query(value = "SELECT count(d.id) as count, d.statut as statut FROM Demande d WHERE d.user.email= :email GROUP BY d.statut")
List<DashboardChart> loadSignaturesChart(@Param("email") String email);
}
/*
SELECT statut as statut, SUM(count) as total
FROM (
SELECT count(d.id) as count, d.statut as statut
FROM demande d
INNER JOIN user_entity u ON u.id = d.user_id
WHERE u.email = 'anasjegoual.04@gmail.com'
GROUP BY d.statut
UNION ALL
SELECT count(dem.id) as count, dem.statut as statut
FROM demande dem
INNER JOIN destinataire dest ON dem.id = dest.demande_id
WHERE dest.email = 'anasjegoual.04@gmail.com'
GROUP BY dem.statut
) GROUP BY statut;
*/
\ No newline at end of file
......@@ -9,6 +9,7 @@ import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public interface DestinataireRepository extends JpaRepository<Destinataire, Long>, JpaSpecificationExecutor<Destinataire> {
......@@ -16,4 +17,5 @@ public interface DestinataireRepository extends JpaRepository<Destinataire, Long
@Query(value = "SELECT count(d.id) as count, d.statut as statut FROM Destinataire d WHERE d.email= :email GROUP BY d.statut")
List<DashboardChart> loadSignDm(@Param("email") String email);
Optional<Destinataire> findByEmail(String email);
}
package bmci.esign.backendend.repositories;
import bmci.esign.backendend.models.Notification;
import bmci.esign.backendend.models.enums.ENotification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface NotificationRepository extends JpaRepository<Notification, Long> {
List<Notification> findAllByDestinataire_EmailAndStatus(String email, ENotification status);
Notification findByDemandeId(Long id);
}
......@@ -4,7 +4,10 @@ import bmci.esign.backendend.models.User;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
User findByEmail(String email);
Optional<User> findByEmail(String email);
boolean existsByEmail(String email);
}
package bmci.esign.backendend.services;
import bmci.esign.backendend.dto.AuthorityDto;
import bmci.esign.backendend.models.Authority;
import java.util.List;
public interface AuthorityService {
AuthorityDto addAuthority(AuthorityDto authorityDto);
Authority addAuthority(Authority authority);
AuthorityDto getAuthority(Long id);
List<AuthorityDto> getAll();
}
......@@ -23,6 +23,6 @@ public interface DemandeService {
List<DemandeDto> getAllbyUser(Long userId);
Map<String, Object> getForUserDemandeSearch(Long userId, int page, int limit, String statut, String eDemande, Date dateStart);
Page<DemandeDto> findDemandesByUser(FilterDto filterDto);
List<DashboardChart> loadSignaturesChart(String email);
List<DashboardChart> loadReceivedChart(String email);
List<DashboardChart> loadGlobalData(String email);
}
package bmci.esign.backendend.services;
import bmci.esign.backendend.dto.NotificationDto;
import bmci.esign.backendend.models.Notification;
import bmci.esign.backendend.models.enums.ENotification;
import java.util.List;
public interface NotificationService {
List<NotificationDto> loadActiveNotificationsByUserEmail(String email);
// Notification createNotification(NotificationDto dto);
Notification createNotification(Notification notification);
void changeStatusByDmId(Long id,Long demandeId, ENotification status);
}
......@@ -21,8 +21,11 @@ public class AuthorityServiceImpl implements AuthorityService {
private IMapClassWithDto<Authority, AuthorityDto> authorityMapping;
@Override
public AuthorityDto addAuthority(AuthorityDto authorityDto) {
return null;
public Authority addAuthority(Authority authority) {
if(authority == null){
return null;
}
return authorityRepository.save(authority);
}
@Override
......
package bmci.esign.backendend.services.impl;
import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.identity.User;
import org.springframework.stereotype.Service;
@Service
public class CamundaUserService {
private final IdentityService identityService;
public CamundaUserService(IdentityService identityService) {
this.identityService = identityService;
}
public void createUser(String userId, String firstName, String lastName, String email, String password) {
User user = identityService.newUser(userId);
user.setFirstName(firstName);
user.setLastName(lastName);
user.setEmail(email);
user.setPassword(password);
identityService.saveUser(user);
}
public void updateUser(String userId, String newFirstName, String newLastName, String newEmail) {
User user = identityService.createUserQuery().userId(userId).singleResult();
if (user != null) {
user.setFirstName(newFirstName);
user.setLastName(newLastName);
user.setEmail(newEmail);
identityService.saveUser(user);
}
}
}
......@@ -8,6 +8,7 @@ import bmci.esign.backendend.dto.response.DashboardChart;
import bmci.esign.backendend.dto.services.IMapClassWithDto;
import bmci.esign.backendend.models.*;
import bmci.esign.backendend.models.enums.EDemande;
import bmci.esign.backendend.models.enums.ENotification;
import bmci.esign.backendend.models.enums.EStatut;
import bmci.esign.backendend.repositories.DemandeRepository;
import bmci.esign.backendend.repositories.DestinataireRepository;
......@@ -15,6 +16,7 @@ import bmci.esign.backendend.repositories.DocumentRepository;
import bmci.esign.backendend.repositories.PositionRepository;
import bmci.esign.backendend.services.DemandeService;
import bmci.esign.backendend.services.EmailService;
import bmci.esign.backendend.services.NotificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
......@@ -23,7 +25,6 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
......@@ -55,6 +56,8 @@ public class DemandeServiceImpl implements DemandeService {
private IMapClassWithDto<Destinataire, DestinataireDto> destinataireMapping;
@Autowired
private IMapClassWithDto<Position, PositionDto> positionMapping;
@Autowired
private NotificationService notificationService;
@Override
public DemandeDto addDemande(DemandeDto demandeDto) {
......@@ -133,7 +136,7 @@ public class DemandeServiceImpl implements DemandeService {
@Override
public void processEmail(Demande demande) {
log.info("PROCESSING<>{processEmail} REQUEST {}",demande);
log.info("PROCESSING<>{processEmail}...");
List<Destinataire> destinataires = demande.getDestinataires().stream().filter(destinataire ->
destinataire.getStatut().name().equals(EStatut.WAITING.name())).collect(Collectors.toList());
......@@ -147,6 +150,7 @@ public class DemandeServiceImpl implements DemandeService {
if(demande1.isPresent()){
demande.setEdemande(demande1.get().getEdemande());
demandeRepository.save(demande);
// notificationService.changeStatusByDmId(null, demande.getId(), ENotification.VIEW);
}
}
}else if(demande.getEdemande().equals(EDemande.PARALLELE)){
......@@ -158,6 +162,7 @@ public class DemandeServiceImpl implements DemandeService {
if(demande1.isPresent()){
demande.setEdemande(demande1.get().getEdemande());
demandeRepository.save(demande);
// notificationService.changeStatusByDmId(null, demande.getId(), ENotification.VIEW);
}
}else{
......@@ -232,7 +237,14 @@ public class DemandeServiceImpl implements DemandeService {
model.put("message", demande.getMessage());
model.put("name", dest.getName());
emailService.SendEmail(mailRequest,model);
}
String message = String.format("Cher %s, vous avez une nouvelle demande avec le sujet '%s'. Veuillez la consulter dès que possible.",
demande.getUser().getLastname(),
demande.getObjetMail());
for(Destinataire destinataire : demande.getDestinataires()){
notificationService.createNotification(new Notification(message, demande.getObjetMail(),destinataire,demande, ENotification.WAITING));
}
}
@Override
public void deleteDemande(Long id) {
......@@ -355,17 +367,31 @@ public class DemandeServiceImpl implements DemandeService {
@Override
@Transactional(readOnly = true)
public List<DashboardChart> loadSignaturesChart(String email) {
log.info("PROCESSING<>{loadSignaturesChart}... EMAIL: {}", email);
public List<DashboardChart> loadReceivedChart(String email) {
log.info("PROCESSING<>{Load_received_Request}... EMAIL: {}", email);
try {
if(email.isEmpty()){
return null;
}
return demandeRepository.loadSignaturesChart(email);
return demandeMapping.mappingData(demandeRepository.loadSignaturesChart(email));
}catch (Exception e){
log.error("EXCEPTION<>loadSignaturesChart: {}", e.getMessage(), e);
return Collections.emptyList();
}
}
@Override
@Transactional(readOnly = true)
public List<DashboardChart> loadGlobalData(String email) {
log.info("PROCESSING<>{loadGlobalData}... EMAIL: {}", email);
try {
if(email.isEmpty()){
return null;
}
return demandeRepository.loadSignaturesChart(email);
}catch (Exception e){
log.error("EXCEPTION<>loadSignaturesChart: {}", e.getMessage(), e);
return Collections.emptyList();
}
}
}
\ No newline at end of file
......@@ -124,7 +124,7 @@ public class DestinataireServiceImpl implements DestinataireService {
if(email.isEmpty()){
return null;
}
return destinataireRepository.loadSignDm(email);
return destinataireMapping.mappingData(destinataireRepository.loadSignDm(email));
}catch (Exception e){
log.error("EXCEPTION<>loadSignaturesChart: {}", e.getMessage(), e);
return Collections.emptyList();
......
package bmci.esign.backendend.services.impl;
import bmci.esign.backendend.dto.NotificationDto;
import bmci.esign.backendend.dto.services.IMapClassWithDto;
import bmci.esign.backendend.models.Notification;
import bmci.esign.backendend.models.enums.ENotification;
import bmci.esign.backendend.repositories.NotificationRepository;
import bmci.esign.backendend.repositories.UserRepository;
import bmci.esign.backendend.services.NotificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collections;
import java.util.List;
@Service
@Slf4j
public class NotificationServiceImpl implements NotificationService {
private final NotificationRepository notificationRepository;
private final UserRepository userRepository;
private final IMapClassWithDto<Notification, NotificationDto> mapClassWithDto;
public NotificationServiceImpl(NotificationRepository notificationRepository, UserRepository userRepository, IMapClassWithDto<Notification, NotificationDto> mapClassWithDto) {
this.notificationRepository = notificationRepository;
this.userRepository = userRepository;
this.mapClassWithDto = mapClassWithDto;
}
@Override
@Transactional(readOnly = true)
public List<NotificationDto> loadActiveNotificationsByUserEmail(String email) {
log.info("PROCESSING<>{loadActiveNotificationsByUserEmail}.. REQUEST {}", email);
try {
if (!userRepository.existsByEmail(email)) {
log.warn("loadActiveNotificationsByUserEmail: User Not Found with EMAIL {}", email);
return Collections.emptyList();
}
List<Notification> notifications = notificationRepository.findAllByDestinataire_EmailAndStatus(email, ENotification.WAITING);
return mapClassWithDto.convertListToListDto(notifications, NotificationDto.class);
} catch (Exception e) {
log.error("Exception occurred while loading active notifications for User EMAIL {}: {}", email, e.getMessage());
return Collections.emptyList();
}
}
@Override
@Transactional(rollbackFor = Exception.class)
// public Notification createNotification(NotificationDto dto) {
public Notification createNotification(Notification notification) {
log.info("PROCESSING<>{createNotification}.. REQUEST {}", notification);
try {
// Notification notif = mapClassWithDto.convertToEntity(dto, Notification.class);
// log.info("AFTER convert To Entity {}",notif);
Notification savedNotification = notificationRepository.save(notification);
log.info("createNotification: Notification created with ID {}", savedNotification.getId());
return savedNotification;
} catch (Exception e) {
log.error("Exception occurred while creating notification: {}", e.getMessage());
throw e;
}
}
@Override
@Transactional
public void changeStatusByDmId(Long id, Long demandeId, ENotification status) {
log.info("PROCESSING<>{changeStatus}.. REQUEST_ ID {} Demande_Id {}", id, demandeId);
try {
Notification notification;
if(id == null){
notification = notificationRepository.findByDemandeId(demandeId);
}else{
notification = notificationRepository.findById(id).get();
}
notification.setStatus(status);
notificationRepository.save(notification);
} catch (Exception e) {
log.error("Exception occurred while creating notification: {}", e.getMessage());
throw e;
}
}
}
......@@ -55,7 +55,7 @@ public class UserServiceImpl implements UserService {
@Override
public UserDto findByEmail(String email) {
log.info("PROCESSING<>{findByEmail}... REQUEST: {}", email);
User user = userRepository.findByEmail(email);
return userMapping.convertToDto(user, UserDto.class);
Optional<User> user = userRepository.findByEmail(email);
return user.map(value -> userMapping.convertToDto(value, UserDto.class)).orElse(null);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment