#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package RPS::DB::Item::Session;
use strict;
use warnings;
use lib '/app/tools/common/lib';
use Common::Assert;
use Common::DB::Item;
use Common::DB::ItemCollection;

use base 'Common::DB::Item';

use lib '/app/tools/appuser/lib';
use AppUser::DB::Item::UserTerms;

use constant kTable => 'session';
use constant kDB    => Common::DB::Item::kCommonDB();

sub RemoveOldSessions {
    my ( $class, $masterUserID ) = @_;
    assert($masterUserID);

    my $sql = "DELETE FROM " . kTable . " WHERE master_user_id=$masterUserID";
    my $dbo = Common::RSApp::GetCommonDB();
    $dbo->DoCmd($sql);
}

sub CheckTerms {
    my ( $class, $sessionID ) = @_;

    my $sql     = "SELECT terms_version, client_id FROM " . kTable . " WHERE session_id=$sessionID";
    my $dbo     = Common::RSApp::GetCommonDB();
    my $sth     = $dbo->DoCmd($sql);
    my ($terms, $clientID) = $sth->fetchrow_array();

    # Users can log in as long as they have agreed to the first version of the terms.
    # Also, if they are logging into the portal, ignore terms.
    if ( $terms >= 1 || $clientID == 998 ) {
        return 1;
    } else {
        return undef;
    }
}

sub UpdateTerms {
    my ( $class, $sessionID ) = @_;

    my $sql = "UPDATE " . kTable . " SET terms_version=" . AppUser::DB::Item::UserTerms::TERMS_VERSION . " WHERE session_id=$sessionID";
    my $dbo = Common::RSApp::GetCommonDB();
    $dbo->DoCmd($sql);
}

###
1;    #
###
