Skip to content

Commit

Permalink
Merge branch 'main' into HER-23-add-teacher-to-roles-in-user
Browse files Browse the repository at this point in the history
  • Loading branch information
mhope21 authored Dec 5, 2024
2 parents 4d24b39 + e5ea3dc commit 422f755
Show file tree
Hide file tree
Showing 22 changed files with 4,575 additions and 200 deletions.
4 changes: 2 additions & 2 deletions app/controllers/api/v1/admin_dashboard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ class Api::V1::AdminDashboardController < ApplicationController
# GET /api/v1/admin_dashboard
def index
users_count = User.count
kit_requests_count = KitRequest.count
orders_count = Order.count
total_donations = Donation.sum(:amount)
if current_user.role === "admin"
render json: {
users_count: users_count,
kit_requests_count: kit_requests_count,
orders_count: orders_count,
total_donations: total_donations
}, status: :ok
else
Expand Down
46 changes: 0 additions & 46 deletions app/controllers/api/v1/kit_requests_controller.rb

This file was deleted.

46 changes: 46 additions & 0 deletions app/controllers/api/v1/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Api::V1::OrdersController < ApplicationController
load_and_authorize_resource

# GET /api/v1/orders
def index
render json: @orders, status: :ok
end

# POST /api/v1/orders
def create
@order.user = current_user # Automatically associate user

if @order.save
render json: @order, status: :created
else
render json: { errors: @order.errors.full_messages }, status: :unprocessable_entity
end
end

# GET /api/v1/orders/:id
def show
render json: @order, status: :ok
end

# PATCH/PUT /api/v1/orders/:id
def update
if @order.update(order_params)
render json: @order, status: :ok
else
render json: { errors: @order.errors.full_messages }, status: :unprocessable_entity
end
end

# DELETE /api/v1/orders/:id
def destroy
@order.destroy
render json: { message: "Order successfully deleted." }, status: :no_content
end


private

def order_params
params.require(:order).permit(:phone, :school_name, :school_address, :school_year, :kit_id, :comments)
end
end
8 changes: 4 additions & 4 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ def initialize(user)
can :destroy, User
can :manage, KitItem
can :create, KitItem
can :manage, KitRequest
can :manage, Order
can :manage, Donation
else
# Allow users to update their own profile
can :update, User, id: user.id
can :read, KitRequest, user_id: user.id # Users can read their own kit requests
can :create, KitRequest # Users can create new kit requests
can [ :update ], KitRequest, user_id: user.id # Users can update their own kit requests
can :read, Order, user_id: user.id # Users can read their own kit requests
can :create, Order # Users can create new kit requests
can [ :update ], Order, user_id: user.id # Users can update their own kit requests
can :read, Kit
can :read, KitItem
can :create, Donation
Expand Down
2 changes: 1 addition & 1 deletion app/models/kit_request.rb → app/models/order.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class KitRequest < ApplicationRecord
class Order < ApplicationRecord
belongs_to :kit
belongs_to :user

Expand Down
3 changes: 2 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class User < ApplicationRecord

# Defines available roles and sets default role as user.
ROLES = %w[admin user teacher].freeze
has_many :kit_requests

has_many :orders
has_many :donations, -> { where(canceled: false) }
has_many :contacts

Expand Down
16 changes: 0 additions & 16 deletions app/serializers/kit_request_serializer.rb

This file was deleted.

16 changes: 16 additions & 0 deletions app/serializers/order_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class OrderSerializer < ActiveModel::Serializer
# Defines how to display the Order model
attributes :id, :order_name, :order_email, :ordered_kit, :kit_id, :user_id, :school_name, :school_address, :school_year, :comments, :phone, :created_at

def order_name
object.user.name if object.user.present?
end

def ordered_kit
object.kit.name if object.kit.present?
end

def order_email
object.user.email if object.user.present?
end
end
7 changes: 6 additions & 1 deletion config/credentials.yml.enc
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
lb3sm1G2uY1hyRbmz1v+gB16QKPzLMoVLVEUgEDuGVNtQVWu3h3PND2FpMi2xza+ffh5FhR2Awv5f9w5Ke1nAubo+PAaXV8XNebRM9Gi65/lNHGKfaBn6WvZwZn/rJPynp05mlU/+QsGLTZjJ1Ed+1rE/C6bYZ3qdKmiqHTXrmA0pTS4H3k5PAaJUkWZL9Yq0TKyo/v1iHQQtxDsqyxsNjXZZpFWcvmUPHLxNXY/iZqFP2+ApP1ot+2isL5VO5rHbaeJODXmhg1NvbO73UJRECok/EIKiaIKMQPM7PEJ4Q6cXOYnsHbamyszcdDlzeM8+7uWSONk0SUHbMvp7o6iqQ1hXEtRYPW7kpuwE7IUjbe2vstOHQ/ADvHwMEiQyTPAWGJroGesmJaMx065FI2a7isssMKv--JLpaTvFQzsCEDEYh--0AmLOnu/mo4CfzKtkig9yQ==
# aws:
# access_key_id: 123
# secret_access_key: 345

# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.
secret_key_base: 3373452bfea693dbb70c378706f87e31819a445d845d7d967477c389936eb07769620b88e80092374c89cd3290685710f876f469f58afe49b1ff54f983a4b070
1 change: 1 addition & 0 deletions config/credentials.yml.enc.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lb3sm1G2uY1hyRbmz1v+gB16QKPzLMoVLVEUgEDuGVNtQVWu3h3PND2FpMi2xza+ffh5FhR2Awv5f9w5Ke1nAubo+PAaXV8XNebRM9Gi65/lNHGKfaBn6WvZwZn/rJPynp05mlU/+QsGLTZjJ1Ed+1rE/C6bYZ3qdKmiqHTXrmA0pTS4H3k5PAaJUkWZL9Yq0TKyo/v1iHQQtxDsqyxsNjXZZpFWcvmUPHLxNXY/iZqFP2+ApP1ot+2isL5VO5rHbaeJODXmhg1NvbO73UJRECok/EIKiaIKMQPM7PEJ4Q6cXOYnsHbamyszcdDlzeM8+7uWSONk0SUHbMvp7o6iqQ1hXEtRYPW7kpuwE7IUjbe2vstOHQ/ADvHwMEiQyTPAWGJroGesmJaMx065FI2a7isssMKv--JLpaTvFQzsCEDEYh--0AmLOnu/mo4CfzKtkig9yQ==
1 change: 1 addition & 0 deletions config/master.key.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9682574f0b7086363f4e838d4509b74d
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
resources :users
resources :donations
resources :contacts
resources :kit_requests, only: [ :index, :create, :show, :update, :destroy ] do
resources :orders, only: [ :index, :create, :show, :update, :destroy ] do
collection do
get "current", to: "kit_requests#current"
get "current", to: "orders#current"
end
end
resources :kits do
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20241122015154_rename_kit_requests_to_orders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameKitRequestsToOrders < ActiveRecord::Migration[7.2]
def change
rename_table :kit_requests, :orders
end
end
30 changes: 15 additions & 15 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
require 'faker'

# Clear tables
KitRequest.destroy_all
Order.destroy_all
KitItem.destroy_all
Kit.destroy_all

Expand Down Expand Up @@ -292,9 +292,9 @@
perspectives_kit.kit_items << goldfish_boy_book << sevens_book << frankie_book << earth_blue_book << same_book << awesome_guide_book << mockingbird_book << classroom_book << neurotribes_book
impact_kit.kit_items << curious_dog_book << rosie_book << different_book << classroom_book << neurotribes_book

# Seeding KitRequests
# Seeding Orders

KitRequest.create([
Order.create([
{ school_year: '2024-2025', kit: discovery_kit },
{ school_year: '2024-2025', kit: empowerment_kit }
])
Loading

0 comments on commit 422f755

Please sign in to comment.