1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-20 14:54:15 +01:00

(feat) project statuses api

This commit is contained in:
Karen 2023-01-16 17:37:44 +01:00 committed by Sylvain
parent 7209dfbe7b
commit 4aba30c5e3
11 changed files with 208 additions and 0 deletions

View File

@ -0,0 +1,46 @@
# frozen_string_literal: true
# API Controller for resources of type Status
# Status are used to check Projects state
class API::StatusesController < ApplicationController
before_action :set_status, only: %i[update destroy]
before_action :authenticate_user!, only: %i[create update destroy]
def index
@statuses = Status.all
end
def create
authorize Status
@status = Status.new(status_params)
if @status.save
render json: @status, status: :created
else
render json: @status.errors, status: :unprocessable_entity
end
end
def update
authorize Status
if @status.update(status_params)
render json: @status, status: :ok
else
render json: @status.errors, status: :unprocessable_entity
end
end
def destroy
authorize Status
@status.destroy
head :no_content
end
private
def set_status
@status = Status.find(params[:id])
end
def status_params
params.require(:status).permit(:label)
end
end

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
# Here we link status to their projects
class ProjectStatus < ApplicationRecord
belongs_to :project
belongs_to :status
end

6
app/models/status.rb Normal file
View File

@ -0,0 +1,6 @@
# frozen_string_literal: true
# Set statuses for projects (new, pending, done...)
class Status < ApplicationRecord
validates :label, presence: true
end

View File

@ -0,0 +1,16 @@
# frozen_string_literal: true
# Check if user is an admin to allow create, update and destroy status
class StatusPolicy < ApplicationPolicy
def create?
user.admin?
end
def update?
create?
end
def destroy?
create?
end
end

View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
json.array!(@statuses) do |status|
json.extract! status, :id, :label
end

View File

@ -45,6 +45,7 @@ Rails.application.routes.draw do
resources :components
resources :themes
resources :licences
resources :statuses
resources :admins, only: %i[index create destroy]
resources :settings, only: %i[show update index], param: :name do
patch '/bulk_update', action: 'bulk_update', on: :collection

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
# From this migration, we set statuses for projects (new, pending, done...)
class CreateStatuses < ActiveRecord::Migration[5.2]
def change
create_table :statuses do |t|
t.string :label
t.timestamps
end
end
end

View File

@ -0,0 +1,13 @@
# frozen_string_literal: true
# From this migration, we link status to their projects
class CreateProjectStatuses < ActiveRecord::Migration[5.2]
def change
create_table :project_statuses do |t|
t.references :project, foreign_key: true
t.references :status, foreign_key: true
t.timestamps
end
end
end

View File

@ -865,6 +865,15 @@ ActiveRecord::Schema.define(version: 2023_01_31_104958) do
t.index ["user_id"], name: "index_profiles_on_user_id"
end
create_table "project_statuses", force: :cascade do |t|
t.bigint "project_id"
t.bigint "status_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["project_id"], name: "index_project_statuses_on_project_id"
t.index ["status_id"], name: "index_project_statuses_on_status_id"
end
create_table "project_steps", id: :serial, force: :cascade do |t|
t.text "description"
t.integer "project_id"
@ -1400,6 +1409,8 @@ ActiveRecord::Schema.define(version: 2023_01_31_104958) do
add_foreign_key "prices", "plans"
add_foreign_key "product_stock_movements", "products"
add_foreign_key "products", "product_categories"
add_foreign_key "project_statuses", "projects"
add_foreign_key "project_statuses", "statuses"
add_foreign_key "project_steps", "projects"
add_foreign_key "project_users", "projects"
add_foreign_key "project_users", "users"

23
test/fixtures/statuses.yml vendored Normal file
View File

@ -0,0 +1,23 @@
status_1:
id: 1
label: new
created_at: 2023-01-16 15:25:13.744539000 Z
updated_at: 2023-01-16 15:25:13.744539000 Z
status_2:
id: 2
label: pending
created_at: 2023-01-16 15:25:13.744539000 Z
updated_at: 2023-01-16 15:25:13.744539000 Z
status_3:
id: 3
label: done
created_at: 2023-01-16 15:25:13.744539000 Z
updated_at: 2023-01-16 15:25:13.744539000 Z
status_4:
id: 4
label: disused
created_at: 2023-01-16 15:25:13.744539000 Z
updated_at: 2023-01-16 15:25:13.744539000 Z

View File

@ -0,0 +1,68 @@
# frozen_string_literal: true
require 'test_helper'
class StatusesTest < ActionDispatch::IntegrationTest
def setup
@admin = User.find_by(username: 'admin')
login_as(@admin, scope: :user)
end
test 'create a status' do
post '/api/statuses',
params: {
label: 'Open'
}.to_json,
headers: default_headers
# Check response format & status
assert_equal 201, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the correct status was created
res = json_response(response.body)
status = Status.where(id: res[:id]).first
assert_not_nil status, 'status was not created in database'
assert_equal 'Open', res[:label]
end
test 'update a status' do
patch '/api/statuses/1',
params: {
label: 'Done'
}.to_json,
headers: default_headers
# Check response format & status
assert_equal 200, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the status was updated
res = json_response(response.body)
assert_equal 1, res[:id]
assert_equal 'Done', res[:label]
end
test 'list all statuses' do
get '/api/statuses'
# Check response format & status
assert_equal 200, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the list items are ok
statuses = json_response(response.body)
assert_equal Status.count, statuses.count
end
test 'delete a status' do
status = Status.create!(label: 'Gone too soon')
delete "/api/statuses/#{status.id}"
assert_response :success
assert_empty response.body
assert_raise ActiveRecord::RecordNotFound do
status.reload
end
end
end