Skip to content

Commit

Permalink
[mds-db] Fix editGeography (#74)
Browse files Browse the repository at this point in the history
* Fix `editGeography`.

* ts assist

* Fixed `editGeography` with TS assist.
  • Loading branch information
janedotx authored Nov 14, 2019
1 parent 501bd08 commit 59c42a9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
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

0 comments on commit 59c42a9

Please sign in to comment.