|
| 1 | +import Model, { attr, belongsTo } from '@ember-data/model'; |
| 2 | +import { computed } from '@ember/object'; |
| 3 | +import { format as formatDate, isValid as isValidDate, formatDistanceToNow } from 'date-fns'; |
| 4 | + |
| 5 | +export default class MaintenanceScheduleModel extends Model { |
| 6 | + /** @ids */ |
| 7 | + @attr('string') uuid; |
| 8 | + @attr('string') public_id; |
| 9 | + @attr('string') company_uuid; |
| 10 | + |
| 11 | + /** @polymorphic relationships */ |
| 12 | + @belongsTo('maintenance-subject', { polymorphic: true, async: false }) subject; |
| 13 | + @belongsTo('facilitator', { polymorphic: true, async: false }) default_assignee; |
| 14 | + /** @computed names — server-side convenience fields (read-only) */ |
| 15 | + @attr('string') subject_name; |
| 16 | + @attr('string') default_assignee_name; |
| 17 | + |
| 18 | + /** @attributes */ |
| 19 | + @attr('string') code; |
| 20 | + @attr('string') title; |
| 21 | + @attr('string') description; |
| 22 | + @attr('string') name; |
| 23 | + @attr('string') type; |
| 24 | + @attr('string') status; |
| 25 | + @attr('string') interval_method; |
| 26 | + |
| 27 | + /** @interval — time-based */ |
| 28 | + @attr('string') interval_type; |
| 29 | + @attr('number') interval_value; |
| 30 | + @attr('string') interval_unit; |
| 31 | + |
| 32 | + /** @interval — distance / engine-hours */ |
| 33 | + @attr('number') interval_distance; |
| 34 | + @attr('number') interval_engine_hours; |
| 35 | + |
| 36 | + /** @baseline readings */ |
| 37 | + @attr('number') last_service_odometer; |
| 38 | + @attr('number') last_service_engine_hours; |
| 39 | + @attr('date') last_service_date; |
| 40 | + |
| 41 | + /** @next-due thresholds */ |
| 42 | + @attr('date') next_due_date; |
| 43 | + @attr('number') next_due_odometer; |
| 44 | + @attr('number') next_due_engine_hours; |
| 45 | + |
| 46 | + /** @work-order defaults */ |
| 47 | + @attr('string') default_priority; |
| 48 | + |
| 49 | + @attr('string') instructions; |
| 50 | + @attr('raw') meta; |
| 51 | + @attr('string') slug; |
| 52 | + |
| 53 | + /** @reminders — array of integer day offsets, e.g. [15, 7, 3] */ |
| 54 | + @attr('raw') reminder_offsets; |
| 55 | + |
| 56 | + /** @dates */ |
| 57 | + @attr('date') last_triggered_at; |
| 58 | + @attr('date') deleted_at; |
| 59 | + @attr('date') created_at; |
| 60 | + @attr('date') updated_at; |
| 61 | + |
| 62 | + /** @computed */ |
| 63 | + @computed('status') get isActive() { |
| 64 | + return this.status === 'active'; |
| 65 | + } |
| 66 | + |
| 67 | + @computed('status') get isPaused() { |
| 68 | + return this.status === 'paused'; |
| 69 | + } |
| 70 | + |
| 71 | + @computed('next_due_date') get nextDueAt() { |
| 72 | + if (!isValidDate(this.next_due_date)) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + return formatDate(this.next_due_date, 'yyyy-MM-dd HH:mm'); |
| 76 | + } |
| 77 | + |
| 78 | + @computed('next_due_date') get nextDueAtShort() { |
| 79 | + if (!isValidDate(this.next_due_date)) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + return formatDate(this.next_due_date, 'dd, MMM yyyy'); |
| 83 | + } |
| 84 | + |
| 85 | + @computed('next_due_date') get nextDueAgo() { |
| 86 | + if (!isValidDate(this.next_due_date)) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + return formatDistanceToNow(this.next_due_date, { addSuffix: true }); |
| 90 | + } |
| 91 | + |
| 92 | + @computed('updated_at') get updatedAgo() { |
| 93 | + if (!isValidDate(this.updated_at)) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + return formatDistanceToNow(this.updated_at); |
| 97 | + } |
| 98 | + |
| 99 | + @computed('updated_at') get updatedAt() { |
| 100 | + if (!isValidDate(this.updated_at)) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + return formatDate(this.updated_at, 'yyyy-MM-dd HH:mm'); |
| 104 | + } |
| 105 | + |
| 106 | + @computed('updated_at') get updatedAtShort() { |
| 107 | + if (!isValidDate(this.updated_at)) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + return formatDate(this.updated_at, 'dd, MMM'); |
| 111 | + } |
| 112 | + |
| 113 | + @computed('created_at') get createdAgo() { |
| 114 | + if (!isValidDate(this.created_at)) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + return formatDistanceToNow(this.created_at); |
| 118 | + } |
| 119 | + |
| 120 | + @computed('created_at') get createdAt() { |
| 121 | + if (!isValidDate(this.created_at)) { |
| 122 | + return null; |
| 123 | + } |
| 124 | + return formatDate(this.created_at, 'yyyy-MM-dd HH:mm'); |
| 125 | + } |
| 126 | + |
| 127 | + @computed('created_at') get createdAtShort() { |
| 128 | + if (!isValidDate(this.created_at)) { |
| 129 | + return null; |
| 130 | + } |
| 131 | + return formatDate(this.created_at, 'dd, MMM'); |
| 132 | + } |
| 133 | +} |
0 commit comments