Skip to content

Commit

Permalink
Cambio en el trigger de bitacora de asignaciones
Browse files Browse the repository at this point in the history
  • Loading branch information
Drago177 committed Nov 21, 2024
1 parent 8b72e90 commit 7fcc8cf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
7 changes: 5 additions & 2 deletions models/cliente.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.orm import Mapped, mapped_column, relationship
from extensiones import db
from models.usuario import Usuario

class Cliente(db.Model):
id: Mapped[int] = mapped_column(primary_key=True)
Expand All @@ -12,6 +13,7 @@ class Cliente(db.Model):
telefono: Mapped[str] = mapped_column(nullable=True)
usuario_cliente: Mapped[str] = mapped_column(nullable=True)
contrasena: Mapped[str] = mapped_column(nullable=True)
operador: Mapped['Usuario'] = relationship('Usuario', backref='cliente')

def serialize(self):
return {
Expand All @@ -22,7 +24,8 @@ def serialize(self):
'email': self.email,
'telefono': self.telefono,
'usuario_cliente': self.usuario_cliente,
'contrasena': self.contrasena
'contrasena': self.contrasena,
'operador': self.operador if self.operador else None
}

@staticmethod
Expand Down
31 changes: 16 additions & 15 deletions triggers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,28 @@ BEFORE UPDATE ON repuesto
FOR EACH ROW
EXECUTE FUNCTION generarOrdenCompra();

CREATE OR REPLACE FUNCTION actualizar_bitacora_operador() RETURNS TRIGGER AS $$
CREATE OR REPLACE FUNCTION actualizar_bitacora_asignaciones() RETURNS TRIGGER AS $$
DECLARE
tipo text;
id_op_aux int;
id_vehic_aux int;
BEGIN
IF NEW.id_operador = OLD.id_operador THEN
RETURN NEW;
END IF;
IF NEW.id_operador IS NULL THEN
tipo := 'DESASIGNACION';
id_op_aux := OLD.id_operador;
IF NEW.id_operador IS NOT NULL THEN
INSERT INTO bitacora_asignaciones (id_vehiculo, id_usuario, fecha_hora_asignacion)
VALUES (NEW.id, NEW.id_operador, NOW());
ELSE
tipo := 'ASIGNACION';
id_op_aux := NEW.id_operador;
UPDATE bitacora_asignaciones
SET fecha_hora_desasignacion = NOW()
WHERE id = (SELECT id
FROM bitacora_asignaciones
WHERE id_vehiculo = NEW.id
ORDER BY fecha_hora_desasignacion DESC
LIMIT 1
);
END IF;
INSERT INTO bitacora_asig_operador (id_operador, id_vehiculo, fecha, tipo)
VALUES (id_op_aux, OLD.id, NOW(), tipo);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE TRIGGER actualizar_bitacora_operador_trg
CREATE OR REPLACE TRIGGER actualizar_bitacora_asignaciones_trg
AFTER UPDATE ON vehiculo
FOR EACH ROW
EXECUTE FUNCTION actualizar_bitacora_operador();
WHEN (OLD.id_operador IS DISTINCT FROM NEW.id_operador)
EXECUTE FUNCTION actualizar_bitacora_asignaciones();

0 comments on commit 7fcc8cf

Please sign in to comment.