Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
MPESA_CONSUMER_KEY: '8sPrVuOjH01sH5FUzGGaxhv5CdjHmxzA'
MPESA_CONSUMER_SECRET: 'm3wfaKluCOrD2B92'
MPESA_PASSKEY: 'bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919'
MPESA_SHORTCODE: '174379'
MPESA_INITIATOR_NAME: 'testapi'
MPESA_INITIATOR_PASSWORD: 'MTc0Mzc5YmZiMjc5ZjlhYTliZGJjZjE1OGU5N2RkNzFhNDY3Y2QyZTBjODkzMDU5YjEwZjc4ZTZiNzJhZGExZWQyYzkxOTIwMjMwMTEyMTIxMjAy'
CALLBACK_URL: 'https://84a0-197-232-128-93.in.ngrok.io'
REGISTER_URL: "https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl"
10 changes: 4 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.1.1"
ruby "3.0.0"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.4"
Expand All @@ -12,16 +12,15 @@ gem "sprockets-rails"
# Use sqlite3 as the database for Active Record
gem "sqlite3", "~> 1.4"

gem "pg", "~> 1.2"

# Use the Puma web server [https://github.com/puma/puma]
gem "puma", "~> 5.0"

# Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem "jbuilder"


# Use Redis adapter to run Action Cable in production
# gem "redis", "~> 4.0"

# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]
# gem "kredis"

Expand All @@ -40,6 +39,7 @@ gem "bootsnap", require: false
# Use jwt and Bcrypt to authenticate users
gem "bcrypt", "~> 3.1.7"
gem "jwt", "~> 2.2.3"
gem "rest-client", "~> 2.1.0"

# cors
gem "rack-cors", "~> 1.1.1"
Expand All @@ -61,8 +61,6 @@ group :development do

# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"

gem "bcrypt", "~> 3.1.7"
gem "active_model_serializers", "~> 0.10.12"
end

Expand Down
20 changes: 20 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,14 @@ GEM
debug (1.7.1)
irb (>= 1.5.0)
reline (>= 0.3.1)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
erubi (1.12.0)
globalid (1.0.0)
activesupport (>= 5.0)
http-accept (1.7.0)
http-cookie (1.0.5)
domain_name (~> 0.5)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
io-console (0.6.0)
Expand All @@ -120,6 +125,9 @@ GEM
marcel (1.0.2)
matrix (0.4.2)
method_source (1.0.0)
mime-types (3.4.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2022.0105)
mini_mime (1.1.2)
minitest (5.17.0)
msgpack (1.6.0)
Expand All @@ -132,9 +140,11 @@ GEM
timeout
net-smtp (0.3.3)
net-protocol
netrc (0.11.0)
nio4r (2.5.8)
nokogiri (1.14.0-x86_64-linux)
racc (~> 1.4)
pg (1.4.5)
prettier_print (1.2.0)
public_suffix (5.0.1)
puma (5.6.5)
Expand Down Expand Up @@ -175,6 +185,11 @@ GEM
regexp_parser (2.6.1)
reline (0.3.2)
io-console (~> 0.5)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
rexml (3.2.5)
ruby-lsp (0.3.8)
language_server-protocol (~> 3.17.0)
Expand All @@ -200,6 +215,9 @@ GEM
timeout (0.3.1)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
unf (0.1.4)
unf_ext
unf_ext (0.0.8.2)
web-console (4.2.0)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
Expand Down Expand Up @@ -228,9 +246,11 @@ DEPENDENCIES
debug
jbuilder
jwt (~> 2.2.3)
pg (~> 1.2)
puma (~> 5.0)
rack-cors (~> 1.1.1)
rails (~> 7.0.4)
rest-client (~> 2.1.0)
ruby-lsp (~> 0.3.7)
selenium-webdriver
sprockets-rails
Expand Down
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: rake db:migrate && bin/rails server -b 0.0.0.0 -p ${PORT:-3000}
25 changes: 14 additions & 11 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
class ApplicationController < ActionController::Base
# skip_before_action :verify_authenticity_token
# skip_before_action :verify_authenticity_token

# include JWToken
# include JWToken

# def encode_token(payload)
# JWToken.encode(payload, 'my_s3cr3t')
# end
# def encode_token(payload)
# JWToken.encode(payload, 'my_s3cr3t')
# end

# skip_before_action :verify_authenticity_token
# skip_before_action :verify_authenticity_token

rescue_from ActiveRecord::RecordInvalid,
with: :render_unprocessable_entity_response

rescue_from ActiveRecord::RecordInvalid, with: :render_unprocessable_entity_response

def render_unprocessable_entity_response(exception)
render json: {errors: exception.record.errors.full_messages}, status: :unprocessable_entity
end
def render_unprocessable_entity_response(exception)
render json: {
errors: exception.record.errors.full_messages
},
status: :unprocessable_entity
end
end
11 changes: 10 additions & 1 deletion app/controllers/authentication_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ class AuthenticationController < ApplicationController
def login
user = User.find_by(phonenumber: params[:phonenumber])
if user && user.authenticate(params[:password_digest])
render json: {user: user, token: encode_token({user_id: user.id})}
render json: {user: user}
else
render json: {error: "Invalid username or password"}, status: :unauthorized
end
end

def admin
user = User.find_by(phonenumber: params[:phonenumber])
if user && user.authenticate(params[:password_digest]) && user.isAdmin
render json: {user: user}
else
render json: {error: "Invalid username or password"}, status: :unauthorized
end
Expand Down
38 changes: 35 additions & 3 deletions app/controllers/bookings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,33 @@ def index
end

def create
booking = Booking.create(booking_params)
render json: booking
from = Station.find_by(station_name: booking_params[:from]).fare
to = Station.find_by(station_name: booking_params[:to]).fare

#find absolute difference
diff = (from - to).abs

#find time now + diff*10 minutes
time = Time.now + diff * 10 * 60



new_booking = {
bus_id: booking_params[:bus_id],
user_id: params[:user_id],
from_id: Station.find_by(station_name: booking_params[:from]).id,
to_id: Station.find_by(station_name: booking_params[:to]).id,
fare: diff * 10,
isActive: false,
}

booking = Booking.create(new_booking)
return render json: {
id: booking.id,
bus_name: booking.bus.bus_name,
fare: booking.fare,
time: time
}
end

def update
Expand All @@ -30,8 +55,15 @@ def destroy

def pay
booking = find_booking

phonenumber = booking.user.phonenumber

fare = booking.fare

booking.bus.update(passengers: booking.bus.passengers + 1)
booking.update(isActive: true)

render json: {booking: booking, phonenumber: phonenumber, fare: fare}
#call stk push

#if stk push success
Expand All @@ -47,6 +79,6 @@ def find_booking
end

def booking_params
params.require(:booking).permit(:user_id, :bus_id, :from_id, :to_id, :fare)
params.permit(:from,:to,:bus_id)
end
end
12 changes: 10 additions & 2 deletions app/controllers/buses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ def available
)

#select buses where seats are greater than passengers
buses = buses.select { |bus| bus.seater > bus.passengers }
to_send_buses = []
buses.each do |bus|
puts bus.seater > bus.passengers

if bus.seater > bus.passengers
to_send_buses.push(bus)
end
end


fare_from = Station.find_by(station_name: params_for_available[:from]).fare
fare_to = Station.find_by(station_name: params_for_available[:to]).fare
Expand All @@ -28,7 +36,7 @@ def available
#find time now + diff*10 minutes
time = Time.now + diff * 10 * 60

render json: { buses: buses, time: time , fare: diff*10 }
render json: to_send_buses
end

def show
Expand Down
Loading