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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
|
use crate::api::ApiClient;
use crate::models::api_error_detail;
use serde_json::{Map, Value};
/// Asset CRUD operations that can be reused across different entity types
pub struct AssetOperations;
impl AssetOperations {
/// Apply updates to one or more assets
pub fn apply_updates<T>(
api: &ApiClient,
updated: Map<String, Value>,
pending_edit_ids: &mut Vec<i64>,
easy_dialog_item_id: Option<&str>,
advanced_dialog_item_id: Option<&str>,
find_asset_fn: impl Fn(i64) -> Option<T>,
limit: Option<u32>,
reload_fn: impl FnOnce(&ApiClient, Option<u32>),
) where
T: serde::Serialize,
{
let ids: Vec<i64> = std::mem::take(pending_edit_ids);
log::info!("Pending edit IDs from ribbon: {:?}", ids);
if ids.is_empty() {
// Try to get ID from either dialog or from the embedded ID in the diff
let item_id = updated
.get("__editor_item_id")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
.or_else(|| easy_dialog_item_id.map(|s| s.to_string()))
.or_else(|| advanced_dialog_item_id.map(|s| s.to_string()));
if let Some(id_str) = item_id {
if let Ok(id) = id_str.parse::<i64>() {
// Compute diff against the current asset so we only send changed fields
let only_changed = if let Some(orig) = find_asset_fn(id) {
log::info!("Found original asset data for comparison");
let orig_value = serde_json::to_value(&orig).unwrap_or_default();
let mut diff = Map::new();
for (k, v) in updated.iter() {
match orig_value.get(k) {
Some(ov) if ov == v => {
log::debug!("Field '{}' unchanged: {:?}", k, v);
}
_ => {
log::info!(
"Field '{}' CHANGED: old={:?}, new={:?}",
k,
orig_value.get(k),
v
);
diff.insert(k.clone(), v.clone());
}
}
}
log::info!("Final diff map to send: {:?}", diff);
diff
} else {
log::warn!(
"Asset ID {} not found in local cache, sending full update",
id
);
updated.clone()
};
if only_changed.is_empty() {
log::warn!("No changes detected - update will be skipped!");
} else {
log::info!("Calling update_one with {} changes", only_changed.len());
}
Self::update_one(api, id, &only_changed);
} else {
log::error!("FAILED to parse asset ID: '{}' - UPDATE SKIPPED!", id_str);
}
} else {
log::error!("NO ITEM ID FOUND - This is the bug! UPDATE COMPLETELY SKIPPED!");
log::error!("Easy dialog item_id: {:?}", easy_dialog_item_id);
log::error!("Advanced dialog item_id: {:?}", advanced_dialog_item_id);
}
} else {
log::info!("Bulk edit mode for {} assets", ids.len());
// Bulk edit: apply provided fields to each id without diffing per-record
for id in ids {
log::info!("Bulk updating asset ID: {}", id);
Self::update_one(api, id, &updated);
}
}
reload_fn(api, limit);
}
/// Handle quick-add fields for category, zone, and supplier when editing
pub fn preprocess_quick_adds(api: &ApiClient, data: &mut Map<String, Value>) {
// CATEGORY
let new_cat_name = data
.get("new_category_name")
.and_then(|v| v.as_str())
.unwrap_or("")
.trim()
.to_string();
let new_cat_code = data
.get("new_category_code")
.and_then(|v| v.as_str())
.unwrap_or("")
.trim()
.to_string();
let has_selected_cat = data.get("category_id").and_then(|v| v.as_i64()).is_some()
|| data
.get("category_id")
.and_then(|v| v.as_str())
.map(|s| !s.trim().is_empty())
.unwrap_or(false);
if !has_selected_cat && !new_cat_name.is_empty() && !new_cat_code.is_empty() {
let values = serde_json::json!({
"category_name": new_cat_name,
"category_code": new_cat_code,
});
match api.insert("categories", values) {
Ok(resp) if resp.success => {
if let Some(id) = resp.data {
data.insert("category_id".into(), Value::Number((id as i64).into()));
}
}
Ok(resp) => {
log::error!(
"Quick-add category failed: {}",
api_error_detail(&resp.error)
);
}
Err(e) => {
log::error!("Quick-add category err: {}", e);
}
}
}
// ZONE
let new_zone_name = data
.get("new_zone_name")
.and_then(|v| v.as_str())
.unwrap_or("")
.trim()
.to_string();
// Prefer new_zone_mini_code (new), fallback to legacy new_zone_code
let new_zone_mini_code = data
.get("new_zone_mini_code")
.and_then(|v| v.as_str())
.unwrap_or("")
.trim()
.to_string();
let legacy_new_zone_code = data
.get("new_zone_code")
.and_then(|v| v.as_str())
.unwrap_or("")
.trim()
.to_string();
let parent_id_val = data.get("new_zone_parent_id").cloned();
let has_selected_zone = data.get("zone_id").and_then(|v| v.as_i64()).is_some()
|| data
.get("zone_id")
.and_then(|v| v.as_str())
.map(|s| !s.trim().is_empty())
.unwrap_or(false);
if !has_selected_zone && !new_zone_name.is_empty() {
// parent optional, parse int if provided
let mut zone_obj = Map::new();
zone_obj.insert("zone_name".into(), Value::String(new_zone_name));
// Determine mini_code to use
let mini_code = if !new_zone_mini_code.is_empty() {
new_zone_mini_code.clone()
} else {
legacy_new_zone_code.clone()
};
if !mini_code.is_empty() {
// Compute full zone_code using parent if provided
let full_code = if let Some(v) = parent_id_val.clone() {
if let Some(pid) = v
.as_i64()
.or_else(|| v.as_str().and_then(|s| s.parse::<i64>().ok()))
{
// Fetch parent's zone_code
if let Ok(resp) = api.select(
"zones",
Some(vec!["zone_code".into()]),
Some(serde_json::json!({"id": pid})),
None,
Some(1),
) {
if resp.success {
if let Some(rows) = resp.data {
if let Some(row) = rows.into_iter().next() {
let pcode = row
.get("zone_code")
.and_then(|v| v.as_str())
.unwrap_or("");
format!("{}-{}", pcode, mini_code)
} else {
mini_code.clone()
}
} else {
mini_code.clone()
}
} else {
mini_code.clone()
}
} else {
mini_code.clone()
}
} else {
mini_code.clone()
}
} else {
mini_code.clone()
};
// Send both mini_code and computed full zone_code
zone_obj.insert("mini_code".into(), Value::String(mini_code));
zone_obj.insert("zone_code".into(), Value::String(full_code));
}
if let Some(v) = parent_id_val {
if let Some(n) = v.as_i64() {
zone_obj.insert("parent_id".into(), Value::Number(n.into()));
} else if let Some(s) = v.as_str() {
if let Ok(n) = s.parse::<i64>() {
zone_obj.insert("parent_id".into(), Value::Number(n.into()));
}
}
}
match api.insert("zones", Value::Object(zone_obj)) {
Ok(resp) if resp.success => {
if let Some(id) = resp.data {
data.insert("zone_id".into(), Value::Number((id as i64).into()));
}
}
Ok(resp) => {
log::error!("Quick-add zone failed: {}", api_error_detail(&resp.error));
}
Err(e) => {
log::error!("Quick-add zone err: {}", e);
}
}
}
// SUPPLIER
let new_supplier_name = data
.get("new_supplier_name")
.and_then(|v| v.as_str())
.unwrap_or("")
.trim()
.to_string();
let has_selected_supplier = data.get("supplier_id").and_then(|v| v.as_i64()).is_some()
|| data
.get("supplier_id")
.and_then(|v| v.as_str())
.map(|s| !s.trim().is_empty())
.unwrap_or(false);
if !has_selected_supplier && !new_supplier_name.is_empty() {
let values = serde_json::json!({ "name": new_supplier_name });
match api.insert("suppliers", values) {
Ok(resp) if resp.success => {
if let Some(id) = resp.data {
data.insert("supplier_id".into(), Value::Number((id as i64).into()));
}
}
Ok(resp) => {
log::error!(
"Quick-add supplier failed: {}",
api_error_detail(&resp.error)
);
}
Err(e) => {
log::error!("Quick-add supplier err: {}", e);
}
}
}
}
/// Filter update data to only include allowed fields with proper type coercion
pub fn filtered_update_fields(data: &Map<String, Value>) -> Map<String, Value> {
// Allow only writable/meaningful asset fields (exclude IDs, timestamps, joined names)
let allowed = [
"asset_tag",
"asset_type",
"name",
"category_id",
"zone_id",
"zone_plus",
"zone_note",
"manufacturer",
"model",
"serial_number",
"status",
"label_template_id",
"price",
"purchase_date",
"warranty_until",
"expiry_date",
"supplier_id",
"lendable",
"lending_status",
"due_date",
"no_scan",
"quantity_available",
"quantity_total",
"quantity_used",
"minimum_role_for_lending",
"audit_task_id",
"asset_image",
"notes",
"additional_fields",
];
let allowed_set: std::collections::HashSet<&str> = allowed.iter().copied().collect();
let mut out = Map::new();
for (k, v) in data.iter() {
// Skip internal editor fields
if k.starts_with("__editor_") {
continue;
}
// Map template-only "description" to asset "notes" to avoid DB column mismatch
if k == "description" {
let coerced = if let Some(s) = v.as_str() {
Value::String(s.to_string())
} else if v.is_null() {
Value::Null
} else {
Value::String(v.to_string())
};
if !out.contains_key("notes") {
out.insert("notes".to_string(), coerced);
}
continue;
}
if !allowed_set.contains(k.as_str()) {
continue;
}
// Coerce common types where Advanced Editor may send strings
let coerced = match k.as_str() {
// Integers (IDs and quantities)
"category_id"
| "zone_id"
| "label_template_id"
| "supplier_id"
| "audit_task_id"
| "quantity_available"
| "quantity_total"
| "quantity_used"
| "minimum_role_for_lending" => {
if let Some(n) = v.as_i64() {
Value::Number(n.into())
} else if let Some(s) = v.as_str() {
if s.trim().is_empty() {
Value::Null
} else if let Ok(n) = s.trim().parse::<i64>() {
Value::Number(n.into())
} else {
Value::Null
}
} else {
v.clone()
}
}
// Booleans
"lendable" => {
if let Some(b) = v.as_bool() {
Value::Bool(b)
} else if let Some(s) = v.as_str() {
match s.trim().to_lowercase().as_str() {
"true" | "1" | "yes" => Value::Bool(true),
"false" | "0" | "no" => Value::Bool(false),
_ => v.clone(),
}
} else {
v.clone()
}
}
// Price as decimal number
"price" => {
if let Some(f) = v.as_f64() {
Value::Number(
serde_json::Number::from_f64(f)
.unwrap_or_else(|| serde_json::Number::from(0)),
)
} else if let Some(s) = v.as_str() {
if s.trim().is_empty() {
Value::Null
} else if let Ok(f) = s.trim().parse::<f64>() {
Value::Number(
serde_json::Number::from_f64(f)
.unwrap_or_else(|| serde_json::Number::from(0)),
)
} else {
Value::Null
}
} else {
v.clone()
}
}
// Date fields: accept YYYY-MM-DD strings; treat empty strings as NULL
"purchase_date" | "warranty_until" | "expiry_date" | "due_date" => {
if let Some(s) = v.as_str() {
let t = s.trim();
if t.is_empty() {
Value::Null
} else {
Value::String(t.to_string())
}
} else if v.is_null() {
Value::Null
} else {
// Fallback: stringify other types
Value::String(v.to_string())
}
}
// String fields - ensure they're strings (not null if empty)
"asset_tag" | "asset_type" | "name" | "manufacturer" | "model"
| "serial_number" | "status" | "zone_plus" | "zone_note" | "lending_status"
| "no_scan" | "notes" => {
if let Some(s) = v.as_str() {
Value::String(s.to_string())
} else if v.is_null() {
Value::Null
} else {
Value::String(v.to_string())
}
}
_ => v.clone(),
};
out.insert(k.clone(), coerced);
}
out
}
/// Update a single entity record
pub fn update_one(api: &ApiClient, id: i64, data: &Map<String, Value>) {
Self::update_one_table(api, "assets", id, data);
}
/// Update a single record in any table
pub fn update_one_table(api: &ApiClient, table: &str, id: i64, data: &Map<String, Value>) {
log::info!("=== UPDATE_ONE START for {} ID {} ===", table, id);
log::info!("Raw input data: {:?}", data);
let values_map = Self::filtered_update_fields(data);
log::info!("Filtered values_map: {:?}", values_map);
if values_map.is_empty() {
log::warn!(
"No allowed fields found after filtering for {} ID {}, SKIPPING UPDATE",
table,
id
);
log::warn!("Original data keys: {:?}", data.keys().collect::<Vec<_>>());
return;
}
let values = Value::Object(values_map.clone());
let where_clause = serde_json::json!({"id": id});
log::info!("SENDING UPDATE to server:");
log::info!(" TABLE: {}", table);
log::info!(" WHERE: {:?}", where_clause);
log::info!(" VALUES: {:?}", values);
match api.update(table, values, where_clause) {
Ok(resp) if resp.success => {
log::info!("Successfully updated {} ID {}", table, id);
}
Ok(resp) => {
log::error!(
"Server rejected update for {} ID {}: {}",
table,
id,
api_error_detail(&resp.error)
);
}
Err(e) => {
log::error!("Network/API error updating {} ID {}: {}", table, id, e);
}
}
log::info!("=== UPDATE_ONE END ===");
}
/// Insert a new asset record with preprocessing and return its DB id (if available)
pub fn insert_new_asset(
api: &ApiClient,
mut data: Map<String, Value>,
limit: Option<u32>,
reload_fn: impl FnOnce(&ApiClient, Option<u32>),
) -> Option<i64> {
log::info!("=== INSERT_NEW_ASSET START ===");
log::info!("Raw asset data: {:?}", data);
// Process quick-add fields first
Self::preprocess_quick_adds(api, &mut data);
// Ensure mandatory defaults if missing or blank
let needs_default = |v: Option<&Value>| -> bool {
match v {
None => true,
Some(Value::Null) => true,
Some(Value::String(s)) => s.trim().is_empty(),
_ => false,
}
};
if needs_default(data.get("asset_type")) {
data.insert("asset_type".into(), Value::String("N".to_string()));
}
if needs_default(data.get("status")) {
data.insert("status".into(), Value::String("Good".to_string()));
}
// Filter to allowed fields
let filtered_data = Self::filtered_update_fields(&data);
log::info!("Filtered asset data: {:?}", filtered_data);
if filtered_data.is_empty() {
log::error!("No valid data to insert");
return None;
}
let values = Value::Object(filtered_data);
log::info!("SENDING INSERT to server: {:?}", values);
let result = match api.insert("assets", values) {
Ok(resp) if resp.success => {
log::info!("Successfully created new asset");
let id = resp.data.map(|d| d as i64);
log::info!("New asset DB id from server: {:?}", id);
reload_fn(api, limit);
id
}
Ok(resp) => {
log::error!(
"Server rejected asset creation: {}",
api_error_detail(&resp.error)
);
None
}
Err(e) => {
log::error!("Network/API error creating asset: {}", e);
None
}
};
log::info!("=== INSERT_NEW_ASSET END ===");
result
}
/// Find an asset by ID in a collection
pub fn find_by_id<T>(
collection: &[T],
id: i64,
id_extractor: impl Fn(&T) -> Option<i64>,
) -> Option<T>
where
T: Clone,
{
collection
.iter()
.find(|item| id_extractor(item) == Some(id))
.cloned()
}
/// Get selected IDs from a collection based on row indices
#[allow(dead_code)]
pub fn get_selected_ids<T>(
collection: &[T],
selected_rows: &std::collections::HashSet<usize>,
id_extractor: impl Fn(&T) -> Option<i64>,
) -> Vec<i64> {
let mut ids = Vec::new();
for &row in selected_rows {
if let Some(item) = collection.get(row) {
if let Some(id) = id_extractor(item) {
ids.push(id);
}
}
}
ids
}
/// Filter and search through JSON data
#[allow(dead_code)]
pub fn filter_and_search(
data: &[Value],
search_query: &str,
search_fields: &[&str],
) -> Vec<Value> {
if search_query.is_empty() {
return data.to_vec();
}
let search_lower = search_query.to_lowercase();
data.iter()
.filter(|item| {
search_fields.iter().any(|field| {
item.get(field)
.and_then(|v| v.as_str())
.map(|s| s.to_lowercase().contains(&search_lower))
.unwrap_or(false)
})
})
.cloned()
.collect()
}
}
|