Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mds-db] Fix editGeography #74

Merged
merged 4 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/mds-db/geographies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,17 @@ export async function editGeography(geography: Geography) {
}

const client = await getWriteableClient()
const sql = `UPDATE ${schema.TABLE.geographies} SET geography_json=$1 WHERE geography_id='${geography.geography_id}' AND publish_date IS NULL`
await client.query(sql, [geography.geography_json])
const vals = new SqlVals()
const conditions: string[] = []
Object.entries(geography).forEach(([key, value]) => {
if (key === 'geography_json') {
conditions.push(`geography_json = ${vals.add(JSON.stringify(geography.geography_json))}`)
} else {
conditions.push(`${key} = ${vals.add(value)}`)
}
})
const sql = `UPDATE ${schema.TABLE.geographies} SET ${conditions} WHERE geography_id='${geography.geography_id}' AND publish_date IS NULL`
await client.query(sql, vals.values())
return geography
}

Expand Down
3 changes: 2 additions & 1 deletion packages/mds-db/tests/mds-db.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,13 @@ if (pg_info.database) {
const numFeatures = geography_json.features.length
geography_json.features = []
await MDSDBPostgres.editGeography({
name: 'District Seven',
name: 'District Seven Updated Name',
geography_id: DistrictSeven.geography_id,
geography_json
})
const result = await MDSDBPostgres.readSingleGeography(GEOGRAPHY2_UUID)
assert.notEqual(result.geography_json.features.length, numFeatures)
assert.equal(result.name, 'District Seven Updated Name')
assert.equal(result.geography_json.features.length, 0)
})

Expand Down