@@ -13,6 +13,8 @@ import (
1313 "github.com/nuts-foundation/nuts-knooppunt/component/mcsdadmin/static"
1414 tmpls "github.com/nuts-foundation/nuts-knooppunt/component/mcsdadmin/templates"
1515 "github.com/nuts-foundation/nuts-knooppunt/component/mcsdadmin/valuesets"
16+ "github.com/nuts-foundation/nuts-knooppunt/lib/coding"
17+ "github.com/nuts-foundation/nuts-knooppunt/lib/to"
1618 "github.com/rs/zerolog/log"
1719 "github.com/zorgbijjou/golang-fhir-models/fhir-models/caramel"
1820 "github.com/zorgbijjou/golang-fhir-models/fhir-models/fhir"
@@ -198,6 +200,14 @@ func newOrganizationPost(w http.ResponseWriter, r *http.Request) {
198200 var org fhir.Organization
199201 name := r .PostForm .Get ("name" )
200202 org .Name = & name
203+ uraString := r .PostForm .Get ("identifier" )
204+ if uraString == "" {
205+ http .Error (w , "Bad request: missing URA identifier" , http .StatusBadRequest )
206+ return
207+ }
208+ org .Identifier = []fhir.Identifier {
209+ uraIdentifier (uraString ),
210+ }
201211
202212 orgTypeCode := r .PostForm .Get ("type" )
203213 orgType , ok := valuesets .CodableFrom ("organization-type" , orgTypeCode )
@@ -280,6 +290,10 @@ func newEndpointPost(w http.ResponseWriter, r *http.Request) {
280290
281291 var endpoint fhir.Endpoint
282292 address := r .PostForm .Get ("address" )
293+ if address == "" {
294+ http .Error (w , "bad request: missing address" , http .StatusBadRequest )
295+ return
296+ }
283297 endpoint .Address = address
284298
285299 var payloadType fhir.CodeableConcept
@@ -288,7 +302,8 @@ func newEndpointPost(w http.ResponseWriter, r *http.Request) {
288302 if ok {
289303 endpoint .PayloadType = []fhir.CodeableConcept {payloadType }
290304 } else {
291- log .Warn ().Msg ("Failed to find referred payload type" )
305+ http .Error (w , "bad request: missing payload type" , http .StatusBadRequest )
306+ return
292307 }
293308
294309 periodStart := r .PostForm .Get ("period-start" )
@@ -298,8 +313,6 @@ func newEndpointPost(w http.ResponseWriter, r *http.Request) {
298313 Start : & periodStart ,
299314 End : & periodEnd ,
300315 }
301- } else {
302- log .Warn ().Msg ("Missing period" )
303316 }
304317
305318 contactValue := r .PostForm .Get ("contact" )
@@ -308,8 +321,6 @@ func newEndpointPost(w http.ResponseWriter, r *http.Request) {
308321 Value : & contactValue ,
309322 }
310323 endpoint .Contact = []fhir.ContactPoint {contact }
311- } else {
312- log .Warn ().Msg ("Missing contact value" )
313324 }
314325
315326 orgFormStr := r .PostForm .Get ("managing-org" )
@@ -323,12 +334,13 @@ func newEndpointPost(w http.ResponseWriter, r *http.Request) {
323334 }
324335 err = client .Read (reference , & managingOrg )
325336 if err != nil {
326- log .Error (). Err ( err ). Msg ( "Failed to find referred organisation" )
337+ http .Error (w , "internal error: could not find organization" , http . StatusInternalServerError )
327338 return
328339 }
329340 endpoint .ManagingOrganization .Display = managingOrg .Name
330341 } else {
331- log .Warn ().Msg ("Missing organisation value" )
342+ http .Error (w , "bad request: missing managing organization" , http .StatusBadRequest )
343+ return
332344 }
333345
334346 var connectionType fhir.Coding
@@ -337,7 +349,8 @@ func newEndpointPost(w http.ResponseWriter, r *http.Request) {
337349 if ok {
338350 endpoint .ConnectionType = connectionType
339351 } else {
340- log .Warn ().Msg ("Failed to find referred connection type" )
352+ http .Error (w , "bad request: missing connection type" , http .StatusBadRequest )
353+ return
341354 }
342355
343356 purposeOfUseId := r .PostForm .Get ("purpose-of-use" )
@@ -348,14 +361,23 @@ func newEndpointPost(w http.ResponseWriter, r *http.Request) {
348361 ValueCodeableConcept : & purposeOfUse ,
349362 }
350363 endpoint .Extension = append (endpoint .Extension , extension )
351- } else {
352- log .Warn ().Msg ("Failed to find referred purpose of use" )
353364 }
354365
355366 status := r .PostForm .Get ("status" )
356367 endpoint .Status , ok = valuesets .EndpointStatusFrom (status )
357368 if ! ok {
358- log .Warn ().Msg ("Failed to determine status, default to active" )
369+ http .Error (w , "bad request: missing status" , http .StatusBadRequest )
370+ return
371+ }
372+
373+ forOrgStr := r .PostForm .Get ("endpoint-for" )
374+ var owningOrg fhir.Organization
375+ if len (forOrgStr ) > 0 {
376+ err = client .Read ("Organization/" + forOrgStr , & owningOrg )
377+ if err != nil {
378+ http .Error (w , "bad request: could not find organization" , http .StatusBadRequest )
379+ return
380+ }
359381 }
360382
361383 var resEp fhir.Endpoint
@@ -365,6 +387,18 @@ func newEndpointPost(w http.ResponseWriter, r *http.Request) {
365387 return
366388 }
367389
390+ var epRef fhir.Reference
391+ epRef .Type = to .Ptr ("Endpoint" )
392+ epRef .Reference = to .Ptr ("Endpoint/" + * resEp .Id )
393+
394+ owningOrg .Endpoint = append (owningOrg .Endpoint , epRef )
395+
396+ var updatedOrg fhir.Organization
397+ err = client .Update ("Organization/" + * owningOrg .Id , owningOrg , updatedOrg )
398+ if err != nil {
399+ http .Error (w , err .Error (), http .StatusInternalServerError )
400+ }
401+
368402 w .WriteHeader (http .StatusCreated )
369403 renderList [fhir.Endpoint , tmpls.EpListProps ](client , w , tmpls .MakeEpListXsProps )
370404}
@@ -528,3 +562,10 @@ func renderList[R any, DTO any](fhirClient fhirclient.Client, httpResponse http.
528562 Items : dtoFunc (items ),
529563 })
530564}
565+
566+ func uraIdentifier (uraString string ) fhir.Identifier {
567+ var identifier fhir.Identifier
568+ identifier .Value = to .Ptr (uraString )
569+ identifier .System = to .Ptr (coding .URANamingSystem )
570+ return identifier
571+ }
0 commit comments