Skip to content

Commit

Permalink
avniproject/avni-webapp#1214 | Add validations for Language and DateP…
Browse files Browse the repository at this point in the history
…ickerMode during UserAndCatchment file upload
  • Loading branch information
himeshr committed Aug 16, 2024
1 parent 3ac60d2 commit cfa7ca3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public JsonObject with(String key, Object value) {
return this;
}

public JsonObject withEmptyCheck(String key, String value){
public JsonObject withEmptyCheckAndTrim(String key, String value){
if(!S.isEmpty(value)){
super.put(key, value);
super.put(key, value.trim());
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class UserAndCatchmentWriter implements ItemWriter<Row>, Serializable {
private final Pattern compoundHeaderPattern;
private final ResetSyncService resetSyncService;
private static final String METADATA_ROW_START_STRING = "Mandatory field.";
private static final List<String> DATE_PICKER_MODE_OPTIONS = Arrays.asList("calendar", "spinner");

@Autowired
public UserAndCatchmentWriter(CatchmentService catchmentService,
Expand Down Expand Up @@ -79,6 +80,14 @@ private void write(Row row) throws Exception {
String groupsSpecified = row.get("User Groups");
JsonObject syncSettings = constructSyncSettings(row);

if(Objects.isNull(locale)) {
throw new Exception(format("Provided value '%s' for Preferred Language is invalid;", language));
}

if(Objects.isNull(datePickerMode) || !DATE_PICKER_MODE_OPTIONS.contains(datePickerMode)) {
throw new Exception(format("Provided value '%s' for Date picker mode is invalid;", datePickerMode));
}

AddressLevel location = locationRepository.findByTitleLineageIgnoreCase(fullAddress)
.orElseThrow(() -> new Exception(format(
"Provided Location does not exist in Avni. Please add it or check for spelling mistakes '%s'", fullAddress)));
Expand Down Expand Up @@ -109,9 +118,9 @@ private void write(Row row) throws Exception {
user.setSettings(new JsonObject()
.with("locale", locale)
.with("trackLocation", trackLocation)
.withEmptyCheck("datePickerMode", datePickerMode)
.withEmptyCheckAndTrim("datePickerMode", datePickerMode)
.with("showBeneficiaryMode", beneficiaryMode)
.withEmptyCheck(UserSettings.ID_PREFIX, idPrefix));
.withEmptyCheckAndTrim(UserSettings.ID_PREFIX, idPrefix));

user.setOrganisationId(organisation.getId());
user.setAuditInfo(currentUser);
Expand Down

0 comments on commit cfa7ca3

Please sign in to comment.