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
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
// An attribute to hide warnings for unused code.
#![allow(dead_code)]
#![allow(unused_parens)]
#![allow(unused_variables)]

use datastore::Datastore;
use datastore::UserGroup;
use datastore::Itemable;
use datastore::Userable;

use std::cmp;
use config::StaticConfig;

use left_threaded_avl_tree::AVLTree;
use left_threaded_avl_tree::ScoredIdTreeMock;

use std::collections::HashSet;
use std::collections::HashMap;
use std::iter::FromIterator;
use serde_json;
use std;
use serde_json::Error;
use datastore;
use datastore::*;
use suffix_rs::*;
use datastore::PurchaseFunctions;


pub trait Event {
    fn can_be_applied(&self, store: &Datastore) -> bool;
    fn apply(&self, store: &mut Datastore, config: &StaticConfig) -> bool;
}

pub fn default_true() -> bool {
    true
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub enum BLEvents {
    CreateItem {
        itemname: String,
        price_cents: u32,
        category: Option<String>,
    },
    CreateUser { username: String },
    UpdateUser { user_id: u32, username: String, is_billed: bool, is_highlighted: bool, external_user_id: Option<String>,

        #[serde(default = "default_true")]
        is_sepa: bool,},
    UpdateItem {
        item_id: u32,
        itemname: String,
        price_cents: u32,
        category: Option<String>,},
    DeleteItem { item_id: u32 },
    DeleteUser { user_id: u32 },
    MakeSimplePurchase {
        user_id: u32,
        item_id: u32,
        timestamp: i64,
    },
    MakeShoppingCartPurchase {
        user_id: u32,
        specials : Vec<String>,
        item_ids : Vec<u32>,
        timestamp: i64,
    },
    MakeSpecialPurchase {
        user_id: u32,
        special_name: String,
        timestamp: i64,
    },
    MakeFreeForAllPurchase {
        ffa_id: u64,
        item_id: u32,
        timestamp: i64,
    },
    CreateFreeForAll {
        allowed_categories : Vec<String>,
        allowed_drinks : Vec<u32>,
        allowed_number_total : u16,
        text_message : String,
        created_timestamp : i64,
        donor : u32,
    },
    CreateFreeCount {
        allowed_categories : Vec<String>,
        allowed_drinks : Vec<u32>,
        allowed_number_total : u16,
        text_message : String,
        created_timestamp : i64,
        donor : u32,
        recipient : u32,
    },
    CreateFreeBudget {
        cents_worth_total : u64,
        text_message : String,
        created_timestamp : i64,
        donor : u32,
        recipient : u32,
    },
    UndoPurchase { unique_id: u64 },
    CreateBill {
        timestamp_from: i64,
        timestamp_to: i64,
        user_ids: UserGroup,
        comment: String,
    },
    FinalizeBill {
        timestamp_from: i64, //timestamps uniquely identify a bill
        timestamp_to: i64,
    },
    ExportBill {
        timestamp_from: i64, //timestamps uniquely identify a bill
        timestamp_to: i64,
    },
    DeleteUnfinishedBill {
        timestamp_from: i64, //timestamps uniquely identify a bill
        timestamp_to: i64,
    },
    SetPriceForSpecial {
        unique_id: u64,
        price: u32,
    },
    UpdateBill {
        timestamp_from: i64, //timestamps uniquely identify a bill
        timestamp_to: i64,
        comment: String,
        users: UserGroup,
        users_that_will_not_be_billed: HashSet<u32>,
    },
}

fn hashset(data: &[u32]) -> HashSet<u32> {
    let r = HashSet::from_iter(data.iter().cloned());
    return r;
}

impl Event for BLEvents {
    fn can_be_applied(&self, store: &Datastore) -> bool {
        return match self {
            &BLEvents::CreateItem {
                ref itemname,
                price_cents,
                ref category,
            } => true,
            &BLEvents::CreateUser { ref username } => true,
            &BLEvents::CreateBill {
                ref timestamp_from,
                ref timestamp_to,
                ref user_ids,
                ref comment,
            } => {
                !store.purchases.is_empty() &&
                    !store.bills.iter().any(|b| b.timestamp_to == *timestamp_to && b.timestamp_from == *timestamp_from)
            },
            &BLEvents::UpdateItem { ref item_id,
                ref itemname,
                ref price_cents,
                ref category } => store.has_item(*item_id),
            &BLEvents::UpdateUser { ref user_id, ref username, ref is_billed, ref is_highlighted, ref external_user_id, ref is_sepa } => store.has_user(*user_id),
            &BLEvents::DeleteItem { item_id } => store.has_item(item_id),
            &BLEvents::DeleteUser { user_id } => store.has_user(user_id),
            &BLEvents::MakeSimplePurchase {
                user_id,
                item_id,
                timestamp,
            } => store.has_item(item_id) && store.has_user(user_id),
            &BLEvents::MakeSpecialPurchase { ref user_id, ref special_name, ref timestamp } => store.has_user(*user_id),
            &BLEvents::MakeShoppingCartPurchase { ref user_id, ref specials, ref item_ids, ref timestamp } => {

            let mut v : Vec<BLEvents> = Vec::new();
            for x in item_ids {
            v.push(BLEvents::MakeSimplePurchase {user_id: *user_id, item_id: *x, timestamp: *timestamp});
            }
            for x in specials {
            v.push(BLEvents::MakeSpecialPurchase {user_id: *user_id, special_name: x.to_string(), timestamp: *timestamp});
            }

            let mut result = true;
            for x in v {
                result = result & x.can_be_applied(store);
            }
            return result;
            }
            &BLEvents::MakeFreeForAllPurchase { ffa_id, item_id, timestamp } => {
                let mut b = false;
                let x : Option<&Freeby> = store.open_ffa.iter().find(|x|x.get_id() == ffa_id);
                let item: &Item = store.items.get(&item_id).unwrap();
                match x {
                    Some(ffa) => {return ffa.allows(item);},
                    None => {return false;},
                }
            },
            &BLEvents::CreateFreeForAll { ref allowed_categories, ref allowed_drinks, ref allowed_number_total, ref text_message, ref created_timestamp, ref donor  } =>
                {
                    return true;
                },
            &BLEvents::CreateFreeCount { ref allowed_categories, ref allowed_drinks, ref allowed_number_total, ref text_message, ref created_timestamp, ref donor, ref recipient } => (store.has_user(*donor) && store.has_user(*recipient)),
            &BLEvents::CreateFreeBudget { ref cents_worth_total, ref text_message, ref created_timestamp, ref donor, ref recipient } => (store.has_user(*donor) && store.has_user(*recipient)),
            &BLEvents::UndoPurchase { unique_id } => store.get_purchase(unique_id).is_some(),
            &BLEvents::FinalizeBill {  timestamp_from, timestamp_to } => {
                //check if all specials are set with price and all users are too
                match store.get_bill(timestamp_from, timestamp_to) {
                    Some(b) => {
                        println!("Bill for finalization is found, with is_created() = {}, store.get_un_set_users_to_bill(timestamp_from, timestamp_to).is_empty() = {}, and store.get_unpriced_specials_to_bill(timestamp_from, timestamp_to).is_empty() = {}\nstore.get_un_set_users_to_bill(timestamp_from, timestamp_to) = {:?}", b.bill_state.is_created() , store.get_un_set_users_to_bill(timestamp_from, timestamp_to).is_empty() , store.get_unpriced_specials_to_bill(timestamp_from, timestamp_to).is_empty(), store.get_un_set_users_to_bill(timestamp_from, timestamp_to) );
                        return b.bill_state.is_created() && store.get_un_set_users_to_bill(timestamp_from, timestamp_to).is_empty() && store.get_unpriced_specials_to_bill(timestamp_from, timestamp_to).is_empty();},
                    None => {return false;},
                }
            },
            &BLEvents::UpdateBill {  ref timestamp_from, ref timestamp_to, ref comment, ref users, ref users_that_will_not_be_billed } => {
                match store.get_bill(*timestamp_from, *timestamp_to) {
                    Some(b) => {return b.bill_state.is_created();},
                    None => {return false;},
                }
            },
                &BLEvents::ExportBill {  timestamp_from, timestamp_to } => {
                    match store.get_bill(timestamp_from, timestamp_to) {
                        Some(b) => {return b.bill_state.is_finalized();},
                        None => {return false;},
                    }
            },
            &BLEvents::DeleteUnfinishedBill { timestamp_from, timestamp_to } => {
                match store.get_bill(timestamp_from, timestamp_to) {
                    Some(b) => {
                        return b.bill_state.is_created();
                    },
                    None => {return false;},
                }
            },
            &BLEvents::SetPriceForSpecial { unique_id, price } => store.get_purchase(unique_id).is_some(),
        };
    }

    fn apply(&self, store: &mut Datastore, config: &StaticConfig) -> bool {
        return match self {
            &BLEvents::CreateItem {
                ref itemname,
                ref price_cents,
                ref category,
            } => {
                let id = store.item_id_counter;
                for cat in category.iter() {
                    store.categories.insert(cat.to_string());
                }
                store.items.insert(
                    id,
                    datastore::Item {
                        name: itemname.to_string(),
                        item_id: id,
                        cost_cents: *price_cents,
                        category: category.clone(),
                        deleted: false,
                    },
                );
                store.item_id_counter = id + 1u32;

                for (_, value) in &mut store.drink_scores_per_user {
                    (*value).insert(id);
                }



                {
                    let mut items_vec: Vec<datastore::Item> = vec![];

                    for (_, v) in &store.items {
                        let copy : datastore::Item = (v.clone());
                        items_vec.push(copy);
                    }

                    store.items_suffix_tree = MockKDTree::build(&items_vec, false);
                }


                true
            }
            &BLEvents::CreateUser { ref username } => {
                let id = store.user_id_counter;
                store.users.insert(
                    id,
                    datastore::User {
                        username: username.to_string(),
                        external_user_id: None,
                        user_id: id,
                        is_billed: true,
                        is_sepa: true,
                        highlight_in_ui: false,
                        deleted: false,
                    },
                );
                store.user_id_counter = id + 1u32;

                //add per user scores and top items:
                let mut score_tree = ScoredIdTreeMock::default();
                for (_key, _) in &store.items {
                    let _ = score_tree.insert(*_key);
                }

                store.drink_scores_per_user.insert(id, score_tree);
                store.top_drinks_per_user.insert(id, HashSet::new());

                //add to user scores and reextract:
                store.top_user_scores.insert(id);
                store.top_users = hashset(
                    store
                        .top_user_scores
                        .extract_top(config.users_in_top_users)
                        .as_slice(),
                );

                {
                    let mut users_vec: Vec<datastore::User> = vec![];

                    for (_, v) in &store.users {
                        users_vec.push(v.clone());
                    }

                    store.users_suffix_tree = MockKDTree::build(&users_vec, false);
                }


                true
            },
            &BLEvents::UpdateItem { ref item_id,
                ref itemname,
                ref price_cents,
                ref category } => {
                let mut e = store.items.get_mut(item_id).unwrap();
                e.name = itemname.to_string();
                e.cost_cents = *price_cents;
                e.category = category.clone();

                true
            },
            &BLEvents::UpdateUser { ref user_id, ref username, ref is_billed, ref is_highlighted, ref external_user_id, ref is_sepa } => {
                let mut e = store.users.get_mut(user_id).unwrap();
                e.username = username.to_string();
                e.is_billed = *is_billed;
                e.highlight_in_ui = *is_highlighted;
                e.external_user_id = external_user_id.clone();

                //if highlight_in_ui changed, update store.highlighted_users
                if *is_highlighted {
                    let _ = store.highlighted_users.insert(*user_id);
                } else {
                    let _ = store.highlighted_users.remove(user_id);
                }

                true
            },
            &BLEvents::CreateBill {
                ref timestamp_from,
                ref timestamp_to,
                ref user_ids,
                ref comment,
            } => {
                store.bills.push(datastore::Bill {
                    timestamp_from: *timestamp_from,
                    timestamp_to: *timestamp_to,
                    users: user_ids.clone(),
                    bill_state: datastore::BillState::Created,
                    users_that_will_not_be_billed: HashSet::new(),
                    comment: comment.to_string(),
                    finalized_data: datastore::ExportableBillData {
                        all_users: HashMap::new(),
                        all_items: HashMap::new(),
                        user_consumption: HashMap::new(),
                    },
                });
                true
            }
            &BLEvents::DeleteItem { item_id } => {
                let _ = store.items.get_mut(&item_id).map(|it|{it.deleted = true});
                let v = store.items.get(&item_id);
                match v {
                    None => (),
                    Some(item) => {
                        //potentially remove category, if no one else is sharing that category
                        match item.category {
                            None => (),
                            Some(ref category) => {
                                if !store.categories.iter().any(|x| x.eq(category)) {
                                    let _ = store.categories.remove(&category.clone());
                                }
                            }
                        }
                        //remove from personal drink scores and possibly reextract top drinks
                        for (_key, mut value) in &mut store.drink_scores_per_user {
                            value.remove(item_id);
                        }

                        for (_key, value) in &mut store.top_drinks_per_user {
                            match store.drink_scores_per_user.get(&_key) {
                                None => (),
                                Some(drinkscore) => if value.contains(&item_id) {
                                    *value = hashset(
                                        drinkscore
                                            .extract_top(config.top_drinks_per_user as usize)
                                            .as_slice(),
                                    );
                                },
                            }
                        }
                    }
                }

                {
                    let mut items_vec: Vec<datastore::Item> = vec![];

                    for (_, v) in &store.items {
                        if !v.deleted {
                            items_vec.push(v.clone());
                        }
                    }

                    store.items_suffix_tree = MockKDTree::build(&items_vec, false);
                }
                true
            }
            &BLEvents::DeleteUser { user_id } => {


                //if highlight_in_ui, update store.highlighted_users
                match store.users.get(&user_id) {
                    Some(x) => {
                        if x.highlight_in_ui {
                            let _ = store.highlighted_users.remove(&user_id);
                        }
                    },
                    None => (),
                }

                //remove from user hashmap
                let _ = store.users.get_mut(&user_id).map(|it|it.deleted = true);

                //remove user item score
                let _ = store.drink_scores_per_user.remove(&user_id);

                //remove user top items
                let _ = store.top_drinks_per_user.remove(&user_id);

                //remove from user score tree
                let _ = store.top_user_scores.remove(user_id);



                {
                    let mut users_vec: Vec<datastore::User> = vec![];

                    for (_, v) in &store.users {
                        if !v.deleted {
                            users_vec.push(v.clone());
                        }
                    }

                    store.users_suffix_tree = MockKDTree::build(&users_vec, false);
                }

                //remove from top users and renew topusers if that is the case
                if store.top_users.remove(&user_id) {
                    store.top_users = hashset(
                        store
                            .top_user_scores
                            .extract_top(config.users_in_top_users as usize)
                            .as_slice(),
                    );
                    true
                } else {
                    false
                }
            }
            //should return true if it was the most recent purchase

            &BLEvents::MakeSimplePurchase {
                user_id,
                item_id,
                timestamp,
            } => {
                let idx: u64 = store.purchase_count + 1;
                store.purchase_count = idx;

                // add purchase to vector
                store.purchases.push(datastore::Purchase::SimplePurchase {
                    unique_id: idx,
                    timestamp_epoch_millis: timestamp,
                    item_id: item_id,
                    consumer_id: user_id,
                });

                let was_in_before = store.top_users.contains(&user_id);

                // increase item score for user
                if let Some(ref mut drinkscore) = store.drink_scores_per_user.get_mut(&user_id) {
                    drinkscore.increment_by_one(item_id);
                    // if not in top items, potentially extract new set
                    if let Some(topitems) = store.top_drinks_per_user.get_mut(&user_id) {
                        if !(topitems.contains(&item_id)) {
                            *topitems = hashset(
                                drinkscore
                                    .extract_top(config.top_drinks_per_user)
                                    .as_slice(),
                            );
                        }
                    }
                }

                // increase user score
                store.top_user_scores.increment_by_one(user_id);

                // if not in top users, potentially extract new set
                if !(store.top_users.contains(&user_id)) {
                    println!("not in top users for userid = {}", user_id);
                    store.top_users = hashset(
                        store
                            .top_user_scores
                            .extract_top(config.users_in_top_users)
                            .as_slice(),
                    );
                } else {

                }

                //increase cost map value
                let alt_hashmap_1 = HashMap::new();
                let username = store.users.get(&user_id).unwrap().username.to_string();
                let itemname = store.items.get(&item_id).unwrap().name.to_string();
                let user_key = (user_id, store.users.get(&user_id).unwrap().username.to_string());
                let item_key = (item_id, store.items.get(&item_id).unwrap().name.to_string());
                let mut old_cost_map = store
                    .balance_cost_per_user
                    .remove(&user_key)
                    .unwrap_or(alt_hashmap_1);
                let old_cost_value = *old_cost_map.get(&item_key).unwrap_or(&0);
                old_cost_map.insert(
                    item_key,
                    old_cost_value
                        + store
                        .items
                        .get(&item_id)
                        .map(|item| item.cost_cents)
                        .unwrap_or(0),
                );
                store.balance_cost_per_user.insert(user_key, old_cost_map);

                //increase count map value
                let alt_hashmap_2 = HashMap::new();
                let user_key2 : (u32, String) = (user_id, username.to_string());
                let user_key3 : (u32, String) = (user_id, username.to_string());
                let item_key2 : (u32, String) = (item_id, itemname.to_string());
                let item_key3 : (u32, String) = (item_id, itemname.to_string());
                let mut old_count_map = store
                    .balance_count_per_user
                    .remove(&user_key2)
                    .unwrap_or(alt_hashmap_2);
                let old_count_value = *old_count_map.get(&item_key2).unwrap_or(&0);
                old_count_map.insert(item_key3, old_count_value + 1);
                store.balance_count_per_user.insert(user_key3, old_count_map);


                let is_in_now = store.top_users.contains(&user_id);



                ((!was_in_before) & (is_in_now))
            }
            &BLEvents::MakeSpecialPurchase { ref user_id, ref special_name, ref timestamp } => {

                let idx: u64 = store.purchase_count + 1;
                store.purchase_count = idx;


                store.purchases.push(datastore::Purchase::SpecialPurchase {
                    unique_id: idx,
                    timestamp_epoch_millis: *timestamp,
                    special_name: special_name.to_string(),
                    specialcost: None,
                    consumer_id: *user_id,
                });

                true
            },
            &BLEvents::MakeShoppingCartPurchase { ref user_id, ref specials, ref item_ids, ref timestamp } => {
                let mut v : Vec<BLEvents> = Vec::new();
                for x in item_ids {
                    v.push(BLEvents::MakeSimplePurchase {user_id: *user_id, item_id: *x, timestamp: *timestamp});
                }
                for x in specials {
                    v.push(BLEvents::MakeSpecialPurchase {user_id: *user_id, special_name: x.to_string(), timestamp: *timestamp});
                }

                let mut result = true;
                for x in v {
                    result = result & x.apply(store, config);
                }
                return result;
            },

            &BLEvents::MakeFreeForAllPurchase { ffa_id, item_id, timestamp } => {

                //get new id
                let idx: u64 = store.purchase_count + 1;
                store.purchase_count = idx;


                let index = store.open_ffa.iter().position(|x| x.get_id() == ffa_id).unwrap();
                let mut freeby : Freeby = store.open_ffa.remove(index);

                let user_id: u32 = freeby.get_donor();

                {
                //add to purchase vector
                store.purchases.push(datastore::Purchase::FFAPurchase {
                    unique_id: idx,
                    timestamp_epoch_millis: timestamp,
                    item_id: item_id,
                    freeby_id: freeby.get_id(),
                    donor: freeby.get_donor(),
                });
            }

                {

                    println!("Freeby was : {:?}", freeby);
                    //decrease existing freeby
                freeby.decrement();
                    println!("Freeby is : {:?}", freeby);
            }

                //potentially move used up freeby to "old" stack
                if freeby.left() == 0 {
                    //add to new vec
                    let pos = store.used_up_freebies.binary_search_by(|f|f.get_id().cmp(&freeby.get_id())).unwrap_or_else(|e| e);
                    store.used_up_freebies.insert(pos, freeby);
                } else {
                    store.open_ffa.insert(index, freeby);
                }

                //add to cost / count map of donor

                {
                    //increase cost map value
                    let alt_hashmap_1 = HashMap::new();
                    let username = store.users.get(&user_id).unwrap().username.to_string();
                    let itemname = store.items.get(&item_id).unwrap().name.to_string();
                    let user_key = (user_id, store.users.get(&user_id).unwrap().username.to_string());
                    let item_key = (item_id, store.items.get(&item_id).unwrap().name.to_string());
                    let mut old_cost_map = store
                        .balance_cost_per_user
                        .remove(&user_key)
                        .unwrap_or(alt_hashmap_1);
                    let old_cost_value = *old_cost_map.get(&item_key).unwrap_or(&0);
                    old_cost_map.insert(
                        item_key,
                        old_cost_value
                            + store
                            .items
                            .get(&item_id)
                            .map(|item| item.cost_cents)
                            .unwrap_or(0),
                    );
                    store.balance_cost_per_user.insert(user_key, old_cost_map);

                    //increase count map value
                    let alt_hashmap_2 = HashMap::new();
                    let user_key2 : (u32, String) = (user_id, username.to_string());
                    let user_key3 : (u32, String) = (user_id, username.to_string());
                    let item_key2 : (u32, String) = (item_id, itemname.to_string());
                    let item_key3 : (u32, String) = (item_id, itemname.to_string());
                    let mut old_count_map = store
                        .balance_count_per_user
                        .remove(&user_key2)
                        .unwrap_or(alt_hashmap_2);
                    let old_count_value = *old_count_map.get(&item_key2).unwrap_or(&0);
                    old_count_map.insert(item_key3, old_count_value + 1);
                    store.balance_count_per_user.insert(user_key3, old_count_map);
                }

                true

                /*


                let was_in_before = store.top_users.contains(&user_id);

                // increase item score for user
                if let Some(ref mut drinkscore) = store.drink_scores_per_user.get_mut(&user_id) {
                    drinkscore.increment_by_one(item_id);
                    // if not in top items, potentially extract new set
                    if let Some(topitems) = store.top_drinks_per_user.get_mut(&user_id) {
                        if !(topitems.contains(&item_id)) {
                            *topitems = hashset(
                                drinkscore
                                    .extract_top(config.top_drinks_per_user)
                                    .as_slice(),
                            );
                        }
                    }
                }

                // increase user score
                store.top_user_scores.increment_by_one(user_id);

                // if not in top users, potentially extract new set
                if !(store.top_users.contains(&user_id)) {
                    println!("not in top users for userid = {}", user_id);
                    store.top_users = hashset(
                        store
                            .top_user_scores
                            .extract_top(config.users_in_top_users)
                            .as_slice(),
                    );
                } else {

                }

                //increase cost map value
                let alt_hashmap_1 = HashMap::new();
                let username = store.users.get(&user_id).unwrap().username.to_string();
                let itemname = store.items.get(&item_id).unwrap().name.to_string();
                let user_key = (user_id, store.users.get(&user_id).unwrap().username.to_string());
                let item_key = (item_id, store.items.get(&item_id).unwrap().name.to_string());
                let mut old_cost_map = store
                    .balance_cost_per_user
                    .remove(&user_key)
                    .unwrap_or(alt_hashmap_1);
                let old_cost_value = *old_cost_map.get(&item_key).unwrap_or(&0);
                old_cost_map.insert(
                    item_key,
                    old_cost_value
                        + store
                        .items
                        .get(&item_id)
                        .map(|item| item.cost_cents)
                        .unwrap_or(0),
                );
                store.balance_cost_per_user.insert(user_key, old_cost_map);

                //increase count map value
                let alt_hashmap_2 = HashMap::new();
                let user_key2 : (u32, String) = (user_id, username.to_string());
                let user_key3 : (u32, String) = (user_id, username.to_string());
                let item_key2 : (u32, String) = (item_id, itemname.to_string());
                let item_key3 : (u32, String) = (item_id, itemname.to_string());
                let mut old_count_map = store
                    .balance_count_per_user
                    .remove(&user_key2)
                    .unwrap_or(alt_hashmap_2);
                let old_count_value = *old_count_map.get(&item_key2).unwrap_or(&0);
                old_count_map.insert(item_key3, old_count_value + 1);
                store.balance_count_per_user.insert(user_key3, old_count_map);


                let is_in_now = store.top_users.contains(&user_id);

                ((!was_in_before) & (is_in_now))*/
            },
            &BLEvents::CreateFreeForAll { ref allowed_categories, ref allowed_drinks, ref allowed_number_total, ref text_message, ref created_timestamp, ref donor  } => {
                let id = {
                    let x = store.freeby_id_counter + 1;
                    store.freeby_id_counter = x;
                    x
                };

                store.open_ffa.push(Freeby::FFA{
                id: id,
                allowed_categories: allowed_categories.to_vec(),
                allowed_drinks: allowed_drinks.to_vec(),
                allowed_number_total: *allowed_number_total,
                allowed_number_used : 0,
                text_message : text_message.to_string(),
                created_timestamp : *created_timestamp,
                donor: *donor,
                });


                true
            },
            &BLEvents::ExportBill {  timestamp_from, timestamp_to } => {
                let b: &mut Bill = store.get_mut_bill(timestamp_from, timestamp_to).unwrap();
                b.bill_state = datastore::BillState::ExportedAtLeastOnce;
                true
            },
            &BLEvents::CreateFreeCount { ref allowed_categories, ref allowed_drinks, ref allowed_number_total, ref text_message, ref created_timestamp, ref donor, ref recipient } => {
                let id = {
                    let x = store.freeby_id_counter + 1;
                    store.freeby_id_counter = x;
                    x
                };
                if !store.open_freebies.contains_key(recipient) {
                    store.open_freebies.insert(*recipient, vec![]);
                }
                store.open_freebies.get_mut(recipient).unwrap().push( datastore::Freeby::Classic {
                    id: id,
                    allowed_categories: allowed_categories.to_vec(),
                    allowed_drinks: allowed_drinks.to_vec(),
                    allowed_number_total: *allowed_number_total,
                    allowed_number_used: 0,
                    text_message: text_message.to_string(),
                    created_timestamp: *created_timestamp,
                    donor: *donor,
                    recipient: *recipient,
                });

                true
            },
            &BLEvents::CreateFreeBudget { ref cents_worth_total, ref text_message, ref created_timestamp, ref donor, ref recipient } => {
                let id = {
                    let x = store.freeby_id_counter + 1;
                    store.freeby_id_counter = x;
                    x
                };
                if !store.open_freebies.contains_key(recipient) {
                    store.open_freebies.insert(*recipient, vec![]);
                }
                store.open_freebies.get_mut(recipient).unwrap().push( datastore::Freeby::Transfer {
                    id: id,
                    cents_worth_total: *cents_worth_total,
                    cents_worth_used: 0,
                    text_message: text_message.to_string(),
                    created_timestamp: *created_timestamp,
                    donor: *donor,
                    recipient: *recipient,
                });

                true
            },
            &BLEvents::UndoPurchase { unique_id } => {
                //remove purchase from list
                let index = store
                    .purchases
                    .iter()
                    .position(|x| x.has_unique_id(unique_id))
                    .unwrap();
                let old_size = store.purchases.len();
                let element = store.purchases.remove(index);

                let item_key1 : (u32, String) = (*element.get_item_id(), store.items.get(&element.get_item_id()).unwrap().name.to_string());
                let item_key2 : (u32, String) = (*element.get_item_id(), store.items.get(&element.get_item_id()).unwrap().name.to_string());
                let user_key : (u32, String) = (*element.get_user_id(), store.users.get(&element.get_user_id()).unwrap().username.to_string());

                //remove cost and count lists:
                let oldcost = *store
                    .balance_cost_per_user
                    .get(&user_key)
                    .unwrap()
                    .get(&item_key1)
                    .unwrap();
                let oldcount = *store
                    .balance_count_per_user
                    .get(&user_key)
                    .unwrap()
                    .get(&item_key1)
                    .unwrap();


                store
                    .balance_cost_per_user
                    .get_mut(&user_key)
                    .unwrap()
                    .insert(item_key1, oldcost - 1);
                store
                    .balance_count_per_user
                    .get_mut(&user_key)
                    .unwrap()
                    .insert(item_key2, oldcount - 1);


                old_size == index + 1
            },
            //removes purchases from global list and also recomputes counts
            &BLEvents::FinalizeBill {  timestamp_from, timestamp_to } => {


                let bill_idx: usize = store.get_bill_index(timestamp_from, timestamp_to).unwrap();

                {
                    store.bills.get_mut(bill_idx).unwrap().bill_state = BillState::Finalized;
                }

                let bill_cpy: Bill = store.bills[bill_idx].clone();


                let purchase_indices = store.get_purchase_indices_to_bill(&bill_cpy);

                //compute users and create copies
                {
                    let filtered_purs : Vec<Purchase> = purchase_indices.iter().map(|idx| store.purchases[*idx].clone()).collect();
                    for purchase in filtered_purs {

                        let user : User = store.users[purchase.get_user_id()].clone();

                        match purchase {
                            Purchase::SpecialPurchase{
                                unique_id,
                                timestamp_epoch_millis,
                                special_name,
                                specialcost,
                                consumer_id,
                            } => {
                                let day_idx : usize = bill_cpy.get_day_index(timestamp_epoch_millis);
                                let mut bill: &mut Bill = store.bills.get_mut(bill_idx).unwrap();
                                if !bill.finalized_data.all_users.contains_key(&consumer_id) {
                                    bill.finalized_data.all_users.insert(consumer_id, user.clone());
                                    bill.finalized_data.user_consumption.insert(consumer_id, BillUserInstance {
                                        user_id: consumer_id,
                                        per_day: HashMap::new(),
                                    });
                                }

                                if !bill.finalized_data.user_consumption.get_mut(&consumer_id).unwrap().per_day.contains_key(&day_idx) {
                                    bill.finalized_data.user_consumption.get_mut(&consumer_id).unwrap().per_day.insert(day_idx, BillUserDayInstance {
                                        personally_consumed: HashMap::new(),
                                        specials_consumed: Vec::new(),
                                        ffa_giveouts: HashMap::new(),
                                        giveouts_to_user_id: HashMap::new(),
                                    });
                                }
                                bill.finalized_data.user_consumption.get_mut(&consumer_id).unwrap().per_day.get_mut(&day_idx).unwrap().specials_consumed.push(PricedSpecial {
                                    purchase_id: unique_id,
                                    price: specialcost.unwrap(),
                                    name: special_name.to_string(),
                                });
                            },
                            Purchase::SimplePurchase  {
                                unique_id,
                                timestamp_epoch_millis,
                                item_id,
                                consumer_id,
                            } => {
                                let day_idx : usize = bill_cpy.get_day_index(timestamp_epoch_millis);
                                let item: Item = store.items.get(&item_id).unwrap().clone();
                                let count_freeby_idx = store.get_count_freeby_id_useable_for(consumer_id, item_id);
                                let budget_freeby_idx = store.get_budget_freeby_id_useable_for(consumer_id);


                                let mut bill: &mut Bill = store.bills.get_mut(bill_idx).unwrap();
                                if !bill.finalized_data.all_users.contains_key(&consumer_id) {
                                    bill.finalized_data.all_users.insert(consumer_id, user.clone());
                                    bill.finalized_data.user_consumption.insert(consumer_id, BillUserInstance {
                                        user_id: consumer_id,
                                        per_day: HashMap::new(),
                                    });
                                }

                                if !bill.finalized_data.all_items.contains_key(&item_id) {
                                    bill.finalized_data.all_items.insert(item_id, item.clone());
                                }

                                //first, get a count giveout
                                match count_freeby_idx {
                                    Some(cidx) => {
                                        //if count giveout was found, add purchase under donor as a PaidFor
                                        let old_count: u16 = store.open_freebies.get(&consumer_id).unwrap().get(cidx).unwrap().left();
                                        let donor_id: u32 = store.open_freebies.get(&consumer_id).unwrap().get(cidx).unwrap().get_donor();
                                        let donor: User = store.users.get(&donor_id).unwrap().clone();


                                        {
                                            if !bill.finalized_data.all_users.contains_key(&donor_id) {
                                                bill.finalized_data.all_users.insert(donor_id, donor.clone());
                                                bill.finalized_data.user_consumption.insert(donor_id, BillUserInstance {
                                                    user_id: donor_id,
                                                    per_day: HashMap::new(),
                                                });
                                            }

                                            if !bill.finalized_data.user_consumption.get_mut(&donor_id).unwrap().per_day.contains_key(&day_idx) {
                                                bill.finalized_data.user_consumption.get_mut(&donor_id).unwrap().per_day.insert(day_idx, BillUserDayInstance {
                                                    personally_consumed: HashMap::new(),
                                                    specials_consumed: Vec::new(),
                                                    ffa_giveouts: HashMap::new(),
                                                    giveouts_to_user_id: HashMap::new(),
                                                });
                                            }

                                            *bill.finalized_data.user_consumption.get_mut(&donor_id).unwrap().per_day.get_mut(&day_idx).unwrap().giveouts_to_user_id.entry(consumer_id).or_insert(PaidFor {
                                                recipient_id: consumer_id,
                                                count_giveouts_used: HashMap::new(),
                                                budget_given: 0,
                                                budget_gotten: 0,
                                            }).count_giveouts_used.entry(item_id).or_insert(0) += 1;


                                        }

                                        {
                                            store.open_freebies.get_mut(&consumer_id).unwrap().get_mut(cidx).unwrap().decrement();
                                        }

                                        //removed used up freeby
                                        if old_count <= 1 {
                                            let freeby = store.open_freebies.get_mut(&consumer_id).unwrap().remove(cidx);
                                            let pos = store.used_up_freebies.binary_search_by(|f|f.get_id().cmp(&freeby.get_id())).unwrap_or_else(|e| e);
                                            store.used_up_freebies.insert(pos, freeby);
                                        }
                                    },
                                    None => {
                                        //add purchase under consumer
                                        *bill.finalized_data.user_consumption.get_mut(&consumer_id).unwrap().per_day.entry(day_idx).or_insert(BillUserDayInstance {
                                            personally_consumed: HashMap::new(),
                                            specials_consumed: Vec::new(),
                                            ffa_giveouts: HashMap::new(),
                                            giveouts_to_user_id: HashMap::new(),
                                        }).personally_consumed.entry(item_id).or_insert(0u32) += 1;

                                        //if a count giveout does not exist, find a budget giveout, and add decrease / increase if possible
                                        match budget_freeby_idx {
                                            Some(bidx) => {
                                                let max_budget : u64 = store.open_freebies.get(&consumer_id).unwrap().get(bidx).unwrap().get_budget_cents_left();
                                                let donor_id: u32 = store.open_freebies.get(&consumer_id).unwrap().get(bidx).unwrap().get_donor();
                                                let donor: User = store.users.get(&donor_id).unwrap().clone();
                                                let item_cost : u64 = item.cost_cents as u64;
                                                let taken_budget : u64 = cmp::min(max_budget, cmp::max(0u64, item_cost));
                                                let used_up : bool = max_budget <= item_cost;



                                                //remove budget in freeby
                                                {
                                                    store.open_freebies.get_mut(&consumer_id).unwrap().get_mut(bidx).unwrap().remove_budget_by(taken_budget);
                                                }

                                                {
                                                    //add budget (min(freeby.left, max(0, item.cost))) positive to recipient
                                                    if !bill.finalized_data.user_consumption.contains_key(&consumer_id) {
                                                        bill.finalized_data.user_consumption.insert(consumer_id, BillUserInstance {
                                                            user_id: consumer_id,
                                                            per_day: HashMap::new(),
                                                        });
                                                    }
                                                    if !bill.finalized_data.user_consumption.get(&consumer_id).unwrap().per_day.contains_key(&day_idx) {
                                                        bill.finalized_data.user_consumption.get_mut(&consumer_id).unwrap().per_day.insert(day_idx, BillUserDayInstance {
                                                            personally_consumed: HashMap::new(),
                                                            specials_consumed: Vec::new(),
                                                            ffa_giveouts: HashMap::new(),
                                                            giveouts_to_user_id: HashMap::new(),
                                                    });
                                                }

                                                if !bill.finalized_data.user_consumption.get(&consumer_id).unwrap().per_day.get(&day_idx).unwrap().giveouts_to_user_id.contains_key(&donor_id) {
                                                    bill.finalized_data.user_consumption.get_mut(&consumer_id).unwrap().per_day.get_mut(&day_idx).unwrap().giveouts_to_user_id.insert(donor_id, PaidFor {
                                                        recipient_id: donor_id,
                                                        count_giveouts_used: HashMap::new(),
                                                        budget_given: 0,
                                                        budget_gotten: 0,
                                                    });
                                                }

                                                bill.finalized_data.user_consumption.get_mut(&consumer_id).unwrap().per_day.get_mut(&day_idx).unwrap().giveouts_to_user_id.entry(donor_id).or_insert(PaidFor {
                                                    recipient_id: donor_id,
                                                    count_giveouts_used: HashMap::new(),
                                                    budget_given: 0,
                                                    budget_gotten: 0,
                                                }).budget_gotten += taken_budget;

                                                }

                                                {
                                                    //add budget negative to donor
                                                    //add donor if he does not yet exist
                                                    if !bill.finalized_data.user_consumption.contains_key(&donor_id) {
                                                        bill.finalized_data.user_consumption.insert(donor_id, BillUserInstance {
                                                            user_id: donor_id,
                                                            per_day: HashMap::new(),
                                                        });
                                                    }
                                                    if !bill.finalized_data.user_consumption.get(&donor_id).unwrap().per_day.contains_key(&day_idx) {
                                                        bill.finalized_data.user_consumption.get_mut(&donor_id).unwrap().per_day.insert(day_idx, BillUserDayInstance {
                                                            personally_consumed: HashMap::new(),
                                                            specials_consumed: Vec::new(),
                                                            ffa_giveouts: HashMap::new(),
                                                            giveouts_to_user_id: HashMap::new(),
                                                        });
                                                    }

                                                    if !bill.finalized_data.user_consumption.get(&donor_id).unwrap().per_day.get(&day_idx).unwrap().giveouts_to_user_id.contains_key(&consumer_id) {
                                                        bill.finalized_data.user_consumption.get_mut(&donor_id).unwrap().per_day.get_mut(&day_idx).unwrap().giveouts_to_user_id.insert(consumer_id, PaidFor {
                                                            recipient_id: consumer_id,
                                                            count_giveouts_used: HashMap::new(),
                                                            budget_given: 0,
                                                            budget_gotten: 0,
                                                        });
                                                    }

                                                bill.finalized_data.user_consumption.get_mut(&donor_id).unwrap().per_day.get_mut(&day_idx).unwrap().giveouts_to_user_id.entry(consumer_id).or_insert(PaidFor {
                                                    recipient_id: consumer_id,
                                                    count_giveouts_used: HashMap::new(),
                                                    budget_given: 0,
                                                    budget_gotten: 0,
                                                }).budget_given += taken_budget;
                                                }

                                                //removed used up freeby
                                                if used_up {
                                                    let freeby = store.open_freebies.get_mut(&consumer_id).unwrap().remove(bidx);
                                                    let pos = store.used_up_freebies.binary_search_by(|f|f.get_id().cmp(&freeby.get_id())).unwrap_or_else(|e| e);
                                                    store.used_up_freebies.insert(pos, freeby);
                                                }
                                            },
                                            None => (),
                                        }
                                    },
                                }
                            },
                            Purchase::FFAPurchase {
                                unique_id,
                                timestamp_epoch_millis,
                                item_id,
                                freeby_id,
                                donor,
                            } => {
                                let day_idx : usize = bill_cpy.get_day_index(timestamp_epoch_millis);
                                let item: Item = store.items.get(&item_id).unwrap().clone();


                                let mut bill: &mut Bill = store.bills.get_mut(bill_idx).unwrap();
                                if !bill.finalized_data.all_users.contains_key(&donor) {
                                    bill.finalized_data.all_users.insert(donor, user.clone());
                                    bill.finalized_data.user_consumption.insert(donor, BillUserInstance {
                                        user_id: donor,
                                        per_day: HashMap::new(),
                                    });
                                }

                                if !bill.finalized_data.all_items.contains_key(&item_id) {
                                    bill.finalized_data.all_items.insert(item_id, item.clone());
                                }


                                if !bill.finalized_data.user_consumption[&donor].per_day.contains_key(&day_idx) {
                                    bill.finalized_data.user_consumption.get_mut(&donor).unwrap().per_day.insert(day_idx, BillUserDayInstance {
                                        personally_consumed: HashMap::new(),
                                        specials_consumed: Vec::new(),
                                        ffa_giveouts: HashMap::new(),
                                        giveouts_to_user_id: HashMap::new(),
                                    });
                                }

                                *bill.finalized_data.user_consumption.get_mut(&donor).unwrap().per_day.get_mut(&day_idx).unwrap().ffa_giveouts.entry(item_id).or_insert(0u32) += 1;
                            },
                        }

                }
                }

                //TODO: balance_cost_per_user also has to be reduced for each purchase

                //remove purchases from purchases vec
                {
                    store.remove_purchases_indices(purchase_indices);
                }
                true

                //TODO: Open question: how will purchase rank be recomputed? Currently kept
            },
            &BLEvents::DeleteUnfinishedBill { timestamp_from, timestamp_to } => {
                let idx_opt : Option<usize> = store.bills.iter().position(|b|b.timestamp_to == timestamp_to && b.timestamp_from == timestamp_from);
                match idx_opt {
                    Some(idx) => {
                        let _ = store.bills.remove(idx);
                        return true;
                    }
                    None => {
                        return false;
                    },
                }
            },
            &BLEvents::SetPriceForSpecial { unique_id, price } => {
                let x = store.get_purchase_mut(unique_id).unwrap();

                match x {
                    &mut Purchase::SpecialPurchase{
                        ref unique_id,
                        ref timestamp_epoch_millis,
                        ref special_name,
                        ref mut specialcost,
                        ref consumer_id,
                    } => {
                        *specialcost = Some(price);
                        return true;
                    },
                    _ => return false,
                }
            },
            &BLEvents::UpdateBill {  ref timestamp_from, ref timestamp_to, ref comment, ref users, ref users_that_will_not_be_billed } => {
                match store.get_mut_bill(*timestamp_from, *timestamp_to) {
                    Some(b) => {
                        b.comment = comment.to_string();
                        b.users = users.clone();
                        b.users_that_will_not_be_billed = users_that_will_not_be_billed.clone();
                        return true;
                    },
                    None => {return false;},
                }
            },
        };
    }
}


#[cfg(test)]
mod tests {
    use rustix_event_shop::BLEvents;
    use serde_json;
    use std;


    #[test]
    fn hashset_test() {
        let mut hashset = std::collections::HashSet::new();

        let str_1 = "Hello World".to_string();

        hashset.insert(str_1);

        let str_2_a = "Hello".to_string();
        let str_2_b = " World".to_string();
        let str_2 = str_2_a + &str_2_b;

        assert!(hashset.remove(&str_2))
    }


    #[test]
    fn events_serialize_and_deserialize_raw() {
        let v = vec![
            BLEvents::CreateItem {
                itemname: "beer".to_string(),
                price_cents: 95u32,
                category: None,
            },
            BLEvents::CreateItem {
                itemname: "beer 2".to_string(),
                price_cents: 95u32,
                category: None,
            },
            BLEvents::DeleteItem { item_id: 2u32 },
            BLEvents::CreateUser {
                username: "klaus".to_string(),
            },
            BLEvents::MakeSimplePurchase {
                item_id: 1u32,
                user_id: 1u32,
                timestamp: 123456789i64,
            },
        ];

        // Serialize it to a JSON string.
        let json = serde_json::to_string(&v).unwrap();
        let reparsed_content: Vec<BLEvents> = serde_json::from_str(&json).unwrap();
        assert_eq!(reparsed_content, v);
    }

    #[test]
    fn events_serialize_and_deserialize_packed() {
        let v = vec![
            BLEvents::CreateItem {
                itemname: "beer".to_string(),
                price_cents: 95u32,
                category: None,
            },
            BLEvents::CreateItem {
                itemname: "beer 2".to_string(),
                price_cents: 95u32,
                category: None,
            },
            BLEvents::DeleteItem { item_id: 2u32 },
            BLEvents::CreateUser {
                username: "klaus".to_string(),
            },
            BLEvents::MakeSimplePurchase {
                item_id: 1u32,
                user_id: 1u32,
                timestamp: 123456789i64,
            },
        ];

        // Serialize it to a JSON string.
        let json_bytes = serde_json::to_string(&v).unwrap().as_bytes().to_vec();
        let reparsed_content: Vec<BLEvents> =
            serde_json::from_str(std::str::from_utf8(json_bytes.as_ref()).unwrap()).unwrap();
        assert_eq!(reparsed_content, v);
    }
}