Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ptv config #141

Merged
merged 9 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions webapp/src/clj/lipas/backend/ptv/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

(defn generate-ptv-service-descriptions
[search
{:keys [_id sub-category-id city-codes overview]}]
{:keys [sub-category-id city-codes overview]}]
(let [doc (or overview
(let [type-codes (->> (types/by-sub-category sub-category-id)
(map :type-code))
Expand Down Expand Up @@ -107,7 +107,7 @@
:service-channel-ids])

(defn upsert-ptv-service-location!*
[tx ptv-component search user {:keys [org-id site ptv archive?] :as _m}]
[ptv-component {:keys [org-id site ptv archive?] :as _m}]
(let [id (-> ptv :service-channel-ids first)
;; merge or just replace?
site (update site :ptv merge ptv)
Expand All @@ -122,13 +122,18 @@
;; Store the new PTV info to Lipas DB
new-ptv-data (-> ptv
(select-keys persisted-ptv-keys)
(assoc :last-sync now
(assoc :org-id (or (:org-id ptv)
org-id)
:last-sync now
;; Store the current type-code into ptv data, so this can be
;; used to comapre if the services need to recalculated on site data update.
:previous-type-code (:type-code (:type site))
:source-id (:sourceId ptv-resp)
;; Store the PTV status so we can ignore Lipas archived places that we already archived in PTV.
:publishing-status (:publishingStatus ptv-resp)
;; NOTE: The ptv map might not have this value in some cases...?
;; but the value merged with data from site should have it always?
:service-ids (:service-ids (:ptv site))
;; Take the created ID from ptv response and store to Lipas DB right away.
;; TODO: Is there a case where this could be multiple ids?
:service-channel-ids [(:id ptv-resp)])
Expand All @@ -149,7 +154,7 @@
(let [site (db/get-sports-site db lipas-id)
_ (assert (some? site) (str "Sports site " lipas-id " not found in DB"))

[ptv-resp new-ptv-data] (upsert-ptv-service-location!* tx ptv-component search user
[ptv-resp new-ptv-data] (upsert-ptv-service-location!* ptv-component
{:org-id org-id
:site site
:ptv ptv
Expand All @@ -176,9 +181,10 @@
(:serviceChannelNames ptv-resp))}})))

(comment
(require '[integrant.repl.state :as state])

(let [ptv-component (:lipas/ptv integrant.repl.state/system)
org-id ptv-data/liminka-org-id-test
(let [ptv-component (:lipas/ptv state/system)
org-id "7fdd7f84-e52a-4c17-a59a-d7c2a3095ed5"
services (:itemList (ptv/get-org-services ptv-component org-id))]
(->> services
(utils/index-by :sourceId)
Expand Down Expand Up @@ -240,7 +246,7 @@
(vec x)))))
ptv)

[_ptv-resp new-ptv-data] (upsert-ptv-service-location!* tx ptv-component search user
[_ptv-resp new-ptv-data] (upsert-ptv-service-location!* ptv-component
{:org-id org-id
:ptv ptv
:site sports-site
Expand Down
24 changes: 10 additions & 14 deletions webapp/src/clj/lipas/backend/ptv/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
(defn localized-string-schema [string-props]
[:map
{:closed true}
[:fi [:string string-props]]
[:se [:string string-props]]
[:en [:string string-props]]])
[:fi {:optional true} [:string string-props]]
[:se {:optional true} [:string string-props]]
[:en {:optional true} [:string string-props]]])

(def integration-enum
[:enum "lipas-managed" "manual"])

(def ptv-meta
[:map
{:closed true}
;; [:org-id :string]
[:org-id :string]
[:sync-enabled :boolean]

;; These options aren't used now:
Expand All @@ -29,7 +29,7 @@
integration-enum]

[:service-channel-ids [:vector :string]]
;; [:service-ids [:vector :string]]
[:service-ids [:vector :string]]
;; [:languages [:vector :string]]

[:summary (localized-string-schema {:max 150})]
Expand All @@ -53,7 +53,7 @@
{:require-privilege :ptv/manage
:parameters {:body [:map
[:city-codes [:vector :int]]
[ :type-codes {:optional true} [:vector :int]]
[:type-codes {:optional true} [:vector :int]]
[:owners [:vector :string]]]}
:handler
(fn [req]
Expand Down Expand Up @@ -101,11 +101,7 @@
{:post
{:require-privilege :ptv/manage
:parameters {:body [:map
[:org-id :string]
[:city-codes [:vector :int]]
[:owners [:vector :string]]
[:supported-languages [:vector [:enum "fi" "se" "en"]]]
[:sourceId :string]
[:sub-category-id :int]
[:overview {:optional true
:description "Use this to replace the AI input with non-saved site information"}
Expand Down Expand Up @@ -182,16 +178,16 @@
{:require-privilege :ptv/manage
:parameters {:body create-ptv-service-location}
:handler
(fn [{:keys [body-params identity]}]
(fn [req]
{:status 200
:body (ptv-core/upsert-ptv-service-location! db ptv search identity body-params)})}}]
:body (ptv-core/upsert-ptv-service-location! db ptv search (:identity req) (-> req :parameters :body))})}}]

["/actions/save-ptv-meta"
{:post
{:require-privilege :ptv/manage
:coercion reitit.coercion.spec/coercion
:parameters {:body :lipas.sports-site/ptv}
:handler
(fn [{:keys [identity] :as req}]
(fn [req]
{:status 200
:body (ptv-core/save-ptv-integration-definitions db search identity (-> req :parameters :body))})}}]])
:body (ptv-core/save-ptv-integration-definitions db search (:identity req) (-> req :parameters :body))})}}]])
35 changes: 22 additions & 13 deletions webapp/src/clj/lipas/backend/ptv/integration.clj
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,18 @@
In test-env token seems to be valid for 24h."
[{:keys [token-url username password org-id]}]
(let [token-key (if (test-env? token-url) :ptvToken :serviceToken) ; wtf
;; Prod needs a different type of ID for apiUserOrganisation value
user-org-id (or (:prod-org-id (get ptv-data/org-id->params org-id))
org-id)
req {:url token-url
:method :post
:as :json
:accept :json
:content-type :json
:form-params (merge {:username username
:password password}
(when org-id
{:apiUserOrganisation org-id}))}]
(when user-org-id
{:apiUserOrganisation user-org-id}))}]
(-> (client/request req)
:body
token-key)))
Expand Down Expand Up @@ -110,11 +113,13 @@
;; NOTE: Looks like tokens from yesterday aren't valid the next day even if they haven't "expired" yet?
(if (and (not retried?)
(= 401 (:status d))
;; Just retry once for every 401
#_
(= "Bearer error=\"invalid_token\", error_description=\"The access token is not valid.\""
(get (:headers d) "WWW-Authenticate")))
(do
(log/infof "Invalid token, trying to get a new token and retry")
(swap! (:tokens ptv) dissoc (:auth-org-id req))
(swap! (:tokens ptv) dissoc auth-org-id)
(http ptv auth-org-id req true))
(throw (ex-info (format "HTTP Error: %s %s" (:status d) (:body d))
{:resp d
Expand Down Expand Up @@ -190,7 +195,6 @@
[ptv service-location]
(let [org-id (-> service-location :organizationId)
params {:url (make-url ptv "/v11/ServiceChannel/ServiceLocation")
:auth-org-id org-id
:method :post
:form-params service-location}]
(log/info "Create PTV service location" service-location)
Expand Down Expand Up @@ -244,18 +248,20 @@

(def ptv* (:lipas/ptv state/system))

(get-org-services ptv* ptv-data/liminka-org-id-test)
(def org-id* "7fdd7f84-e52a-4c17-a59a-d7c2a3095ed5")

(get-org-services ptv* org-id*)

;; Delete all org services
(doseq [x (:itemList (get-org-services ptv* ptv-data/liminka-org-id-test))]
(doseq [x (:itemList (get-org-services ptv* org-id*))]
(update-service ptv*
(:sourceId x)
{:mainResponsibleOrganization ptv-data/liminka-org-id-test
{:mainResponsibleOrganization org-id*
:publishingStatus "Deleted"}))

(get-service ptv*
ptv-data/liminka-org-id-test
(-> (get-org-services {} ptv-data/liminka-org-id-test)
org-id*
(-> (get-org-services {} org-id*)
:itemList
first
:id))
Expand All @@ -279,14 +285,17 @@
:let [site (core/get-sports-site (user/db) (:lipas-id search-site))]]
(core/index! (user/search) site :sync))

(get-org-service-channels ptv* ptv-data/liminka-org-id-test)
(get-org-service-channels ptv* org-id*)

(http ptv* org-id* {:url (make-url ptv* "/v11/ServiceChannel/b4abd13e-0d36-4ff9-a6c9-94f2f5aee036")
:method :get})

;; Delete all org service locations
(doseq [x (:itemList (get-org-service-channels ptv* ptv-data/liminka-org-id-test))]
(update-service-location ptv* (:id x) {:organizationId ptv-data/liminka-org-id-test
(doseq [x (:itemList (get-org-service-channels ptv* org-id*))]
(update-service-location ptv* (:id x) {:organizationId org-id*
:publishingStatus "Deleted"}))

(update-service-location ptv*
"fc768bb4-268c-4054-9b88-9ecc9a943452"
{:org-id ptv-data/liminka-org-id-test
{:org-id org-id*
:publishingStatus "Deleted"}))
48 changes: 35 additions & 13 deletions webapp/src/cljc/lipas/data/ptv.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,36 @@
;; org 10
#_(def uta-org-id-test "52e0f6dc-ec1f-48d5-a0a2-7a4d8b657d53")

;; Testiorganisaatio 6 (Kunta)
(def uta-org-id-test "3d1759a2-e47a-4947-9a31-cab1c1e2512b")

;; org 9
(def liminka-org-id-test "7fdd7f84-e52a-4c17-a59a-d7c2a3095ed5")

;; org 8
#_(def uta-org-id-test "92374b0f-7d3c-4017-858e-666ee3ca2761")
#_(def uta-org-id-prod "7b83257d-06ad-4e3b-985d-16a5c9d3fced")

;; TODO: Tulossa 5 kuntaa, muut:
;; (Lumijoki. Pyhäjärvi, Ii, Liminka ja Oulu sekä tietenkin bonuksena Utajärvi).
;; TODO: Kuinka valita näytetäänkö test vai prod organisaatiot?

(def organizations
[{:name "Utajärven kunta (test)"
:props {:org-id uta-org-id-test
:props {;; Testiorganisaatio 6 (Kunta)
:org-id "3d1759a2-e47a-4947-9a31-cab1c1e2512b"
:city-codes [889]
:owners ["city" "city-main-owner"]
:supported-languages ["fi"]}}
{:name "Utajärven kunta (prod)"
:props {:org-id "7b83257d-06ad-4e3b-985d-16a5c9d3fced"
;; Production authentication apiUserOrganisation field uses different
;; "persistent org-id" value. This option is used to map the "version org-id"
;; that is used elsewhere to this version for the auth.
:prod-org-id "9f095753-3ca9-4d89-b7e4-3cdf83bb44b2"
:city-codes [889]
:owners ["city" "city-main-owner"]
:supported-languages ["fi" "se" "en"]}}
:supported-languages ["fi"]}}
{:name "Limingan kunta (test)"
:props {:org-id liminka-org-id-test
:props {;; org 9
:org-id "7fdd7f84-e52a-4c17-a59a-d7c2a3095ed5"
:city-codes [425]
:owners ["city" "city-main-owner"]
:supported-languages ["fi" "se" "en"]}} ])
:supported-languages ["fi" #_#_ "se" "en"]}}])

;; For adding default params to some requests from the FE
;; NOTE: This should eventually be replaced with Lipas organizations.
Expand Down Expand Up @@ -70,6 +76,16 @@

(def default-langs ["fi"])

;; NOTE: Right now the UI mostly just uses this always.
;; The languages value is also stored to sports-site :ptv on
;; sync, but that value isn't used now?
;; So it is possible to enable new languages by modying the org config.
;; Summary/description texts are always generated for all languages,
;; but additional languages aren't shown on the FE or sent to PTV.
(defn org-id->languages [org-id]
(or (:supported-languages (get org-id->params org-id))
default-langs))

(defn ->service-source-id
[org-id sub-category-id]
(str "lipas-" org-id "-" sub-category-id))
Expand Down Expand Up @@ -181,7 +197,7 @@
}))

(defn ->ptv-service-location
[_org
[org-id
coord-transform-fn
now
{:keys [status ptv lipas-id location search-meta] :as sports-site}]
Expand All @@ -198,7 +214,7 @@
; (println "Languages resolved" languages)
; (prn location)

{:organizationId (:org-id ptv)
{:organizationId (or (:org-id ptv) org-id)
;; Keep using existing sourceId for sites that were already initialized in PTV,
;; generate a new unique ID (with timestamp) for new sites.
:sourceId (or (:source-id ptv)
Expand Down Expand Up @@ -363,7 +379,12 @@
)

(defn parse-service-source-id [source-id]
())
(-> (re-find #"lipas-.*-(\d*)" source-id)
second
parse-long))

(comment
(parse-service-source-id "lipas-7fdd7f84-e52a-4c17-a59a-d7c2a3095ed5-6100"))

(defn index-services [services]
)
Expand Down Expand Up @@ -420,6 +441,7 @@
(defn sports-site->ptv-input [{:keys [types org-id org-defaults org-langs]} service-channels services site]
(let [service-id (-> site :ptv :service-ids first)
service-channel-id (-> site :ptv :service-channel-ids first)

summary (-> site :ptv :summary)
description (-> site :ptv :description)

Expand Down
11 changes: 7 additions & 4 deletions webapp/src/cljs/lipas/ui/ptv/controls.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@
:on-change (fn [_e v]
(on-change (:value v)))})))

(defui lang-selector [{:keys [value on-change]}]
(defui lang-selector [{:keys [value on-change enabled-languages]}]
($ Tabs
{:value value
:on-change (fn [_e v] (on-change (keyword v)))}
($ Tab {:value "fi" :label "FI"})
($ Tab {:value "se" :label "SE"})
($ Tab {:value "en" :label "EN"})))
(when (or (nil? enabled-languages) (contains? enabled-languages "fi"))
($ Tab {:value "fi" :label "FI"}))
(when (or (nil? enabled-languages) (contains? enabled-languages "se"))
($ Tab {:value "se" :label "SE"}))
(when (or (nil? enabled-languages) (contains? enabled-languages "en"))
($ Tab {:value "en" :label "EN"}))))
Loading