Skip to content

Commit f4b168c

Browse files
Merge pull request #212 from CodeForPhilly/import_library_benefits
2 parents c6f9010 + cc5f8e2 commit f4b168c

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.acme.controller;
2+
3+
import jakarta.inject.Inject;
4+
import jakarta.ws.rs.*;
5+
import jakarta.ws.rs.core.MediaType;
6+
import org.acme.model.domain.EligibilityCheck;
7+
import org.acme.service.LibraryApiMetadataService;
8+
9+
import java.util.List;
10+
11+
@Path("/api")
12+
@Produces(MediaType.APPLICATION_JSON)
13+
public class LibraryCheckResource {
14+
15+
@Inject
16+
LibraryApiMetadataService libraryApiMetadataService; // Inject the singleton bean
17+
18+
@GET
19+
@Path("/library-checks")
20+
public List<EligibilityCheck> getLibraryChecks(@QueryParam("module") String module) {
21+
if (module != null) {
22+
return libraryApiMetadataService.getByModule(module);
23+
}
24+
return libraryApiMetadataService.getAll();
25+
}
26+
}
27+

builder-api/src/main/java/org/acme/model/domain/EligibilityCheck.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.fasterxml.jackson.databind.JsonNode;
56

67
import java.util.List;
78

@@ -15,6 +16,7 @@ public class EligibilityCheck {
1516
private boolean isActive;
1617
private String dmnModel;
1718
private List<InputDefinition> inputs;
19+
private JsonNode situation;
1820
private List<ParameterDefinition> parameters;
1921
private String ownerId;
2022
@JsonProperty("isPublic")
@@ -107,4 +109,12 @@ public Boolean getPublic() {
107109
public void setPublic(Boolean aPublic) {
108110
isPublic = aPublic;
109111
}
112+
113+
public JsonNode getSituation() {
114+
return situation;
115+
}
116+
117+
public void setSituation(JsonNode situation) {
118+
this.situation = situation;
119+
}
110120
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.acme.service;
2+
3+
import com.fasterxml.jackson.core.type.TypeReference;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import io.quarkus.logging.Log;
6+
import jakarta.annotation.PostConstruct;
7+
import jakarta.enterprise.context.ApplicationScoped;
8+
import jakarta.inject.Inject;
9+
import org.acme.model.domain.EligibilityCheck;
10+
import org.acme.persistence.StorageService;
11+
import org.acme.persistence.FirestoreUtils;
12+
13+
import java.util.List;
14+
import java.util.Map;
15+
import java.util.Optional;
16+
17+
18+
@ApplicationScoped
19+
public class LibraryApiMetadataService {
20+
@Inject
21+
private StorageService storageService;
22+
23+
private List<EligibilityCheck> checks;
24+
25+
@PostConstruct
26+
void init() {
27+
try {
28+
29+
// Get path of most recent library schema json document
30+
Optional<Map<String, Object>> configOpt = FirestoreUtils.getFirestoreDocById("system", "config");
31+
if (configOpt.isEmpty()){
32+
Log.error("Failed to load library api config");
33+
return;
34+
}
35+
Map<String, Object> config = configOpt.get();
36+
String schemaPath = config.get("latestJsonStoragePath").toString();
37+
Optional<String> apiSchemaOpt = storageService.getStringFromStorage(schemaPath);
38+
if (apiSchemaOpt.isEmpty()){
39+
Log.error("Failed to load library api schema document");
40+
return;
41+
}
42+
String apiSchemaJson = apiSchemaOpt.get();
43+
44+
ObjectMapper mapper = new ObjectMapper();
45+
46+
checks = mapper.readValue(apiSchemaJson, new TypeReference<List<EligibilityCheck>>() {});
47+
} catch (Exception e) {
48+
throw new RuntimeException("Failed to load library api metadata", e);
49+
}
50+
}
51+
52+
public List<EligibilityCheck> getAll() {
53+
return checks;
54+
}
55+
56+
public List<EligibilityCheck> getByModule(String module) {
57+
return checks.stream()
58+
.filter(e -> module.equals(e.getModule()))
59+
.toList();
60+
}
61+
}
62+

scripts/import-documents.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import firebase_admin
2+
from firebase_admin import credentials, storage, firestore
3+
import json
4+
from datetime import datetime
5+
6+
7+
# -----------------------------------
8+
# INIT FIREBASE
9+
# -----------------------------------
10+
11+
# Add path to service account credentials
12+
cred = credentials.Certificate()
13+
14+
firebase_admin.initialize_app(cred, {
15+
"storageBucket": "benefit-decision-toolkit-play.firebasestorage.app"
16+
})
17+
18+
db = firestore.client()
19+
bucket = storage.bucket()
20+
21+
22+
def save_json_to_storage_and_update_firestore(json_string, firestore_doc_path):
23+
"""
24+
Upload JSON string to Firebase Storage and update Firestore
25+
with the storage path or download URL of the uploaded file.
26+
"""
27+
28+
# ---------------------
29+
# Create filename
30+
# Example: exported_2025-02-12_14-30-59.json
31+
# ---------------------
32+
timestamp = datetime.utcnow().strftime("%Y-%m-%d_%H-%M-%S")
33+
filename = f"LibraryApiSchemaExports/export_{timestamp}.json"
34+
35+
# ---------------------
36+
# Upload to storage
37+
# ---------------------
38+
blob = bucket.blob(filename)
39+
blob.upload_from_string(json_string, content_type="application/json")
40+
41+
# Get the storage path
42+
storage_path = blob.name
43+
44+
# ---------------------
45+
# Update Firestore
46+
# ---------------------
47+
doc_ref = db.document(firestore_doc_path)
48+
doc_ref.set({
49+
"latestJsonStoragePath": storage_path,
50+
"updatedAt": firestore.SERVER_TIMESTAMP
51+
}, merge=True)
52+
53+
print("Uploaded:", storage_path)
54+
print("Firestore updated!")
55+
56+
return storage_path
57+
58+
59+
# -----------------------------------
60+
# Example usage
61+
# -----------------------------------
62+
if __name__ == "__main__":
63+
with open("endpoint_inputs.json", "r") as f:
64+
data = json.load(f)
65+
json_string = json.dumps(data, indent=2)
66+
67+
save_json_to_storage_and_update_firestore(
68+
json_string,
69+
firestore_doc_path="system/config" # Example document path
70+
)

0 commit comments

Comments
 (0)