diff --git a/services/tunnelbroker/src/Database/DatabaseManager.h b/services/tunnelbroker/src/Database/DatabaseManager.h --- a/services/tunnelbroker/src/Database/DatabaseManager.h +++ b/services/tunnelbroker/src/Database/DatabaseManager.h @@ -43,6 +43,9 @@ findSessionItem(const std::string &deviceID); void removeSessionItem(const std::string &sessionID); void updateSessionItemIsOnline(const std::string &sessionID, bool isOnline); + bool updateSessionItemDeviceToken( + const std::string &sessionID, + const std::string &newDeviceToken); void putSessionSignItem(const SessionSignItem &item); std::shared_ptr diff --git a/services/tunnelbroker/src/Database/DatabaseManager.cpp b/services/tunnelbroker/src/Database/DatabaseManager.cpp --- a/services/tunnelbroker/src/Database/DatabaseManager.cpp +++ b/services/tunnelbroker/src/Database/DatabaseManager.cpp @@ -115,6 +115,45 @@ } } +bool DatabaseManager::updateSessionItemDeviceToken( + const std::string &sessionID, + const std::string &newDeviceToken) { + std::shared_ptr item = this->findSessionItem(sessionID); + if (item == nullptr) { + LOG(ERROR) << "Can't find for update sessionItem for sessionID: " + << sessionID; + return false; + } + Aws::DynamoDB::Model::UpdateItemRequest request; + request.SetTableName(item->getTableName()); + + Aws::DynamoDB::Model::AttributeValue attributeKeyValue; + attributeKeyValue.SetS(sessionID); + request.AddKey(DeviceSessionItem::FIELD_SESSION_ID, attributeKeyValue); + Aws::String update_expression("SET #a = :valueA"); + request.SetUpdateExpression(update_expression); + Aws::Map expressionAttributeNames; + expressionAttributeNames["#a"] = DeviceSessionItem::FIELD_NOTIFY_TOKEN; + request.SetExpressionAttributeNames(expressionAttributeNames); + + Aws::DynamoDB::Model::AttributeValue attributeUpdatedValue; + attributeUpdatedValue.SetS(newDeviceToken); + Aws::Map + expressionAttributeValue; + expressionAttributeValue[":valueA"] = attributeUpdatedValue; + request.SetExpressionAttributeValues(expressionAttributeValue); + + const Aws::DynamoDB::Model::UpdateItemOutcome &result = + getDynamoDBClient()->UpdateItem(request); + if (!result.IsSuccess()) { + LOG(ERROR) + << "Error updating device token at updateSessionItemDeviceToken: " + << result.GetError().GetMessage(); + return false; + } + return true; +} + void DatabaseManager::putSessionSignItem(const SessionSignItem &item) { Aws::DynamoDB::Model::PutItemRequest request; request.SetTableName(item.getTableName());