• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

torresj / infosas-api / 57

27 Mar 2025 07:13AM UTC coverage: 92.351% (-1.3%) from 93.642%
57

push

circleci

Jaime Torres Benavente
no staff found message updated

3 of 8 new or added lines in 1 file covered. (37.5%)

326 of 353 relevant lines covered (92.35%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.32
/src/main/java/com/torresj/infosas/services/impl/StaffServiceImpl.java
1
package com.torresj.infosas.services.impl;
2

3
import com.torresj.infosas.dtos.EnrichedSpecificStaffJobBankDto;
4
import com.torresj.infosas.dtos.EnrichedStaffDto;
5
import com.torresj.infosas.dtos.EnrichedStaffExamDto;
6
import com.torresj.infosas.dtos.EnrichedStaffJobBankDto;
7
import com.torresj.infosas.dtos.QueueMessage;
8
import com.torresj.infosas.dtos.StaffDto;
9
import com.torresj.infosas.entities.StaffEntity;
10
import com.torresj.infosas.enums.JobBankType;
11
import com.torresj.infosas.enums.MessageType;
12
import com.torresj.infosas.enums.SpecificJobBankType;
13
import com.torresj.infosas.enums.StaffType;
14
import com.torresj.infosas.exceptions.StaffNotFoundException;
15
import com.torresj.infosas.mappers.StaffMapper;
16
import com.torresj.infosas.repositories.StaffExamRepository;
17
import com.torresj.infosas.repositories.StaffJobBankRepository;
18
import com.torresj.infosas.repositories.StaffRepository;
19
import com.torresj.infosas.repositories.StaffSpecificJobBankRepository;
20
import com.torresj.infosas.services.ProducerService;
21
import com.torresj.infosas.services.StaffService;
22
import lombok.AllArgsConstructor;
23
import lombok.extern.slf4j.Slf4j;
24
import org.springframework.beans.factory.annotation.Value;
25
import org.springframework.data.domain.Limit;
26
import org.springframework.stereotype.Service;
27

28
import java.util.List;
29
import java.util.Set;
30
import java.util.stream.Collectors;
31

32
import static org.apache.commons.lang3.StringUtils.isBlank;
33

34
@Service
35
@Slf4j
1✔
36
@AllArgsConstructor
37
public class StaffServiceImpl implements StaffService {
38

39
    private static final int MAX_NUMBER_OF_STAFFS = 100;
40

41
    private final StaffRepository staffRepository;
42
    private final StaffJobBankRepository staffJobBankRepository;
43
    private final StaffSpecificJobBankRepository staffSpecificJobBankRepository;
44
    private final StaffExamRepository staffExamRepository;
45
    private final StaffMapper staffMapper;
46
    private final ProducerService producerService;
47

48
    @Value("${telegram.notification.channel.id}")
49
    private final String notificationChannelId;
50

51
    @Override
52
    public Set<StaffDto> getStaffsBySurname(String surname) {
53
        return staffRepository.findAllBySurnameContainingIgnoreCase(surname, Limit.of(MAX_NUMBER_OF_STAFFS))
1✔
54
                .stream()
1✔
55
                .map(entity -> {
1✔
56
                    int exams = staffExamRepository.findByStaffId(entity.getId()).size();
1✔
57
                    int jobBanks = staffJobBankRepository.findByStaffId(entity.getId()).size();
1✔
58
                    int specificJobBanks = staffSpecificJobBankRepository.findByStaffId(entity.getId()).size();
1✔
59
                    return toStaffDto(entity, exams, jobBanks, specificJobBanks);
1✔
60
                })
61
                .collect(Collectors.toSet());
1✔
62
    }
63

64
    @Override
65
    public Set<StaffDto> getStaffsByDni(String dni) {
66
        Set<StaffDto> staffs = staffRepository.findAllByDni(dni, Limit.of(MAX_NUMBER_OF_STAFFS))
1✔
67
                .stream()
1✔
68
                .map(entity -> {
1✔
69
                    int exams = staffExamRepository.findByStaffId(entity.getId()).size();
1✔
70
                    int jobBanks = staffJobBankRepository.findByStaffId(entity.getId()).size();
1✔
71
                    int specificJobBanks = staffSpecificJobBankRepository.findByStaffId(entity.getId()).size();
1✔
72
                    return toStaffDto(entity, exams, jobBanks, specificJobBanks);
1✔
73
                })
74
                .collect(Collectors.toSet());
1✔
75

76
        if (staffs.isEmpty()) {
1✔
NEW
77
            producerService.sendMessage(QueueMessage.builder()
×
NEW
78
                    .text("No se ha encontrado a ningún profesional para la búsqueda con dni " + dni )
×
NEW
79
                    .chatId(notificationChannelId)
×
NEW
80
                    .type(MessageType.WARNING)
×
NEW
81
                    .build());
×
82
        }
83

84
        return staffs;
1✔
85
    }
86

87
    @Override
88
    public Set<StaffDto> getStaffs(String name, String surname, StaffType type) {
89
        Set<StaffDto> staffs;
90
        if(isBlank(name) && type == null) {
1✔
91
            staffs = getStaffsBySurname(surname.trim());
×
92
        }else if(!isBlank(name) && type == null) {
1✔
93
            staffs = staffRepository.findAllByNameContainingIgnoreCaseAndSurnameContainingIgnoreCase(name.trim(), surname.trim(), Limit.of(MAX_NUMBER_OF_STAFFS))
1✔
94
                    .stream()
1✔
95
                    .map(entity -> {
1✔
96
                        int exams = staffExamRepository.findByStaffId(entity.getId()).size();
1✔
97
                        int jobBanks = staffJobBankRepository.findByStaffId(entity.getId()).size();
1✔
98
                        int specificJobBanks = staffSpecificJobBankRepository.findByStaffId(entity.getId()).size();
1✔
99
                        return toStaffDto(entity, exams, jobBanks, specificJobBanks);
1✔
100
                    })
101
                    .collect(Collectors.toSet());
1✔
102
        } else if(isBlank(name) && type != null) {
1✔
103
            staffs = staffRepository.findAllBySurnameContainingIgnoreCaseAndType(surname.trim(), type, Limit.of(MAX_NUMBER_OF_STAFFS))
1✔
104
                    .stream()
1✔
105
                    .map(entity -> {
1✔
106
                        int exams = staffExamRepository.findByStaffId(entity.getId()).size();
1✔
107
                        int jobBanks = staffJobBankRepository.findByStaffId(entity.getId()).size();
1✔
108
                        int specificJobBanks = staffSpecificJobBankRepository.findByStaffId(entity.getId()).size();
1✔
109
                        return toStaffDto(entity, exams, jobBanks, specificJobBanks);
1✔
110
                    })
111
                    .collect(Collectors.toSet());
1✔
112
        } else {
113
            staffs = staffRepository.findAllByNameContainingIgnoreCaseAndSurnameContainingIgnoreCaseAndType(name.trim(), surname.trim(), type,  Limit.of(MAX_NUMBER_OF_STAFFS))
1✔
114
                    .stream()
1✔
115
                    .map(entity -> {
1✔
116
                        int exams = staffExamRepository.findByStaffId(entity.getId()).size();
1✔
117
                        int jobBanks = staffJobBankRepository.findByStaffId(entity.getId()).size();
1✔
118
                        int specificJobBanks = staffSpecificJobBankRepository.findByStaffId(entity.getId()).size();
1✔
119
                        return toStaffDto(entity, exams, jobBanks, specificJobBanks);
1✔
120
                    })
121
                    .collect(Collectors.toSet());
1✔
122
        }
123

124
        if (staffs.isEmpty()) {
1✔
125
            producerService.sendMessage(QueueMessage.builder()
×
126
                    .text("No se ha encontrado a ningún profesional para la búsqueda con nombre: " + name + ", apellidos: " + surname + " y categoria profesional " + type)
×
127
                    .chatId(notificationChannelId)
×
128
                    .type(MessageType.WARNING)
×
129
                    .build());
×
130
        }
131
        return staffs;
1✔
132
    }
133

134
    @Override
135
    public EnrichedStaffDto getStaffById(Long id) throws StaffNotFoundException {
136
        var entity = staffRepository.findById(id).orElseThrow(() -> new StaffNotFoundException(id));
1✔
137
        return toEnrichedStaffDto(entity);
1✔
138
    }
139

140
    @Override
141
    public List<EnrichedStaffExamDto> getEnrichedStaffExam(String surname) {
142
        return staffRepository.findAllBySurnameContainingIgnoreCase(surname, Limit.of(MAX_NUMBER_OF_STAFFS))
1✔
143
                .stream()
1✔
144
                .map(staff -> {
1✔
145
                    var staffExam = staffExamRepository.findByStaffId(
1✔
146
                            staff.getId()
1✔
147
                    );
148
                    return staffMapper.toEnrichedStaffExamDto(staff, staffExam);
1✔
149
                })
150
                .filter(enrichedStaffExamDto -> !enrichedStaffExamDto.exams().isEmpty())
1✔
151
                .toList();
1✔
152
    }
153

154
    @Override
155
    public Set<EnrichedStaffJobBankDto> getEnrichedStaffJobBank(String surname, JobBankType type) {
156
        return staffRepository.findAllBySurnameContainingIgnoreCase(surname, Limit.of(MAX_NUMBER_OF_STAFFS))
1✔
157
                .stream()
1✔
158
                .map(staffEntity -> {
1✔
159
                    var staffJobBankEntity = staffJobBankRepository.findByStaffIdAndType(staffEntity.getId(), type);
1✔
160
                    return staffMapper.toEnrichedStaffJobBankDto(staffEntity, staffJobBankEntity);
1✔
161
                })
162
                .filter(enrichedStaffJobBankDto -> enrichedStaffJobBankDto.staffJobBank() != null)
1✔
163
                .collect(Collectors.toSet());
1✔
164
    }
165

166
    @Override
167
    public Set<EnrichedSpecificStaffJobBankDto> getEnrichedSpecificStaffJobBank(String surname, SpecificJobBankType type) {
168
        return staffRepository.findAllBySurnameContainingIgnoreCase(surname, Limit.of(MAX_NUMBER_OF_STAFFS))
1✔
169
                .stream()
1✔
170
                .map(staffEntity -> {
1✔
171
                    var staffJobBankEntity = staffSpecificJobBankRepository.findByStaffIdAndType(staffEntity.getId(), type);
1✔
172
                    return staffMapper.toEnrichedSpecificStaffJobBankDto(staffEntity, staffJobBankEntity);
1✔
173
                })
174
                .filter(enrichedStaffJobBankDto -> enrichedStaffJobBankDto.staffJobBank() != null)
1✔
175
                .collect(Collectors.toSet());
1✔
176
    }
177

178
    private StaffDto toStaffDto(StaffEntity staffEntity, int exams, int jobBanks, int specificJobBanks) {
179
        return new StaffDto(
1✔
180
                staffEntity.getId(),
1✔
181
                staffEntity.getName(),
1✔
182
                staffEntity.getSurname(),
1✔
183
                staffEntity.getDni(),
1✔
184
                staffEntity.getType(),
1✔
185
                exams,
186
                jobBanks,
187
                specificJobBanks
188
        );
189
    }
190

191
    private EnrichedStaffDto toEnrichedStaffDto(StaffEntity staffEntity) {
192
        return new EnrichedStaffDto(
1✔
193
                staffEntity.getId(),
1✔
194
                staffEntity.getName(),
1✔
195
                staffEntity.getSurname(),
1✔
196
                staffEntity.getDni(),
1✔
197
                staffExamRepository.findByStaffId(staffEntity.getId())
1✔
198
                        .stream().
1✔
199
                        map(staffMapper::toStaffExamDto).toList(),
1✔
200
                staffJobBankRepository.findByStaffId(staffEntity.getId())
1✔
201
                        .stream()
1✔
202
                        .map(staffMapper::toStaffJobBankDto).collect(Collectors.toSet()),
1✔
203
                staffSpecificJobBankRepository.findByStaffId(staffEntity.getId())
1✔
204
                        .stream()
1✔
205
                        .map(staffMapper::toStaffSpecificJobBankDto).collect(Collectors.toSet())
1✔
206
        );
207
    }
208
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc