156 lines
5.6 KiB
Python
156 lines
5.6 KiB
Python
from flask import Blueprint, request, jsonify
|
|
from flasgger.utils import swag_from
|
|
from models.shifts import Shift, db
|
|
from models.service_instance import ServiceInstance
|
|
from models.time_schedule import TimeSchedule
|
|
|
|
shift_bp = Blueprint('shift', __name__, url_prefix='/shift/')
|
|
|
|
# Criar um turno
|
|
@shift_bp.route('/', methods=['POST'])
|
|
def create_shift():
|
|
data = request.get_json()
|
|
|
|
service_instance_id = data.get('service_instance_id')
|
|
ServiceInstance.query.get_or_404(service_instance_id)
|
|
|
|
# Cria o turno
|
|
shift = Shift(
|
|
name=data['name'],
|
|
start_time=data['start_time'],
|
|
end_time=data['end_time'],
|
|
interval_start=data.get('interval_start'),
|
|
interval_end=data.get('interval_end'),
|
|
type_interval=data['type_interval'],
|
|
service_instance_id=service_instance_id,
|
|
description=data.get('description'), # Adicionando a descrição
|
|
tolerance=data.get('tolerance'), # Adicionando a tolerância
|
|
)
|
|
|
|
# Associa os dias (TimeSchedules)
|
|
time_schedule_ids = data.get('time_schedule_ids', [])
|
|
if time_schedule_ids:
|
|
shift.time_schedules = TimeSchedule.query.filter(TimeSchedule.id.in_(time_schedule_ids)).all()
|
|
|
|
db.session.add(shift)
|
|
db.session.commit()
|
|
|
|
return jsonify({'message': 'Shift created successfully', 'id': shift.id}), 201
|
|
|
|
|
|
# Atualizar um turno
|
|
@shift_bp.route('/<int:shift_id>', methods=['PUT'])
|
|
@swag_from('../docs/shifts/update.yml')
|
|
def update_shift(shift_id):
|
|
"""Atualiza um turno existente."""
|
|
data = request.get_json()
|
|
|
|
# Localiza o turno ou retorna 404
|
|
shift = Shift.query.get_or_404(shift_id)
|
|
|
|
# Atualiza os campos fornecidos no body
|
|
if 'name' in data:
|
|
shift.name = data['name']
|
|
if 'start_time' in data:
|
|
shift.start_time = data['start_time']
|
|
if 'end_time' in data:
|
|
shift.end_time = data['end_time']
|
|
if 'interval_start' in data:
|
|
shift.interval_start = data['interval_start']
|
|
if 'interval_end' in data:
|
|
shift.interval_end = data['interval_end']
|
|
if 'type_interval' in data:
|
|
shift.type_interval = data['type_interval']
|
|
if 'description' in data:
|
|
shift.description = data['description']
|
|
if 'tolerance' in data:
|
|
shift.tolerance = data['tolerance']
|
|
|
|
if 'service_instance_id' in data:
|
|
ServiceInstance.query.get_or_404(data['service_instance_id']) # Valida a existência
|
|
shift.service_instance_id = data['service_instance_id']
|
|
|
|
# Atualiza os dias (TimeSchedules)
|
|
if 'time_schedule_ids' in data:
|
|
# Recebe a lista de IDs dos dias a serem associados ao turno
|
|
time_schedule_ids = data['time_schedule_ids']
|
|
# Verifica se os IDs existem no banco e atualiza a associação
|
|
shift.time_schedules = TimeSchedule.query.filter(TimeSchedule.id.in_(time_schedule_ids)).all()
|
|
|
|
db.session.commit()
|
|
|
|
return jsonify({'message': 'Shift updated successfully'}), 200
|
|
|
|
|
|
|
|
# Listar turnos
|
|
@shift_bp.route('/', methods=['GET'])
|
|
@swag_from('../docs/shifts/list.yml')
|
|
def list_shifts():
|
|
"""Lista todos os turnos, opcionalmente filtrando por instância de serviço."""
|
|
service_instance_id = request.args.get('service_instance_id')
|
|
|
|
if service_instance_id:
|
|
# Filtrar por service_instance_id
|
|
shifts = Shift.query.filter_by(service_instance_id=service_instance_id).all()
|
|
else:
|
|
# Retornar todos os turnos
|
|
shifts = Shift.query.all()
|
|
|
|
shift_list = [{
|
|
'id': shift.id,
|
|
'name': shift.name,
|
|
"start_time": shift.start_time.strftime("%H:%M"),
|
|
"end_time": shift.end_time.strftime("%H:%M"),
|
|
"interval_start": shift.interval_start.strftime("%H:%M") if shift.interval_start else None,
|
|
"interval_end": shift.interval_end.strftime("%H:%M") if shift.interval_end else None,
|
|
'type_interval': shift.type_interval,
|
|
'service_instance_id': shift.service_instance_id,
|
|
'description': shift.description,
|
|
'tolerance': shift.tolerance,
|
|
# Adicionando a tolerância
|
|
|
|
|
|
# Adicionando os dias associados (TimeSchedules)
|
|
'time_schedules': [
|
|
{'id': ts.id, 'name': ts.name} for ts in shift.time_schedules
|
|
]
|
|
} for shift in shifts]
|
|
|
|
return jsonify(shift_list), 200
|
|
|
|
|
|
@shift_bp.route('/<int:shift_id>', methods=['GET'])
|
|
@swag_from('../docs/shifts/get.yml')
|
|
def get_shift_id(shift_id):
|
|
shift = Shift.query.get(shift_id)
|
|
if not shift:
|
|
return jsonify({'error': 'Escala não encontrada'}), 404
|
|
|
|
return jsonify({
|
|
'id': shift.id,
|
|
'name': shift.name,
|
|
#'day': shift.day,
|
|
"start_time": shift.start_time.strftime("%H:%M"),
|
|
"end_time": shift.end_time.strftime("%H:%M"),
|
|
"interval_start": shift.interval_start.strftime("%H:%M") if shift.interval_start else None,
|
|
"interval_end": shift.interval_end.strftime("%H:%M") if shift.interval_end else None,
|
|
'type_interval' : shift.type_interval,
|
|
'description': shift.description,
|
|
'tolerance': shift.tolerance,
|
|
'time_schedules': [
|
|
{'id': ts.id, 'name': ts.name} for ts in shift.time_schedules
|
|
],
|
|
'service_instance_id': shift.service_instance_id}),200
|
|
|
|
|
|
# Deletar um turno
|
|
@shift_bp.route('/<int:shift_id>', methods=['DELETE'])
|
|
@swag_from('../docs/shifts/delete.yml')
|
|
def delete_shift(shift_id):
|
|
"""Deleta um turno pelo ID."""
|
|
shift = Shift.query.get_or_404(shift_id)
|
|
db.session.delete(shift)
|
|
db.session.commit()
|
|
return jsonify({'message': 'Shift deleted successfully'}), 200
|