change state_users table to user and rename colums in camelCase, adapt

This commit is contained in:
Einhornimmond 2021-09-17 21:13:14 +02:00
parent c9f621e119
commit 0c7bb13b4e
12 changed files with 106 additions and 60 deletions

View File

@ -21,7 +21,7 @@ import { isAuthorized } from './auth/auth'
// TODO implement
// import queryComplexity, { simpleEstimator, fieldConfigEstimator } from "graphql-query-complexity";
const DB_VERSION = '0001-init_db'
const DB_VERSION = '0002-update_user'
const context = (args: any) => {
const authorization = args.req.headers.authorization

View File

@ -223,22 +223,22 @@ class AppController extends Controller
$public_key_bin = hex2bin($json['user']['public_hex']);
$stateUserQuery = $stateUserTable
->find('all')
->where(['public_key' => $public_key_bin])
->where(['pubkey' => $public_key_bin])
->contain('StateBalances', function ($q) {
return $q->order(['record_date' => 'DESC'])
->limit(1);
});
if ($stateUserQuery->count() == 1) {
$stateUser = $stateUserQuery->first();
if ($stateUser->first_name != $json['user']['first_name'] ||
$stateUser->last_name != $json['user']['last_name'] ||
if ($stateUser->firstName != $json['user']['first_name'] ||
$stateUser->lastName != $json['user']['last_name'] ||
$stateUser->disabled != $json['user']['disabled'] ||
//$stateUser->username != $json['user']['username'] ||
// -> throws error
$stateUser->email != $json['user']['email']
) {
$stateUser->first_name = $json['user']['first_name'];
$stateUser->last_name = $json['user']['last_name'];
$stateUser->firstName = $json['user']['first_name'];
$stateUser->lastName = $json['user']['last_name'];
$stateUser->disabled = intval($json['user']['disabled']);
//$stateUser->username = $json['user']['username'];
$stateUser->email = $json['user']['email'];
@ -250,9 +250,9 @@ class AppController extends Controller
//echo $stateUser['id'];
} else {
$newStateUser = $stateUserTable->newEntity();
$newStateUser->public_key = $public_key_bin;
$newStateUser->first_name = $json['user']['first_name'];
$newStateUser->last_name = $json['user']['last_name'];
$newStateUser->pubkey = $public_key_bin;
$newStateUser->firstName = $json['user']['first_name'];
$newStateUser->lastName = $json['user']['last_name'];
$newStateUser->disabled = intval($json['user']['disabled']);
//$newStateUser->username = $json['user']['username'];
$newStateUser->email = $json['user']['email'];

View File

@ -308,7 +308,7 @@ class JsonRequestHandlerController extends AppController {
$stateError = $stateErrorTable->newEntity();
//
$pubkey = hex2bin($jsonData->public_key);
$user_query = $stateUsersTable->find('all')->select(['id'])->where(['public_key' => $pubkey]);
$user_query = $stateUsersTable->find('all')->select(['id'])->where(['pubkey' => $pubkey]);
if($user_query->count() != 1) {
return $this->returnJson(['state' => 'error', 'msg' => 'user not found', 'details' => 'user pubkey hex:' . $jsonData->public_key]);
}
@ -412,7 +412,7 @@ class JsonRequestHandlerController extends AppController {
//$pubkeys->sender
//$pubkeys->receiver
$stateUserTable = TableRegistry::getTableLocator()->get('StateUsers');
$user = $stateUserTable->find('all')->where(['public_key' => hex2bin($pubkeys->sender)])->contain(['StateBalances']);
$user = $stateUserTable->find('all')->where(['pubkey' => hex2bin($pubkeys->sender)])->contain(['StateBalances']);
if(!$user->count()) {
return $this->returnJson(['state' => 'not found', 'msg' => 'user not found or empty balance']);
}
@ -461,7 +461,7 @@ class JsonRequestHandlerController extends AppController {
private function userDelete($userPubkeyHex) {
$stateUserTable = TableRegistry::getTableLocator()->get('StateUsers');
$user = $stateUserTable->find('all')->where(['public_key' => hex2bin($userPubkeyHex)]);
$user = $stateUserTable->find('all')->where(['pubkey' => hex2bin($userPubkeyHex)]);
if(!$user || $user->count == 0) {
return $this->returnJson(['state' => 'error', 'msg' => 'user not found']);
}

View File

@ -132,7 +132,7 @@ class StateUserRolesController extends AppController
$public_hex = hex2bin($requestData['public_hex']);
$stateUser = $this->StateUsers->find('all')->where(['public_key' => $public_hex])->first();
$stateUser = $this->StateUsers->find('all')->where(['pubkey' => $public_hex])->first();
foreach($requestData['role_id'] as $role_id)
{
@ -158,7 +158,7 @@ class StateUserRolesController extends AppController
$publichex = hex2bin($public_hex);
$stateUser = $this->StateUsers->find('all')->where(['public_key' => $publichex])->first();
$stateUser = $this->StateUsers->find('all')->where(['pubkey' => $publichex])->first();
$stateUserRoles = $this->StateUserRoles->find('all')->where(['state_user_id' => $stateUser->id])->all();

View File

@ -182,15 +182,15 @@ class StateUsersController extends AppController
if($account_state == 'email not activated') {
if(count($pubkeySorted) > 0) {
$communityUsers->where(['hex(public_key) IN' => array_keys($pubkeySorted)]);
$communityUsers->where(['hex(pubkey) IN' => array_keys($pubkeySorted)]);
} else {
$communityUsers = null;
}
} else {
$globalSearch = '%' . $searchString . '%';
$communityUsers->where(['OR' => [
'first_name LIKE' => $globalSearch,
'last_name LIKE' => $globalSearch,
'firstName LIKE' => $globalSearch,
'lastName LIKE' => $globalSearch,
'email LIKE' => $globalSearch
]]);
}
@ -199,7 +199,7 @@ class StateUsersController extends AppController
//var_dump($communityUsers->toArray());
if($communityUsers) {
foreach($communityUsers as $u) {
$pubkey_hex = bin2hex(stream_get_contents($u->public_key));
$pubkey_hex = bin2hex(stream_get_contents($u->pubkey));
$u->public_hex = $pubkey_hex;
if(!isset($pubkeySorted[$pubkey_hex])) {
$pubkeySorted[$pubkey_hex] = ['login' => [], 'community' => []];
@ -229,9 +229,9 @@ class StateUsersController extends AppController
$color = 'danger';
if(count($user['community']) == 1) {
$c_user = $user['community'][0];
$finalUser['name'] = $c_user->first_name . ' ' . $c_user->last_name;
$finalUser['first_name'] = $c_user->first_name;
$finalUser['last_name'] = $c_user->last_name;
$finalUser['name'] = $c_user->firstName . ' ' . $c_user->lastName;
$finalUser['first_name'] = $c_user->firstName;
$finalUser['last_name'] = $c_user->lastName;
$finalUser['email'] = $c_user->email;
}
} else if(count($user['login']) == 1) {
@ -415,7 +415,7 @@ class StateUsersController extends AppController
//$user = $jsonData['user'];
//var_dump($jsonData);
$pubkey = hex2bin($jsonData['pubkeyhex']);
$stateUsers = $this->StateUsers->find('all')->where(['public_key' => $pubkey]);
$stateUsers = $this->StateUsers->find('all')->where(['pubkey' => $pubkey]);
if($stateUsers->count() != 1) {
return $this->returnJson(['state' => 'error', 'msg' => 'invalid result count']);
}
@ -448,7 +448,7 @@ class StateUsersController extends AppController
$pubkey = hex2bin($jsonData['pubkeyhex']);
$stateUsers = $this->StateUsers
->find('all')
->where(['public_key' => $pubkey])
->where(['pubkey' => $pubkey])
->select(['id']);
if($stateUsers->count() != 1) {
return $this->returnJson(['state' => 'error', 'msg' => 'invalid result count']);

View File

@ -101,7 +101,7 @@ class TransactionCreationsController extends AppController
$receiverProposal = [];
foreach ($stateUsers as $stateUser) {
$name = $stateUser->email;
$keyHex = bin2hex(stream_get_contents($stateUser->public_key));
$keyHex = bin2hex(stream_get_contents($stateUser->pubkey));
if ($name === null) {
$name = $stateUser->first_name . ' ' . $stateUser->last_name;
}
@ -241,14 +241,14 @@ class TransactionCreationsController extends AppController
$this->log("search for text: ".$requestData['searchText'], 'debug');
$stateUsers = $stateUserTable
->find('all')
->select(['id', 'first_name', 'last_name', 'email'])
->order(['first_name', 'last_name'])
->select(['id', 'firstName', 'lastName', 'email'])
->order(['firstName', 'lastName'])
->where(
['AND' => [
'disabled' => 0,
'OR' => [
'LOWER(first_name) LIKE' => '%'.strtolower($requestData['searchText']).'%',
'LOWER(last_name) LIKE' => '%'.strtolower($requestData['searchText']).'%',
'LOWER(firstName) LIKE' => '%'.strtolower($requestData['searchText']).'%',
'LOWER(lastName) LIKE' => '%'.strtolower($requestData['searchText']).'%',
'LOWER(email) LIKE' => '%'.strtolower($requestData['searchText']).'%'
]
]
@ -265,10 +265,10 @@ class TransactionCreationsController extends AppController
} else {
$stateUsers = $stateUserTable
->find('all')
->select(['id', 'first_name', 'last_name', 'email'])
->select(['id', 'firstName', 'lastName', 'email'])
//->order(['id'])
->where(['disabled' => 0])
->order(['first_name', 'last_name'])
->order(['firstName', 'lastName'])
->contain(['TransactionCreations' => [
'fields' => [
'TransactionCreations.amount',
@ -300,7 +300,7 @@ class TransactionCreationsController extends AppController
//if($sumAmount < 20000000) {
array_push($possibleReceivers, [
'name' => $stateUser->first_name . '&nbsp;' . $stateUser->last_name,
'name' => $stateUser->firstName . '&nbsp;' . $stateUser->lastName,
'id' => $stateUser->id,
'email' => $stateUser->email,
'amount' => $sumAmount,
@ -353,7 +353,7 @@ class TransactionCreationsController extends AppController
}
$receiverUsers = $stateUserTable->find('all')
->where(['id IN' => array_keys($users)])
->select(['public_key', 'email', 'id'])
->select(['pubkey', 'email', 'id'])
->contain(false);
foreach ($receiverUsers as $receiverUser) {
@ -371,7 +371,7 @@ class TransactionCreationsController extends AppController
} else {
$pendings[$id] = $localAmountCent;
}
$pubKeyHex = bin2hex(stream_get_contents($receiverUser->public_key));
$pubKeyHex = bin2hex(stream_get_contents($receiverUser->pubkey));
$requestAnswear = $this->JsonRequestClient->sendRequest(json_encode([
'session_id' => $session->read('session_id'),
'email' => $receiverUser->email,

View File

@ -30,16 +30,14 @@ class StateUser extends Entity
* @var array
*/
protected $_accessible = [
'index_id' => true,
'state_group_id' => true,
'public_key' => true,
'groupId' => true,
'pubkey' => true,
'email' => true,
'first_name' => true,
'last_name' => true,
'firstName' => true,
'lastName' => true,
'disabled' => true,
'username' => true,
'index' => true,
'state_group' => true,
'state_balances' => true,
'state_created' => true,
'transaction_creations' => true,
@ -48,11 +46,11 @@ class StateUser extends Entity
public function getEmailWithName()
{
return $this->first_name . ' ' . $this->last_name . ' <' . $this->email . '>';
return $this->firstName . ' ' . $this->lastName . ' <' . $this->email . '>';
}
public function getNames()
{
return $this->first_name . ' ' . $this->last_name;
return $this->firstName . ' ' . $this->lastName;
}
}

View File

@ -37,18 +37,10 @@ class StateUsersTable extends Table
{
parent::initialize($config);
$this->setTable('state_users');
$this->setTable('user');
$this->setDisplayField('email');
$this->setPrimaryKey('id');
/*$this->belongsTo('Indices', [
'foreignKey' => 'index_id',
'joinType' => 'INNER'
]);*/
$this->belongsTo('StateGroups', [
'foreignKey' => 'state_group_id',
'joinType' => 'INNER'
]);
$this->hasMany('StateBalances', [
'foreignKey' => 'state_user_id'
]);
@ -80,8 +72,8 @@ class StateUsersTable extends Table
->allowEmptyString('id', null, 'create');
$validator
->requirePresence('public_key', 'create')
->notEmptyString('public_key');
->requirePresence('pubkey', 'create')
->notEmptyString('pubkey');
return $validator;
}
@ -124,7 +116,7 @@ class StateUsersTable extends Table
$involvedUser = $this->find('all', [
'contain' => [],
'where' => ['id IN' => $involvedUserIds],
'fields' => ['id', 'first_name', 'last_name', 'email'],
'fields' => ['id', 'firstName', 'lastName', 'email'],
]);
//var_dump($involvedUser->toArray());
$involvedUserIndices = [];

View File

@ -49,13 +49,13 @@ class TransactionBase {
protected function getStateUserId($publicKey) {
$stateUsersTable = self::getTable('state_users');
$stateUser = $stateUsersTable->find('all')->select(['id'])->where(['public_key' => $publicKey])->first();
$stateUser = $stateUsersTable->find('all')->select(['id'])->where(['pubkey' => $publicKey])->first();
if($stateUser) {
return $stateUser->id;
}
// create new entry
$stateUserEntity = $stateUsersTable->newEntity();
$stateUserEntity->public_key = $publicKey;
$stateUserEntity->pubkey = $publicKey;
if($stateUsersTable->save($stateUserEntity)) {
return $stateUserEntity->id;
} else {
@ -77,7 +77,7 @@ class TransactionBase {
protected function getStateUserFromPublickey($publicKey) {
$stateUsersTable = self::getTable('state_users');
$stateUser = $stateUsersTable->find('all')->where(['public_key' => $publicKey])->first();
$stateUser = $stateUsersTable->find('all')->where(['pubkey' => $publicKey])->first();
if($stateUser) {
return $stateUser;
}

View File

@ -95,7 +95,7 @@ class TransactionCreation extends TransactionBase {
$existingCreations2 = $this->transactionCreationsTable
->find('all')
->select(['amount', 'state_user_id', 'target_date'])
->contain(['StateUsers' => ['fields' => ['StateUsers.public_key']]]);
->contain(['StateUsers' => ['fields' => ['StateUsers.pubkey']]]);
$q = $existingCreations2;
$targetDate = $this->protoTransactionCreation->getTargetDate();
@ -230,7 +230,7 @@ class TransactionCreation extends TransactionBase {
$receiverAmount = new \Proto\Gradido\TransferAmount();
$receiverAmount->setPubkey(stream_get_contents($stateUser->public_key));
$receiverAmount->setPubkey(stream_get_contents($stateUser->pubkey));
$receiverAmount->setAmount($transactionCreationEntity->amount);
$protoCreation->setReceiver($receiverAmount);

View File

@ -96,7 +96,7 @@ class TransactionTransfer extends TransactionBase {
$user = $stateUsersTable
->find('all')
->select(['id'])
->where(['public_key' => $senderPublic])
->where(['pubkey' => $senderPublic])
->contain(['StateBalances' => ['fields' => ['amount', 'state_user_id']]])->first();
if(!$user) {
$this->addError($functionName, 'couldn\'t find sender in db' );
@ -114,7 +114,7 @@ class TransactionTransfer extends TransactionBase {
return false;
}
// check if receiver exist
$receiver_user = $stateUsersTable->find('all')->select(['id'])->where(['public_key' => $receiver_public_key])->first();
$receiver_user = $stateUsersTable->find('all')->select(['id'])->where(['pubkey' => $receiver_public_key])->first();
if(!$receiver_user) {
$this->addError($functionName, 'couldn\'t find receiver in db' );
return false;

View File

@ -0,0 +1,56 @@
/* FIRST MIGRATION
*
* This migration is special since it takes into account that
* the database can be setup already but also may not be.
* Therefore you will find all `CREATE TABLE` statements with
* a `IF NOT EXISTS`, all `INSERT` with an `IGNORE` and in the
* downgrade function all `DROP TABLE` with a `IF EXISTS`.
* This ensures compatibility for existing or non-existing
* databases.
*/
export async function upgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
// write upgrade logic as parameter of queryFn
// rename table
await queryFn(`
ALTER TABLE state_users
RENAME TO user;
`)
// drop not used columns
await queryFn(`
ALTER TABLE user
DROP COLUMN index_id;
`)
// rename from snake case to camel case (cakePHP standard to typeorm standard)
await queryFn(`
ALTER TABLE user
CHANGE COLUMN group_id groupId int(10) unsigned NOT NULL DEFAULT '0',
CHANGE COLUMN public_key pubkey binary(32) NOT NULL,
CHANGE COLUMN first_name firstName varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
CHANGE COLUMN last_name lastName varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL;
`)
}
export async function downgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
// write downgrade logic as parameter of queryFn
// rename table
await queryFn(`
ALTER TABLE user
RENAME TO state_users;
`)
// put back dropped column
await queryFn(`
ALTER TABLE state_users
ADD index_id smallint(6) NOT NULL DEFAULT '0';
`)
// rename from camel case to snake case (typeorm standard to cakePHP standard)
await queryFn(`
ALTER TABLE state_users
CHANGE COLUMN groupId group_id int(10) unsigned NOT NULL DEFAULT '0',
CHANGE COLUMN pubkey public_key binary(32) NOT NULL,
CHANGE COLUMN firstName first_name varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
CHANGE COLUMN lastName last_name varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL;
`)
}