;; Copyright (c) 2024, SWGY, Inc. <ron@sw.gy>
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3 of the License, or (at
;; your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software Foundation, Inc.,
;; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
;;
(in-package :swtx)
(defun put-attendee (request-params)
"As a ningle handler, add the provided attendee."
(format T "PUT attendee Params:~%~S~%" request-params)
(handler-case
(let* ((new-attendee-id (generate-id))
(new-attendee
(attendee-from-alist new-attendee-id request-params)))
(format T "New attendee: ~A" new-attendee)
(write-to-db *people-db* new-attendee)
; TODO: Trigger email validation
`(200 () (,(st-json:write-json-to-string (to-hash new-attendee)))))
(bad-attribute-value-error (c)
`(400 () (,(st-json:write-json-to-string (to-hash c)))))))
(defun list-attendees (request-params)
"Return the full set of attendee details."
(with-persistent-auth ((list *role-admin* *role-host*) request-params)
; TODO: Paging would be prudent
(let ((full-list (load-all-attendees *people-db*)))
`(200 () (,(st-json:write-json-to-string
(mapcar #'(lambda (v) (to-hash v)) full-list)))))))
(defun auth-attendee (request-params)
"Build a JWT for the indicated attendee."
(with-oneshot-auth *auth-db* (attendee-email)
(with-txn (:write nil)
(let* ((id (param-val :attendee-id request-params *max-id-length*))
(attendee (load-attendee *people-db* id)))
(if attendee
`(200 () (,(st-json:write-json-to-string
(generate-jwt attendee-email *role-attendee*
:person-id id
:minutes *attendee-auth-minutes*))))
`(404 () ("Not found")))))))
(defun check-auth-attendee (request-params)
"Check that the persistent auth is valid and allowed for this rep."
(with-persistent-auth ((list *role-attendee*) request-params)
`(200 ()
(,(format nil "\"Auth valid for attendee ~A\""
(param-val :attendee-id request-params *max-id-length*))))))
(defun get-attendee (request-params)
"As a ningle handler, retrieve the attendee requested with the :attendee-id
route parameter."
(with-persistent-auth ((list *role-admin* *role-host* *role-attendee*) request-params)
(format T "GET attendee Params:~%~S~%" request-params)
(with-txn (:write nil)
(let* ((id (param-val :attendee-id request-params *max-id-length*))
(attendee (load-attendee *people-db* id)))
(if attendee
`(200 () (,(st-json:write-json-to-string (to-hash attendee))))
`(404 () ("Attendee not found")))))))
(defun update-attendee (request-params)
"As a ningle handler, retrieve the attendee requested with the :attendee-id
route parameter."
(with-persistent-auth ((list *role-admin* *role-host* *role-attendee*) request-params)
(format T "PUT attendee Params:~%~S~%" request-params)
(handler-case
(let* ((id (param-val :attendee-id request-params *max-id-length*))
(attendee (load-attendee *people-db* id)))
(if attendee
(let ((updated-attendee (update attendee request-params)))
(write-to-db *people-db* updated-attendee :overwrite t)
`(200 () (,(st-json:write-json-to-string
(to-hash updated-attendee)))))
`(404 () ("Not found"))))
(bad-attribute-value-error (c)
`(400 () (,(st-json:write-json-to-string (to-hash c))))))))
(defun deactivate-attendee (request-params)
"As a ningle handler, retrieve the attendee requested with the :attendee-id
route parameter and deactivate it."
(with-persistent-auth ((list *role-admin* *role-attendee*) request-params)
(format T "DELETE attendee Params:~%~S~%" request-params)
`(200 () (,(format nil "\"Not implemented\"")))))