Skip to content

Commit

Permalink
create policy with insuree chfid number
Browse files Browse the repository at this point in the history
  • Loading branch information
jobil committed Oct 26, 2023
1 parent 5220bd6 commit c8f5184
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@
import com.squareup.picasso.Target;

import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.openimis.imispolicies.domain.entity.Family;
import org.openimis.imispolicies.domain.entity.FeedbackRequest;
import org.openimis.imispolicies.domain.entity.Insuree;
import org.openimis.imispolicies.domain.entity.PendingFeedback;
import org.openimis.imispolicies.network.exception.HttpException;
import org.openimis.imispolicies.network.exception.UserNotAuthenticatedException;
Expand All @@ -79,6 +81,7 @@
import org.openimis.imispolicies.usecase.CreatePolicy;
import org.openimis.imispolicies.usecase.DeletePolicyRenewal;
import org.openimis.imispolicies.usecase.FetchFamily;
import org.openimis.imispolicies.usecase.FetchInsureeInquire;
import org.openimis.imispolicies.usecase.FetchMasterData;
import org.openimis.imispolicies.usecase.Login;
import org.openimis.imispolicies.usecase.PostFeedback;
Expand Down Expand Up @@ -3094,6 +3097,9 @@ private int Enrol(int CallerId) throws UserException, JSONException, IOException
case -6:
ErrMsg = "[" + CHFNumber + "] " + activity.getString(R.string.Interuption);
break;
case -7:
ErrMsg = "[" + CHFNumber + "] " + activity.getString(R.string.RecordNotFound);
break;
case -400:
ErrMsg = "[" + CHFNumber + "] " + activity.getString(R.string.ServerError);
break;
Expand Down Expand Up @@ -3127,34 +3133,53 @@ private int uploadEnrols(
@NonNull Pair<String, byte[]>[] insureeImages
) throws JSONException {
JSONObject familyObj = familyArray.getJSONObject(0);

Family family = familyFromJSONObject(familyObj, insureesArray, insureeImages);
JSONObject insureeObj = insureesArray.getJSONObject(0);

//search family in webserver by head insuree CHFID
int existingFamilyId = 0;
try {
new UpdateFamily().execute(family);
existingFamilyId = new FetchFamily().fetchFamilyId(insureeObj.getString("CHFID"));
} catch (HttpException e) {
if (e.getCode() != HttpURLConnection.HTTP_NOT_FOUND) {
throw e;
}
} catch (Exception e) {
enrolMessages.add(e.getMessage());
return -400;
e.printStackTrace();
}

for (int j = 0; j < policiesArray.length(); j++) {
JSONArray policyPremiums = new JSONArray();
String policyId = policiesArray.getJSONObject(j).getString("PolicyId");
for (int k = 0; k < premiumsArray.length(); k++) {
JSONObject premiumObject = premiumsArray.getJSONObject(k);
if (StringUtils.equals(policyId, premiumObject.getString("PolicyId"))) {
policyPremiums.put(premiumObject);
if (existingFamilyId == 0){
//insuree don't exist
return -7;
}else{
for (int j = 0; j < policiesArray.length(); j++) {
JSONArray policyPremiums = new JSONArray();
String policyId = policiesArray.getJSONObject(j).getString("PolicyId");
for (int k = 0; k < premiumsArray.length(); k++) {
JSONObject premiumObject = premiumsArray.getJSONObject(k);
if (StringUtils.equals(policyId, premiumObject.getString("PolicyId"))) {
policyPremiums.put(premiumObject);
}
}
policiesArray.getJSONObject(j).put("premium", policyPremiums);
}

List<Family.Policy> policies = familyPolicyFromJSONObject(family.getUuid(), existingFamilyId, policiesArray);
try {
new CreatePolicy().execute(policies);
} catch (Exception e) {
enrolMessages.add(e.getMessage());
return -400;
}
policiesArray.getJSONObject(j).put("premium", policyPremiums);
}

List<Family.Policy> policies = familyPolicyFromJSONObject(family.getUuid(), policiesArray);
/*Family family = familyFromJSONObject(familyObj, insureesArray, insureeImages);
try {
new CreatePolicy().execute(policies);
new UpdateFamily().execute(family);
} catch (Exception e) {
enrolMessages.add(e.getMessage());
return -400;
}
}*/

return 0;
}
Expand Down Expand Up @@ -3226,6 +3251,7 @@ private Family.Member familyMemberFromJSONObject(
@NonNull
private List<Family.Policy> familyPolicyFromJSONObject(
@NonNull String familyUUID,
@NotNull int familyId,
@NonNull JSONArray array
) throws JSONException {
List<Family.Policy> policies = new ArrayList<>();
Expand All @@ -3235,7 +3261,7 @@ private List<Family.Policy> familyPolicyFromJSONObject(
policies.add(new Family.Policy(
/* id = */ Integer.parseInt(object.getString("PolicyId")),
/* uuid = */ policyUuid,
/* familyId = */ Integer.parseInt(object.getString("FamilyId")),
/* familyId = */ familyId,
/* familyUuid = */ familyUUID,
/* enrollDate = */ Objects.requireNonNull(JsonUtils.getDateOrDefault(object, "EnrollDate")),
/* startDate = */ Objects.requireNonNull(JsonUtils.getDateOrDefault(object, "StartDate")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public FetchFamily(@NonNull GetFamilyGraphQLRequest getFamilyGraphQLRequest) {
this.getFamilyGraphQLRequest = getFamilyGraphQLRequest;
}

@WorkerThread
@NonNull
public int fetchFamilyId(@NonNull String headChfid) throws Exception{
GetFamilyQuery.Node node = getFamilyGraphQLRequest.get(headChfid);
return IdUtils.getIdFromGraphQLString(node.id());
}

@WorkerThread
@NonNull
public Family execute(@NonNull String headChfId) throws Exception {
Expand Down

0 comments on commit c8f5184

Please sign in to comment.