Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devices API #1699

Merged
merged 13 commits into from
Jun 26, 2024
22 changes: 10 additions & 12 deletions app/controllers/internal_api/v1/users/devices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,15 @@ def index
render :index, locals: { devices: }, status: :ok
end

def create
authorize @user, policy_class: Users::DevicePolicy
device = @user.devices.new(device_params.merge(issued_to: @user, issued_by: @user.current_workspace))
device.save!
render :create, locals: { device: }, status: :ok
end

def show
authorize device, policy_class: Users::DevicePolicy
render :show, locals: { device: }, status: :ok
end

def update
authorize device, policy_class: Users::DevicePolicy
device.update!(device_params)
render :update, locals: { device: }, status: :ok
def create
authorize @user, policy_class: Users::DevicePolicy
BulkDevicesService.new(device_params, set_user).process
render json: { notice: I18n.t("devices.update.success") }
end

private
Expand All @@ -38,8 +31,13 @@ def device
end

def device_params
shared_params = [:device_type, :name, :serial_number, :is_insured, :insurance_activation_date,
:insurance_expiry_date, specifications: [:processor, :ram, :graphics]]

params.require(:device).permit(
:device_type, :name, :serial_number, specifications: {}
add_devices: shared_params,
update_devices: [:id] + shared_params,
remove_devices: []
)
end
end
34 changes: 21 additions & 13 deletions app/models/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
#
# Table name: devices
#
# id :bigint not null, primary key
# device_type :string default("laptop")
# insurance_bought_date :date
# insurance_expiry_date :date
# is_insured :boolean default(FALSE)
# name :string
# serial_number :string
# specifications :jsonb
# created_at :datetime not null
# updated_at :datetime not null
# company_id :bigint not null
# user_id :bigint not null
# id :bigint not null, primary key
# device_type :string default("laptop")
# insurance_activation_date :date
# insurance_expiry_date :date
# is_insured :boolean default(FALSE)
# name :string
# serial_number :string
# specifications :jsonb
# created_at :datetime not null
# updated_at :datetime not null
# company_id :bigint not null
# user_id :bigint not null
#
# Indexes
#
Expand All @@ -42,14 +42,22 @@ class Device < ApplicationRecord
# Validations
after_initialize :set_default_specifications, if: :new_record?
validates :name, length: { maximum: 100 }
validates :insurance_activation_date, presence: true, if: :is_insured?
validates :insurance_expiry_date, presence: true, if: :is_insured?
validates :insurance_expiry_date, comparison: { greater_than_or_equal_to: :insurance_activation_date },
if: :is_insured?

private

def set_default_specifications
self.specifications = {
self.specifications ||= {
"processor": "",
"ram": "",
"graphics": ""
}
end

def is_insured?
is_insured
end
end
46 changes: 46 additions & 0 deletions app/services/bulk_devices_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

class BulkDevicesService
attr_reader :user, :device_params

def initialize(device_params, user)
@device_params = device_params
@user = user
end

def process
ActiveRecord::Base.transaction do
add_new_devices
update_devices
remove_devices
end
end

private

def add_new_devices
return if device_params[:add_devices].blank?

device_params[:add_devices].each do |params|
new_device = user.devices.new(params)
new_device.issued_to = user
new_device.issued_by = user.current_workspace
new_device.save!
end
end

def update_devices
return if device_params[:update_devices].blank?

device_params[:update_devices].each do |params|
device = Device.find_by!(id: params[:id])
device.update!(params.except(:id))
end
end

def remove_devices
return if device_params[:remove_devices].blank?

user.devices.where(id: device_params[:remove_devices]).destroy_all
end
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# frozen_string_literal: true

json.extract! device, :device_type, :name, :serial_number, :specifications
json.extract! device, *device.attributes.keys.map(&:to_sym)
5 changes: 4 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ en:
employment:
update:
success: Employment updated successfully
devices:
update:
success: Devices updated successfully
registration:
sign_up: sign up
sign_in: sign in
Expand Down Expand Up @@ -295,4 +298,4 @@ en:
invalid: Invalid email or password
expenses:
update: "Expense updated successfully"
destroy: "Expense deleted successfully"
destroy: "Expense deleted successfully"
9 changes: 9 additions & 0 deletions db/migrate/20240311144124_add_insurance_details_to_devices.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class AddInsuranceDetailsToDevices < ActiveRecord::Migration[7.0]
def change
add_column :devices, :is_insured, :boolean, default: false
add_column :devices, :insurance_activation_date, :date
add_column :devices, :insurance_expiry_date, :date
end
end
4 changes: 2 additions & 2 deletions db/schema.rb

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

3 changes: 3 additions & 0 deletions spec/factories/devices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
issued_by factory: :company
name { Faker::Alphanumeric.alphanumeric }
serial_number { Faker::Alphanumeric.alphanumeric }
is_insured { true }
insurance_activation_date { Faker::Date.between(from: "2019-04-01", to: Date.today) }
insurance_expiry_date { Faker::Date.between(from: self.insurance_activation_date, to: Date.today) }
end
end
6 changes: 6 additions & 0 deletions spec/models/device_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@
expect(device.device_type).to eq("laptop")
end
end

describe "validate comparisons" do
it "insurance expiry date should not be after bought date" do
expect(device.insurance_expiry_date).to be >= device.insurance_activation_date
end
end
end
36 changes: 13 additions & 23 deletions spec/requests/internal_api/v1/users/devices/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@
before do
send_request :post, internal_api_v1_user_devices_path(
user_id: user.id,
device: device_details
device: { add_devices: [device_details] }
), headers: auth_headers(user)
end

it "is successful" do
expect(response).to have_http_status(:ok)
expect(json_response["device_type"]).to eq("laptop")
expect(json_response["name"]).to eq(JSON.parse(device_details[:name].to_json))
expect(json_response["serial_number"]).to eq(JSON.parse(device_details[:serial_number].to_json))
expect(json_response["notice"]).to eq(I18n.t("devices.update.success"))
end
end

Expand All @@ -41,15 +39,13 @@
create(:employment, company:, user: employee)
send_request :post, internal_api_v1_user_devices_path(
user_id: employee.id,
device: device_details
device: { add_devices: [device_details] }
), headers: auth_headers(user)
end

it "is successful" do
expect(response).to have_http_status(:ok)
expect(json_response["device_type"]).to eq("laptop")
expect(json_response["name"]).to eq(JSON.parse(device_details[:name].to_json))
expect(json_response["serial_number"]).to eq(JSON.parse(device_details[:serial_number].to_json))
expect(json_response["notice"]).to eq(I18n.t("devices.update.success"))
end
end

Expand All @@ -58,7 +54,7 @@
create(:employment, company: company2, user: user2)
send_request :post, internal_api_v1_user_devices_path(
user_id: user2.id,
device: device_details
device: { add_devices: [device_details] }
), headers: auth_headers(user)
end

Expand All @@ -72,7 +68,7 @@
create(:employment, company:, user: employee)
send_request :post, internal_api_v1_user_devices_path(
user_id: "abc",
device: device_details
device: { add_devices: [device_details] }
), headers: auth_headers(user)
end

Expand All @@ -92,15 +88,13 @@
before do
send_request :post, internal_api_v1_user_devices_path(
user_id: user.id,
device: device_details
device: { add_devices: [device_details] }
), headers: auth_headers(user)
end

it "is successful" do
expect(response).to have_http_status(:ok)
expect(json_response["device_type"]).to eq("laptop")
expect(json_response["name"]).to eq(JSON.parse(device_details[:name].to_json))
expect(json_response["serial_number"]).to eq(JSON.parse(device_details[:serial_number].to_json))
expect(json_response["notice"]).to eq(I18n.t("devices.update.success"))
end
end

Expand All @@ -109,15 +103,13 @@
create(:employment, company:, user: employee)
send_request :post, internal_api_v1_user_devices_path(
user_id: employee.id,
device: device_details
device: { add_devices: [device_details] }
), headers: auth_headers(user)
end

it "is successful" do
expect(response).to have_http_status(:ok)
expect(json_response["device_type"]).to eq("laptop")
expect(json_response["name"]).to eq(JSON.parse(device_details[:name].to_json))
expect(json_response["serial_number"]).to eq(JSON.parse(device_details[:serial_number].to_json))
expect(json_response["notice"]).to eq(I18n.t("devices.update.success"))
end
end
end
Expand All @@ -132,15 +124,13 @@
before do
send_request :post, internal_api_v1_user_devices_path(
user_id: user.id,
device: device_details
device: { add_devices: [device_details] }
), headers: auth_headers(user)
end

it "is successful" do
expect(response).to have_http_status(:ok)
expect(json_response["device_type"]).to eq("laptop")
expect(json_response["name"]).to eq(JSON.parse(device_details[:name].to_json))
expect(json_response["serial_number"]).to eq(JSON.parse(device_details[:serial_number].to_json))
expect(json_response["notice"]).to eq(I18n.t("devices.update.success"))
end
end

Expand All @@ -149,7 +139,7 @@
create(:employment, company:, user: employee)
send_request :post, internal_api_v1_user_devices_path(
user_id: employee.id,
device: device_details
device: { add_devices: [device_details] }
), headers: auth_headers(user)
end

Expand Down
Loading
Loading