Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/data_access/employment.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Meta:
"should_force_nb_worker_info",
"should_see_certificate_info",
"is_acknowledged",
"last_active_at",
)

id = graphene.Field(
Expand Down Expand Up @@ -107,6 +108,11 @@ class Meta:
graphene.Boolean,
description="Indique si l'on doit forcer la demande des informations liées au nombre de chauffeurs pour ce rattachement",
)
last_active_at = graphene.Field(
TimeStamp,
required=False,
description="Horodatage de la dernière activité du salarié",
)

@with_authorization_policy(
only_self_employment,
Expand Down
2 changes: 2 additions & 0 deletions app/models/employment.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class Employment(UserEventBaseModel, Dismissable, HasBusiness):
)
team = db.relationship(Team, backref="employments")

last_active_at = db.Column(db.DateTime, index=True, nullable=True)

# Needed for anonymization process if the submitter is still linked to an active user
@declared_attr
def submitter_id(cls):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""add_mission_trigger_to_populate_last_active_at_in_employment

Revision ID: de1be8351df4
Revises: 1c6085670a9f
Create Date: 2025-12-16 23:22:14.012041

"""

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "de1be8351df4"
down_revision = "1c6085670a9f"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"employment",
sa.Column("last_active_at", sa.DateTime(), nullable=True, index=True),
)
op.execute(
"""
CREATE OR REPLACE FUNCTION insert_last_active_at()
RETURNS TRIGGER AS $$
BEGIN
UPDATE employment e
SET last_active_at = m.creation_time
FROM mission m
WHERE m.id = NEW.mission_id
AND e.user_id = NEW.user_id
AND e.validation_status = 'approved'
AND e.company_id = m.company_id
AND e.end_date IS NULL
AND e.dismissed_at IS NULL
AND (e.last_active_at < m.creation_time OR e.last_active_at IS NULL);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER after_insert_mission_end
AFTER INSERT ON mission_end
FOR EACH ROW
EXECUTE FUNCTION insert_last_active_at();
"""
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.execute(
"""
DROP TRIGGER IF EXISTS after_insert_mission_end ON mission_end;
DROP FUNCTION IF EXISTS insert_last_active_at();
"""
)
op.drop_column("employment", "last_active_at")
# ### end Alembic commands ###