From 3998e6e82dede32a9157e8e1e51105aae4da90e5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Mon, 15 Nov 2021 00:47:54 +0100
Subject: [PATCH] nahravani fotek pres piratar

---
 .gitlab-ci.yml                  |  2 +-
 lib/CF2022/Controller/Orders.pm | 76 +++++++++++++++++++++++++--------
 2 files changed, 60 insertions(+), 18 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 64e8cd7..048092c 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.8.0
+  IMAGE_VER: 0.9.0
 
 services:
   - docker:20.10.9-dind
diff --git a/lib/CF2022/Controller/Orders.pm b/lib/CF2022/Controller/Orders.pm
index 477fd38..b7fb11c 100644
--- a/lib/CF2022/Controller/Orders.pm
+++ b/lib/CF2022/Controller/Orders.pm
@@ -1,10 +1,11 @@
 package CF2022::Controller::Orders;
 use Mojo::Base 'Mojolicious::Controller';
 use Mojo::UserAgent;
+use Mojo::Asset::File;
 
 use constant MAIN_PRODUCTS        => qr/^(1|2|3)$/; # typy akreditace
 use constant NONFREE_PRODUCTS     => qr/^(1)$/;     # akreditace vyzadujici token
-use constant SUBSIDY_PRUDUCTS     => qr/^(58)$/;    # prispevkove polozky
+use constant SUBSIDY_PRODUCTS     => qr/^(58)$/;    # prispevkove polozky
 use constant FALLBACK_PRODUCT     => 2;             # fallback akreditace
 
 use feature 'signatures';
@@ -66,12 +67,26 @@ sub create ($c ) {
         }
     }
 
-    # korekce akreditace
+    # kontrola duplicity
+    my $exists = $c->schema->resultset('Order')->search({
+        deleted => undef,
+        email   => $args->{email},
+        api     => $url,
+    })->count;
+
+    $exists ||= $c->schema->resultset('Order')->search({
+        deleted  => undef,
+        sso_uuid => $claims->{sub},
+        api      => $url,
+    })->count if $claims;
 
+    return $c->error(400, 'Duplicity' ) if $exists;
+
+    # korekce akreditace
     PRODUCT:
     foreach my $product ( @{ $args->{products} } ) {
 
-        next PRODUCT if $product->{id} =~ SUBSIDY_PRUDUCTS && ! $product->{price};
+        next PRODUCT if $product->{id} =~ SUBSIDY_PRODUCTS && ! $product->{price};
 
         if ( $product->{id} =~ MAIN_PRODUCTS ) {
             next PRODUCT if $accredited++; # neumoznit dvoji akreditace
@@ -100,6 +115,19 @@ sub create ($c ) {
         fees             => [],
     };
 
+    # nahrani fotky
+    if ( $group->{role} eq 'member' ) {
+        my $photo_id = $c->_upload_photo($claims->{preferred_username});
+        if ( $photo_id ) {
+            push @answers, (
+                {
+                    question => 28,
+                    answer   => $photo_id,
+                },
+            );
+        }
+    }
+
     PRODUCT:
     foreach my $product ( @products ) {
 
@@ -116,22 +144,8 @@ sub create ($c ) {
         push @{ $order->{positions} }, $item;
     }
 
-    # kontrola duplicity
-
-    my $exists = $c->schema->resultset('Order')->search({
-        email => $order->{email},
-        api   => $url,
-    })->count;
-
-    $exists ||= $c->schema->resultset('Order')->search({
-        sso_uuid => $claims->{sub},
-        api      => $url,
-    })->count if $claims;
-
-    return $c->error(400, 'Duplicity' ) if $exists;
 
     # odeslani do pretixu
-
     my $ua = Mojo::UserAgent->new;
     my $rc = $ua->post( $url,
         { Authorization => 'Token ' . $c->config->{pretix_token} },
@@ -206,4 +220,32 @@ sub get ($c ) {
     );
 }
 
+sub _upload_photo ($c, $username ) {
+
+    my $ua = Mojo::UserAgent->new;
+    my $rc = $ua->get(
+        $c->config->{piratar_url} . $username . '.jpg'
+    )->result;
+
+    return if ! $rc->is_success;
+
+    my $photo = "/tmp/$username.jpg";
+    $rc->save_to($photo);
+
+    my $tx = $ua->build_tx(POST => $c->config->{pretix_api} . '/upload', {
+        Authorization         => 'Token ' . $c->config->{pretix_token},
+        'Content-type'        => 'image/jpeg',
+        'Content-Disposition' => qq{attachment; filename="$username.jpg"},
+    });
+
+    $tx->req->content->asset(Mojo::Asset::File->new(path => $photo));
+
+    $rc = $ua->start($tx)->result;
+
+    return if ! $rc->is_success;
+
+    return $rc->json->{id};
+
+}
+
 1;
-- 
GitLab