July 4 - Update with recurring/scheduled inspections
This commit is contained in:
@@ -5,4 +5,5 @@ from app.models.inspection import (InspectionTemplate, ChecklistItem,
|
||||
from app.models.issue import Issue
|
||||
from app.models.project import Project, CustomerAssignment
|
||||
from app.models.api_token import RefreshToken, DeviceToken
|
||||
from app.models.notification_matrix import NotificationMatrix
|
||||
from app.models.notification_matrix import NotificationMatrix
|
||||
from app.models.inspection_schedule import InspectionSchedule
|
||||
@@ -0,0 +1,68 @@
|
||||
"""
|
||||
app/models/inspection_schedule.py
|
||||
---------------------------------
|
||||
Recurring inspection schedules (phase34).
|
||||
|
||||
An InspectionSchedule declares that a given template should be inspected at a
|
||||
given facility (optionally scoped to one area) by a given inspector on a fixed
|
||||
cadence. A token-protected cron endpoint (`/inspection-schedules/run`) walks
|
||||
all active, due schedules and *materialises* a real `Inspection` row in
|
||||
`in_progress` status — exactly as if the inspector had clicked "Start
|
||||
Inspection" — then notifies the assigned inspector. The inspector opens it from
|
||||
their queue and fills it out through the normal execute flow.
|
||||
|
||||
This is purely additive: no existing inspection behaviour changes. A schedule is
|
||||
just an automated `inspections.start()`.
|
||||
"""
|
||||
|
||||
from app import db
|
||||
from app.utils.time_utils import now_eastern
|
||||
|
||||
|
||||
class InspectionSchedule(db.Model):
|
||||
__tablename__ = 'inspection_schedules'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(255), nullable=False)
|
||||
|
||||
template_id = db.Column(
|
||||
db.Integer, db.ForeignKey('inspection_templates.id', ondelete='CASCADE'),
|
||||
nullable=False
|
||||
)
|
||||
facility_id = db.Column(
|
||||
db.Integer, db.ForeignKey('facilities.id', ondelete='CASCADE'),
|
||||
nullable=False
|
||||
)
|
||||
area_id = db.Column(
|
||||
db.Integer, db.ForeignKey('areas.id', ondelete='SET NULL'),
|
||||
nullable=True
|
||||
)
|
||||
inspector_id = db.Column(
|
||||
db.Integer, db.ForeignKey('users.id', ondelete='CASCADE'),
|
||||
nullable=False
|
||||
)
|
||||
|
||||
# daily | weekly | monthly | quarterly — mirrors InspectionTemplate.frequency
|
||||
frequency = db.Column(
|
||||
db.Enum('daily', 'weekly', 'monthly', 'quarterly'),
|
||||
nullable=False, default='weekly'
|
||||
)
|
||||
active = db.Column(db.Boolean, nullable=False, default=True)
|
||||
|
||||
created_by = db.Column(
|
||||
db.Integer, db.ForeignKey('users.id', ondelete='SET NULL'),
|
||||
nullable=True
|
||||
)
|
||||
created_at = db.Column(db.DateTime, nullable=False, default=now_eastern)
|
||||
last_run_at = db.Column(db.DateTime, nullable=True) # last successful materialisation
|
||||
next_run_at = db.Column(db.DateTime, nullable=True) # when the next inspection is due
|
||||
|
||||
# Relationships — explicit foreign_keys because two columns point at users.id.
|
||||
template = db.relationship('InspectionTemplate', foreign_keys=[template_id])
|
||||
facility = db.relationship('Facility', foreign_keys=[facility_id])
|
||||
area = db.relationship('Area', foreign_keys=[area_id])
|
||||
inspector = db.relationship('User', foreign_keys=[inspector_id])
|
||||
creator = db.relationship('User', foreign_keys=[created_by])
|
||||
|
||||
def __repr__(self):
|
||||
return f'<InspectionSchedule {self.id} {self.name!r} {self.frequency}>'
|
||||
@@ -29,6 +29,10 @@ EVENT_SCORE_ALERT = 'score_alert'
|
||||
|
||||
EVENT_ADMIN_BROADCAST = 'admin_broadcast' # bulk messages sent by admin to all apps
|
||||
|
||||
# Fired when a recurring InspectionSchedule materialises a due inspection and
|
||||
# notifies the assigned inspector (phase34).
|
||||
EVENT_INSPECTION_SCHEDULED = 'inspection_scheduled'
|
||||
|
||||
ALL_EVENT_TYPES = {
|
||||
EVENT_ISSUE_ASSIGNED: 'Issue assigned to me',
|
||||
EVENT_ISSUE_STATUS: 'Issue status changed',
|
||||
@@ -38,6 +42,7 @@ ALL_EVENT_TYPES = {
|
||||
EVENT_INSPECTION_DONE: 'Inspection completed',
|
||||
EVENT_SLA_ALERT: 'SLA at-risk / breached alerts',
|
||||
EVENT_ADMIN_BROADCAST: 'Admin broadcast (system announcements)',
|
||||
EVENT_INSPECTION_SCHEDULED: 'Scheduled inspection due (assigned to me)',
|
||||
# Customer-facing — only relevant for customer role accounts
|
||||
EVENT_CUSTOMER_INSPECTION_DONE: 'Inspection completed at my facility (portal)',
|
||||
EVENT_CUSTOMER_ISSUE_UPDATED: 'Issue created or updated at my facility (portal)',
|
||||
|
||||
Reference in New Issue
Block a user