mirror of
https://gitlab.com/spaceti-app/integrations/acs/colnod-connector.git
synced 2026-07-12 13:10:45 +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);
|
||||
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 updateAuthToken();
|
||||
|
||||
private:
|
||||
std::shared_ptr<serversdk::Request> requestLogin();
|
||||
|
||||
@ -123,6 +125,7 @@ signals:
|
||||
*/
|
||||
void downloadDataFinished(std::shared_ptr<Data> = std::shared_ptr<Data>());
|
||||
void dataProcessFinished(bool success = true);
|
||||
void authTokenUpdated(bool success);
|
||||
|
||||
private:
|
||||
serversdk::RequestManager* mReqMan = nullptr;
|
||||
|
||||
@ -155,8 +155,8 @@ void DatabaseAPI::reupdateDeleted(const QList<QString>& ids)
|
||||
for (const auto &id : ids){
|
||||
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"));
|
||||
QString tempId = "'" + id + "'";
|
||||
query.bindValue(":id", tempId);
|
||||
// QString tempId = "'" + id + "'";
|
||||
query.bindValue(":id", id);
|
||||
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";
|
||||
@ -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
|
||||
bool DatabaseAPI::uploadTokens(const QHash<QString, std::shared_ptr<Token>> &tokens, const QSet<QString> &filters)
|
||||
{
|
||||
bool res = true;
|
||||
auto query = mDbMan->createQuery();
|
||||
for (const auto &token : tokens) {
|
||||
if (exists(token->id(), "colnod_token")) {
|
||||
@ -192,15 +193,16 @@ bool DatabaseAPI::uploadTokens(const QHash<QString, std::shared_ptr<Token>> &tok
|
||||
|
||||
if (!query.isActive()) {
|
||||
mValid = false;
|
||||
res = false;
|
||||
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 res = true;
|
||||
auto query = mDbMan->createQuery();
|
||||
for (const auto &subject : subjects) {
|
||||
for (const auto &attribute : subject->getAttributes()) {
|
||||
@ -213,6 +215,7 @@ bool DatabaseAPI::uploadSubjects(const QHash<QString, std::shared_ptr<Subject>>
|
||||
}
|
||||
if (!query.isActive()) {
|
||||
mValid = false;
|
||||
res = false;
|
||||
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()) {
|
||||
mValid = false;
|
||||
qWarning(database) << "ERROR durning inserting/updating of subject attribute:" << subject->name();
|
||||
return false;
|
||||
res = 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)
|
||||
@ -239,8 +242,8 @@ bool DatabaseAPI::deleteItems(const QList<QString> &ids, const QString &tableNam
|
||||
if (tableName == "colnod_subject") {
|
||||
for (const auto &id : ids) {
|
||||
query.prepare("DELETE FROM colnod_subject_attribute WHERE subject_id = :id");
|
||||
QString tempId = "'" + id + "'";
|
||||
query.bindValue(":id", tempId);
|
||||
// QString tempId = "'" + id + "'";
|
||||
query.bindValue(":id", id);
|
||||
if (!executeQuery(query)) {
|
||||
qWarning(database) << "ERROR during marking as deleted:" << id;
|
||||
mValid = false;
|
||||
@ -251,8 +254,8 @@ bool DatabaseAPI::deleteItems(const QList<QString> &ids, const QString &tableNam
|
||||
|
||||
for (const auto &id : ids) {
|
||||
query.prepare(QString("UPDATE %1 SET deleted = true, last_update = :now, last_modified = :now WHERE id = :id").arg(tableName));
|
||||
QString tempId = "'" + id + "'";
|
||||
query.bindValue(":id", tempId);
|
||||
// QString tempId = "'" + id + "'";
|
||||
query.bindValue(":id", id);
|
||||
query.bindValue(":now", nowString);
|
||||
if (!executeQuery(query)) {
|
||||
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();
|
||||
query.prepare("SELECT * FROM colnod_token WHERE id = :id");
|
||||
QString tempId = "'" + id + "'";
|
||||
query.bindValue(":id", tempId);
|
||||
// QString tempId = "'" + id + "'";
|
||||
query.bindValue(":id", id);
|
||||
if (!executeQuery(query)) {
|
||||
qCritical(database) << "ERROR during accessing existing tokens. EXIT";
|
||||
return nullptr;
|
||||
@ -288,8 +291,8 @@ int DatabaseAPI::exists(const QString &id, const QString &tableName)
|
||||
{
|
||||
auto query = mDbMan->createQuery();
|
||||
query.prepare(QString("SELECT 1 FROM %1 WHERE id = :id").arg(tableName));
|
||||
QString tempId = "'" + id + "'";
|
||||
query.bindValue(":id", tempId);
|
||||
// QString tempId = "'" + id + "'";
|
||||
query.bindValue(":id", id);
|
||||
if (!executeQuery(query)) {
|
||||
qCritical(database) << "ERROR during accessing existing tokens. EXIT(1)";
|
||||
return -1;
|
||||
@ -301,8 +304,8 @@ qint16 DatabaseAPI::getNumOfTokensOfSubject(const QString &id)
|
||||
{
|
||||
auto query = mDbMan->createQuery();
|
||||
query.prepare("SELECT 1 FROM colnod_token WHERE subject_id = :id AND description = 'mobile'");
|
||||
QString tempId = "'" + id + "'";
|
||||
query.bindValue(":id", tempId);
|
||||
// QString tempId = "'" + id + "'";
|
||||
query.bindValue(":id", id);
|
||||
if (!executeQuery(query)) {
|
||||
qCritical(database) << "ERROR during accessing existing tokens. EXIT(1)";
|
||||
return -1;
|
||||
@ -314,8 +317,8 @@ QString DatabaseAPI::getSubjectNameByTokenId(const QString &tokenId)
|
||||
{
|
||||
auto query = mDbMan->createQuery();
|
||||
query.prepare("SELECT name FROM colnod_subject WHERE id = (SELECT subject_id FROM colnod_token WHERE id = :tokenId)");
|
||||
QString tempId = "'" + tokenId + "'";
|
||||
query.bindValue(":tokenId", tempId);
|
||||
// QString tempId = "'" + tokenId + "'";
|
||||
query.bindValue(":tokenId", tokenId);
|
||||
if (!executeQuery(query)) {
|
||||
qCritical(database) << "ERROR during accessing existing subject. EXIT(1)";
|
||||
return "error";
|
||||
|
||||
@ -81,10 +81,18 @@ bool SyncManager::startSynchronization()
|
||||
}
|
||||
|
||||
// REUPDATE tokens pushed from database to colnod (cause of lastUpdate time)
|
||||
|
||||
|
||||
// download data changes in this sync
|
||||
colnod->updateAuthToken();
|
||||
connect(colnod.get(), &ColnodAPI::authTokenUpdated, [&,syncBegin](bool success) {
|
||||
if (!success) {
|
||||
qCritical(syncmanager) << "Cannot login to Colnod.";
|
||||
return;
|
||||
}
|
||||
Timestamp time;
|
||||
time.setTimestamp(syncBegin);
|
||||
|
||||
// download data changes in this sync
|
||||
auto tokensRequest = colnod->getTokensUpdated(time);
|
||||
|
||||
// updated data are ready
|
||||
@ -117,6 +125,8 @@ bool SyncManager::startSynchronization()
|
||||
}
|
||||
QCoreApplication::exit();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
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.";
|
||||
return nullptr;
|
||||
}
|
||||
if ( token ) {
|
||||
// alredy synced ( tokens are same )
|
||||
if ( !token->id().isEmpty() ) {
|
||||
// already synced ( tokens are same )
|
||||
if (token == colnodToken) {
|
||||
alreadySynced.append(colnodToken->id());
|
||||
continue;
|
||||
@ -193,7 +203,7 @@ std::unique_ptr<PreparedData> SyncManager::prepareData(const std::shared_ptr<Dat
|
||||
|
||||
for (const auto &id : colnodData->deletedTokens) {
|
||||
auto corespondingToken = database->getToken(id);
|
||||
if ( !corespondingToken) {
|
||||
if ( corespondingToken->id().isEmpty()) {
|
||||
qWarning(syncmanager) << "Token:" << id << ", deleted in colnod, but not exists in db";
|
||||
continue;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user