-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathitem.ex
414 lines (341 loc) · 11 KB
/
item.ex
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
defmodule App.Item do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
alias PaperTrail
alias App.{Repo, Tag, ItemTag, Timer}
alias __MODULE__
require Logger
@derive {Jason.Encoder,
except: [:__meta__, :__struct__, :timer, :inserted_at, :updated_at]}
schema "items" do
field :cid, :string
field :person_id, :integer
field :status, :integer
field :text, :string
has_many :timer, Timer
many_to_many(:tags, Tag, join_through: ItemTag, on_replace: :delete)
timestamps()
end
@doc false
def changeset(item, attrs) do
item
|> cast(attrs, [:cid, :person_id, :status, :text])
|> validate_required([:text, :person_id])
|> App.Cid.put_cid()
end
def changeset_with_tags(item, attrs) do
changeset(item, attrs)
|> put_assoc(:tags, attrs.tags)
end
def draft_changeset(item, attrs) do
item
|> cast(attrs, [:person_id, :status, :text])
|> validate_required([:person_id])
|> App.Cid.put_cid()
end
@doc """
`create_item/1` creates an `item`.
## Examples
iex> create_item(%{text: "Learn LiveView"})
{:ok, %Item{}}
iex> create_item(%{text: nil})
{:error, %Ecto.Changeset{}}
"""
def create_item(attrs) do
%Item{}
|> changeset(attrs)
|> PaperTrail.insert(originator: %{id: Map.get(attrs, :person_id, 0)})
|> App.ListItems.add_papertrail_item_to_all_list()
end
@doc """
Creates an `item` with tags.
## Examples
iex> create_item_with_tags(%{text: "Learn LiveView", tags: [tag1, tag2]})
{:ok, %Item{}}
iex> create_item_with_tags(%{text: nil})
{:error, %Ecto.Changeset{}}
"""
def create_item_with_tags(attrs) do
%Item{}
|> changeset_with_tags(attrs)
|> PaperTrail.insert(originator: %{id: Map.get(attrs, :person_id, 0)})
|> App.ListItems.add_papertrail_item_to_all_list()
end
@doc """
`get_item!/1` gets a single Item.
Raises `Ecto.NoResultsError` if the Item does not exist.
## Examples
iex> get_item!(123)
%Item{}
iex> get_item!(456)
** (Ecto.NoResultsError)
"""
def get_item!(id) do
Item
|> Repo.get!(id)
|> Repo.preload(tags: from(t in Tag, order_by: t.text))
end
def get_draft_item(person_id) do
Repo.get_by(Item, status: 7, person_id: person_id) ||
Repo.insert!(%Item{person_id: person_id, status: 7})
end
@doc """
`get_item/1` gets a single Item.
Returns nil if the Item does not exist
## Examples
iex> get_item(1)
%Timer{}
iex> get_item(1313)
nil
"""
def get_item(id, preload_tags \\ false) do
item =
Item
|> Repo.get(id)
if(preload_tags == true) do
item |> Repo.preload(tags: from(t in Tag, order_by: t.text))
else
item
end
end
@doc """
Returns the list of items where the status is different to "deleted"
## Examples
iex> list_items()
[%Item{}, ...]
"""
def list_items do
Item
|> where([i], is_nil(i.status) or i.status != 6)
|> Repo.all()
end
def list_person_items(person_id) do
Item
|> where(person_id: ^person_id)
|> Repo.all()
|> Repo.preload(tags: from(t in Tag, order_by: t.text))
end
@doc """
Updates a item.
## Examples
iex> update_item(item, %{field: new_value})
{:ok, %Item{}}
iex> update_item(item, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_item(%Item{} = item, attrs) do
item
|> Item.changeset(attrs)
|> PaperTrail.update(originator: %{id: Map.get(attrs, :person_id, 0)})
end
def update_draft(%Item{} = item, attrs) do
item
|> Item.draft_changeset(attrs)
|> Repo.update()
end
@doc """
Update an item and its associated tags
"""
# def update_item_with_tags(%Item{} = item, attrs) do
# item
# |> Item.changeset_with_tags(attrs)
# |> Repo.update()
# end
def delete_item(id) do
get_item!(id)
|> Item.changeset(%{status: 6})
|> Repo.update()
end
# defp reorder_list_to_add_item(%Item{position: position}) do
# # Increments the positions above a given position.
# # We are making space for the item to be added.
# from(i in Item,
# where: i.position > ^position,
# update: [inc: [position: 1]]
# )
# |> Repo.update_all([])
# end
def all_items_for_person(person_id) do
Item
|> where(person_id: ^person_id)
|> Repo.all()
end
# 🐲 H E R E B E D R A G O N S! 🐉
# ⏳ Working with Time is all Dragons! 🙄
# 👩💻 Feedback/Pairing/Refactoring Welcome! 🙏
@doc """
`items_with_timers/1` Returns a List of items with the latest associated timers.
This list is ordered with the position that is detailed inside the Items schema.
## Examples
iex> items_with_timers()
[
%{text: "hello", person_id: 1, status: 2, start: 2022-07-14 09:35:18},
%{text: "world", person_id: 2, status: 7, start: 2022-07-15 04:20:42}
]
"""
#
def items_with_timers(person_id \\ 0) do
all_list = App.List.get_all_list_for_person(person_id)
# dbg(all_list)
seq = App.ListItems.get_list_items(all_list.cid)
# dbg(seq)
sql = """
SELECT i.id, i.cid, i.text, i.status, i.person_id, i.updated_at,
t.start, t.stop, t.id as timer_id
FROM items i
FULL JOIN timers AS t ON t.item_id = i.id
WHERE i.cid = any($1)
AND i.status IS NOT NULL
AND i.text IS NOT NULL
ORDER BY timer_id ASC;
"""
values =
Ecto.Adapters.SQL.query!(Repo, sql, [seq])
|> map_columns_to_values()
items_tags =
list_person_items(person_id)
|> Enum.reduce(%{}, fn i, acc -> Map.put(acc, i.id, i) end)
accumulate_item_timers(values, seq)
|> Enum.map(fn t ->
if t != nil do
Map.put(t, :tags, items_tags[t.id].tags)
end
end)
end
@doc """
`map_columns_to_values/1` takes an Ecto SQL query result
which has the List of columns and rows separate
and returns a List of Maps where the keys are the column names and values the data.
Sadly, Ecto returns rows without column keys so we have to map them manually:
ref: https://groups.google.com/g/elixir-ecto/c/0cubhSd3QS0/m/DLdQsFrcBAAJ
"""
def map_columns_to_values(res) do
Enum.map(res.rows, fn row ->
Enum.zip(res.columns, row)
|> Map.new()
|> AtomicMap.convert(safe: false)
end)
end
@doc """
`map_timer_diff/1` transforms a list of items_with_timers
into a flat map where the key is the timer_id and the value is the difference
between timer.stop and timer.start
If there is no active timer return {0, 0}.
If there is no timer.stop return Now - timer.start
## Examples
iex> list = [
%{ stop: nil, id: 3, start: nil, timer_id: nil },
%{ stop: ~N[2022-07-17 11:18:24], id: 1, start: ~N[2022-07-17 11:18:18], timer_id: 1 },
%{ stop: ~N[2022-07-17 11:18:31], id: 1, start: ~N[2022-07-17 11:18:26], timer_id: 2 },
%{ stop: ~N[2022-07-17 11:18:24], id: 2, start: ~N[2022-07-17 11:18:00], timer_id: 3 },
%{ stop: nil, id: 2, start: seven_seconds_ago, timer_id: 4 }
]
iex> map_timer_diff(list)
%{0 => 0, 1 => 6, 2 => 5, 3 => 24, 4 => 7}
"""
def map_timer_diff(list) do
Map.new(list, fn item ->
if is_nil(item.timer_id) do
# item without any active timer
{0, 0}
else
{item.timer_id, timer_diff(item)}
end
end)
end
@doc """
`timer_diff/1` calculates the difference between timer.stop and timer.start
If there is no active timer OR timer has not ended return 0.
The reasoning is: an *active* timer (no end) does not have to
be subtracted from the timer.start in the UI ...
Again, DRAGONS!
"""
def timer_diff(timer) do
# ignore timers that have not ended (current timer is factored in the UI!)
if is_nil(timer.stop) do
0
else
NaiveDateTime.diff(timer.stop, timer.start)
end
end
@doc """
`accumulate_item_timers/1` aggregates the elapsed time
for all the timers associated with an item
and then subtract that time from the start value of the *current* active timer.
This is done to create the appearance that a single timer is being started/stopped
when in fact there are multiple timers in the backend.
For MVP we *could* have just had a single timer ...
and given the "ugliness" of this code, I wish I had done that!!
But the "USP" of our product [IMO] is that
we can track the completion of a task across multiple work sessions.
And having multiple timers is the *only* way to achieve that.
If you can think of a better way of achieving the same result,
please share: github.com/dwyl/app-mvp-phoenix/issues/103
This function *relies* on the list of items being ordered by timer_id ASC
because it "pops" the last timer and ignores it to avoid double-counting.
"""
def accumulate_item_timers(items_with_timers, seq) do
# e.g: %{0 => 0, 1 => 6, 2 => 5, 3 => 24, 4 => 7}
timer_id_diff_map = map_timer_diff(items_with_timers)
# e.g: %{1 => [2, 1], 2 => [4, 3], 3 => []}
item_id_timer_id_map =
Map.new(items_with_timers, fn i ->
{i.id,
Enum.map(items_with_timers, fn it ->
if i.id == it.id, do: it.timer_id, else: nil
end)
# stackoverflow.com/questions/46339815/remove-nil-from-list
|> Enum.reject(&is_nil/1)}
end)
# this one is "wasteful" but I can't think of how to simplify it ...
item_id_timer_diff_map =
Map.new(items_with_timers, fn item ->
timer_id_list = Map.get(item_id_timer_id_map, item.id, [0])
# Remove last item from list before summing to avoid double-counting
{_, timer_id_list} = List.pop_at(timer_id_list, -1)
{item.id,
Enum.reduce(timer_id_list, 0, fn timer_id, acc ->
Map.get(timer_id_diff_map, timer_id) + acc
end)}
end)
# creates a nested map: %{ item.cid: %{id: 1, text: "my item", etc.}}
cid_item_map =
Map.new(items_with_timers, fn item ->
time_elapsed = Map.get(item_id_timer_diff_map, item.id)
start =
if is_nil(item.start),
do: nil,
else: NaiveDateTime.add(item.start, -time_elapsed)
{item.cid, %{item | start: start}}
end)
# return the list of items in the order of seq
Enum.map(seq, fn cid -> cid_item_map[cid] end)
end
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Below this point is item transition code that will be DELETED! #
# We just need it to update all existing items to add cid ... #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
@doc """
`update_all_items_cid/0` updates all `item` records with a `cid` value.
This will not be needed once all records are transitioned.
"""
def update_all_items_cid do
items = list_items()
Enum.each(items, fn i ->
# coveralls-ignore-start
unless Map.has_key?(i, :cid) do
item = %{
person_id: i.person_id,
status: i.status,
text: i.text,
id: i.id
}
i
|> changeset(Map.put(item, :cid, Cid.cid(item)))
|> Repo.update()
end
# coveralls-ignore-stop
end)
end
end