From 96263514dd657dbccbd3b97beacafc6cf7905990 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Tue, 14 Mar 2023 23:36:43 +0100
Subject: [PATCH] Cistka konfigurace, zmena mapovani id produktu

---
 .gitlab-ci.yml                    |  2 +-
 cf2022.conf                       | 11 -------
 lib/CF2022/Controller/Orders.pm   | 30 ++++++++++-------
 lib/CF2022/Controller/Products.pm | 54 +++++++++++++++++++++++++------
 4 files changed, 64 insertions(+), 33 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index c6fec68..4555bb7 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -2,7 +2,7 @@ image: docker:20.10.9
 
 variables:
   DOCKER_TLS_CERTDIR: "/certs"
-  IMAGE_VER: 0.13.4
+  IMAGE_VER: 0.14.0
 
 services:
   - docker:20.10.9-dind
diff --git a/cf2022.conf b/cf2022.conf
index 9e291f9..ddeee9c 100644
--- a/cf2022.conf
+++ b/cf2022.conf
@@ -2,15 +2,4 @@
   secrets               => ['14283e549647774b17d10e1d75bcf16c2969673e'],
   oauth_url             => 'https://auth.pirati.cz/auth/realms/pirati',
   groups_url            => 'https://iapi.pirati.cz/v1/octopus/groups',
-  pretix_api            => 'https://pretix.pir-test.eu/api/v1',
-  pretix_token          => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
-  pretix_testmode       => 1,
-  pretix_organizer      => 'pirati',
-  pretix_event          => 'cf2022',
-  pretix_qid_sso        => 29, # identifikator otazky SSO UID
-  pretix_qid_region     => 8, # identifikator otazky Kraj
-  pretix_qid_photo      => 28, # identifikator otazky Foto
-  pretix_product_member => 1, # produkt pro cleny (NEMAPOVANY!)
-  pretix_product_regp   => 2, # produkt pro regp (NEMAPOVANY!)
-  mail_domain           => 'pirati.cz',
 }
diff --git a/lib/CF2022/Controller/Orders.pm b/lib/CF2022/Controller/Orders.pm
index 5bf42e8..94f78d4 100644
--- a/lib/CF2022/Controller/Orders.pm
+++ b/lib/CF2022/Controller/Orders.pm
@@ -4,11 +4,6 @@ use Mojo::UserAgent;
 use Mojo::Asset::File;
 use Image::PNG::QRCode 'qrpng';
 
-use constant MAIN_PRODUCTS        => qr/^(1|2|3)$/; # typy akreditace
-use constant NONFREE_PRODUCTS     => qr/^(1)$/;     # akreditace vyzadujici token
-use constant SUBSIDY_PRODUCTS     => qr/^(58)$/;    # prispevkove polozky
-use constant FALLBACK_PRODUCT     => 2;             # fallback akreditace
-
 use constant TPL_VS      => '%d%07d';
 use constant TPL_PAYMENT => 'SPD*1.0*ACC:%s*AM:%f*MSG:%s, %s*X-ID:%s*X-VS:%d*X-SS:%d*X-KS:%d';
 
@@ -25,6 +20,15 @@ sub create ($c ) {
     my $accredited    = 0;
     my $accreditation = 0;
 
+    # clasifikator typu
+    my $pt = {};
+    CLASS:
+    foreach my $class ( qw(main nonfree subsidy) ) {
+        foreach my $id ( split /\D+/, $cfg->{"products_$class"} ) {
+            $pt->{$class}{$id} = 1;
+        }
+    }
+
     my $url = sprintf ('%s/organizers/%s/events/%s/orders/',
         $c->config->{pretix_api},
         $c->config->{pretix_organizer},
@@ -60,7 +64,7 @@ sub create ($c ) {
             },
         );
 
-        $accreditation = $c->config->{'pretix_product_' . $group->{role}};
+        $accreditation = $c->config->{'products_' . $group->{role}};
 
         if ( $group->{role} eq 'member' ) {
             $args->{email} = $claims->{preferred_username}
@@ -90,16 +94,18 @@ sub create ($c ) {
     PRODUCT:
     foreach my $product ( @{ $args->{products} } ) {
 
-        next PRODUCT if $product->{id} =~ SUBSIDY_PRODUCTS && ! $product->{price};
+        $product->{id} = $c->mapped_product_id($product->{id});
+
+        next PRODUCT if $pt->{subsidy}{$product->{id}} && ! $product->{price};
 
-        if ( $product->{id} =~ MAIN_PRODUCTS ) {
+        if ( $pt->{main}{ $product->{id} } ) {
             next PRODUCT if $accredited++; # neumoznit dvoji akreditace
 
             if ( $accreditation ) {
                 $product->{id} = $accreditation;
             }
-            elsif ($product->{id} =~ NONFREE_PRODUCTS) {
-                $product->{id} = FALLBACK_PRODUCT;
+            elsif ( $pt->{nonfree}{ $product->{id} } ) {
+                $product->{id} = $cfg->{products_fallback};
             }
         }
 
@@ -108,7 +114,7 @@ sub create ($c ) {
     }
 
     # fallback akreditace
-    @products = ( { id => FALLBACK_PRODUCT}, @products ) if ! $accredited;
+    @products = ( { id => $cfg->{products_fallback} }, @products ) if ! $accredited;
 
     my $order = {
         email            => $args->{email},
@@ -136,7 +142,7 @@ sub create ($c ) {
     foreach my $product ( @products ) {
 
         my $item = {
-            item          => $c->mapped_product_id($product->{id}),
+            item          => $product->{id},
             variation     => $c->mapped_variation_id($product->{variation}),
             attendee_name => $args->{name},
             price         => $product->{price},
diff --git a/lib/CF2022/Controller/Products.pm b/lib/CF2022/Controller/Products.pm
index 644303c..88f46e8 100644
--- a/lib/CF2022/Controller/Products.pm
+++ b/lib/CF2022/Controller/Products.pm
@@ -5,22 +5,56 @@ use Mojo::UserAgent;
 sub list {
     my $c = shift->openapi->valid_input or return;
 
-    my $url = sprintf ('%s/organizers/%s/events/%s/items/',
+    my $ua = Mojo::UserAgent->new;
+
+    my $products = $ua->get( sprintf ('%s/organizers/%s/events/%s/items/',
         $c->config->{pretix_api},
         $c->config->{pretix_organizer},
         $c->config->{pretix_event},
-    );
-
-    my $ua = Mojo::UserAgent->new;
+    ), {
+        Authorization => 'Token ' . $c->config->{pretix_token}
+    } )->result->json;
 
-    my $records = $ua->get( $url, {
+    my $quotas = $ua->get( sprintf ('%s/organizers/%s/events/%s/quotas/',
+        $c->config->{pretix_api},
+        $c->config->{pretix_organizer},
+        $c->config->{pretix_event},
+    ), {
         Authorization => 'Token ' . $c->config->{pretix_token}
     } )->result->json;
 
+    my $q = {};
+
+    QUOTE:
+    foreach my $record ( @{ $quotas->{results} } ) {
+        next if ! $record->{size};
+
+        my $quota = $ua->get( sprintf ('%s/organizers/%s/events/%s/quotas/%d/availability/',
+            $c->config->{pretix_api},
+            $c->config->{pretix_organizer},
+            $c->config->{pretix_event},
+            $record->{id},
+        ), {
+            Authorization => 'Token ' . $c->config->{pretix_token}
+        } )->result->json;
+
+        ITEM:
+        foreach my $item_id ( @{ $record->{items} } ) {
+            $q->{$item_id}{total} += $quota->{total_size};
+            $q->{$item_id}{available} += $quota->{available_number};
+            foreach my $variation_id ( @{ $record->{variations} } ) {
+                $q->{$item_id}{$variation_id}{total} += $quota->{total_size};
+                $q->{$item_id}{$variation_id}{available} += $quota->{available_number};
+            }
+        }
+    }
+
+$c->trace($q);
+
     my @options = ();
 
     RECORD:
-    foreach my $record ( @{ $records->{results} } ) {
+    foreach my $record ( @{ $products->{results} } ) {
         next RECORD if ! $record->{active};
 
         push @options, {
@@ -29,11 +63,13 @@ sub list {
             price       => $record->{default_price} + 0,
             free_price  => $record->{free_price},
             category_id => $record->{category},
+            quotes      => $q->{$record->{id}},
             variations  => [
                 map {{
-                    id    => $_->{id},
-                    value => $_->{value}{en},
-                    price => $_->{price} + 0
+                    id     => $_->{id},
+                    value  => $_->{value}{en},
+                    price  => $_->{price} + 0,
+                    quotes => $q->{$record->{id}}{$_->{id} },
                 }} @{ $record->{variations} }
             ],
         };
-- 
GitLab