add new api

This commit is contained in:
Sunandar
2017-02-03 17:56:27 +06:30
parent b4c9e8cc8e
commit 538c898e06
12 changed files with 117 additions and 75 deletions

View File

@@ -1,67 +1,82 @@
class Api::BatchLineItemsController < ApplicationController
skip_before_filter :verify_authenticity_token
def register
batch_id=params[:batch_id]
batch_id=params[:batch_id]
session_token=params[:session_token]
manufacture_uid = params[:card_manufacture]
user_id=""
manufacture_uid = params[:card_manufacture]
client_id=""
check_member= Member.authenticate_session_token(session_token)
if !check_member.nil?
if !check_member.nil?
encrypt_key=""
find_user=User.find_by_id(check_member.user_id)
if !find_user.nil?
encrypt_key=find_user.secrect_key
user_id=find_user.id
end
wristband_code=""
if !encrypt_key.nil?
check_manufacture = BatchLineItem.find_by_manufacture_uid(manufacture_uid)
if check_manufacture.nil?
serial_no=BatchLineItem.generate_serial_no(user_id.to_s)
str="manufacture_uid="+manufacture_uid.to_s+"&serial_no="+serial_no.to_s
digest_data= Digest::MD5.hexdigest(str)
hex_data=digest_data.hex
hex_str=hex_data.to_s
wristband_code=hex_str[0..15]
check_wirstband=BatchLineItem.find_by_wristband_code(wristband_code)
if check_wirstband.nil?
puts 'Check Wristband'
card_type=""
find_batch= Batch.find_by_id(batch_id)
if !find_batch.nil?
card_type=find_batch.adult_or_child.upcase
end
batchLineItem=BatchLineItem.new
batchLineItem.wristband_code=wristband_code
batchLineItem.serial_no=serial_no
batchLineItem.batch_id=batch_id
batchLineItem.manufacture_uid = manufacture_uid
batchLineItem.card_type = card_type
if batchLineItem.save
lookup=Lookup.find_by_name('generate_serial_no')
max_serail_no=lookup.max_value
lookup.max_value=max_serail_no.to_i+1
lookup.save
find_batch = Batch.find_by_id(batch_id)
if !find_batch.nil?
card_type=find_batch.adult_or_child.upcase
find_user=Client.find_by_id(find_batch.client_id)
if !find_user.nil?
encrypt_key=find_user.secrect_key
product_type=find_user.product_type
client_id=find_user.id
@out = true,batchLineItem.serial_no,batchLineItem.wristband_code
if !encrypt_key.nil?
check_manufacture = BatchLineItem.find_by_manufacture_uid(manufacture_uid)
if check_manufacture.nil?
serial_no=BatchLineItem.generate_serial_no(client_id.to_s)
if product_type.downcase=="ticket"
str="manufacture_uid="+manufacture_uid.to_s+"&serial_no="+serial_no.to_s
digest_data= Digest::MD5.hexdigest(str)
hex_data=digest_data.hex
hex_str=hex_data.to_s
wristband_code=hex_str[0..15]
check_wirstband=BatchLineItem.find_by_asset_identity(wristband_code)
if check_wirstband.nil?
@out=create_product(wristband_code,serial_no,batch_id,manufacture_uid,card_type)
else
@out=false,'Wristband is already registered!'
end
elsif product_type=="card"
@out=create_product(wristband_code,serial_no,batch_id,manufacture_uid,card_type)
end
else
@out=false,'Error occurs in registration encoder!'
end
@out=false,'Wristband is already registered!'
end
else
@out=false,'Wristband is already registered!'
@out=false,'Invalid User!'
end
else
@out=false,'Wristband is already registered!'
@out=false,'Client does not exist!'
end
else
@out=false,'Invalid User!'
@out=false,'Batch does not exist!'
end
else
else
@out=false,'Sorry!Unauthorized user!'
end
end
end
end
def create_product(wristband_code,serial_no,batch_id,manufacture_uid,card_type)
batchLineItem=BatchLineItem.new
batchLineItem.asset_identity=wristband_code
batchLineItem.serial_no=serial_no
batchLineItem.batch_id=batch_id
batchLineItem.manufacture_uid = manufacture_uid
batchLineItem.asset_type = card_type
batchLineItem.encoded_at = DateTime.now.beginning_of_day.utc.to_time.strftime("%Y-%m-%d")
batchLineItem.verified_at =DateTime.now.beginning_of_day.utc.to_time.strftime("%Y-%m-%d")
if batchLineItem.save
lookup=Lookup.find_by_name('generate_serial_no')
max_serail_no=lookup.max_value
lookup.max_value=max_serail_no.to_i+1
lookup.save
@result = true,batchLineItem.serial_no,batchLineItem.asset_identity
else
@result=false,'Error occurs in registration encoder!'
end
end
end

View File

@@ -9,9 +9,10 @@ class Api::BatchesController < ApplicationController
order_ref = params[:order_ref]
card_qty = params[:card_qty]
adult_or_child=params[:adult_or_child]
client_id=params[:client_id]
user_id=check_member.user_id
batch = Batch.create_batch(created_by,date,order_ref,card_qty,user_id,adult_or_child)
batch = Batch.create_batch(created_by,date,order_ref,card_qty,user_id,adult_or_child,client_id)
if !batch.nil?
@out=true,batch.id
else

View File

@@ -8,7 +8,7 @@ class BatchLineItemsController < ApplicationController
@batches=Batch.where('user_id=?',user_id)
@batchLineItems=BatchLineItem.joins('inner join batches on batches.id=batch_line_items.batch_id')
.where('batches.user_id=?',user_id)
.select('batch_line_items.*,batches.order_ref as batch_name').page(params[:page]).per(2)
.select('batch_line_items.*,batches.order_ref as batch_name').page(params[:page])
end
def export
batch_list=params[:batch]

View File

@@ -29,10 +29,10 @@ class UsersController < ApplicationController
@user = User.new(user_params)
email=params[:registered_email]
@user.is_active=true
cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
key=cipher.random_key
secrect_key= Base64.encode64(key)
@user.secrect_key=secrect_key
# cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
# key=cipher.random_key
# secrect_key= Base64.encode64(key)
# @user.secrect_key=secrect_key
@email=email
respond_to do |format|
if @user.save
@@ -100,6 +100,6 @@ class UsersController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:name, :nrc, :email, :phone, :address,:is_active,:secrect_key)
params.require(:user).permit(:name, :nrc, :email, :phone, :address,:is_active)
end
end

View File

@@ -1,7 +1,7 @@
class Batch < ApplicationRecord
has_many :batch_line_item
def self.create_batch(created_by,date,order_ref, card_qty,user_id,adult_or_child)
def self.create_batch(created_by,date,order_ref, card_qty,user_id,adult_or_child,client_id)
batch = Batch.new
batch.created_by = created_by
batch.order_ref = order_ref
@@ -13,6 +13,7 @@ class Batch < ApplicationRecord
batch.qty_fail = 0
batch.user_id=user_id
batch.export_count=0
batch.client_id=client_id
batch.adult_or_child=adult_or_child.upcase
if batch.save
return batch

View File

@@ -20,7 +20,7 @@ class BatchLineItem < ApplicationRecord
return account_no
end
def self.generate_serial_no(user_id)
def self.generate_serial_no(client_id)
find_lookup=Lookup.find_by_name('generate_serial_no')
if !find_lookup.nil?
max_value=find_lookup.max_value
@@ -34,10 +34,11 @@ class BatchLineItem < ApplicationRecord
ends=sufix_len-value_len-1
sufix_str= sufix_str[start..ends]
prefix=prefix[start..2]
prefix_str=prefix.to_s + user_id.to_s
prefix_str=prefix.to_s + client_id.to_s
serial_no=prefix_str.to_s+sufix_str+max_value.to_s
return serial_no
return serial_no
end
end
def self.to_csv(user_id)

View File

@@ -17,6 +17,9 @@
<li class="nav-item">
<%= link_to "User",users_path, :class => "nav-link" %>
</li>
<li class="nav-item">
<%= link_to "Client",clients_path, :class => "nav-link" %>
</li>
</ul>
<div class="float-xs-right">
<ul class="nav navbar-nav right">