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

DataBiosphere / consent / #5956

23 May 2025 03:02PM UTC coverage: 78.558% (-0.06%) from 78.617%
#5956

push

web-flow
[DT-1690] Remove update library card endpoint (#2537)

10046 of 12788 relevant lines covered (78.56%)

0.79 hits per line

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

90.65
/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 void deleteLibraryCardById(Integer id) {
56
    LibraryCard card = findLibraryCardById(id);
×
57
    throwIfNull(card);
×
58
    libraryCardDAO.deleteLibraryCardById(id);
×
59
  }
×
60

61
  public List<LibraryCard> findAllLibraryCards() {
62
    return libraryCardDAO.findAllLibraryCards();
×
63
  }
64

65
  public List<LibraryCard> findLibraryCardsByUserId(Integer userId) {
66
    return libraryCardDAO.findLibraryCardsByUserId(userId);
1✔
67
  }
68

69
  public List<LibraryCard> findLibraryCardsByInstitutionId(Integer institutionId) {
70
    return libraryCardDAO.findLibraryCardsByInstitutionId(institutionId);
×
71
  }
72

73
  public LibraryCard findLibraryCardById(Integer libraryCardId) {
74
    LibraryCard libraryCard = libraryCardDAO.findLibraryCardById(libraryCardId);
1✔
75
    throwIfNull(libraryCard);
1✔
76
    return libraryCard;
1✔
77
  }
78

79
  public LibraryCard findLibraryCardWithDaasById(Integer libraryCardId) {
80
    LibraryCard libraryCard = libraryCardDAO.findLibraryCardDaaById(libraryCardId);
1✔
81
    throwIfNull(libraryCard);
1✔
82
    return libraryCard;
1✔
83
  }
84

85
  public void addDaaToLibraryCard(Integer libraryCardId, Integer daaId) {
86
    libraryCardDAO.createLibraryCardDaaRelation(libraryCardId, daaId);
1✔
87
  }
1✔
88

89
  public void removeDaaFromLibraryCard(Integer libraryCardId, Integer daaId) {
90
    libraryCardDAO.deleteLibraryCardDaaRelation(libraryCardId, daaId);
1✔
91
  }
1✔
92

93
  public List<LibraryCard> addDaaToUserLibraryCardByInstitution(User user, User signingOfficial, Integer daaId) {
94
    if (signingOfficial.getInstitutionId() == null) {
1✔
95
      throw new BadRequestException("This signing official does not have an institution.");
1✔
96
    }
97
    List<LibraryCard> libraryCards = new ArrayList<>(libraryCardDAO.findLibraryCardsByUserId(user.getUserId()));
1✔
98
    if (libraryCards.isEmpty()) {
1✔
99
      LibraryCard lc = createLibraryCardForSigningOfficial(user, signingOfficial);
1✔
100
      libraryCards.add(lc);
1✔
101
    }
102
    // typically there should be one library card per user per institution
103
    for (LibraryCard libraryCard : libraryCards) {
1✔
104
      addDaaToLibraryCard(libraryCard.getId(), daaId);
1✔
105
    }
1✔
106
    return libraryCardDAO.findLibraryCardsByUserId(user.getUserId());
1✔
107
  }
108

109
  public List<LibraryCard> removeDaaFromUserLibraryCards(User user, Integer daaId) {
110
    List<LibraryCard> libraryCards = findLibraryCardsByUserId(user.getUserId());
1✔
111
    // typically there should be one library card per user
112
    for (LibraryCard libraryCard : libraryCards) {
1✔
113
      removeDaaFromLibraryCard(libraryCard.getId(), daaId);
1✔
114
    }
1✔
115
    return libraryCards;
1✔
116
  }
117

118
  public LibraryCard createLibraryCardForSigningOfficial(User user, User signingOfficial) {
119
    LibraryCard lc = new LibraryCard();
1✔
120
    lc.setUserId(user.getUserId());
1✔
121
    lc.setUserName(user.getDisplayName());
1✔
122
    lc.setUserEmail(user.getEmail());
1✔
123
    lc.setCreateUserId(signingOfficial.getUserId());
1✔
124
    LibraryCard createdLc = createLibraryCard(lc, user);
1✔
125
    return createdLc;
1✔
126
  }
127

128
  private void checkForValidInstitution(Integer institutionId, String userEmail) {
129
    checkInstitutionId(institutionId);
1✔
130
    Institution institution = institutionDAO.findInstitutionById(institutionId);
1✔
131

132
    if (Objects.isNull(institution)) {
1✔
133
      throw new IllegalArgumentException("Invalid Institution Id");
×
134
    }
135

136
    var userInstitution = institutionService.findInstitutionForEmail(userEmail);
1✔
137
    if (userInstitution == null || !userInstitution.getId().equals(institutionId)) {
1✔
138
      throw new BadRequestException(
1✔
139
          "User email %s does not match institution %s".formatted(userEmail, institution.getName()));
1✔
140
    }
141
  }
1✔
142

143
  private void checkInstitutionId(Integer institutionId) {
144
    if (Objects.isNull(institutionId)) {
1✔
145
      throw new IllegalArgumentException("Institution ID is a required parameter");
×
146
    }
147
  }
1✔
148

149
  private void throwIfNull(LibraryCard libraryCard) {
150
    if (Objects.isNull(libraryCard)) {
1✔
151
      throw new NotFoundException("LibraryCard not found.");
1✔
152
    }
153
  }
1✔
154

155
  //helper method for create method, checks to see if card already exists
156
  private void checkIfCardExists(LibraryCard payload) {
157
    Integer userId = payload.getUserId();
1✔
158
    String email = payload.getUserEmail();
1✔
159
    Optional<LibraryCard> foundCard;
160
    List<LibraryCard> results;
161

162
    if (Objects.nonNull(payload.getUserId())) {
1✔
163
      results = libraryCardDAO.findLibraryCardsByUserId(userId);
1✔
164
    } else if (Objects.nonNull(email)) {
1✔
165
      results = libraryCardDAO.findAllLibraryCardsByUserEmail(email);
1✔
166
    } else {
167
      throw new BadRequestException();
1✔
168
    }
169
    foundCard = results.stream().filter(card -> {
1✔
170
      Boolean sameUserId = Objects.nonNull(userId) && card.getUserId().equals(userId);
1✔
171
      Boolean sameUserEmail = Objects.nonNull(email) && card.getUserEmail().equalsIgnoreCase(email);
1✔
172
      return (sameUserId || sameUserEmail);
1✔
173
    }).findFirst();
1✔
174

175
    if (foundCard.isPresent()) {
1✔
176
      throw new ConsentConflictException();
1✔
177
    }
178
  }
1✔
179

180
  // Helper method to process user data on create LC payload.
181
  // Needed since CREATE has a unique situation where admins can create LCs without an active
182
  // user (save with userEmail instead).
183
  private void processUserOnNewLC(LibraryCard card) {
184
    if (card.getUserId() == null) {
1✔
185
      // No user ID is provided, email must exist in card request.
186
      if (card.getUserEmail() == null) {
1✔
187
        throw new BadRequestException();
×
188
      }
189
      // If a user is found, update the card to have the correct userId associated.
190
      User user = userDAO.findUserByEmail(card.getUserEmail());
1✔
191
      if (user != null) {
1✔
192
        card.setUserId(user.getUserId());
×
193
      }
194
    } else {
1✔
195
      // check if userId exists
196
      User user = userDAO.findUserById(card.getUserId());
1✔
197
      if (user == null) {
1✔
198
        throw new BadRequestException();
1✔
199
      }
200
      if (card.getUserEmail() == null) {
1✔
201
        // if no email is provided in the card request, use the one from the user.
202
        card.setUserEmail(user.getEmail());
1✔
203
      } else if (!(user.getEmail().equalsIgnoreCase(card.getUserEmail()))) {
1✔
204
        // Emails do not match, throw an error.
205
        throw new ConsentConflictException();
1✔
206
      }
207
      card.setUserName(user.getDisplayName());
1✔
208
    }
209
  }
1✔
210

211
  private boolean checkIsAdmin(User user) {
212
    return user.getRoles()
1✔
213
        .stream()
1✔
214
        .anyMatch(role -> role.getName().equalsIgnoreCase(UserRoles.ADMIN.getRoleName()));
1✔
215
  }
216
}
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