|
6 | 6 | #include "queries/phonebook/QueryContactGetByID.hpp"
|
7 | 7 | #include "queries/phonebook/QueryContactUpdate.hpp"
|
8 | 8 | #include "queries/phonebook/QueryContactRemove.hpp"
|
| 9 | +#include "queries/phonebook/QueryMergeContactsList.hpp" |
| 10 | +#include "queries/phonebook/QueryCheckContactsListDuplicates.hpp" |
9 | 11 | #include <Utils.hpp>
|
10 | 12 |
|
11 | 13 | #include <queries/phonebook/QueryContactGet.hpp>
|
@@ -159,6 +161,12 @@ auto ContactRecordInterface::runQuery(std::shared_ptr<db::Query> query) -> std::
|
159 | 161 | else if (typeid(*query) == typeid(db::query::NumberGetByID)) {
|
160 | 162 | return numberGetByIdQuery(query);
|
161 | 163 | }
|
| 164 | + else if (typeid(*query) == typeid(db::query::MergeContactsList)) { |
| 165 | + return mergeContactsListQuery(query); |
| 166 | + } |
| 167 | + else if (typeid(*query) == typeid(db::query::CheckContactsListDuplicates)) { |
| 168 | + return checkContactsListDuplicatesQuery(query); |
| 169 | + } |
162 | 170 | else {
|
163 | 171 | error_db_data("Unexpected query type.");
|
164 | 172 | return nullptr;
|
@@ -357,6 +365,26 @@ auto ContactRecordInterface::numberGetByIdQuery(std::shared_ptr<db::Query> query
|
357 | 365 | return response;
|
358 | 366 | }
|
359 | 367 |
|
| 368 | +auto ContactRecordInterface::mergeContactsListQuery(std::shared_ptr<db::Query> query) |
| 369 | + -> std::unique_ptr<db::QueryResult> |
| 370 | +{ |
| 371 | + auto mergeQuery = static_cast<db::query::MergeContactsList *>(query.get()); |
| 372 | + auto ret = ContactRecordInterface::MergeContactsList(mergeQuery->getContactsList()); |
| 373 | + auto response = std::make_unique<db::query::MergeContactsListResult>(ret); |
| 374 | + response->setRequestQuery(query); |
| 375 | + return response; |
| 376 | +} |
| 377 | + |
| 378 | +auto ContactRecordInterface::checkContactsListDuplicatesQuery(std::shared_ptr<db::Query> query) |
| 379 | + -> std::unique_ptr<db::QueryResult> |
| 380 | +{ |
| 381 | + auto mergeQuery = static_cast<db::query::CheckContactsListDuplicates *>(query.get()); |
| 382 | + auto response = std::make_unique<db::query::CheckContactsListDuplicatesResult>( |
| 383 | + std::move(ContactRecordInterface::CheckContactsListDuplicates(mergeQuery->getContactsList()))); |
| 384 | + response->setRequestQuery(query); |
| 385 | + return response; |
| 386 | +} |
| 387 | + |
360 | 388 | auto ContactRecordInterface::splitNumberIDs(const std::string &numberIDs) -> std::vector<std::uint32_t>
|
361 | 389 | {
|
362 | 390 | std::stringstream source(numberIDs);
|
@@ -1172,3 +1200,52 @@ auto ContactRecordInterface::GetNumbersIdsByContact(std::uint32_t contactId) ->
|
1172 | 1200 | }
|
1173 | 1201 | return numbersIds;
|
1174 | 1202 | }
|
| 1203 | + |
| 1204 | +auto ContactRecordInterface::MergeContactsList(std::vector<ContactRecord> &contacts) -> bool |
| 1205 | +{ |
| 1206 | + std::vector<ContactNumberHolder> contactNumberHolders; |
| 1207 | + auto numberMatcher = buildNumberMatcher(contactNumberHolders); |
| 1208 | + |
| 1209 | + for (auto &contact : contacts) { |
| 1210 | + // Important: Comparing only single number contacts |
| 1211 | + if (contact.numbers.size() > 1) { |
| 1212 | + LOG_WARN("Contact with multiple numbers detected - ignoring all numbers except first"); |
| 1213 | + } |
| 1214 | + auto matchedNumber = numberMatcher.bestMatch(contact.numbers[0].number, utils::PhoneNumber::Match::POSSIBLE); |
| 1215 | + |
| 1216 | + if (matchedNumber == numberMatcher.END) { |
| 1217 | + if (!Add(contact)) { |
| 1218 | + LOG_ERROR("Contacts list merge fail when adding the contact."); |
| 1219 | + return false; |
| 1220 | + } |
| 1221 | + } |
| 1222 | + else { |
| 1223 | + // Complete override of the contact data |
| 1224 | + contact.ID = matchedNumber->getContactID(); |
| 1225 | + Update(contact); |
| 1226 | + // Rebuild number matcher |
| 1227 | + numberMatcher = buildNumberMatcher(contactNumberHolders); |
| 1228 | + } |
| 1229 | + } |
| 1230 | + return true; |
| 1231 | +} |
| 1232 | + |
| 1233 | +auto ContactRecordInterface::CheckContactsListDuplicates(std::vector<ContactRecord> &contacts) |
| 1234 | + -> std::vector<ContactRecord> |
| 1235 | +{ |
| 1236 | + std::vector<ContactRecord> duplicates; |
| 1237 | + std::vector<ContactNumberHolder> contactNumberHolders; |
| 1238 | + auto numberMatcher = buildNumberMatcher(contactNumberHolders); |
| 1239 | + |
| 1240 | + for (auto &contact : contacts) { |
| 1241 | + // Important: Comparing only single number contacts |
| 1242 | + if (contact.numbers.size() > 1) { |
| 1243 | + LOG_WARN("Contact with multiple numbers detected - ignoring all numbers except first"); |
| 1244 | + } |
| 1245 | + auto matchedNumber = numberMatcher.bestMatch(contact.numbers[0].number, utils::PhoneNumber::Match::POSSIBLE); |
| 1246 | + if (matchedNumber != numberMatcher.END) { |
| 1247 | + duplicates.push_back(contact); |
| 1248 | + } |
| 1249 | + } |
| 1250 | + return duplicates; |
| 1251 | +} |
0 commit comments