-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from adrianrbp/6-shift-management-api
6-shift management api
- Loading branch information
Showing
54 changed files
with
1,205 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
class ApplicationController < ActionController::API | ||
include JsonResponse | ||
end |
9 changes: 9 additions & 0 deletions
9
backend/app/controllers/company_services/engineers_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module CompanyServices | ||
class EngineersController < ApplicationController | ||
def index | ||
company_service = CompanyService.find(params[:company_service_id]) | ||
week = params[:week] | ||
@engineers = company_service.engineers | ||
end | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
backend/app/controllers/company_services/shifts_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module CompanyServices | ||
class ShiftsController < ApplicationController | ||
def index | ||
@shifts = FetchShiftsService.new(params[:company_service_id],params[:week]).call | ||
end | ||
end | ||
end |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module JsonResponse | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found | ||
rescue_from ActionController::RoutingError, with: :render_not_found | ||
|
||
before_action :ensure_json_request | ||
before_action :set_default_format | ||
|
||
def render_not_found | ||
render json: { error: 'Not Found' }, status: :not_found | ||
end | ||
|
||
end | ||
|
||
private | ||
def set_default_format | ||
request.format = :json | ||
end | ||
|
||
def ensure_json_request | ||
return if request.format.json? | ||
render json: { error: 'Not Acceptable' }, status: :not_acceptable | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class WeeksController < ApplicationController | ||
def index | ||
@weeks = WeekService.new(params[:company_service_id]).call | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# == Schema Information | ||
# | ||
# Table name: company_service_engineers | ||
# | ||
# id :bigint not null, primary key | ||
# company_service_id :bigint not null | ||
# engineer_id :bigint not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
class CompanyServiceEngineer < ApplicationRecord | ||
belongs_to :company_service | ||
belongs_to :engineer | ||
|
||
validate :limited_engineers | ||
|
||
private | ||
|
||
def limited_engineers | ||
if company_service.present? && company_service.company_service_engineers.count >= 3 | ||
errors.add(:base, "Cannot assign more than 3 engineers to a company service") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# == Schema Information | ||
# | ||
# Table name: engineers | ||
# | ||
# id :bigint not null, primary key | ||
# name :string | ||
# color :string | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
class Engineer < ApplicationRecord | ||
has_many :company_service_engineers, dependent: :destroy | ||
has_many :company_services, through: :company_service_engineers | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# == Schema Information | ||
# | ||
# Table name: shifts | ||
# | ||
# id :bigint not null, primary key | ||
# company_service_id :bigint not null | ||
# week :string | ||
# day :string | ||
# start_time :time | ||
# end_time :time | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
class Shift < ApplicationRecord | ||
belongs_to :company_service | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
class FetchShiftsService | ||
def initialize(company_service_id, week) | ||
@company_service = CompanyService.find(company_service_id) | ||
@week = week | ||
end | ||
|
||
def call | ||
shifts_by_day.map do |day, shifts| | ||
{ | ||
day: day, | ||
dayLabel: formatted_day_label(day), | ||
time_blocks: format_time_blocks(shifts) | ||
} | ||
end | ||
end | ||
|
||
private | ||
|
||
def shifts_by_day | ||
# "Monday" => [shifts] | ||
@company_service.shifts | ||
.where(week: @company_service.contract_start_week) | ||
.group_by(&:day) | ||
end | ||
|
||
def formatted_day_label(day) | ||
current_year = Date.today.year | ||
week_number = @week.split('-').last.to_i | ||
day_index = Date::DAYNAMES.index(day.capitalize) | ||
day_index = day_index == 0 ? 7 : day_index | ||
date = Date.commercial( | ||
current_year, | ||
week_number, | ||
day_index | ||
) | ||
# I18n.l(date, format: "%A %d de %B", locale: I18n.locale) | ||
I18n.l(date, format: :long, locale: :es) | ||
end | ||
|
||
def format_time_blocks(shifts) | ||
shifts.map do |shift| | ||
{ | ||
start_time: shift.start_time.strftime("%H:%W"), | ||
end_time: shift.end_time.strftime("%H:%W"), | ||
amount_of_hours: ((shift.end_time - shift.start_time) / 1.hour).to_i, | ||
engineer: nil #format_engineer(shift.engineer) | ||
} | ||
end | ||
end | ||
|
||
def format_engineer(engineer) | ||
return nil unless engineer.present? | ||
|
||
{ | ||
id: engineer.id, | ||
name: engineer.name, | ||
color: engineer.color | ||
} | ||
end | ||
end |
Oops, something went wrong.