-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/73 new post service order (#100)
* [@adrilene/@denisousa] Issue #73 - Iniciando testes. * [@Adrilene/@denisousa] issue #73 atualizacao no requirements * [@Adrilene/@denisousa] issue #73 validacao do post * [@Adrilene/@denisousa] issue #73 validacao com schemas * [@adrilene/@denisousa] issue #73 WIP testes do post * [@adrilene/@denisousa] Issue #73 - Testes de validação. * [@adrilene/@denisousa] Issue #73 - Iniciando registro da ordem de serviço. * [@adrilene/@denisousa] issue #73 alterações no model e no schema (required/fields) Alguns arquivos fora do escopo descrito no card tiveram de ser alterados. Ainda não começamos a usar o repository, pois haveria conflito da arquitetura da v2 proposta inicialmente com a da atual. * [@adrilene/@denisousa] Issue #73 - Inserção de nova ordem de serviço com validações de Ids de equipamento e item. * [@adrilene/@denisousa] Issue #73 - Resolvendo comentários do PR. * [@adrilene/@denisousa] Issue #73 - Correção testes de itens. Co-authored-by: Denis Menezes <[email protected]>
- Loading branch information
Showing
16 changed files
with
491 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,73 @@ | ||
from flask_restful import Resource | ||
from ..helpers.helper_response import get_response, error_response | ||
from ..validation.validation_request import invalid_deleted_parameter | ||
from ..services.service_order_service import ServiceOrderService | ||
from http import HTTPStatus | ||
from flask import request | ||
from flask_restful import Resource | ||
from ..helpers.helper_response import get_response | ||
from ..validation.validation_request import invalid_deleted_parameter | ||
from api.v2.controllers.validators.validation_request import validate_request | ||
from api.v2.services.service_order_service import ServiceOrderService | ||
from api.v2.models.schemas.service_order_schema import ServiceOrderSchema | ||
from api.v2.utils.util_response import error_response, post_response | ||
from .validators.validation_request import validate_request_id | ||
|
||
|
||
class ServiceOrdersManyController(Resource): | ||
def get(self): | ||
deleted_included = request.args.get("deleted") | ||
if invalid_deleted_parameter(deleted_included): | ||
return error_response( | ||
"Parameter deleted is not equal true.", HTTPStatus.BAD_REQUEST) | ||
"Parameter deleted is not equal true.", HTTPStatus.BAD_REQUEST | ||
) | ||
if deleted_included: | ||
service_orders = ServiceOrderService().fetch_all() | ||
service_orders = ServiceOrderService().fetch_all() | ||
else: | ||
service_orders = ServiceOrderService().fetch_active() | ||
|
||
return get_response(service_orders, deleted_included) | ||
|
||
def post(self): | ||
body = request.get_json() | ||
|
||
validate, message = validate_request(body) | ||
if not validate: | ||
return error_response(message) | ||
|
||
errors = [] | ||
for index, service_order in enumerate(body["content"]): | ||
validate, message = ServiceOrderSchema().validate_save(service_order) | ||
if not validate: | ||
errors.append({index: message}) | ||
continue | ||
|
||
validate, message = validate_request_id( | ||
"equipamento", service_order["equipamento_id"] | ||
) | ||
if not validate: | ||
errors.append({index: message}) | ||
|
||
accessories = service_order["triagem"]["acessorios"] | ||
for accessory in accessories: | ||
validate, message = validate_request_id("item", accessory["item_id"]) | ||
if not validate: | ||
errors.append({index: message}) | ||
|
||
items = service_order["diagnostico"]["itens"] | ||
for item in items: | ||
validate, message = validate_request_id("item", item["item_id"]) | ||
if not validate: | ||
errors.append({index: message}) | ||
|
||
service_order["numero_ordem_servico"] = ServiceOrderService().format_service_order_number( | ||
service_order["numero_ordem_servico"] | ||
) | ||
|
||
if ServiceOrderService().check_duplicates_service_order_number(service_order["numero_ordem_servico"]): | ||
errors.append({index: 'Service Order number already exists'}) | ||
|
||
if errors: | ||
return error_response(errors) | ||
|
||
content = [] | ||
for service_order in body["content"]: | ||
content.append(ServiceOrderService().save_service_order(service_order)) | ||
|
||
return post_response(content) |
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,88 @@ | ||
from bson import ObjectId | ||
from api.v2.services.item_service import ItemService | ||
from api.services.equipamento_service import listar_equipamento_by_id | ||
|
||
|
||
def invalid_deleted_parameter(param): | ||
return param and param != "true" | ||
|
||
|
||
def validate_is_list(body): | ||
if isinstance(type(body), list): | ||
return (False, "Not a list.") | ||
return (True, "OK") | ||
|
||
|
||
def validate_post(body): | ||
if "_id" in body: | ||
return (False, "ID must not be sent") | ||
return (True, "OK") | ||
|
||
|
||
def validate_request(body): | ||
if not body: | ||
return (False, "No body found") | ||
|
||
if not "content" in body: | ||
return (False, "No content found") | ||
|
||
if not isinstance(body["content"], list): | ||
return (False, "No list found.") | ||
|
||
if not len(body["content"]): | ||
return (False, "Empty list. Nothing to do.") | ||
for item in body["content"]: | ||
if not item: | ||
return (False, "Some entry has no data.") | ||
|
||
return (True, "OK") | ||
|
||
|
||
def validate_id_included(entity): | ||
if "_id" not in entity: | ||
return (False, "Missing ID") | ||
return (True, "OK") | ||
|
||
|
||
def validate_id_objectID(_id): | ||
try: | ||
ObjectId(_id) | ||
except Exception: | ||
return (False, "Invalid ID") | ||
return (True, "OK") | ||
|
||
|
||
def validate_id_exists(entity, _id): | ||
if entity == "item": | ||
if not ItemService().fetch_item_by_id(_id): | ||
return (False, "Nonexistent item ID") | ||
|
||
if entity == "equipamento": | ||
if not listar_equipamento_by_id(_id): | ||
return (False, "Nonexistent equipamento ID") | ||
|
||
return (True, "OK") | ||
|
||
|
||
def validate_request_dict_id(entity): | ||
validate, message = validate_id_included(entity) | ||
if not validate: | ||
return (validate, message) | ||
|
||
validate, message = validate_request_id(entity["_id"]) | ||
if not validate: | ||
return (validate, message) | ||
|
||
return (True, "OK") | ||
|
||
|
||
def validate_request_id(entity, _id): | ||
validate, message = validate_id_objectID(_id) | ||
if not validate: | ||
return (validate, message) | ||
|
||
validate, message = validate_id_exists(entity, _id) | ||
if not validate: | ||
return (validate, message) | ||
|
||
return (True, "OK") |
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,20 @@ | ||
from marshmallow import ValidationError | ||
|
||
|
||
class SchemaBase: | ||
def validate_save(self, body): | ||
if "_id" in body: | ||
return False, "Id must not be sent" | ||
|
||
if "updated_at" in body: | ||
return False, "Updated must not be sent" | ||
|
||
if "deleted_at" in body: | ||
return False, "Deleted must not be sent" | ||
|
||
try: | ||
self.load(body) | ||
except ValidationError as err: | ||
return False, err.messages | ||
|
||
return True, "OK" |
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,92 @@ | ||
from api.v2.models import service_order_model | ||
from .schema_base import SchemaBase | ||
from marshmallow import Schema, fields, validate | ||
|
||
|
||
class AccessorySchema(Schema): | ||
class Meta: | ||
model = service_order_model.Acessorio | ||
fields = ("item_id", | ||
"acompanha", | ||
"quantidade", | ||
"estado_de_conservacao") | ||
|
||
item_id = fields.String(required=True) | ||
acompanha = fields.Boolean(required=True) | ||
quantidade = fields.Integer(required=True) | ||
estado_de_conservacao = fields.String(required=False) | ||
|
||
|
||
class ScreeningSchema(Schema): | ||
class Meta: | ||
model = service_order_model.Triagem | ||
fields = ("estado_de_conservacao", | ||
"foto_antes_limpeza", | ||
"acessorios", | ||
"foto_apos_limpeza") | ||
|
||
foto_antes_limpeza = fields.String(required=False) | ||
estado_de_conservacao = fields.String(required=False) | ||
acessorios = fields.List(fields.Nested(AccessorySchema), required=False) | ||
foto_apos_limpeza = fields.String(required=False) | ||
|
||
|
||
class ItemDiganotico(Schema): | ||
class Meta: | ||
model = service_order_model.Item | ||
fields = ("item_id", | ||
"quantidade") | ||
|
||
item_id = fields.String(required=True) | ||
quantidade = fields.Integer(required=True) | ||
|
||
|
||
class DiagnosticSchema(Schema): | ||
class Meta: | ||
model = service_order_model.Triagem | ||
fields = ("resultado_tecnico", | ||
"demanda_servicos", | ||
"observacoes", | ||
"itens") | ||
|
||
resultado_tecnico = fields.String(required=False) | ||
demanda_servicos = fields.String(required=False) | ||
observacoes = fields.String(required=False) | ||
itens = fields.List(fields.Nested(ItemDiganotico), required=False) | ||
|
||
|
||
class CalibrationSchema(Schema): | ||
class Meta: | ||
model = service_order_model.Calibragem | ||
fields = ( | ||
"status", | ||
) | ||
|
||
status = fields.String(required=False) | ||
|
||
|
||
class ServiceOrderSchema(Schema, SchemaBase): | ||
class Meta: | ||
model = service_order_model.OrdemServico | ||
fields = ("_id", | ||
"equipamento_id", | ||
"numero_ordem_servico", | ||
"created_at", | ||
"updated_at", | ||
"triagem", | ||
"clinico", | ||
"tecnico", | ||
"foto_equipamento_chegada", | ||
"status", | ||
"diagnostico", | ||
"calibragem") | ||
|
||
equipamento_id = fields.String(required=False) | ||
numero_ordem_servico = fields.Integer(required=True) | ||
created_at = fields.DateTime(required=False) | ||
updated_at = fields.DateTime(required=False) | ||
triagem = fields.Nested(ScreeningSchema, required=False) | ||
diagnostico = fields.Nested(DiagnosticSchema, required=False) | ||
calibragem = fields.Nested(CalibrationSchema, required=True) | ||
status = fields.String(validate=validate.OneOf(["triagem", "diagnostico"]), | ||
required=True) |
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
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,22 @@ | ||
from flask import make_response, jsonify | ||
from http import HTTPStatus | ||
|
||
def error_response(message, status=400): | ||
return make_response( | ||
jsonify({ | ||
"error": message, | ||
}), | ||
status | ||
) | ||
|
||
|
||
def get_response(content, deleted): | ||
response = {'content': content} | ||
|
||
response['deleted'] = True if deleted else False | ||
|
||
return make_response(jsonify(response), HTTPStatus.OK) | ||
|
||
|
||
def post_response(content): | ||
return make_response(jsonify({'content': content}), 201) |
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 |
---|---|---|
|
@@ -33,4 +33,4 @@ python-slugify==4.0.0 | |
Werkzeug==1.0.1 | ||
WTForms==2.2.1 | ||
zipp==3.1.0 | ||
mockito | ||
mockito==1.2.1 |
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
Oops, something went wrong.