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
use crate::db::pool::DbPool;
use crate::db::queries::{insert_event, load_events_by_date, load_pair_by_index};
use crate::errors::{AppError, AppResult};
use crate::models::event::Event;
use crate::models::event_type::EventType;
use crate::models::location::Location;
use crate::ui::messages::success;
use chrono::{NaiveDate, NaiveTime};
use rusqlite::params;
/// High-level business logic for the `add` command.
pub struct AddLogic;
fn upsert_event(conn: &rusqlite::Connection, ev: &Event) -> AppResult<()> {
if ev.id == 0 {
insert_event(conn, ev)?;
} else {
crate::db::queries::update_event(conn, ev)?;
}
Ok(())
}
impl AddLogic {
#[allow(clippy::too_many_arguments)]
pub fn apply(
pool: &mut DbPool,
date: NaiveDate,
position: Location,
start: Option<NaiveTime>,
lunch: Option<i32>,
work_gap: Option<bool>,
end: Option<NaiveTime>,
edit_mode: bool,
edit_pair: Option<usize>,
pos: Option<String>,
) -> AppResult<()> {
// ------------------------------------------------
// Resolve final position (only if --pos is provided)
// ------------------------------------------------
let pos_final = match &pos {
Some(code) => Location::from_code(code).ok_or_else(|| {
AppError::InvalidPosition(format!(
"Invalid location code '{}'. Use a valid code such as 'office', 'remote', 'customer', ...",
code
))
})?,
None => position,
};
// ------------------------------------------------
// 1️⃣ EDIT MODE
// ------------------------------------------------
if edit_mode {
let pair_num = edit_pair
.ok_or_else(|| AppError::InvalidTime("Missing --pair when using --edit.".into()))?;
let (mut ev_in, mut ev_out) = load_pair_by_index(&pool.conn, &date, pair_num)?;
// POSITION
if let Some(ref mut e) = ev_in
&& pos.is_some()
{
e.location = pos_final;
}
if let Some(ref mut e) = ev_out
&& pos.is_some()
{
e.location = pos_final;
}
// IN time
if let Some(start_time) = start {
if let Some(ref mut e) = ev_in {
e.time = start_time;
} else {
ev_in = Some(Event::new(
0,
date,
start_time,
EventType::In,
pos_final,
lunch,
false,
));
}
}
// OUT time
if let Some(end_time) = end {
if let Some(ref mut e) = ev_out {
e.time = end_time;
} else {
ev_out = Some(Event::new(
0,
date,
end_time,
EventType::Out,
pos_final,
Some(0),
false,
));
}
}
// LUNCH
if let Some(lunch_val) = lunch
&& let Some(ref mut e) = ev_out
{
e.lunch = Some(lunch_val);
}
// WORK GAP (solo se esplicitamente richiesto)
if let Some(wg) = work_gap {
if let Some(ref mut e) = ev_out {
e.work_gap = wg;
} else {
return Err(AppError::InvalidTime(
"Cannot modify work_gap: pair has no OUT event.".into(),
));
}
}
// Save
if let Some(ref e) = ev_in {
upsert_event(&pool.conn, e)?;
}
if let Some(ref e) = ev_out {
upsert_event(&pool.conn, e)?;
}
crate::db::queries::recalc_pairs_for_date(&mut pool.conn, &date)?;
let (icon, msg) = if Some(work_gap) == Some(Option::from(true)) {
("🔗", "Work gap enabled")
} else if Some(work_gap) == Some(Option::from(false)) {
("✂️", "Work gap removed")
} else {
("✏️", "Pair updated")
};
success(format!("{} {} for pair {}.", icon, msg, pair_num));
return Ok(());
}
// ------------------------------------------------
// 2️⃣ INSERT MODE
// ------------------------------------------------
let lunch_val = lunch.unwrap_or(0);
let date_str = date.to_string();
let wg = work_gap.unwrap_or(false); // 🔑 risolvo QUI
let events_today = load_events_by_date(pool, &date)?;
let has_events = !events_today.is_empty();
// --work-gap valido solo con OUT
if wg && end.is_none() {
return Err(AppError::InvalidTime(
"--work-gap can only be used when adding an OUT event.".into(),
));
}
// CASE A: only lunch update
if start.is_none() && end.is_none() && lunch.is_some() {
if !has_events {
return Err(AppError::InvalidTime(
"Cannot set lunch on a date with no events.".into(),
));
}
pool.conn.execute(
r#"
UPDATE events
SET lunch_break = ?1
WHERE id = (
SELECT id FROM events
WHERE date = ?2
ORDER BY time DESC
LIMIT 1
)
"#,
params![lunch_val, &date_str],
)?;
success(format!(
"Lunch updated to {} minutes for {}.",
lunch_val, date_str
));
return Ok(());
}
// CASE B: nothing to do
if start.is_none() && end.is_none() {
return Err(AppError::InvalidTime(
"Nothing to do: specify at least --in, --out or --lunch.".into(),
));
}
// CASE C: IN only
if let Some(start_time) = start
&& end.is_none()
{
let ev_in = Event::new(
0,
date,
start_time,
EventType::In,
pos_final,
Some(lunch_val),
false,
);
insert_event(&pool.conn, &ev_in)?;
crate::db::queries::recalc_pairs_for_date(&mut pool.conn, &date)?;
success(format!("Added IN at {} on {}.", start_time, date_str));
return Ok(());
}
// CASE D: OUT only
if start.is_none()
&& let Some(end_time) = end
{
let last_in = events_today
.iter()
.rev()
.find(|ev| ev.kind == EventType::In)
.cloned()
.ok_or_else(|| {
AppError::InvalidTime("Cannot add OUT without a previous IN.".into())
})?;
if end_time <= last_in.time {
return Err(AppError::InvalidTime(
"OUT must be later than the previous IN.".into(),
));
}
let out_position = if pos.is_some() {
pos_final
} else {
last_in.location
};
let ev_out = Event::new(
0,
date,
end_time,
EventType::Out,
out_position,
Some(lunch_val),
wg,
);
insert_event(&pool.conn, &ev_out)?;
crate::db::queries::recalc_pairs_for_date(&mut pool.conn, &date)?;
success(format!(
"Added OUT on {} ({} → {}).",
date_str, last_in.time, end_time
));
return Ok(());
}
// CASE E: full pair
if let (Some(start_time), Some(end_time)) = (start, end) {
if end_time <= start_time {
return Err(AppError::InvalidTime("END must be later than IN.".into()));
}
let in_position = pos_final;
let ev_in = Event::new(
0,
date,
start_time,
EventType::In,
in_position,
Some(lunch_val),
false,
);
let ev_out = Event::new(0, date, end_time, EventType::Out, in_position, Some(0), wg);
insert_event(&pool.conn, &ev_in)?;
insert_event(&pool.conn, &ev_out)?;
crate::db::queries::recalc_pairs_for_date(&mut pool.conn, &date)?;
success(format!(
"Added IN/OUT pair on {}: {} → {}.",
date_str, start_time, end_time
));
return Ok(());
}
Err(AppError::InvalidTime(
"Unhandled combination of parameters.".into(),
))
}
}