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

torresj / infosas-api / 45

21 Feb 2025 10:32AM UTC coverage: 92.691% (-0.3%) from 93.0%
45

push

circleci

Jaime Torres Benavente
log added

0 of 1 new or added line in 1 file covered. (0.0%)

279 of 301 relevant lines covered (92.69%)

0.93 hits per line

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

92.39
/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.StaffExamType;
14
import com.torresj.infosas.enums.StaffType;
15
import com.torresj.infosas.exceptions.StaffNotFoundException;
16
import com.torresj.infosas.mappers.StaffMapper;
17
import com.torresj.infosas.repositories.StaffExamRepository;
18
import com.torresj.infosas.repositories.StaffJobBankRepository;
19
import com.torresj.infosas.repositories.StaffRepository;
20
import com.torresj.infosas.repositories.StaffSpecificJobBankRepository;
21
import com.torresj.infosas.services.ProducerService;
22
import com.torresj.infosas.services.StaffService;
23
import lombok.AllArgsConstructor;
24
import lombok.extern.slf4j.Slf4j;
25
import org.springframework.beans.factory.annotation.Value;
26
import org.springframework.data.domain.Limit;
27
import org.springframework.stereotype.Service;
28

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

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

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

40
    private static final int MAX_NUMBER_OF_STAFFS = 100;
41

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

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

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

65
    @Override
66
    public Set<StaffDto> getStaffs(String name, String surname, StaffType type) {
67
        Set<StaffDto> staffs;
68
        if(isBlank(name) && type == null) {
1✔
69
            staffs = getStaffsBySurname(surname.trim());
×
70
        }else if(!isBlank(name) && type == null) {
1✔
71
            return staffRepository.findAllByNameContainingIgnoreCaseAndSurnameContainingIgnoreCase(name.trim(), surname.trim(), Limit.of(MAX_NUMBER_OF_STAFFS))
1✔
72
                    .stream()
1✔
73
                    .map(entity -> {
1✔
74
                        int exams = staffExamRepository.findByStaffId(entity.getId()).size();
1✔
75
                        int jobBanks = staffJobBankRepository.findByStaffId(entity.getId()).size();
1✔
76
                        int specificJobBanks = staffSpecificJobBankRepository.findByStaffId(entity.getId()).size();
1✔
77
                        return toStaffDto(entity, exams, jobBanks, specificJobBanks);
1✔
78
                    })
79
                    .collect(Collectors.toSet());
1✔
80
        } else if(isBlank(name) && type != null) {
1✔
81
            staffs = staffRepository.findAllBySurnameContainingIgnoreCaseAndType(surname.trim(), type, Limit.of(MAX_NUMBER_OF_STAFFS))
1✔
82
                    .stream()
1✔
83
                    .map(entity -> {
1✔
84
                        int exams = staffExamRepository.findByStaffId(entity.getId()).size();
1✔
85
                        int jobBanks = staffJobBankRepository.findByStaffId(entity.getId()).size();
1✔
86
                        int specificJobBanks = staffSpecificJobBankRepository.findByStaffId(entity.getId()).size();
1✔
87
                        return toStaffDto(entity, exams, jobBanks, specificJobBanks);
1✔
88
                    })
89
                    .collect(Collectors.toSet());
1✔
90
        } else {
91
            staffs = staffRepository.findAllByNameContainingIgnoreCaseAndSurnameContainingIgnoreCaseAndType(name.trim(), surname.trim(), type,  Limit.of(MAX_NUMBER_OF_STAFFS))
1✔
92
                    .stream()
1✔
93
                    .map(entity -> {
1✔
94
                        int exams = staffExamRepository.findByStaffId(entity.getId()).size();
1✔
95
                        int jobBanks = staffJobBankRepository.findByStaffId(entity.getId()).size();
1✔
96
                        int specificJobBanks = staffSpecificJobBankRepository.findByStaffId(entity.getId()).size();
1✔
97
                        return toStaffDto(entity, exams, jobBanks, specificJobBanks);
1✔
98
                    })
99
                    .collect(Collectors.toSet());
1✔
100
        }
101
        if (staffs.isEmpty()) {
1✔
NEW
102
            log.info("No staff found for {} {} {}", name, surname, type);
×
103
            producerService.sendMessage(QueueMessage.builder()
×
104
                    .text("No se ha encontrado a ningún profesional para la búsqueda con nombre: " + name + ", apellidos: " + surname + " y categoria profesional " + type)
×
105
                    .chatId(notificationChannelId)
×
106
                    .type(MessageType.WARNING)
×
107
                    .build());
×
108
        }
109
        return staffs;
1✔
110
    }
111

112
    @Override
113
    public EnrichedStaffDto getStaffById(Long id) throws StaffNotFoundException {
114
        var entity = staffRepository.findById(id).orElseThrow(() -> new StaffNotFoundException(id));
1✔
115
        return toEnrichedStaffDto(entity);
1✔
116
    }
117

118
    @Override
119
    public List<EnrichedStaffExamDto> getEnrichedStaffExam(String surname, StaffExamType type) {
120
        return staffRepository.findAllBySurnameContainingIgnoreCase(surname, Limit.of(MAX_NUMBER_OF_STAFFS))
1✔
121
                .stream()
1✔
122
                .map(staff -> {
1✔
123
                    var provisional = staffExamRepository.findByStaffIdAndTypeAndProvisional(
1✔
124
                            staff.getId(),
1✔
125
                            type,
126
                            true
127
                    );
128
                    var definitive = staffExamRepository.findByStaffIdAndTypeAndProvisional(
1✔
129
                            staff.getId(),
1✔
130
                            type,
131
                            false
132
                    );
133

134
                    return staffMapper.toEnrichedStaffExamDto(staff, provisional, definitive);
1✔
135
                })
136
                .filter(enrichedStaffExamDto ->
1✔
137
                        enrichedStaffExamDto.definitiveExam() != null || enrichedStaffExamDto.provisionalExam() != null
1✔
138
                ).toList();
1✔
139
    }
140

141
    @Override
142
    public Set<EnrichedStaffJobBankDto> getEnrichedStaffJobBank(String surname, JobBankType type) {
143
        return staffRepository.findAllBySurnameContainingIgnoreCase(surname, Limit.of(MAX_NUMBER_OF_STAFFS))
1✔
144
                .stream()
1✔
145
                .map(staffEntity -> {
1✔
146
                    var staffJobBankEntity = staffJobBankRepository.findByStaffIdAndType(staffEntity.getId(), type);
1✔
147
                    return staffMapper.toEnrichedStaffJobBankDto(staffEntity, staffJobBankEntity);
1✔
148
                })
149
                .filter(enrichedStaffJobBankDto -> enrichedStaffJobBankDto.staffJobBank() != null)
1✔
150
                .collect(Collectors.toSet());
1✔
151
    }
152

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

165
    private StaffDto toStaffDto(StaffEntity staffEntity, int exams, int jobBanks, int specificJobBanks) {
166
        return new StaffDto(
1✔
167
                staffEntity.getId(),
1✔
168
                staffEntity.getName(),
1✔
169
                staffEntity.getSurname(),
1✔
170
                staffEntity.getDni(),
1✔
171
                staffEntity.getType(),
1✔
172
                exams,
173
                jobBanks,
174
                specificJobBanks
175
        );
176
    }
177

178
    private EnrichedStaffDto toEnrichedStaffDto(StaffEntity staffEntity) {
179
        return new EnrichedStaffDto(
1✔
180
                staffEntity.getId(),
1✔
181
                staffEntity.getName(),
1✔
182
                staffEntity.getSurname(),
1✔
183
                staffEntity.getDni(),
1✔
184
                staffExamRepository.findByStaffId(staffEntity.getId())
1✔
185
                        .stream().
1✔
186
                        map(staffMapper::toStaffExamDto).toList(),
1✔
187
                staffJobBankRepository.findByStaffId(staffEntity.getId())
1✔
188
                        .stream()
1✔
189
                        .map(staffMapper::toStaffJobBankDto).collect(Collectors.toSet()),
1✔
190
                staffSpecificJobBankRepository.findByStaffId(staffEntity.getId())
1✔
191
                        .stream()
1✔
192
                        .map(staffMapper::toStaffSpecificJobBankDto).collect(Collectors.toSet())
1✔
193
        );
194
    }
195
}
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