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

DataBiosphere / consent / #5928

19 May 2025 12:15PM UTC coverage: 78.727% (-0.06%) from 78.785%
#5928

push

web-flow
DT-1660: Remove LC institution and era commons values (#2526)

20 of 22 new or added lines in 5 files covered. (90.91%)

3 existing lines in 3 files now uncovered.

10003 of 12706 relevant lines covered (78.73%)

0.79 hits per line

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

89.68
/src/main/java/org/broadinstitute/consent/http/service/LibraryCardService.java
1
package org.broadinstitute.consent.http.service;
2

3
import com.google.inject.Inject;
4
import jakarta.ws.rs.BadRequestException;
5
import jakarta.ws.rs.NotFoundException;
6
import java.util.ArrayList;
7
import java.util.Date;
8
import java.util.List;
9
import java.util.Objects;
10
import java.util.Optional;
11
import org.broadinstitute.consent.http.db.InstitutionDAO;
12
import org.broadinstitute.consent.http.db.LibraryCardDAO;
13
import org.broadinstitute.consent.http.db.UserDAO;
14
import org.broadinstitute.consent.http.enumeration.UserRoles;
15
import org.broadinstitute.consent.http.exceptions.ConsentConflictException;
16
import org.broadinstitute.consent.http.models.Institution;
17
import org.broadinstitute.consent.http.models.LibraryCard;
18
import org.broadinstitute.consent.http.models.User;
19

20
public class LibraryCardService {
21

22
  private final LibraryCardDAO libraryCardDAO;
23
  private final InstitutionDAO institutionDAO;
24
  private final InstitutionService institutionService;
25
  private final UserDAO userDAO;
26

27
  @Inject
28
  public LibraryCardService(LibraryCardDAO libraryCardDAO, InstitutionDAO institutionDAO,
29
      InstitutionService institutionService,
30
      UserDAO userDAO) {
1✔
31
    this.libraryCardDAO = libraryCardDAO;
1✔
32
    this.institutionDAO = institutionDAO;
1✔
33
    this.institutionService = institutionService;
1✔
34
    this.userDAO = userDAO;
1✔
35
  }
1✔
36

37
  public LibraryCard createLibraryCard(LibraryCard libraryCard, User user) {
38
    throwIfNull(libraryCard);
1✔
39
    boolean isAdmin = checkIsAdmin(user);
1✔
40
    checkIfCardExists(libraryCard);
1✔
41
    processUserOnNewLC(libraryCard);
1✔
42
    if (!isAdmin) {
1✔
43
      checkForValidInstitution(user.getInstitutionId(), libraryCard.getUserEmail());
1✔
44
    }
45
    Date createDate = new Date();
1✔
46
    Integer id = libraryCardDAO.insertLibraryCard(
1✔
47
        libraryCard.getUserId(),
1✔
48
        libraryCard.getUserName(),
1✔
49
        libraryCard.getUserEmail(),
1✔
50
        libraryCard.getCreateUserId(),
1✔
51
        createDate);
52
    return libraryCardDAO.findLibraryCardById(id);
1✔
53
  }
54

55
  public LibraryCard updateLibraryCard(LibraryCard libraryCard, Integer id, Integer userId) {
56
    LibraryCard updateCard = libraryCardDAO.findLibraryCardById(id);
1✔
57
    throwIfNull(updateCard);
1✔
58
    checkUserId(userId);
1✔
59
    checkForValidUser(libraryCard.getUserId());
1✔
60

61
    Date updateDate = new Date();
1✔
62
    libraryCardDAO.updateLibraryCardById(
1✔
63
        id,
64
        libraryCard.getUserId(),
1✔
65
        libraryCard.getUserName(),
1✔
66
        libraryCard.getUserEmail(),
1✔
67
        userId,
68
        updateDate
69
    );
70
    return libraryCardDAO.findLibraryCardById(id);
1✔
71
  }
72

73
  public void deleteLibraryCardById(Integer id) {
74
    LibraryCard card = findLibraryCardById(id);
×
75
    throwIfNull(card);
×
76
    libraryCardDAO.deleteLibraryCardById(id);
×
77
  }
×
78

79
  public List<LibraryCard> findAllLibraryCards() {
80
    return libraryCardDAO.findAllLibraryCards();
×
81
  }
82

83
  public List<LibraryCard> findLibraryCardsByUserId(Integer userId) {
84
    return libraryCardDAO.findLibraryCardsByUserId(userId);
1✔
85
  }
86

87
  public List<LibraryCard> findLibraryCardsByInstitutionId(Integer institutionId) {
88
    return libraryCardDAO.findLibraryCardsByInstitutionId(institutionId);
×
89
  }
90

91
  public LibraryCard findLibraryCardById(Integer libraryCardId) {
92
    LibraryCard libraryCard = libraryCardDAO.findLibraryCardById(libraryCardId);
1✔
93
    throwIfNull(libraryCard);
1✔
94
    return libraryCard;
1✔
95
  }
96

97
  public LibraryCard findLibraryCardWithDaasById(Integer libraryCardId) {
98
    LibraryCard libraryCard = libraryCardDAO.findLibraryCardDaaById(libraryCardId);
1✔
99
    throwIfNull(libraryCard);
1✔
100
    return libraryCard;
1✔
101
  }
102

103
  public void addDaaToLibraryCard(Integer libraryCardId, Integer daaId) {
104
    libraryCardDAO.createLibraryCardDaaRelation(libraryCardId, daaId);
1✔
105
  }
1✔
106

107
  public void removeDaaFromLibraryCard(Integer libraryCardId, Integer daaId) {
108
    libraryCardDAO.deleteLibraryCardDaaRelation(libraryCardId, daaId);
1✔
109
  }
1✔
110

111
  public List<LibraryCard> addDaaToUserLibraryCardByInstitution(User user, User signingOfficial, Integer daaId) {
112
    if (signingOfficial.getInstitutionId() == null) {
1✔
113
      throw new BadRequestException("This signing official does not have an institution.");
1✔
114
    }
115
    List<LibraryCard> libraryCards = new ArrayList<>(libraryCardDAO.findLibraryCardsByUserId(user.getUserId()));
1✔
116
    if (libraryCards.isEmpty()) {
1✔
117
      LibraryCard lc = createLibraryCardForSigningOfficial(user, signingOfficial);
1✔
118
      libraryCards.add(lc);
1✔
119
    }
120
    // typically there should be one library card per user per institution
121
    for (LibraryCard libraryCard : libraryCards) {
1✔
122
      addDaaToLibraryCard(libraryCard.getId(), daaId);
1✔
123
    }
1✔
124
    return libraryCardDAO.findLibraryCardsByUserId(user.getUserId());
1✔
125
  }
126

127
  public List<LibraryCard> removeDaaFromUserLibraryCards(User user, Integer daaId) {
128
    List<LibraryCard> libraryCards = findLibraryCardsByUserId(user.getUserId());
1✔
129
    // typically there should be one library card per user
130
    for (LibraryCard libraryCard : libraryCards) {
1✔
131
      removeDaaFromLibraryCard(libraryCard.getId(), daaId);
1✔
132
    }
1✔
133
    return libraryCards;
1✔
134
  }
135

136
  public LibraryCard createLibraryCardForSigningOfficial(User user, User signingOfficial) {
137
    LibraryCard lc = new LibraryCard();
1✔
138
    lc.setUserId(user.getUserId());
1✔
139
    lc.setUserName(user.getDisplayName());
1✔
140
    lc.setUserEmail(user.getEmail());
1✔
141
    lc.setCreateUserId(signingOfficial.getUserId());
1✔
142
    LibraryCard createdLc = createLibraryCard(lc, user);
1✔
143
    return createdLc;
1✔
144
  }
145

146
  private void checkForValidInstitution(Integer institutionId, String userEmail) {
147
    checkInstitutionId(institutionId);
1✔
148
    Institution institution = institutionDAO.findInstitutionById(institutionId);
1✔
149

150
    if (Objects.isNull(institution)) {
1✔
UNCOV
151
      throw new IllegalArgumentException("Invalid Institution Id");
×
152
    }
153

154
    var userInstitution = institutionService.findInstitutionForEmail(userEmail);
1✔
155
    if (userInstitution == null || !userInstitution.getId().equals(institutionId)) {
1✔
156
      throw new BadRequestException(
1✔
157
          "User email %s does not match institution %s".formatted(userEmail, institution.getName()));
1✔
158
    }
159
  }
1✔
160

161
  private void checkForValidUser(Integer userId) {
162
    if (Objects.isNull(userId)) {
1✔
163
      return;
×
164
    }
165

166
    User user = userDAO.findUserById(userId);
1✔
167
    if (Objects.isNull(user)) {
1✔
168
      throw new IllegalArgumentException("Invalid User Id");
×
169
    }
170
  }
1✔
171

172
  private void checkInstitutionId(Integer institutionId) {
173
    if (Objects.isNull(institutionId)) {
1✔
174
      throw new IllegalArgumentException("Institution ID is a required parameter");
×
175
    }
176
  }
1✔
177

178
  private void checkUserId(Integer userId) {
179
    if (Objects.isNull(userId)) {
1✔
180
      throw new IllegalArgumentException("User ID is a required parameter");
×
181
    }
182
  }
1✔
183

184
  private void throwIfNull(LibraryCard libraryCard) {
185
    if (Objects.isNull(libraryCard)) {
1✔
186
      throw new NotFoundException("LibraryCard not found.");
1✔
187
    }
188
  }
1✔
189

190
  //helper method for create method, checks to see if card already exists
191
  private void checkIfCardExists(LibraryCard payload) {
192
    Integer userId = payload.getUserId();
1✔
193
    String email = payload.getUserEmail();
1✔
194
    Optional<LibraryCard> foundCard;
195
    List<LibraryCard> results;
196

197
    if (Objects.nonNull(payload.getUserId())) {
1✔
198
      results = libraryCardDAO.findLibraryCardsByUserId(userId);
1✔
199
    } else if (Objects.nonNull(email)) {
1✔
200
      results = libraryCardDAO.findAllLibraryCardsByUserEmail(email);
1✔
201
    } else {
202
      throw new BadRequestException();
1✔
203
    }
204
    foundCard = results.stream().filter(card -> {
1✔
205
      Boolean sameUserId = Objects.nonNull(userId) && card.getUserId().equals(userId);
1✔
206
      Boolean sameUserEmail = Objects.nonNull(email) && card.getUserEmail().equalsIgnoreCase(email);
1✔
207
      return (sameUserId || sameUserEmail);
1✔
208
    }).findFirst();
1✔
209

210
    if (foundCard.isPresent()) {
1✔
211
      throw new ConsentConflictException();
1✔
212
    }
213
  }
1✔
214

215
  // Helper method to process user data on create LC payload.
216
  // Needed since CREATE has a unique situation where admins can create LCs without an active
217
  // user (save with userEmail instead).
218
  private void processUserOnNewLC(LibraryCard card) {
219
    if (card.getUserId() == null) {
1✔
220
      // No user ID is provided, email must exist in card request.
221
      if (card.getUserEmail() == null) {
1✔
222
        throw new BadRequestException();
×
223
      }
224
      // If a user is found, update the card to have the correct userId associated.
225
      User user = userDAO.findUserByEmail(card.getUserEmail());
1✔
226
      if (user != null) {
1✔
227
        card.setUserId(user.getUserId());
×
228
      }
229
    } else {
1✔
230
      // check if userId exists
231
      User user = userDAO.findUserById(card.getUserId());
1✔
232
      if (user == null) {
1✔
233
        throw new BadRequestException();
1✔
234
      }
235
      if (card.getUserEmail() == null) {
1✔
236
        // if no email is provided in the card request, use the one from the user.
237
        card.setUserEmail(user.getEmail());
1✔
238
      } else if (!(user.getEmail().equalsIgnoreCase(card.getUserEmail()))) {
1✔
239
        // Emails do not match, throw an error.
240
        throw new ConsentConflictException();
1✔
241
      }
242
      card.setUserName(user.getDisplayName());
1✔
243
    }
244
  }
1✔
245

246
  private boolean checkIsAdmin(User user) {
247
    return user.getRoles()
1✔
248
        .stream()
1✔
249
        .anyMatch(role -> role.getName().equalsIgnoreCase(UserRoles.ADMIN.getRoleName()));
1✔
250
  }
251
}
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