fix enregistrementd'un rapport de checks

This commit is contained in:
Jérémy Lecour 2022-01-27 23:52:09 +01:00 committed by Jérémy Lecour
parent 5450bc7c37
commit 3693efd572

View file

@ -2,17 +2,18 @@ class Api::V1::CheckReportsController < Api::V1::BaseController
# POST /check-reports or /check-reports.json
def create
@check_report = CheckReport.new(check_report_params)
if @check_report.checks.empty?
render json: { message: "At least 1 check must be present" }, status: :unprocessable_entity
else
checks_params = ActionController::Parameters.new(@check_report.checks).permit(:name, :description).map { |check|
checks_params = @check_report.checks.map { |check|
{
hostname: @check_report.hostname,
date: @check_report.date,
name: check.fetch(:name, ""),
description: check.fetch(:description, "")
name: check.fetch("name", ""),
description: check.fetch("description", "")
}
}
checks = Check.create(checks_params)
@ -20,7 +21,7 @@ class Api::V1::CheckReportsController < Api::V1::BaseController
persisted = checks.count(&:persisted?)
invalid = checks.count(&:invalid?)
if checks.all?(:persisted?)
if checks.all?(&:persisted?)
render json: { message: "#{persisted} checks created" }, status: :created
else
render json: { message: "error while creating checks : #{persisted} persisted, #{invalid} invalid" }, status: :unprocessable_entity