void Manager::update_goals(Manager::CompanyData &data) { fmt::print(stderr, "UPDATE GOALS {} {} \n", data.is_processing, GetOwner(data.company)); if (data.is_processing) return; if (data.new_goals.size() > data.goals.size()) { fmt::print(stderr, "CREATING GOAL {} {}\n", data.new_goals.size(), data.goals.size()); std::string new_text = data.new_goals[data.goals.size()]; data.is_processing = goal::Create(data.company, new_text) .with_callback([this, new_text, &data] (bool res) { fmt::print(stderr, "GOAL CREATED {} {}\n", _new_goal_id, new_text); data.is_processing = false; if (res) { GoalData goal; goal.id = _new_goal_id; goal.text = new_text; data.goals.push_back(goal); this->update_goals(data); } else { ConsoleError("Goal creation failed (company_id={})", GetOwner(data.company)); } }) .queue(); return; } std::sort( data.goals.begin(), data.goals.end(), [](const auto &a, const auto &b) -> bool { return a.id < b.id; } ); Commands commands; for (int i = 0; i < data.goals.size(); i++) { auto &goal = data.goals[i]; if (i < data.new_goals.size()) { auto &new_text = data.new_goals[i]; if (new_text == goal.text) continue; fmt::print(stderr, "SET GOAL {} {}\n", goal.id, new_text); commands.push_back( cmd::goal::SetText(goal.id, new_text) .with_callback([new_text, &goal] (bool res) { if (res) { goal.text = new_text; } else { ConsoleError("Goal text change failed (goal_id={}, new_text='{}')", goal.id, new_text); } }) ); } else { auto goal_id = goal.id; fmt::print(stderr, "DEL GOAL {}\n", goal_id); commands.push_back( cmd::goal::Remove(goal.id) .with_callback([goal_id, &data] (bool res) { if (res) { data.goals.erase( std::remove_if( data.goals.begin(), data.goals.end(), [goal_id] (const auto &goal) -> bool { return goal.id == goal_id; } ), data.goals.end() ); } else { ConsoleError("Goal removal failed (goal_id={})", goal_id); } }) ); } } if (!commands.empty()) { data.is_processing = cmd::Queue(commands, [this, &data] (bool res) { data.is_processing = false; if (res) this->update_goals(data); }); } }