BUGFIX: abstract shouldn't die for registrations without work
[angular-drzb] / angular-server.pl
index 29db13a..80d2392 100755 (executable)
@@ -6,7 +6,8 @@ use Time::HiRes;
 use Clone qw(clone);
 use Mojo::UserAgent;
 
-our $VERSION = '0.3';
+our $VERSION = `git describe`;
+chomp $VERSION;
 
 sub new_uuid { Time::HiRes::time * 100000 }
 
@@ -68,6 +69,16 @@ helper locale => sub {
        return $locale{ $lang } || "MISSING $lang $_[1]";
 };
 
+helper ip => sub {
+       my $self = shift;
+       return
+               $self->req->headers->header('X-Forwarded-For')
+               || $self->req->headers->header('X-Real-IP')
+               || $self->tx->{remote_address}
+       ;
+};
+
+
 get '/js/services.js' => sub {
        my $self = shift;
        $self->stash( VERSION => $VERSION );
@@ -174,6 +185,12 @@ any [ 'post' ] => '/data/:database/:entity' => sub {
                $json->{entity} = $entity;
                warn "NEW $id\n";
        }
+
+       $json->{x_audit} = {
+               t  => Time::HiRes::time,
+               ip => $self->ip(),
+       };
+
        warn "## $database $entity $id body ",dump($self->req->body, $json);
 
        my $new = _couchdb_put "/$database/$entity.$id" => $json;
@@ -213,10 +230,12 @@ get '/:database/_design/:design/_view/:view' => sub {
                                organizations => {
                                        map    => q| function(doc) {
 if ( doc.user.organization != '' ) {
-  emit(doc.user.organization, 1);
+  if ( doc.user.organization )
+    emit(doc.user.organization, 1);
   if ( doc.user.persons ) {
     doc.user.persons.forEach( function(person) {
-      emit(person.organization, 1);
+      if ( person.organization )
+        emit(person.organization, 1);
     });
   }
 }
@@ -247,13 +266,13 @@ hook after_dispatch => sub {
 
        return if $self->res->headers->header('ETag');
 
-       my $our_etag = Mojo::ByteStream->new($body)->md5_sum;
+       my $our_etag = Mojo::ByteStream->new($body . $VERSION)->md5_sum;
        $self->res->headers->header('ETag' => $our_etag);
 
        my $browser_etag = $self->req->headers->header('If-None-Match');
        return unless $browser_etag && $browser_etag eq $our_etag;
 
-       $self->app->log->info("HTTP cache hit ", dump( $self->req->url->to_string ), $our_etag );
+       $self->app->log->debug("HTTP cache hit " . $self->req->url->to_string . " $our_etag" );
 
        $self->res->code(304);
        $self->res->body('');