mirror of
https://gitlab.com/spaceti-app/integrations/acs/colnod-connector.git
synced 2026-07-12 18:10:37 +02:00
Add update auth token
This commit is contained in:
parent
59363f8997
commit
4283707daf
@ -1 +1 @@
|
|||||||
Subproject commit 8aa786fa51dbbd9c82ec20f10892295c93c4054c
|
Subproject commit fcf43b043e032151a15c40a3778bc195f45ae509
|
||||||
@ -386,3 +386,36 @@ bool ColnodAPI::checkResponse(const QByteArray &responseData)
|
|||||||
QJsonDocument doc = QJsonDocument::fromJson(responseData);
|
QJsonDocument doc = QJsonDocument::fromJson(responseData);
|
||||||
return doc.object().contains("items");
|
return doc.object().contains("items");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ColnodAPI::updateAuthToken() {
|
||||||
|
auto login = requestLogin();
|
||||||
|
Q_ASSERT(login);
|
||||||
|
if (login->networkReply() == nullptr) {
|
||||||
|
qCCritical(colnod) << "Not possible to create login request";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
connect(login.get(), &serversdk::Request::finished, this, [=](){
|
||||||
|
// Get login token and check if is correct
|
||||||
|
if (!checkRequest(login)) {
|
||||||
|
emit authTokenUpdated(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
AuthToken token;
|
||||||
|
|
||||||
|
if (checkResponse(login->responseData())) {
|
||||||
|
qCWarning(colnod) << "empty response data when logging in";
|
||||||
|
emit authTokenUpdated(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
token.fromJson(login->responseData());
|
||||||
|
authToken = token.id();
|
||||||
|
if (authToken.isEmpty()) {
|
||||||
|
qCCritical(colnod) << "Token uuid is empty";
|
||||||
|
emit authTokenUpdated(false);;
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
qDebug(colnod) << "Auth token updated.";
|
||||||
|
emit authTokenUpdated(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@ -89,6 +89,8 @@ public:
|
|||||||
|
|
||||||
bool isValid();
|
bool isValid();
|
||||||
|
|
||||||
|
bool updateAuthToken();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<serversdk::Request> requestLogin();
|
std::shared_ptr<serversdk::Request> requestLogin();
|
||||||
|
|
||||||
@ -123,6 +125,7 @@ signals:
|
|||||||
*/
|
*/
|
||||||
void downloadDataFinished(std::shared_ptr<Data> = std::shared_ptr<Data>());
|
void downloadDataFinished(std::shared_ptr<Data> = std::shared_ptr<Data>());
|
||||||
void dataProcessFinished(bool success = true);
|
void dataProcessFinished(bool success = true);
|
||||||
|
void authTokenUpdated(bool success);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
serversdk::RequestManager* mReqMan = nullptr;
|
serversdk::RequestManager* mReqMan = nullptr;
|
||||||
|
|||||||
@ -155,8 +155,8 @@ void DatabaseAPI::reupdateDeleted(const QList<QString>& ids)
|
|||||||
for (const auto &id : ids){
|
for (const auto &id : ids){
|
||||||
query.prepare("UPDATE colnod_token SET last_update = CURRENT_TIMESTAMP() WHERE id = :id");
|
query.prepare("UPDATE colnod_token SET last_update = CURRENT_TIMESTAMP() WHERE id = :id");
|
||||||
// query.bindValue(":last_update", QDateTime::fromMSecsSinceEpoch(getNow()).toString("yyyy-MM-dd hh:mm:ss.zzz"));
|
// query.bindValue(":last_update", QDateTime::fromMSecsSinceEpoch(getNow()).toString("yyyy-MM-dd hh:mm:ss.zzz"));
|
||||||
QString tempId = "'" + id + "'";
|
// QString tempId = "'" + id + "'";
|
||||||
query.bindValue(":id", tempId);
|
query.bindValue(":id", id);
|
||||||
if (!executeQuery(query)) {
|
if (!executeQuery(query)) {
|
||||||
if (!executeQuery(query)) {
|
if (!executeQuery(query)) {
|
||||||
qWarning(database) << "ERROR during updating last update of deleted token:" << id << ", it will be synced from Colnod to db in next sync. Last modified will be invalid";
|
qWarning(database) << "ERROR during updating last update of deleted token:" << id << ", it will be synced from Colnod to db in next sync. Last modified will be invalid";
|
||||||
@ -179,6 +179,7 @@ qint64 DatabaseAPI::getNow()
|
|||||||
// All tokens which are pushed to Colnad are update back to db, because of diffrent last_update and init of new tokens
|
// All tokens which are pushed to Colnad are update back to db, because of diffrent last_update and init of new tokens
|
||||||
bool DatabaseAPI::uploadTokens(const QHash<QString, std::shared_ptr<Token>> &tokens, const QSet<QString> &filters)
|
bool DatabaseAPI::uploadTokens(const QHash<QString, std::shared_ptr<Token>> &tokens, const QSet<QString> &filters)
|
||||||
{
|
{
|
||||||
|
bool res = true;
|
||||||
auto query = mDbMan->createQuery();
|
auto query = mDbMan->createQuery();
|
||||||
for (const auto &token : tokens) {
|
for (const auto &token : tokens) {
|
||||||
if (exists(token->id(), "colnod_token")) {
|
if (exists(token->id(), "colnod_token")) {
|
||||||
@ -192,15 +193,16 @@ bool DatabaseAPI::uploadTokens(const QHash<QString, std::shared_ptr<Token>> &tok
|
|||||||
|
|
||||||
if (!query.isActive()) {
|
if (!query.isActive()) {
|
||||||
mValid = false;
|
mValid = false;
|
||||||
|
res = false;
|
||||||
qWarning(database) << "ERROR durning inserting/updating of token:" << token->name();
|
qWarning(database) << "ERROR durning inserting/updating of token:" << token->name();
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DatabaseAPI::uploadSubjects(const QHash<QString, std::shared_ptr<Subject>> &subjects)
|
bool DatabaseAPI::uploadSubjects(const QHash<QString, std::shared_ptr<Subject>> &subjects)
|
||||||
{
|
{
|
||||||
|
bool res = true;
|
||||||
auto query = mDbMan->createQuery();
|
auto query = mDbMan->createQuery();
|
||||||
for (const auto &subject : subjects) {
|
for (const auto &subject : subjects) {
|
||||||
for (const auto &attribute : subject->getAttributes()) {
|
for (const auto &attribute : subject->getAttributes()) {
|
||||||
@ -213,6 +215,7 @@ bool DatabaseAPI::uploadSubjects(const QHash<QString, std::shared_ptr<Subject>>
|
|||||||
}
|
}
|
||||||
if (!query.isActive()) {
|
if (!query.isActive()) {
|
||||||
mValid = false;
|
mValid = false;
|
||||||
|
res = false;
|
||||||
qWarning(database) << "ERROR durning inserting/updating of subject attribute:" << attribute->attributeValue();
|
qWarning(database) << "ERROR durning inserting/updating of subject attribute:" << attribute->attributeValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -224,11 +227,11 @@ bool DatabaseAPI::uploadSubjects(const QHash<QString, std::shared_ptr<Subject>>
|
|||||||
}
|
}
|
||||||
if (!query.isActive()) {
|
if (!query.isActive()) {
|
||||||
mValid = false;
|
mValid = false;
|
||||||
qWarning(database) << "ERROR durning inserting/updating of subject attribute:" << subject->name();
|
res = false;
|
||||||
return false;
|
qWarning(database) << "ERROR durning inserting/updating of subject:" << subject->name();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DatabaseAPI::deleteItems(const QList<QString> &ids, const QString &tableName)
|
bool DatabaseAPI::deleteItems(const QList<QString> &ids, const QString &tableName)
|
||||||
@ -239,8 +242,8 @@ bool DatabaseAPI::deleteItems(const QList<QString> &ids, const QString &tableNam
|
|||||||
if (tableName == "colnod_subject") {
|
if (tableName == "colnod_subject") {
|
||||||
for (const auto &id : ids) {
|
for (const auto &id : ids) {
|
||||||
query.prepare("DELETE FROM colnod_subject_attribute WHERE subject_id = :id");
|
query.prepare("DELETE FROM colnod_subject_attribute WHERE subject_id = :id");
|
||||||
QString tempId = "'" + id + "'";
|
// QString tempId = "'" + id + "'";
|
||||||
query.bindValue(":id", tempId);
|
query.bindValue(":id", id);
|
||||||
if (!executeQuery(query)) {
|
if (!executeQuery(query)) {
|
||||||
qWarning(database) << "ERROR during marking as deleted:" << id;
|
qWarning(database) << "ERROR during marking as deleted:" << id;
|
||||||
mValid = false;
|
mValid = false;
|
||||||
@ -251,8 +254,8 @@ bool DatabaseAPI::deleteItems(const QList<QString> &ids, const QString &tableNam
|
|||||||
|
|
||||||
for (const auto &id : ids) {
|
for (const auto &id : ids) {
|
||||||
query.prepare(QString("UPDATE %1 SET deleted = true, last_update = :now, last_modified = :now WHERE id = :id").arg(tableName));
|
query.prepare(QString("UPDATE %1 SET deleted = true, last_update = :now, last_modified = :now WHERE id = :id").arg(tableName));
|
||||||
QString tempId = "'" + id + "'";
|
// QString tempId = "'" + id + "'";
|
||||||
query.bindValue(":id", tempId);
|
query.bindValue(":id", id);
|
||||||
query.bindValue(":now", nowString);
|
query.bindValue(":now", nowString);
|
||||||
if (!executeQuery(query)) {
|
if (!executeQuery(query)) {
|
||||||
qWarning(database) << "ERROR during marking as deleted:" << id;
|
qWarning(database) << "ERROR during marking as deleted:" << id;
|
||||||
@ -267,8 +270,8 @@ std::shared_ptr<Token> DatabaseAPI::getToken(const QString &id)
|
|||||||
{
|
{
|
||||||
auto query = mDbMan->createQuery();
|
auto query = mDbMan->createQuery();
|
||||||
query.prepare("SELECT * FROM colnod_token WHERE id = :id");
|
query.prepare("SELECT * FROM colnod_token WHERE id = :id");
|
||||||
QString tempId = "'" + id + "'";
|
// QString tempId = "'" + id + "'";
|
||||||
query.bindValue(":id", tempId);
|
query.bindValue(":id", id);
|
||||||
if (!executeQuery(query)) {
|
if (!executeQuery(query)) {
|
||||||
qCritical(database) << "ERROR during accessing existing tokens. EXIT";
|
qCritical(database) << "ERROR during accessing existing tokens. EXIT";
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@ -288,8 +291,8 @@ int DatabaseAPI::exists(const QString &id, const QString &tableName)
|
|||||||
{
|
{
|
||||||
auto query = mDbMan->createQuery();
|
auto query = mDbMan->createQuery();
|
||||||
query.prepare(QString("SELECT 1 FROM %1 WHERE id = :id").arg(tableName));
|
query.prepare(QString("SELECT 1 FROM %1 WHERE id = :id").arg(tableName));
|
||||||
QString tempId = "'" + id + "'";
|
// QString tempId = "'" + id + "'";
|
||||||
query.bindValue(":id", tempId);
|
query.bindValue(":id", id);
|
||||||
if (!executeQuery(query)) {
|
if (!executeQuery(query)) {
|
||||||
qCritical(database) << "ERROR during accessing existing tokens. EXIT(1)";
|
qCritical(database) << "ERROR during accessing existing tokens. EXIT(1)";
|
||||||
return -1;
|
return -1;
|
||||||
@ -301,8 +304,8 @@ qint16 DatabaseAPI::getNumOfTokensOfSubject(const QString &id)
|
|||||||
{
|
{
|
||||||
auto query = mDbMan->createQuery();
|
auto query = mDbMan->createQuery();
|
||||||
query.prepare("SELECT 1 FROM colnod_token WHERE subject_id = :id AND description = 'mobile'");
|
query.prepare("SELECT 1 FROM colnod_token WHERE subject_id = :id AND description = 'mobile'");
|
||||||
QString tempId = "'" + id + "'";
|
// QString tempId = "'" + id + "'";
|
||||||
query.bindValue(":id", tempId);
|
query.bindValue(":id", id);
|
||||||
if (!executeQuery(query)) {
|
if (!executeQuery(query)) {
|
||||||
qCritical(database) << "ERROR during accessing existing tokens. EXIT(1)";
|
qCritical(database) << "ERROR during accessing existing tokens. EXIT(1)";
|
||||||
return -1;
|
return -1;
|
||||||
@ -314,8 +317,8 @@ QString DatabaseAPI::getSubjectNameByTokenId(const QString &tokenId)
|
|||||||
{
|
{
|
||||||
auto query = mDbMan->createQuery();
|
auto query = mDbMan->createQuery();
|
||||||
query.prepare("SELECT name FROM colnod_subject WHERE id = (SELECT subject_id FROM colnod_token WHERE id = :tokenId)");
|
query.prepare("SELECT name FROM colnod_subject WHERE id = (SELECT subject_id FROM colnod_token WHERE id = :tokenId)");
|
||||||
QString tempId = "'" + tokenId + "'";
|
// QString tempId = "'" + tokenId + "'";
|
||||||
query.bindValue(":tokenId", tempId);
|
query.bindValue(":tokenId", tokenId);
|
||||||
if (!executeQuery(query)) {
|
if (!executeQuery(query)) {
|
||||||
qCritical(database) << "ERROR during accessing existing subject. EXIT(1)";
|
qCritical(database) << "ERROR during accessing existing subject. EXIT(1)";
|
||||||
return "error";
|
return "error";
|
||||||
|
|||||||
@ -81,43 +81,53 @@ bool SyncManager::startSynchronization()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// REUPDATE tokens pushed from database to colnod (cause of lastUpdate time)
|
// REUPDATE tokens pushed from database to colnod (cause of lastUpdate time)
|
||||||
Timestamp time;
|
|
||||||
time.setTimestamp(syncBegin);
|
|
||||||
|
|
||||||
// download data changes in this sync
|
// download data changes in this sync
|
||||||
auto tokensRequest = colnod->getTokensUpdated(time);
|
colnod->updateAuthToken();
|
||||||
|
connect(colnod.get(), &ColnodAPI::authTokenUpdated, [&,syncBegin](bool success) {
|
||||||
// updated data are ready
|
if (!success) {
|
||||||
connect(tokensRequest.get(), &serversdk::Request::finished, [=](){
|
qCritical(syncmanager) << "Cannot login to Colnod.";
|
||||||
|
|
||||||
auto uploadedData = std::make_shared<Data>();
|
|
||||||
uploadedData->loadTokensFromJson(tokensRequest->responseData());
|
|
||||||
|
|
||||||
if (!uploadedData) {
|
|
||||||
qCritical(syncmanager) << "Not possible to get correct data from colnod";
|
|
||||||
QCoreApplication::exit(-1);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!database->uploadTokens(uploadedData->updatedTokens, {"lastModified"})) {
|
Timestamp time;
|
||||||
qCritical(syncmanager) << "Not possible to upload data to database";
|
time.setTimestamp(syncBegin);
|
||||||
QCoreApplication::exit(-1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
database->reupdateDeleted(databaseData->deletedTokens);
|
|
||||||
|
|
||||||
if (!database->isValid() || !colnod->isValid()) {
|
auto tokensRequest = colnod->getTokensUpdated(time);
|
||||||
qWarning(syncmanager) << "Unsuccessful FINISH, check warnings";
|
|
||||||
}
|
// updated data are ready
|
||||||
else {
|
connect(tokensRequest.get(), &serversdk::Request::finished, [=](){
|
||||||
// set new time of last sync to this time of this run
|
|
||||||
if (!database->setLastSync(syncBegin)) {
|
auto uploadedData = std::make_shared<Data>();
|
||||||
|
uploadedData->loadTokensFromJson(tokensRequest->responseData());
|
||||||
|
|
||||||
|
if (!uploadedData) {
|
||||||
|
qCritical(syncmanager) << "Not possible to get correct data from colnod";
|
||||||
|
QCoreApplication::exit(-1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!database->uploadTokens(uploadedData->updatedTokens, {"lastModified"})) {
|
||||||
|
qCritical(syncmanager) << "Not possible to upload data to database";
|
||||||
|
QCoreApplication::exit(-1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
database->reupdateDeleted(databaseData->deletedTokens);
|
||||||
|
|
||||||
|
if (!database->isValid() || !colnod->isValid()) {
|
||||||
qWarning(syncmanager) << "Unsuccessful FINISH, check warnings";
|
qWarning(syncmanager) << "Unsuccessful FINISH, check warnings";
|
||||||
}
|
}
|
||||||
qInfo(syncmanager) << "Successful FINISH";
|
else {
|
||||||
}
|
// set new time of last sync to this time of this run
|
||||||
QCoreApplication::exit();
|
if (!database->setLastSync(syncBegin)) {
|
||||||
|
qWarning(syncmanager) << "Unsuccessful FINISH, check warnings";
|
||||||
|
}
|
||||||
|
qInfo(syncmanager) << "Successful FINISH";
|
||||||
|
}
|
||||||
|
QCoreApplication::exit();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
colnod->processData(preparedData->databaseData);
|
colnod->processData(preparedData->databaseData);
|
||||||
});
|
});
|
||||||
@ -171,8 +181,8 @@ std::unique_ptr<PreparedData> SyncManager::prepareData(const std::shared_ptr<Dat
|
|||||||
qCritical(syncmanager) << "Not possible to access database.";
|
qCritical(syncmanager) << "Not possible to access database.";
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
if ( token ) {
|
if ( !token->id().isEmpty() ) {
|
||||||
// alredy synced ( tokens are same )
|
// already synced ( tokens are same )
|
||||||
if (token == colnodToken) {
|
if (token == colnodToken) {
|
||||||
alreadySynced.append(colnodToken->id());
|
alreadySynced.append(colnodToken->id());
|
||||||
continue;
|
continue;
|
||||||
@ -193,7 +203,7 @@ std::unique_ptr<PreparedData> SyncManager::prepareData(const std::shared_ptr<Dat
|
|||||||
|
|
||||||
for (const auto &id : colnodData->deletedTokens) {
|
for (const auto &id : colnodData->deletedTokens) {
|
||||||
auto corespondingToken = database->getToken(id);
|
auto corespondingToken = database->getToken(id);
|
||||||
if ( !corespondingToken) {
|
if ( corespondingToken->id().isEmpty()) {
|
||||||
qWarning(syncmanager) << "Token:" << id << ", deleted in colnod, but not exists in db";
|
qWarning(syncmanager) << "Token:" << id << ", deleted in colnod, but not exists in db";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user