;; 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 list-all-interactions (request-params)
"Return all interactions for this conference."
(with-persistent-auth ((list *role-admin*) request-params)
(let ((interactions (load-interactions *interactions-db* :prefix "i.")))
`(200 () ,(mapcar #'to-hash interactions)))))
(defun list-vendor-interactions (request-params)
"Return all interactions for all reps associated with a specific vendor"
(with-persistent-auth ((list *role-vendor*) request-params)
; Due to persistent auth checks, I don't need to worry about sending in
; false prefixes such as vendor-id = '3', as such a check would fail
; the persistent auth vendor role check.
(let* ((vendor-id (param-val :vendor-id request-params *max-id-length*))
(prefix (format nil "i.~A." vendor-id))
(interactions
(load-interactions *interactions-db* :prefix prefix)))
(format T "Listing interactions with prefix ~A~%" prefix)
`(200 () ,(mapcar #'to-hash interactions)))))
(defun list-rep-interactions (request-params)
"Return all interactions for a particular vendor rep."
(with-persistent-auth ((list *role-rep*) request-params)
(let* ((vid (param-val :vendor-id request-params *max-id-length*))
(rid (param-val :rep-id request-params *max-id-length*))
(prefix (format nil "i.~A.~A." vid rid))
(interactions (load-interactions *interactions-db* :prefix prefix)))
(format T "Listing interactions with prefix ~A~%" prefix)
`(200 () (,(st-json:write-json-to-string
(mapcar #'to-hash interactions)))))))
(defun put-interaction (request-params)
"Create a new interaction, time now, for the specified attendee."
(with-persistent-auth ((list *role-rep*) request-params)
(handler-case
(let* ((vendor-id (param-val :vendor-id request-params *max-id-length*))
(rep-id (param-val :rep-id request-params *max-id-length*))
(attendee-id
(param-val "attendee-id" request-params *max-id-length*))
(interaction
(make-interaction :vendor-id vendor-id
:rep-id rep-id
:attendee-id attendee-id
:ts (format nil "~A" (get-universal-time)))))
(write-to-db *interactions-db* interaction)
`(200 () ("\"Created\"")))
(bad-attribute-value-error (c)
`(400 () (,(st-json:write-json-to-string (to-hash c))))))))