mapping_lifeline_wpforms

This commit is contained in:
Johannes Lötzsch 2022-03-09 14:24:03 +01:00
parent 015561b6f8
commit 01e0db2004
5 changed files with 114 additions and 23 deletions

View File

@ -7,23 +7,51 @@
[beherbergung.model.ngo :as ngo]
[clojure.edn]
[clojure.spec.gen.alpha :as gen]
[beherbergung.db.export :refer [write-edn]]))
[beherbergung.db.export :refer [write-edn]]
[clojure.string :refer [split]]))
(defn JaNein->bool [JaNein]
({"Ja" true "Nein" false} JaNein))
#_(def mapping {:accessible #(JaNein->bool (get % "Ist die Unterkunft rollstuhlgerecht?"))
:note "Nachricht"})
(def mapping {:accessible :accessible
:note :note})
(defn ->Int [s]
(try
(Integer/parseInt s)
(catch NumberFormatException _e)))
(def mapping_lifeline_wpforms {;; TODO: the times are not parsed till now
:time_from_str "frühestes Einzugsdatum"
:time_duration_str "Möglicher Aufenthalt (Dauer)"
:beds ["Verfügbare Betten" ->Int]
:languages ["Sprachen (sprechen / verstehen)" #(split % #",")]
:place_country "Land"
:place_city "Ort"
:place_zip "PLZ"
:place_street "Straße"
:place_street_number"Hausnummer"
:accessible ["Ist die Unterkunft rollstuhlgerecht?" JaNein->bool]
:animals_allowed ["Haustiere erlaubt?" JaNein->bool]
:animals_present ["Sind Haustiere im Haushalt vorhanden?" JaNein->bool]
:contact_name_full "Name"
:contact_phone "Telefonnummer"
:contact_email "E-Mail"
:note "Nachricht"})
(def mapping_identity {:accessible :accessible
:note :note})
(defn unify
[offers]
[offers mapping]
(let [mapping->fn (fn [mapping]
(fn [dataset] (->> mapping
(map (fn [[k v]]
[k (cond (fn? v)
(v dataset)
(vector? v)
(let [[orig_kw parse_fn] v]
(parse_fn (get dataset orig_kw)))
:else
(get dataset v))]))
(into {}))))]
@ -31,9 +59,29 @@
(s/def ::t_boolean t/boolean #_ (s/with-gen t/boolean #(s/gen boolean?)))
(s/def ::t_string t/string #_ (s/with-gen t/string #(s/gen string?)))
(s/def ::t_int t/int #_ (s/with-gen t/int #(s/gen int?)))
(s/def ::time_from_str (s/nilable ::t_string))
(s/def ::time_duration_str (s/nilable ::t_string))
(s/def ::beds (s/nilable ::t_int))
(s/def ::languages (s/nilable (s/* ::t_string)))
(s/def ::place_country (s/nilable ::t_string))
(s/def ::place_city (s/nilable ::t_string))
(s/def ::place_zip (s/nilable ::t_string))
(s/def ::place_street (s/nilable ::t_string))
(s/def ::place_street_number (s/nilable ::t_string))
(s/def ::accessible (s/nilable ::t_boolean))
(s/def ::animals_allowed (s/nilable ::t_boolean))
(s/def ::animals_present (s/nilable ::t_boolean))
(s/def ::contact_name_full (s/nilable ::t_string))
(s/def ::contact_phone (s/nilable ::t_string))
(s/def ::contact_email (s/nilable ::t_string))
(s/def ::note (s/nilable ::t_string))
(s/def ::offer (s/keys :req-un [::accessible ::note]))
(s/def ::offer (s/keys :req-un [::time_from_str ::time_duration_str ::beds ::languages
::place_country ::place_city ::place_zip ::place_street ::place_street_number
::accessible ::animals_allowed ::animals_present
::contact_name_full ::contact_phone ::contact_email
::note]))
(comment
(write-edn "./data/sample-data/example.edn"
@ -52,7 +100,8 @@
(when ngo:id
;; TODO: take it from the db and filter it by visibility to the ngo
(if (:import-file env)
(unify (clojure.edn/read-string (slurp (:import-file env))))
(unify (clojure.edn/read-string (slurp (:import-file env)))
mapping_lifeline_wpforms)
(gen/sample (s/gen ::offer))))))
(s/def ::get_offers (t/resolver #'get_offers))

View File

@ -10,7 +10,7 @@
:db-export-prefix "./data/export/"
:db-validate true
:import-file "./data/sample-data/example.edn"
:import-file nil #_"./data/sample-data/example.edn"
;:mail-host ""
;:mail-user ""

View File

@ -7,7 +7,7 @@
(def password "i!A;z\\\"'^G3Q)w])%83)")
(defn get_offers [variables]
(let [response (graphql {:query "query x($auth: Auth) { get_offers(auth: $auth){ accessible note } }"
(let [response (graphql {:query "query x($auth: Auth) { get_offers(auth: $auth){ time_from_str time_duration_str beds languages place_country place_city place_zip place_street place_street_number accessible animals_allowed animals_present contact_name_full contact_phone contact_email note } }"
:variables variables})]
(get-in response [:data :get_offers])))
@ -18,7 +18,7 @@
(let [offers (get_offers {:auth {:mail mail :password password}})]
(is (= 10 (count offers))) ;; 10 is the default sample size of gen/sample
(is (= {:accessible true, :note "hkUGJ8"}
(nth offers 7))))) ;; picked one of the random generated datasets that contains not so many trivial values
(nth offers 0))))) ;; picked one of the random generated datasets that contains not so many trivial values
(deftest wrong-login
(let [offers (get_offers {:auth {:mail mail :password "wrong"}})]

View File

@ -18,29 +18,29 @@ export type Scalars = {
/** Authentication requires either a valid mail+password combination or a jwt obtained by an earlier login. */
export type Auth = {
/** Self descriptive. */
jwt: Scalars['String'];
/** Self descriptive. */
mail: Scalars['String'];
/** Self descriptive. */
password: Scalars['String'];
/** Self descriptive. */
jwt: Scalars['String'];
};
/** The type that query operations will be rooted at. */
export type QueryType = {
__typename?: 'QueryType';
/** Export an encrypted database dump */
export?: Maybe<Export>;
/** The offers that are visible for the ngo, belonging to the login */
get_offers?: Maybe<Array<Get_Offers>>;
/** For a username+password get a jwt containing the login:id */
login: Login;
/** The offers that are visible for the ngo, belonging to the login */
get_offers?: Maybe<Array<Get_Offers>>;
/** Export an encrypted database dump */
export?: Maybe<Export>;
};
/** The type that query operations will be rooted at. */
export type QueryTypeExportArgs = {
password: Scalars['String'];
export type QueryTypeLoginArgs = {
auth: Auth;
};
@ -51,23 +51,37 @@ export type QueryTypeGet_OffersArgs = {
/** The type that query operations will be rooted at. */
export type QueryTypeLoginArgs = {
auth: Auth;
export type QueryTypeExportArgs = {
password: Scalars['String'];
};
/** Export an encrypted database dump */
export type Export = {
__typename?: 'export';
err?: Maybe<Scalars['String']>;
/** Self descriptive. */
exit: Scalars['Int'];
out?: Maybe<Scalars['String']>;
err?: Maybe<Scalars['String']>;
};
/** The offers that are visible for the ngo, belonging to the login */
export type Get_Offers = {
__typename?: 'get_offers';
time_from_str?: Maybe<Scalars['String']>;
time_duration_str?: Maybe<Scalars['String']>;
beds?: Maybe<Scalars['Int']>;
languages?: Maybe<Array<Scalars['String']>>;
place_country?: Maybe<Scalars['String']>;
place_city?: Maybe<Scalars['String']>;
place_zip?: Maybe<Scalars['String']>;
place_street?: Maybe<Scalars['String']>;
place_street_number?: Maybe<Scalars['String']>;
accessible?: Maybe<Scalars['Boolean']>;
animals_allowed?: Maybe<Scalars['Boolean']>;
animals_present?: Maybe<Scalars['Boolean']>;
contact_name_full?: Maybe<Scalars['String']>;
contact_phone?: Maybe<Scalars['String']>;
contact_email?: Maybe<Scalars['String']>;
note?: Maybe<Scalars['String']>;
};
@ -89,7 +103,7 @@ export type GetOffersQueryVariables = Exact<{
}>;
export type GetOffersQuery = { __typename?: 'QueryType', get_offers?: Array<{ __typename?: 'get_offers', accessible?: boolean | null, note?: string | null }> | null };
export type GetOffersQuery = { __typename?: 'QueryType', get_offers?: Array<{ __typename?: 'get_offers', time_from_str?: string | null, time_duration_str?: string | null, beds?: number | null, languages?: Array<string> | null, place_country?: string | null, place_city?: string | null, place_zip?: string | null, place_street?: string | null, place_street_number?: string | null, accessible?: boolean | null, animals_allowed?: boolean | null, animals_present?: boolean | null, contact_name_full?: string | null, contact_phone?: string | null, contact_email?: string | null, note?: string | null }> | null };
export const LoginDocument = `
@ -114,7 +128,21 @@ export const useLoginQuery = <
export const GetOffersDocument = `
query GetOffers($auth: Auth!) {
get_offers(auth: $auth) {
time_from_str
time_duration_str
beds
languages
place_country
place_city
place_zip
place_street
place_street_number
accessible
animals_allowed
animals_present
contact_name_full
contact_phone
contact_email
note
}
}

View File

@ -8,7 +8,21 @@ export const login = gql`
export const get_offers = gql`
query GetOffers($auth: Auth!) {
get_offers(auth: $auth) {
time_from_str
time_duration_str
beds
languages
place_country
place_city
place_zip
place_street
place_street_number
accessible
animals_allowed
animals_present
contact_name_full
contact_phone
contact_email
note
}
}`