Skip to content

Commit

Permalink
[@adrilene/@denisousa] Issue #73 - Inserção de nova ordem de serviço …
Browse files Browse the repository at this point in the history
…com validações de Ids de equipamento e item.
  • Loading branch information
Adrilene committed Jun 25, 2020
1 parent aa3d338 commit cab31d4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 56 deletions.
30 changes: 22 additions & 8 deletions api/v2/controllers/service_orders_many_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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 api.v2.services.item_service import ItemService
from .validators.validation_request import validate_request_id


class ServiceOrdersManyController(Resource):
Expand Down Expand Up @@ -38,20 +38,34 @@ def post(self):
errors.append({index: message})
continue

accessories = service_order['triagem']['acessorios']
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:
if not ItemService().fetch_item_by_id(accessory['item_id']):
errors.append({index: 'Item Id not found'})
validate, message = validate_request_id("item", accessory["item_id"])
if not validate:
errors.append({index: message})

items = service_order['diagnostico']['itens']
items = service_order["diagnostico"]["itens"]
for item in items:
if not ItemService().fetch_item_by_id(item['item_id']):
errors.append({index: 'Item Id not found'})
validate, message = validate_request_id("item", item["item_id"])
if not validate:
errors.append({index: message})

service_order["numero_ordem_servico"] = ServiceOrderService().create_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().register_service_order(service_order))
Expand Down
82 changes: 39 additions & 43 deletions api/v2/controllers/validators/validation_request.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
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):
Expand All @@ -7,86 +9,80 @@ def invalid_deleted_parameter(param):

def validate_is_list(body):
if isinstance(type(body), list):
return (False, 'Wrong format.')
return (True, 'OK')


def validate_exists_id(id, service):
try:
ObjectId(id)
except Exception:
return (False, 'Invalid ID')

if not service.fetch_item_by_id(id):
return (False, 'Nonexistent ID')

return (True, 'OK')
return (False, "Wrong format.")
return (True, "OK")


def validate_post(body):
if '_id' in body:
return (False, 'ID must not be sent')
return (True, 'OK')
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')
return (False, "No body found")

if not 'content' in body:
return (False, 'No content found')
if not "content" in body:
return (False, "No content found")

if not isinstance(body['content'], list):
return (False, 'No list 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 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 (False, "Some entry has no data.")

return (True, 'OK')
return (True, "OK")


def validate_id_included(entity):
if '_id' not in entity:
return (False, 'Missing ID')
return (True, 'OK')
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')
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")

def validate_item_id_exists(_id, service):
if not service.fetch_item_by_id(_id):
return (False, 'Nonexistent ID')
return (True, 'OK')
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'])
validate, message = validate_request_id(entity["_id"])
if not validate:
return (validate, message)
return (True, 'OK')
return (validate, message)

return (True, "OK")


def validate_request_id(_id):
def validate_request_id(entity, _id):
validate, message = validate_id_objectID(_id)
if not validate:
return (validate, message)

validate, message = validate_item_id_exists(_id)
validate, message = validate_id_exists(entity, _id)
if not validate:
return (validate, message)

return (True, 'OK')
return (True, "OK")
5 changes: 3 additions & 2 deletions api/v2/models/schemas/service_order_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ class Meta:
"tecnico",
"foto_equipamento_chegada",
"status",
"diagnostico")
"diagnostico",
"calibragem")

equipamento_id = fields.String(required=False)
numero_ordem_servico = fields.String(required=True)
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)
Expand Down
10 changes: 7 additions & 3 deletions api/v2/services/service_order_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
class ServiceOrderService(ServiceBase):
def fetch_active(self):
return self.parser_mongo_response_to_list(
OrdemServico.objects(deleted_at__exists=False))
OrdemServico.objects(deleted_at__exists=False)
)

def fetch_all(self):
return self.parser_mongo_response_to_list(
OrdemServico.objects())
return self.parser_mongo_response_to_list(OrdemServico.objects())

def register_service_order(self, body):
service_order = OrdemServico(**body).save()
return str(service_order.id)

def create_service_order_number(self, service_order_number):
return str(service_order_number).zfill(4)

def check_duplicates_service_order_number(self, service_order_number):
return OrdemServico.objects(numero_ordem_servico=service_order_number)

0 comments on commit cab31d4

Please sign in to comment.