-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotices.php
More file actions
194 lines (169 loc) · 5.48 KB
/
Copy pathNotices.php
File metadata and controls
194 lines (169 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace App\Libraries;
use App\Entities\Notice;
use App\Models\JobModel;
use App\Models\UserModel;
use CodeIgniter\I18n\Time;
use Countable;
use Generator;
use IteratorAggregate;
use Tatter\Chat\Models\MessageModel;
use Tatter\Workflows\Factories\ActionFactory;
/**
* Notices Class
*/
final class Notices implements Countable, IteratorAggregate
{
/**
* The collection of Notices.
*
* @var Notice[]
*/
private $notices = [];
/**
* Creates a new instance from cache - falls back to detection.
*
* @return self
*/
public static function createFromCache()
{
return new self(cache('notices'));
}
/**
* Loads the Filesystem helper and adds any initial files.
*
* @param Notice[] $notices Any initial notices
*/
public function __construct(?array $notices = null)
{
$this->notices = $notices;
if ($notices === null) {
$this->detect();
}
}
/**
* Scans for and stores each Notice
*/
public function detect(): void
{
$this->notices = [];
$this->getFromStaffJobs();
$this->getFromClientChats();
cache()->save('notices', $this->notices);
}
/**
* Detects all active Jobs awaiting staff action.
*/
private function getFromStaffJobs(): void
{
foreach (model(JobModel::class)->builder()
->select('jobs.*, users.id AS user_id, users.firstname, users.lastname, stages.action_id')
->join('jobs_users', 'jobs.id = jobs_users.job_id', 'left')
->join('users', 'jobs_users.user_id = users.id', 'left')
->join('stages', 'jobs.stage_id = stages.id', 'inner')
->where('jobs.deleted_at', null)
->get()->getResultArray() as $row) {
// Filter by staff Actions
$action = ActionFactory::find($row['action_id'])::getAttributes();
if ($action['role'] === 'manageJobs') {
$this->notices[$row['id']] = new Notice([
'job_id' => $row['id'],
'job_name' => $row['name'],
'user_id' => $row['user_id'],
'user_name' => $row['firstname'] . ' ' . $row['lastname'],
'status' => 'Awaiting Staff',
'content' => $action['summary'] ?: '[empty]',
'created_at' => $row['updated_at'],
]);
}
}
}
/**
* Detects all recent client Chat activity.
*/
private function getFromClientChats(): void
{
// Get all "Staff"
if (! $staff = model(UserModel::class)->findStaffIds()) {
return;
}
helper(['text']);
$messages = [];
// Get the last week of messages
$rows = model(MessageModel::class)
->builder()
->select('chat_messages.*, chat_conversations.uid, chat_participants.user_id')
->join('chat_conversations', 'chat_messages.conversation_id = chat_conversations.id')
->join('chat_participants', 'chat_messages.participant_id = chat_participants.id')
->like('uid', 'job-', 'after') // Bug workaround by not fully-qualifying the field
->where('chat_messages.created_at >', new Time('-1 week'))
->get()->getResultArray();
// Only keep messages without a staff response
foreach ($rows as $row) {
[, $jobId] = explode('-', $row['uid']);
if (in_array($row['user_id'], $staff, true)) {
unset($messages[$jobId]);
} else {
$messages[$jobId] = $row;
}
}
// Create Notices from the remaining rows
foreach ($messages as $jobId => $row) {
$job = model(JobModel::class)->withDeleted()->find($jobId);
$user = model(UserModel::class)->withDeleted()->find($row['user_id']);
$this->notices[$jobId] = new Notice([
'job_id' => $job->id,
'job_name' => $job->name,
'user_id' => $user->id,
'user_name' => $user->name,
'status' => 'Client Message',
'content' => character_limiter($row['content'], 100) ?: '[empty]',
'created_at' => $row['created_at'],
]);
}
}
/**
* Pushes a new Notice into the collection
*
* @return $this
*/
public function push(Notice $notice)
{
$this->notices[$notice->job_id] = $notice;
return $this;
}
/**
* Optimizes and returns the Notices.
*
* @return Notice[]
*/
private function get(): array
{
$sort = array_column($this->notices, 'sort');
array_multisort($sort, SORT_DESC, SORT_NUMERIC, $this->notices, SORT_DESC);
return $this->notices;
}
//--------------------------------------------------------------------
// Interface Methods
//--------------------------------------------------------------------
/**
* Returns the current number of Notices in the collection.
* Fulfills Countable.
*/
public function count(): int
{
return count($this->get());
}
/**
* Yields as an Iterator for the current Notices.
* Fulfills IteratorAggregate.
*
* @return Generator<Notice>
*/
public function getIterator(): Generator
{
foreach ($this->get() as $notice) {
yield $notice;
}
}
}