Skip to content

Commit

Permalink
Merge branch 'avniproject:master' into autoquess
Browse files Browse the repository at this point in the history
  • Loading branch information
ombhardwajj authored Sep 4, 2024
2 parents 4d8697c + b65c4e7 commit dac5397
Show file tree
Hide file tree
Showing 261 changed files with 5,639 additions and 1,447 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,17 @@ debug_server_remote_db: build_server

build_server: ## Builds the jar file
./gradlew clean build -x test
build-server: build_server

boot_run:
OPENCHS_DATABASE=$(DB) ./gradlew bootRun

test_server: rebuild_testdb ## Run tests
GRADLE_OPTS="-Xmx256m" ./gradlew clean test
test-server: test_server

test_server_without_clean_rebuild: ## Run tests
GRADLE_OPTS="-Xmx256m" ./gradlew test

test_server_with_remote_db:
make rebuild_testdb su=$(DBUSER) dbServer=$(DBSERVER)
Expand Down
2 changes: 2 additions & 0 deletions avni-server-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ dependencies {
compile 'org.owasp.encoder:encoder:1.2.3'
implementation 'org.apache.commons:commons-csv:1.10.0'
compile 'com.googlecode.libphonenumber:libphonenumber:8.12.32'
testImplementation 'org.slf4j:slf4j-reload4j:2.0.6'
compile 'com.google.guava:guava:33.2.1-jre'
}

bootRun {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ public ResponseEntity<MessageRuleContract> findOne(@PathVariable("id") Long id )
}

private boolean isAString(String s) {
return s != null && !s.isEmpty();
return !StringUtils.isEmpty(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.avni.messaging.external.GlificRestClient;
import org.avni.messaging.service.PhoneNumberNotAvailableOrIncorrectException;
import org.avni.server.util.PhoneNumberUtil;
import org.avni.server.util.RegionUtil;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.ParameterizedTypeReference;
Expand Down Expand Up @@ -62,7 +63,7 @@ public GlificContactRepository(GlificRestClient glificRestClient) {
}

public String getOrCreateContact(String phoneNumber, String fullName) throws PhoneNumberNotAvailableOrIncorrectException, GlificNotConfiguredException {
if (!PhoneNumberUtil.isValidPhoneNumber(phoneNumber)) {
if (!PhoneNumberUtil.isValidPhoneNumber(phoneNumber, RegionUtil.getCurrentUserRegion())) {
throw new PhoneNumberNotAvailableOrIncorrectException();
}

Expand All @@ -73,15 +74,15 @@ public String getOrCreateContact(String phoneNumber, String fullName) throws Pho
}

private String createContact(String phoneNumber, String fullName) throws GlificNotConfiguredException {
String message = OPTIN_CONTACT_JSON.replace(PHONE_NUMBER, PhoneNumberUtil.getPhoneNumberInGlificFormat(phoneNumber))
String message = OPTIN_CONTACT_JSON.replace(PHONE_NUMBER, PhoneNumberUtil.getPhoneNumberInGlificFormat(phoneNumber, RegionUtil.getCurrentUserRegion()))
.replace(FULL_NAME, fullName);
GlificOptinContactResponse glificOptinContactResponse = glificRestClient.callAPI(message, new ParameterizedTypeReference<GlificResponse<GlificOptinContactResponse>>() {
});
return glificOptinContactResponse.getOptinContact().getContact().getId();
}

private GlificGetContactsResponse getContact(String phoneNumber) throws GlificNotConfiguredException {
String message = GET_CONTACT_JSON.replace(PHONE_NUMBER, PhoneNumberUtil.getPhoneNumberInGlificFormat(phoneNumber));
String message = GET_CONTACT_JSON.replace(PHONE_NUMBER, PhoneNumberUtil.getPhoneNumberInGlificFormat(phoneNumber, RegionUtil.getCurrentUserRegion()));
return glificRestClient.callAPI(message, new ParameterizedTypeReference<GlificResponse<GlificGetContactsResponse>>() {
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.avni.server.service.UserService;
import org.avni.server.util.A;
import org.avni.server.util.PhoneNumberUtil;
import org.avni.server.util.RegionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -144,7 +145,7 @@ private void logAndNotifyError(GlificContactGroupContactsResponse.GlificContactG
}

private Optional<String> findNameOfTheContact(GlificContactGroupContactsResponse.GlificContactGroupContacts contactGroupContact) {
PhoneNumberUtil.getStandardFormatPhoneNumber(contactGroupContact.getPhone());
PhoneNumberUtil.getStandardFormatPhoneNumber(contactGroupContact.getPhone(), RegionUtil.getCurrentUserRegion());
Optional<String> name = Stream.<Supplier<Optional<String>>>of(
() -> userService.findByPhoneNumber(contactGroupContact.getPhone()).map(User::getName),
() -> individualService.findByPhoneNumber(contactGroupContact.getPhone()).map(Individual::getFirstName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@

@Service
public class MessageTemplateService {

private GlificMessageTemplateRepository messageTemplateRepository;
private OrganisationConfigService organisationConfigService;
private final GlificMessageTemplateRepository messageTemplateRepository;
private final OrganisationConfigService organisationConfigService;

@Autowired
public MessageTemplateService(GlificMessageTemplateRepository messageTemplateRepository, OrganisationConfigService organisationConfigService) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.avni.messaging.service;

import com.bugsnag.Bugsnag;
import org.avni.messaging.contract.MessageRuleContract;
import org.avni.messaging.domain.*;
import org.avni.messaging.domain.exception.GlificGroupMessageFailureException;
import org.avni.messaging.domain.exception.GlificNotConfiguredException;
Expand All @@ -10,6 +11,7 @@
import org.avni.server.domain.RuleExecutionException;
import org.avni.server.domain.User;
import org.avni.server.framework.security.UserContextHolder;
import org.avni.server.service.EntityTypeRetrieverService;
import org.avni.server.service.RuleService;
import org.avni.server.web.request.rules.response.ScheduleRuleResponseEntity;
import org.joda.time.DateTime;
Expand All @@ -34,20 +36,21 @@ public class MessagingService {
private final MessageReceiverService messageReceiverService;
private final MessageRequestService messageRequestService;
private final RuleService ruleService;
private MessageRequestQueueRepository messageRequestQueueRepository;
private ManualMessageRepository manualMessageRepository;
private GroupMessagingService groupMessagingService;
private Bugsnag bugsnag;
private final MessageRequestQueueRepository messageRequestQueueRepository;
private final ManualMessageRepository manualMessageRepository;
private final GroupMessagingService groupMessagingService;
private final Bugsnag bugsnag;

private IndividualMessagingService individualMessagingService;
private final IndividualMessagingService individualMessagingService;
private final EntityTypeRetrieverService entityTypeRetrieverService;

@Autowired
public MessagingService(MessageRuleRepository messageRuleRepository, MessageReceiverService messageReceiverService,
MessageRequestService messageRequestService,
MessageRequestQueueRepository messageRequestQueueRepository,
ManualMessageRepository manualMessageRepository,
RuleService ruleService, GroupMessagingService groupMessagingService,
IndividualMessagingService individualMessagingService, Bugsnag bugsnag) {
IndividualMessagingService individualMessagingService, Bugsnag bugsnag, EntityTypeRetrieverService entityTypeRetrieverService) {
this.messageRuleRepository = messageRuleRepository;
this.messageReceiverService = messageReceiverService;
this.messageRequestService = messageRequestService;
Expand All @@ -57,6 +60,7 @@ public MessagingService(MessageRuleRepository messageRuleRepository, MessageRece
this.groupMessagingService = groupMessagingService;
this.bugsnag = bugsnag;
this.individualMessagingService = individualMessagingService;
this.entityTypeRetrieverService = entityTypeRetrieverService;
}

public MessageRule find(Long id) {
Expand Down Expand Up @@ -182,4 +186,12 @@ private void sendManualMessage(MessageRequest messageRequest) throws PhoneNumber
else
individualMessagingService.sendManualMessage(messageRequest);
}

public void saveRules(MessageRuleContract[] messageRuleContracts) {
for (MessageRuleContract messageRuleContract : messageRuleContracts) {
MessageRule messageRule = this.find(messageRuleContract.getUuid());
messageRule = MessageRuleContract.toModel(messageRuleContract, messageRule, entityTypeRetrieverService);
this.saveRule(messageRule);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
import org.avni.server.web.validation.ValidationException;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
Expand Down Expand Up @@ -182,7 +183,7 @@ public boolean isVoided() {

public List<ValidationResult> validate() {
ArrayList<ValidationResult> validationResults = new ArrayList<>();
if (this.getType() != null && !this.getType().trim().isEmpty()) {
if (!StringUtils.isEmpty(this.getType())) {
try {
FormElementType.valueOf(this.getType());
} catch (IllegalArgumentException | NullPointerException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ public class FormElementGroup extends OrganisationAwareEntity {
@NotNull
private String name;

@Column
private String display;

@NotNull
private Double displayOrder;

Expand Down Expand Up @@ -99,12 +96,9 @@ public static FormElementGroup create() {
}

public String getDisplay() {
return display;
return getName();
}

public void setDisplay(String display) {
this.display = display;
}

FormElement findFormElementByConcept(String conceptName) {
return formElements.stream().filter(x -> x.getConcept().getName().equals(conceptName)).findAny().orElse(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
package org.avni.server.builder;

public class BuilderException extends RuntimeException {
private final String bundleSpecificMessage;

public BuilderException(String message) {
super(message);
bundleSpecificMessage = null;
}

public BuilderException(String message, String bundleMessage) {
super(message);
this.bundleSpecificMessage = bundleMessage;
}

public String getUserMessage() {
return super.getMessage();
}

@Override
public String getMessage() {
if (bundleSpecificMessage == null) {
return super.getMessage();
}
return String.format("%s (%s)", super.getMessage(), bundleSpecificMessage);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.avni.server.builder;

import org.avni.server.application.Form;

import java.util.Map;

import org.avni.server.dao.application.FormRepository;
import org.avni.server.domain.ChecklistDetail;
import org.avni.server.domain.ChecklistItemDetail;
Expand All @@ -14,6 +11,7 @@

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChecklistDetailBuilder extends BaseBuilder<ChecklistDetail, ChecklistDetailBuilder> {

Expand Down Expand Up @@ -51,14 +49,15 @@ public ChecklistDetailBuilder withItems(List<ChecklistItemDetailRequest> items)
.withVoided(item.isVoided())
.withform(form)
.withConcept(concept)
.withLeadItem(builtItems.get(item.getDependentOn()))
.withScheduleOnExpiryOfDependency(item.getScheduleOnExpiryOfDependency())
.withMinDaysFromStartDate(item.getMinDaysFromStartDate())
.withMinDaysFromDependent(item.getMinDaysFromDependent())
.withExpiresAfter(item.getExpiresAfter())
.build();
builtItems.put(builtItemDetail.getUuid(), builtItemDetail);
});
//set dependentOn after all items in request have been processed so order of items in request does not matter for dependents
items.forEach(item -> getExistingChecklistItemDetail(this.get(), item).setLeadChecklistItemDetail(builtItems.get(item.getDependentOn())));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public FormBuilder withFormElementGroups(List<FormElementGroupContract> formElem
.withBackgroundColour(formElementGroupContract.getBackgroundColour())
.withStartTime(formElementGroupContract.getStartTime())
.withStayTime(formElementGroupContract.getStayTime())
.withDisplay(formElementGroupContract.getDisplay())
.withDisplayOrder(formElementGroupContract.getDisplayOrder())
.withRule(formElementGroupContract.getRule())
.withDeclarativeRule(formElementGroupContract.getDeclarativeRule())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import org.avni.server.domain.Concept;
import org.avni.server.domain.DeclarativeRule;
import org.avni.server.domain.Documentation;
import org.springframework.util.StringUtils;

import static java.lang.String.format;

public class FormElementBuilder extends BaseBuilder<FormElement, FormElementBuilder> {
private final FormElementGroup formElementGroup;
Expand All @@ -17,6 +20,9 @@ public FormElementBuilder(FormElementGroup formElementGroup, FormElement existin
}

public FormElementBuilder withName(String name) {
if (StringUtils.hasLength(name) && name.length() > 255) {
throw new BuilderException(format("FormElement name \"%s\" exceeds allowed length of 255 characters", name));
}
this.set("Name", name, String.class);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import org.avni.server.service.DocumentationService;
import org.avni.server.web.request.application.FormElementContract;
import org.avni.server.web.request.application.FormElementGroupContract;
import org.springframework.util.StringUtils;

import java.util.List;

import static java.lang.String.format;

public class FormElementGroupBuilder extends BaseBuilder<FormElementGroup, FormElementGroupBuilder> {
private final ConceptService conceptService;
private final DocumentationService documentationService;
Expand All @@ -31,6 +34,9 @@ public FormElementGroupBuilder(Form form, FormElementGroup existingFormElementGr
}

public FormElementGroupBuilder withName(String name) {
if (StringUtils.hasLength(name) && name.length() > 255) {
throw new BuilderException(format("FormElementGroup name \"%s\" exceeds allowed length of 255 characters", name));
}
this.set("Name", name, String.class);
return this;
}
Expand All @@ -45,11 +51,6 @@ public FormElementGroupBuilder withIsVoided(boolean isVoided) {
return this;
}

public FormElementGroupBuilder withDisplay(String display) {
this.set("Display", display, String.class);
return this;
}

public FormElementGroupBuilder withRule(String rule) {
this.get().setRule(rule);
return this;
Expand Down Expand Up @@ -95,7 +96,7 @@ private Concept getExistingConcept(String uuid, FormElement formElement) throws
Concept concept = formElement.getConcept() != null && formElement.getConcept().getUuid().equals(uuid) ?
formElement.getConcept() : conceptService.get(uuid);
if (concept == null) {
throw new FormBuilderException(String.format("Concept with uuid '%s' not found", uuid));
throw new FormBuilderException(format("Concept with uuid '%s' not found", uuid));
}
return concept;
}
Expand Down Expand Up @@ -142,7 +143,7 @@ private FormElement getQuestionGroup(FormElementContract formElementContract) th
if (formElementContract.getParentFormElementUuid() != null) {
group = getExistingFormElement(formElementContract.getParentFormElementUuid());
if (group == null) {
throw new FormBuilderException(String.format("Parent form element with uuid '%s' not found", formElementContract.getParentFormElementUuid()));
throw new FormBuilderException(format("Parent form element with uuid '%s' not found", formElementContract.getParentFormElementUuid()));
}
}
return group;
Expand All @@ -164,7 +165,7 @@ public FormElementGroupBuilder withoutFormElements(Organisation organisation, Li
private FormElement getFormElement(FormElementContract formElementContract) throws FormBuilderException {
FormElement formElement = get().findFormElement(formElementContract.getUuid());
if (formElement == null) {
throw new FormBuilderException(String.format("FormElement with uuid '%s' not found", formElementContract.getUuid()));
throw new FormBuilderException(format("FormElement with uuid '%s' not found", formElementContract.getUuid()));
}
return formElement;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@
import java.util.UUID;

public class LocationBuilder extends BaseBuilder<AddressLevel, LocationBuilder> {

private final AddressLevelType type;
private LocationRepository locationRepository;
private final LocationRepository locationRepository;

public LocationBuilder(AddressLevel existingEntity, AddressLevelType type) {
super(existingEntity, new AddressLevel());
this.type = type;
locationRepository = ApplicationContextProvider.getContext().getBean(LocationRepository.class);
}

public LocationBuilder copy(LocationContract locationRequest) throws BuilderException {
public LocationBuilder copy(LocationContract locationRequest) {
get().setUuid(locationRequest.getUuid() == null ? UUID.randomUUID().toString() : locationRequest.getUuid());
get().setTitle(locationRequest.getName());
get().setType(type);
get().setLegacyId(locationRequest.getLegacyId());
get().setVoided(locationRequest.isVoided());
get().setGpsCoordinates(locationRequest.getGpsCoordinates());
get().setLocationProperties(locationRequest.getLocationProperties());
withParentLocation(locationRequest);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.avni.server.common;

public class BulkItemSaveException extends RuntimeException {
public BulkItemSaveException(Object contract, Exception exception) {
super(String.format("%s. %s.", exception.getMessage(), contract.toString()), exception);
}
}
Loading

0 comments on commit dac5397

Please sign in to comment.