Change the measure model: less pre-determined, more configurable.

This commit is contained in:
Jonathan Bernard 2019-03-10 15:59:02 -05:00
parent b629ec1a14
commit 4965d162ad
8 changed files with 43 additions and 44 deletions

View File

@ -1,4 +1,4 @@
import cliutils, docopt, logging, jester, json, os, strutils, tables
import cliutils, docopt, logging, jester, json, os, sequtils, strutils, tables
import personal_measure_apipkg/api
import personal_measure_apipkg/configuration
@ -64,6 +64,7 @@ Usage:
personal_measure_api test [options]
personal_measure_api serve [options]
personal_measure_api hashpwd <password> [options]
personal_measure_api create-admin <email> <password>
Options:
@ -77,6 +78,22 @@ Options:
let args = docopt(doc, version = PM_API_VERSION)
let ctx = initContext(args)
if args["create-admin"]:
let allUsers = ctx.db.getAllUsers();
if allUsers.anyIt(it.isAdmin):
raise newException(Exception, "admin user already exists, refusing to create a new admin")
let pwdAndSalt = hashPwd($args["<password>"], ctx.cfg.pwdCost)
let adminUser = User(
displayName: "Admin",
email: $args["<email>"],
hashedPwd: pwdAndSalt.pwd,
salt: pwdAndSalt.salt,
isAdmin: true)
echo ctx.db.createUser(adminUser)
if args["hashpwd"]:
if args["--salt"]:
echo hashWithSalt($args["<password>"], $args["--salt"])

View File

@ -251,6 +251,25 @@ proc start*(ctx: PMApiContext): void =
resp(Http200, $(%session.user), JSON)
put "/user":
checkAuth()
try:
let jsonBody = parseJson(request.body)
var updatedUser = session.user
# if jsonBody.hasKey("email") # TODO: add verification?
if jsonBody.hasKey("displayName"):
updatedUser.displayName = jsonBody["displayName"].getStr()
jsonResp(Http200, $(%ctx.db.updateUser(updatedUser)))
except JsonParsingError: jsonResp(Http400, getCurrentExceptionMsg())
except BadRequestError: jsonResp(Http400, getCurrentExceptionMsg())
except:
error "Could not update user information:\n\t" & getCurrentExceptionMsg()
jsonResp(Http500)
get "/users":
checkAuth(true)
@ -381,19 +400,7 @@ proc start*(ctx: PMApiContext): void =
slug: slug,
name: name,
description: jsonBody.getIfExists("description").getStr(""),
domainSource:
if jsonBody.hasKey("domainSource"): some(jsonBody["domainSource"].getStr)
else: none[string](),
domainUnits: jsonBody.getIfExists("domainUnits").getStr(""),
rangeSource:
if jsonBody.hasKey("rangeSource"): some(jsonBody["rangeSource"].getStr)
else: none[string](),
rangeUnits: jsonBody.getIfExists("rangeUnits").getStr(""),
analysis: @[])
if jsonBody.hasKey("analysis"):
for a in jsonBody["analysis"].getElems:
newMeasure.analysis.add(a.getStr)
config: jsonBody.getOrFail("config"))
resp($(%ctx.db.createMeasure(newMeasure)), JSON)

View File

@ -23,11 +23,7 @@ type
slug*: string
name*: string
description*: string
domainSource*: Option[string]
domainUnits*: string
rangeSource*: Option[string]
rangeUnits*: string
analysis*: seq[string]
config*: JsonNode
Measurement* = object
id*: UUID
@ -81,18 +77,3 @@ proc `%`*(tok: ApiToken): JsonNode =
if tok.expires.isSome:
result["expires"] = %(tok.expires.get.formatIso8601)
proc `%`*(m: Measure): JsonNode =
result = %*{
"id": $m.id,
"userId": $m.userId,
"slug": m.slug,
"name": m.name,
"description": m.description,
"domainUnits": m.domainUnits,
"rangeUnits": m.rangeUnits,
"analysis": m.analysis
}
if m.domainSource.isSome: result["domainSource"] = %(m.domainSource.get)
if m.rangeSource.isSome: result["rangeSource"] = %(m.rangeSource.get)

View File

@ -26,11 +26,6 @@ create table "measures" (
name varchar not null,
description varchar not null default '',
config jsonb not null default '{}'::json,
domain_source varchar default null,
domain_units varchar not null default '',
range_source varchar default null,
range_units varchar not null default '',
analysis varchar[] not null default '{}',
unique(user_id, slug)
);

View File

@ -0,0 +1 @@
Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6Imp3dCJ9.eyJleHAiOjE1NTIzMjYxODMsInN1YiI6IjA3NWJjYzhmLWE4N2QtNDRjZS04MDk5LWQ3ZGExZjYxYTc1YSIsImlhdCI6MTU1MjIzOTc4M30.RjE0MjYzMkFDOTc4RjZFNTYyMDVBMTY5MjExNkJGQ0QzNkQ1QjZFNzVCODBDMThENTc1M0Y0MjhDMkNFMjE3RA

1
api/temp/credential Symbolic link
View File

@ -0,0 +1 @@
auth-token-jdb.txt

1
api/temp/pm-api Symbolic link
View File

@ -0,0 +1 @@
../src/util/bash/client.sh

6
web/src/models.d.ts vendored
View File

@ -18,11 +18,7 @@ export interface Measure {
slug: string;
name: string;
description: string;
domainSource?: string;
domainUnites: string;
rangeSource?: string;
rangeUnits: string;
analysis: string[];
config: object;
}
export interface Measurement {