Skip to content

Commit

Permalink
add assistant info to json request
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Huttner authored and Aaron Huttner committed Aug 23, 2016
1 parent aa49994 commit d26a94d
Show file tree
Hide file tree
Showing 20 changed files with 473 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.rockthevote.grommet.data.api.model.ApiContactMethod;
import com.rockthevote.grommet.data.api.model.ApiGeoLocation;
import com.rockthevote.grommet.data.api.model.ApiName;
import com.rockthevote.grommet.data.api.model.ApiRegistrationHelper;
import com.rockthevote.grommet.data.api.model.ApiRockyRequest;
import com.rockthevote.grommet.data.api.model.ApiRockyRequestWrapper;
import com.rockthevote.grommet.data.api.model.ApiSignature;
Expand Down Expand Up @@ -49,15 +50,20 @@
import rx.schedulers.Schedulers;
import timber.log.Timber;

import static com.rockthevote.grommet.data.db.model.Address.Type.ASSISTANT_ADDRESS;
import static com.rockthevote.grommet.data.db.model.Address.Type.MAILING_ADDRESS;
import static com.rockthevote.grommet.data.db.model.Address.Type.PREVIOUS_ADDRESS;
import static com.rockthevote.grommet.data.db.model.Address.Type.REGISTRATION_ADDRESS;
import static com.rockthevote.grommet.data.db.model.ContactMethod.Type.ASSISTANT_PHONE;
import static com.rockthevote.grommet.data.db.model.Name.Type.ASSISTANT_NAME;
import static com.rockthevote.grommet.data.db.model.Name.Type.CURRENT_NAME;
import static com.rockthevote.grommet.data.db.model.Name.Type.PREVIOUS_NAME;
import static com.rockthevote.grommet.data.db.model.RockyRequest.GENERATED_DATE;
import static com.rockthevote.grommet.data.db.model.RockyRequest.STATUS;
import static com.rockthevote.grommet.data.db.model.RockyRequest.Status.ABANDONED;
import static com.rockthevote.grommet.data.db.model.RockyRequest.Status.FORM_COMPLETE;
import static com.rockthevote.grommet.data.db.model.RockyRequest.Status.IN_PROGRESS;
import static com.rockthevote.grommet.data.db.model.RockyRequest.Status.REGISTER_FAILURE;
import static com.rockthevote.grommet.data.db.model.RockyRequest.Status.REGISTER_SERVER_FAILURE;
import static com.rockthevote.grommet.data.db.model.RockyRequest.Status.REGISTER_SUCCESS;
import static java.util.Map.Entry;

Expand Down Expand Up @@ -148,7 +154,7 @@ private void doWork(final RockyRequest rockyRequest) {
.subscribe(regResponse -> {
RockyRequest.Status status =
!regResponse.isError() && regResponse.response().isSuccessful()
? REGISTER_SUCCESS : REGISTER_FAILURE;
? REGISTER_SUCCESS : REGISTER_SERVER_FAILURE;

UploadNotification.notify(getApplicationContext(), status);

Expand All @@ -164,7 +170,7 @@ private void doWork(final RockyRequest rockyRequest) {
/**
* check for <p>
* {@link RockyRequest.Status#ABANDONED},<p>
* {@link RockyRequest.Status#REGISTER_FAILURE},<p>
* {@link RockyRequest.Status#REGISTER_SERVER_FAILURE},<p>
* {@link RockyRequest.Status#REGISTER_SUCCESS} <p>
* as well as {@link RockyRequest.Status#IN_PROGRESS} that are more than one hour old
* rows and delete them
Expand All @@ -174,7 +180,7 @@ private void cleanup() {
String completedRows = ""
+ STATUS + " IN ("
+ "'" + ABANDONED + "', "
+ "'" + REGISTER_FAILURE + "',"
+ "'" + REGISTER_SERVER_FAILURE + "',"
+ "'" + REGISTER_SUCCESS + "'"
+ ")";

Expand Down Expand Up @@ -255,7 +261,7 @@ public <K extends Enum<K>, Object> Observable<EnumMap<K, Object>> toEnumMap(
* @param additionalInfo
* @return {@link ApiRockyRequestWrapper} object
*/
@SuppressWarnings("Convert2streamapi")
@SuppressWarnings({"Convert2streamapi", "ArraysAsListWithZeroOrOneArgument"})
private ApiRockyRequestWrapper zipRockyRequest(RockyRequest rockyRequest,
EnumMap<Address.Type, Address> addresses,
EnumMap<ContactMethod.Type, ContactMethod> contactMethods,
Expand All @@ -265,47 +271,74 @@ private ApiRockyRequestWrapper zipRockyRequest(RockyRequest rockyRequest,
EnumMap<AdditionalInfo.Type, AdditionalInfo> additionalInfo) {


ApiAddress apiRegAddress = ApiAddress.fromDb(addresses.get(REGISTRATION_ADDRESS));
// Get address info objects
ApiAddress apiRegAddress = ApiAddress.fromAddress(addresses.get(REGISTRATION_ADDRESS));
ApiAddress apiMailAddress = rockyRequest.hasMailingAddress() ?
ApiAddress.fromDb(addresses.get(MAILING_ADDRESS)) : null;
ApiAddress.fromAddress(addresses.get(MAILING_ADDRESS)) : null;
ApiAddress apiPrevAddress = rockyRequest.hasPreviousAddress() ?
ApiAddress.fromDb(addresses.get(PREVIOUS_ADDRESS)) : null;
ApiAddress.fromAddress(addresses.get(PREVIOUS_ADDRESS)) : null;

ApiName apiName = ApiName.fromDb(names.get(Name.Type.CURRENT_NAME));
// Get name info objects
ApiName apiName = ApiName.fromName(names.get(CURRENT_NAME));
ApiName apiPrevName = rockyRequest.hasPreviousName() ?
ApiName.fromDb(names.get(Name.Type.PREVIOUS_NAME)) : null;

List<ApiContactMethod> apiContactMethods = new ArrayList<>(contactMethods.size());
ApiName.fromName(names.get(PREVIOUS_NAME)) : null;

// Get registrant contact info objects
List<ApiContactMethod> apiContactMethods = new ArrayList<>();
for (Entry<ContactMethod.Type, ContactMethod> entry : contactMethods.entrySet()) {
apiContactMethods.add(ApiContactMethod.fromDb(entry.getValue(), rockyRequest.phoneType()));
if (ASSISTANT_PHONE != entry.getKey()) {
apiContactMethods.add(ApiContactMethod.fromContactMethod(
entry.getValue(), rockyRequest.phoneType()));
}
}

// Get voter classification objects
List<ApiVoterClassification> apiClassifications = new ArrayList<>();
for (Entry<VoterClassification.Type, VoterClassification> entry : classifications.entrySet()) {
apiClassifications.add(ApiVoterClassification.fromDb(entry.getValue()));
apiClassifications.add(ApiVoterClassification.fromVoterClassification(entry.getValue()));
}

// Get voter ID objects
List<ApiVoterId> apiVoterIds = new ArrayList<>();
for (Entry<VoterId.Type, VoterId> entry : voterIds.entrySet()) {
apiVoterIds.add(ApiVoterId.fromDb(entry.getValue()));
apiVoterIds.add(ApiVoterId.fromVoterId(entry.getValue()));
}

// get additional info objects
List<ApiAdditionalInfo> apiAdditionalInfo = new ArrayList<>();
for (Entry<AdditionalInfo.Type, AdditionalInfo> entry : additionalInfo.entrySet()) {
apiAdditionalInfo.add(ApiAdditionalInfo.fromDb(entry.getValue()));
apiAdditionalInfo.add(ApiAdditionalInfo.fromAdditionalInfo(entry.getValue()));
}

ApiSignature apiSignature = ApiSignature.fromDb(rockyRequest);
ApiGeoLocation apiGeoLocation = ApiGeoLocation.fromDb(rockyRequest);

// build voter registration helper object
List<ApiContactMethod> helperContactMethods = new ArrayList<>();
for (Entry<ContactMethod.Type, ContactMethod> entry : contactMethods.entrySet()) {
if (ASSISTANT_PHONE == entry.getKey()) {
helperContactMethods.add(ApiContactMethod.fromContactMethod(
entry.getValue(), rockyRequest.phoneType()));
}
}

ApiRegistrationHelper helper = !rockyRequest.hasAssistant() ? null :
ApiRegistrationHelper.builder()
.address(ApiAddress.fromAddress(addresses.get(ASSISTANT_ADDRESS)))
.name(ApiName.fromName(names.get(ASSISTANT_NAME)))
.contactMethods(helperContactMethods)
.build();

// build voter registration api object
ApiVoterRegistration apiVoterRegistration = ApiVoterRegistration.fromDb(rockyRequest,
apiMailAddress, apiPrevAddress, apiRegAddress, apiName, apiPrevName, apiClassifications,
apiSignature, apiVoterIds, apiContactMethods, apiAdditionalInfo);
apiSignature, apiVoterIds, apiContactMethods, apiAdditionalInfo, helper);

// build voter records request api object
ApiVoterRecordsRequest apiVoterRecordsRequest = ApiVoterRecordsRequest.fromDb(rockyRequest,
apiVoterRegistration);

// build rocky request api object
ApiRockyRequest apiRockyRequest = ApiRockyRequest.fromDb(rockyRequest,
apiVoterRecordsRequest, apiGeoLocation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract static class Builder {
}

@Nullable
public static ApiAdditionalInfo fromDb(AdditionalInfo additionalInfo) {
public static ApiAdditionalInfo fromAdditionalInfo(AdditionalInfo additionalInfo) {
if (null == additionalInfo) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void toJson(JsonWriter writer, ApiAddress value) throws IOException {
}

@Nullable
public static ApiAddress fromDb(Address address) {
public static ApiAddress fromAddress(Address address) {
if(null == address){
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ abstract static class Builder{
}

@Nullable
public static ApiContactMethod fromDb(ContactMethod contactMethod, PhoneType phoneType){
public static ApiContactMethod fromContactMethod(ContactMethod contactMethod, PhoneType phoneType) {
if (null == contactMethod) {
return null;
}

List<String> capabilities = new ArrayList<>();
// right now we only support phone (no fax)
if(contactMethod.type() == ContactMethod.Type.PHONE){
if (contactMethod.type() == ContactMethod.Type.PHONE ||
contactMethod.type() == ContactMethod.Type.ASSISTANT_PHONE) {
capabilities.add(ContactMethod.Capability.VOICE.toString());
if(phoneType == PhoneType.MOBILE){
capabilities.add(ContactMethod.Capability.SMS.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ abstract static class Builder {
}

@Nullable
public static ApiName fromDb(Name name) {
public static ApiName fromName(Name name) {
if (null == name) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.rockthevote.grommet.data.api.model;

import com.google.auto.value.AutoValue;
import com.squareup.moshi.Json;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;

import java.util.List;

@AutoValue
public abstract class ApiRegistrationHelper {

@Json(name = "registration_helper_type")
public abstract String type();

public abstract ApiName name();

public abstract ApiAddress address();

@Json(name = "contact_methods")
abstract List<ApiContactMethod> contactMethods();


public static JsonAdapter<ApiRegistrationHelper> jsonAdapter(Moshi moshi) {
return new AutoValue_ApiRegistrationHelper.MoshiJsonAdapter(moshi);
}

public static Builder builder() {
return new AutoValue_ApiRegistrationHelper.Builder()
.type("assistant");
}

@AutoValue.Builder
public abstract static class Builder {

public abstract Builder type(String type);

public abstract Builder name(ApiName name);

public abstract Builder address(ApiAddress address);

public abstract Builder contactMethods(List<ApiContactMethod> contactMethods);

public abstract ApiRegistrationHelper build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ abstract static class Builder {
abstract ApiVoterClassification build();
}

public static ApiVoterClassification fromDb(VoterClassification classification) {
public static ApiVoterClassification fromVoterClassification(VoterClassification classification) {
return builder()
.type(classification.type().toString())
.assertion(classification.assertion())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract static class Builder {
abstract ApiVoterId build();
}

public static ApiVoterId fromDb(VoterId voterId) {
public static ApiVoterId fromVoterId(VoterId voterId) {
return builder()
.type(voterId.type().toString())
.stringValue(voterId.value())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
@AutoValue
public abstract class ApiVoterRegistration {

@Json(name = "registration_helper")
@Nullable
abstract ApiRegistrationHelper registrationHelper();

@Json(name = "date_of_birth")
abstract String dateOfBirth();

Expand Down Expand Up @@ -61,6 +65,7 @@ public abstract class ApiVoterRegistration {
@Json(name = "additional_info")
abstract List<ApiAdditionalInfo> additionalInfo();


public static JsonAdapter<ApiVoterRegistration> jsonAdapter(Moshi moshi) {
return new AutoValue_ApiVoterRegistration.MoshiJsonAdapter(moshi);
}
Expand All @@ -71,6 +76,8 @@ static Builder builder() {

@AutoValue.Builder
abstract static class Builder {
abstract Builder registrationHelper(ApiRegistrationHelper registrationHelper);

abstract Builder dateOfBirth(String value);

abstract Builder mailingAddress(ApiAddress value);
Expand Down Expand Up @@ -114,7 +121,8 @@ public static ApiVoterRegistration fromDb(RockyRequest rockyRequest,
ApiSignature signature,
List<ApiVoterId> voterIds,
List<ApiContactMethod> contactMethods,
List<ApiAdditionalInfo> additionalInfo) {
List<ApiAdditionalInfo> additionalInfo,
ApiRegistrationHelper registrationHelper) {
return builder()
.dateOfBirth(Dates.formatAsISO8601_ShortDate(rockyRequest.dateOfBirth()))
.mailingAddress(mailingAddress)
Expand All @@ -131,6 +139,7 @@ public static ApiVoterRegistration fromDb(RockyRequest rockyRequest,
.voterIds(voterIds)
.contactMethods(contactMethods)
.additionalInfo(additionalInfo)
.registrationHelper(registrationHelper)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public class DbOpenHelper extends SQLiteOpenHelper {
+ RockyRequest.LATITUDE + " REAL,"
+ RockyRequest.LONGITUDE + " REAL,"
+ RockyRequest.HAS_PREVIOUS_NAME + " INTEGER DEFAULT " + Db.BOOLEAN_FALSE + ","
+ RockyRequest.HAS_PREVIOUS_ADDRESS + " INTEGER DEFAULT " + Db.BOOLEAN_FALSE
+ RockyRequest.HAS_PREVIOUS_ADDRESS + " INTEGER DEFAULT " + Db.BOOLEAN_FALSE + ","
+ RockyRequest.HAS_ASSISTANT + " INTEGER DEFAULT " + Db.BOOLEAN_FALSE
+ ")";

private static final String CREATE_ADDRESS = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public abstract class RockyRequest implements Parcelable, BaseColumns {
public static final String LONGITUDE = "longitude";
public static final String HAS_PREVIOUS_NAME = "has_previous_name";
public static final String HAS_PREVIOUS_ADDRESS = "has_previous_address";
public static final String HAS_ASSISTANT = "has_assistant";

public static final String SELECT_BY_ID = ""
+ "SELECT * FROM "
Expand Down Expand Up @@ -107,6 +108,8 @@ public abstract class RockyRequest implements Parcelable, BaseColumns {

public abstract boolean hasPreviousAddress();

public abstract boolean hasAssistant();

public static final Func1<Cursor, RockyRequest> MAPPER = cursor -> {
long id = Db.getLong(cursor, _ID);
Status status = Status.fromString(Db.getString(cursor, STATUS));
Expand All @@ -131,12 +134,13 @@ public abstract class RockyRequest implements Parcelable, BaseColumns {
long longitude = Db.getLong(cursor, LONGITUDE);
boolean hasPreviousName = Db.getBoolean(cursor, HAS_PREVIOUS_NAME);
boolean hasPreviousAddress = Db.getBoolean(cursor, HAS_PREVIOUS_ADDRESS);
boolean hasAssistant = Db.getBoolean(cursor, HAS_ASSISTANT);

return new AutoValue_RockyRequest(id, status, language, phoneType, partnerId, optInEmail, optInSMS,
optInVolunteer, partnerOptInSMS, partnerOptInEmail,
sourceTrackingId, partnerTrackingId, openTrackingId,
generatedDate, dateOfBirth, hasMailingAddress, race, party, signature,
latitude, longitude, hasPreviousName, hasPreviousAddress);
latitude, longitude, hasPreviousName, hasPreviousAddress, hasAssistant);
};

public static final class Builder {
Expand Down Expand Up @@ -262,6 +266,11 @@ public Builder hasPreviousAddress(boolean hasPreviousAddress) {
return this;
}

public Builder hasAssistant(boolean hasAssistant) {
values.put(HAS_ASSISTANT, hasAssistant);
return this;
}

public ContentValues build() {
return values;
}
Expand Down Expand Up @@ -333,7 +342,8 @@ public enum Status {
ABANDONED("abandoned"),
FORM_COMPLETE("form_complete"),
REGISTER_SUCCESS("register_success"),
REGISTER_FAILURE("register_failure");
REGISTER_SERVER_FAILURE("register_server_failure"),
REGISTER_CLIENT_FAILURE("register_client_failure");

private final String status;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ int getNotifTitle(RockyRequest.Status status) {
case REGISTER_SUCCESS:
return R.string.upload_notification_title_success;
default:
case REGISTER_FAILURE:
case REGISTER_SERVER_FAILURE:
return R.string.upload_notification_title_failure;
}
}
Expand All @@ -82,7 +82,7 @@ private static int getNotificationId(RockyRequest.Status status) {
case REGISTER_SUCCESS:
return NOTIFICATION_SUCCESS_ID;
default:
case REGISTER_FAILURE:
case REGISTER_SERVER_FAILURE:
return NOTIFICATION_FAILURE_ID;
}
}
Expand All @@ -92,7 +92,7 @@ private static String getNotificationTag(RockyRequest.Status status) {
case REGISTER_SUCCESS:
return NOTIFICATION_SUCCESS_TAG;
default:
case REGISTER_FAILURE:
case REGISTER_SERVER_FAILURE:
return NOTIFICATION_FAILURE_TAG;
}
}
Expand Down
Loading

0 comments on commit d26a94d

Please sign in to comment.