From 538c898e06cb1c3b183ab0f649d421283be5a4e0 Mon Sep 17 00:00:00 2001 From: Sunandar Date: Fri, 3 Feb 2017 17:56:27 +0630 Subject: [PATCH] add new api --- .../api/batch_line_items_controller.rb | 113 ++++++++++-------- app/controllers/api/batches_controller.rb | 3 +- .../batch_line_items_controller.rb | 2 +- app/controllers/users_controller.rb | 10 +- app/models/batch.rb | 3 +- app/models/batch_line_item.rb | 7 +- app/views/layouts/_navigation.html.erb | 3 + config/routes.rb | 4 + db/migrate/20170119083734_create_batches.rb | 1 + .../20170121053223_create_batch_line_items.rb | 12 +- db/migrate/20170121083753_create_user.rb | 3 +- db/schema.rb | 31 +++-- 12 files changed, 117 insertions(+), 75 deletions(-) diff --git a/app/controllers/api/batch_line_items_controller.rb b/app/controllers/api/batch_line_items_controller.rb index c62c835..6ca977f 100644 --- a/app/controllers/api/batch_line_items_controller.rb +++ b/app/controllers/api/batch_line_items_controller.rb @@ -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 \ No newline at end of file diff --git a/app/controllers/api/batches_controller.rb b/app/controllers/api/batches_controller.rb index 782f31b..6487039 100644 --- a/app/controllers/api/batches_controller.rb +++ b/app/controllers/api/batches_controller.rb @@ -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 diff --git a/app/controllers/batch_line_items_controller.rb b/app/controllers/batch_line_items_controller.rb index 7033c6d..471084b 100644 --- a/app/controllers/batch_line_items_controller.rb +++ b/app/controllers/batch_line_items_controller.rb @@ -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] diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index bd5958e..1f20112 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/models/batch.rb b/app/models/batch.rb index 1616711..a4fed70 100644 --- a/app/models/batch.rb +++ b/app/models/batch.rb @@ -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 diff --git a/app/models/batch_line_item.rb b/app/models/batch_line_item.rb index 6a5500b..199afe2 100644 --- a/app/models/batch_line_item.rb +++ b/app/models/batch_line_item.rb @@ -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) diff --git a/app/views/layouts/_navigation.html.erb b/app/views/layouts/_navigation.html.erb index 0227204..0a3dada 100644 --- a/app/views/layouts/_navigation.html.erb +++ b/app/views/layouts/_navigation.html.erb @@ -17,6 +17,9 @@ +