;; 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-vendor (request-params)
"As a ningle handler, add the provided vendor."
(with-persistent-auth ((list *role-admin* *role-host*) request-params)
(format T "PUT vendor Params:~%~S~%" request-params)
(handler-case
(let* ((new-vendor-id (generate-id))
(new-vendor (vendor-from-alist new-vendor-id request-params)))
; TODO: Trigger email validation
(write-to-db *people-db* new-vendor)
`(200 () (,(st-json:write-json-to-string (to-hash new-vendor)))))
(bad-attribute-value-error (c)
`(400 () (,(st-json:write-json-to-string (to-hash c))))))))
(defun list-vendors (request-params)
"As a ningle handler, list all vendors."
(format T "GET vendor Params:~%~S~%" request-params)
(let ((vendor-list (load-all-vendors *people-db*)))
`(200 () (,(st-json:write-json-to-string
(mapcar #'(lambda (v) (to-hash v)) vendor-list))))))
(defun get-vendor (request-params)
"As a ningle handler, retrieve the vendor requested with the :vendor-id
route parameter."
(format T "GET vendor Params:~%~S~%" request-params)
(with-txn (:write nil)
(let* ((id (param-val :vendor-id request-params *max-id-length*))
(vendor (load-vendor *people-db* id)))
(if vendor
`(200 () (,(st-json:write-json-to-string (to-hash vendor))))
`(404 () ("Not found"))))))
(defun update-vendor (request-params)
"As a ningle handler, update the vendor requested with the :vendor-id
route parameter."
(with-persistent-auth ((list *role-admin* *role-host* *role-vendor*) request-params)
(format T "PUT vendor Params:~%~S~%" request-params)
(handler-case
(let* ((id (param-val :vendor-id request-params *max-id-length*))
(vendor (load-vendor *people-db* id)))
(if vendor
(let ((updated-vendor (update vendor request-params)))
(write-to-db *people-db* updated-vendor :overwrite t)
`(200 () (,(st-json:write-json-to-string
(to-hash updated-vendor)))))
`(404 () ("Not found"))))
(bad-attribute-value-error (c)
`(400 () (,(st-json:write-json-to-string (to-hash c))))))))
(defun deactivate-vendor (request-params)
"As a ningle handler, retrieve the vendor requested with the :vendor-id
route parameter and deactivate it."
(with-persistent-auth ((list *role-admin*) request-params)
(format T "DELETE vendor Params:~%~S~%" request-params)
`(200 () (,(format nil "\"Not Implemented\"")))))
(defun auth-vendor (request-params)
"Create persistent auth for this vendor."
(with-oneshot-auth *auth-db* (vendor-email)
(with-txn (:write nil)
(let* ((vendor-id (param-val :vendor-id request-params *max-id-length*))
(vendor (load-vendor *people-db* vendor-id)))
(if vendor
`(200 () (,(st-json:write-json-to-string
(generate-jwt vendor-email *role-vendor*
:vendor-id vendor-id
:minutes *vendor-auth-minutes*))))
`(404 () ("Vendor not found")))))))
(defun check-auth-vendor (request-params)
"Check that the persistent auth is valid and allowed for this rep."
(with-persistent-auth ((list *role-vendor*) request-params)
`(200 () (,(format nil "\"Auth valid for vendor ~A\""
(param-val :vendor-id request-params *max-id-length*))))))