Skip to content

Commit

Permalink
Display SDS dataset IDs either with survey metadata or in SDS section (
Browse files Browse the repository at this point in the history
  • Loading branch information
liamtoozer authored May 8, 2024
1 parent 791e9a3 commit ac0f892
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 152 deletions.
6 changes: 6 additions & 0 deletions authentication/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ func generateClaimsV2(claimValues map[string][]string, schema QuestionnaireSchem
claims["tx_id"] = TxID.String()
claims["version"] = "v2"

// always send survey_id for business/test surveys unless it's already in survey metadata
_, ok := claimValues["survey_id"]
if !ok && !isSocialSurvey(schema.SurveyType) {
claimValues["survey_id"] = []string{schema.SurveyId}
}

surveyMetadata := make(map[string]interface{})
data := make(map[string]interface{})

Expand Down
2 changes: 1 addition & 1 deletion launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func redirectURL(w http.ResponseWriter, r *http.Request) {
} else if launchVersion != "" {
http.Redirect(w, r, hostURL+"/session?token="+token, 301)
} else {
http.Error(w,"Invalid Action", 500)
http.Error(w, "Invalid Action", 500)
}
}

Expand Down
65 changes: 48 additions & 17 deletions static/javascript/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@
// store fetch so it only needs to be re-done if the survey changes
let supplementaryDataSets = null;

// We always need survey_id from top-level schema metadata for SDS retrieval
let schemaSurveyId = null;

function clearSurveyMetadataFields() {
document.querySelector("#survey_metadata_fields").innerHTML = "";
showSupplementaryData(false);
Expand Down Expand Up @@ -386,14 +389,31 @@ function showCIRMetdata(cirInstrumentId, cirSchema) {
}

function updateSDSDropdown() {
const surveyId = document.getElementById("survey_id")?.value;
const surveyId = schemaSurveyId;
const periodId = document.getElementById("period_id")?.value;

const supplementaryDataSection = document.querySelector(
"#supplementary_data",
);
const sdsDatasetIdElement = document.querySelector("#sds_dataset_id");

loadSDSDatasetMetadata(surveyId, periodId)
.then((sds_metadata_response) => {
if (sds_metadata_response?.length) {
document.querySelector("#supplementary_data").innerHTML = "";
supplementaryDataSets = sds_metadata_response;
showSupplementaryData(true);
showSubmitFlushButtons(true);

if (
!document
.querySelector("#survey_metadata")
.contains(sdsDatasetIdElement)
) {
// add sds_dataset_id field into the SDS metadata section if not already in survey metadata
supplementaryDataSection.innerHTML = `<div class="field-container">${getLabelFor("sds_dataset_id")}<select id="sds_dataset_id" name="sds_dataset_id" class="qa-sds_dataset_id" onchange="loadSupplementaryDataInfo()"></select></div>`;
}

document.querySelector("#sds_dataset_id").innerHTML =
sds_metadata_response
.map(
Expand Down Expand Up @@ -428,6 +448,9 @@ function loadSchemaMetadata(schemaName, schemaUrl, cirInstrumentId) {
document.querySelector("#survey_metadata").innerHTML = "";
document.querySelector("#survey_metadata").innerHTML = "";

// We always need survey_id from top-level schema metadata for SDS retrieval
schemaSurveyId = schema_response.survey_id;

if (schema_response.metadata.length > 0) {
document.querySelector("#survey_metadata").innerHTML =
schema_response.metadata
Expand Down Expand Up @@ -483,22 +506,30 @@ function loadSupplementaryDataInfo() {
const selectedDataset = supplementaryDataSets?.find(
(d) => d["dataset_id"] === selectedDatasetId,
);
if (selectedDataset) {
const sdsDatasetMetadataKeys = [
"title",
"sds_schema_version",
"total_reporting_units",
"schema_version",
"sds_dataset_version",
];

document.querySelector("#supplementary_data").innerHTML =
sdsDatasetMetadataKeys
.map(
(key) =>
`<div class="field-container">${getLabelFor(key)}${getInputField(key, "text", selectedDataset[key], true)}</div>`,
)
.join("");

if (!selectedDataset) {
return;
}

const sdsDatasetMetadataKeys = [
"title",
"total_reporting_units",
"schema_version",
"sds_dataset_version",
];

const sdsMetadataSection = document.querySelector("#supplementary_data");
const sdsMetadataField = (key) =>
`<div class="field-container">${getLabelFor(key)}${getInputField(key, "text", selectedDataset[key], true)}</div>`;

if (sdsMetadataSection.contains(document.querySelector("#sds_dataset_id"))) {
sdsMetadataSection.innerHTML += sdsDatasetMetadataKeys
.map(sdsMetadataField)
.join("");
} else {
sdsMetadataSection.innerHTML = sdsDatasetMetadataKeys
.map(sdsMetadataField)
.join("");
}
}

Expand Down
2 changes: 1 addition & 1 deletion surveys/surveys.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ type DatasetMetadata struct {
SurveyID string `json:"survey_id"`
PeriodID string `json:"period_id"`
Title string `json:"title"`
SdsSchemaVersion int `json:"sds_schema_version"`
SdsPublishedAt string `json:"sds_published_at"`
TotalReportingUnits int `json:"total_reporting_units"`
SchemaVersion string `json:"schema_version"`
Expand Down Expand Up @@ -253,6 +252,7 @@ func GetSupplementaryDataSets(surveyId string, periodId string) ([]DatasetMetada

log.Printf("SDS API Base URL: %s", hostURL)
url := fmt.Sprintf("%s/v1/dataset_metadata?survey_id=%s&period_id=%s", hostURL, surveyId, periodId)
log.Printf("Getting SDS metadata: %s", url)
resp, err := client.Get(url)

if err != nil || (resp.StatusCode != 200 && resp.StatusCode != 404) {
Expand Down
Loading

0 comments on commit ac0f892

Please sign in to comment.