web: EntryProposal form implementation.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"eventParentId": "ffff5ca5cb0547b6a99fb78cdb9b0170",
|
||||
"knownOrigins": [ "http://curl.localhost" ],
|
||||
"knownOrigins": [ "http://localhost:8080", "http://curl.localhost" ],
|
||||
"notionApiBaseUrl": "https://api.notion.com/v1",
|
||||
"notionVersion": "2021-08-16",
|
||||
"port": 8300
|
||||
|
@@ -126,9 +126,15 @@ proc start*(cfg: HffEntryFormsApiConfig): void =
|
||||
get "/version":
|
||||
jsonResp(Http200, $(%("hff_entry_forms_api v" & HFF_ENTRY_FORMS_API_VERSION)))
|
||||
|
||||
options "/add-page": optionsResp(@[HttpPost])
|
||||
options "/event-proposals/config": optionsResp(@[HttpGet])
|
||||
|
||||
post "/propose-event":
|
||||
get "/event-proposals/config":
|
||||
withApiErrors:
|
||||
dataResp(%getEventProposalConfig(cfg))
|
||||
|
||||
options "/event-proposals": optionsResp(@[HttpPost])
|
||||
|
||||
post "/event-proposals":
|
||||
withApiErrors:
|
||||
let ep = parseEventProposal(parseJson(request.body))
|
||||
if createProposedEvent(cfg, ep): statusResp(Http200)
|
||||
|
@@ -10,6 +10,11 @@ type
|
||||
date*: DateTime
|
||||
budgetInDollars*: int
|
||||
|
||||
MultiSelectOption = tuple[value: string, color: string]
|
||||
|
||||
EventProposalConfig* = object
|
||||
departmentOptions*: seq[MultiSelectOption]
|
||||
|
||||
proc getOrFail(n: JsonNode, key: string): JsonNode =
|
||||
## convenience method to get a key from a JObject or raise an exception
|
||||
if not n.hasKey(key):
|
||||
@@ -75,3 +80,14 @@ proc asNotionPage*(ep: EventProposal): JsonNode =
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
proc `%`(mso: MultiSelectOption): JsonNode =
|
||||
%*{
|
||||
"value": mso.value,
|
||||
"color": mso.color
|
||||
}
|
||||
|
||||
proc `%`*(epc: EventProposalConfig): JsonNode =
|
||||
%*{
|
||||
"departments": epc.departmentOptions
|
||||
}
|
||||
|
@@ -1,16 +1,43 @@
|
||||
import json, logging, std/httpclient, strutils
|
||||
import json, logging, std/httpclient, sequtils, strutils
|
||||
|
||||
import ./models, ./service
|
||||
|
||||
proc createProposedEvent*(cfg: HffEntryFormsApiConfig, ep: EventProposal): bool =
|
||||
proc makeHttpClient(cfg: HffEntryFormsApiConfig): HttpClient =
|
||||
let headers = newHttpHeaders([
|
||||
("Content-Type", "application/json"),
|
||||
("Authorization", "Bearer " & cfg.integrationToken),
|
||||
("Notion-Version", cfg.notionVersion)
|
||||
], true)
|
||||
let http = newHttpClient(headers = headers, )
|
||||
|
||||
debug $headers
|
||||
return newHttpClient(headers = headers, )
|
||||
|
||||
proc getEventProposalConfig*(cfg: HffEntryFormsApiConfig): EventProposalConfig =
|
||||
let http = makeHttpClient(cfg)
|
||||
|
||||
let apiResp = http.get(cfg.notionApiBaseUrl & "/databases/" & cfg.eventParentId)
|
||||
debug apiResp.status
|
||||
|
||||
if not apiResp.status.startsWith("2"):
|
||||
debug apiResp.body
|
||||
raiseApiError(Http500,
|
||||
"unable to read event propsal configuration from notion API")
|
||||
|
||||
let bodyJson = parseJson(apiResp.body)
|
||||
let departmentOptionsJson = bodyJson{
|
||||
"properties", "Department", "multi_select", "options"}
|
||||
|
||||
if departmentOptionsJson.isNil:
|
||||
raiseApiError(Http500,
|
||||
"missing read department values from Notion API-supplied event schema")
|
||||
|
||||
return EventProposalConfig(
|
||||
departmentOptions: departmentOptionsJson.toSeq.mapIt(
|
||||
( value: it["name"].getStr, color: it["color"].getStr )
|
||||
)
|
||||
)
|
||||
|
||||
proc createProposedEvent*(cfg: HffEntryFormsApiConfig, ep: EventProposal): bool =
|
||||
let http = makeHttpClient(cfg)
|
||||
let epNotionPage = ep.asNotionPage
|
||||
epNotionPage["parent"] = %*{ "database_id": cfg.eventParentId }
|
||||
|
||||
|
Reference in New Issue
Block a user