The method works, but I would like to know if there is any way to make it more readable, optimized?
I have user data (i want to import/update it). Accounts is finded by user data.
========
User order data is entered into the system.
userData.Id - user Id; userData.OrderNumber - user order number.
The "UpdateAccount" method below is used by the user. The user enters their Id and OrderNumber into the system (there are FirstName, LastName, etc., but they are irrelevant here because they are used in the UpdateAllUserDataToAccount method). Therefore, it is necessary to discover whether an account with the relevant data exists in the system.
You can update account data if the account has userData.OrderNumber or userData.Id. If no account under userData.OrderNumber or userData.Id is found in the system then create a new account and update its data. Do not allow anything if userData.OrderNumber, userData.Id is in different account (can not exist in the system in different account).
An account can have userData.OrderNumber in the system and not userData.Id because the employee registered the parcel but did not have a user id. An account can have userData.Id in the system and not userData.OrderNumber because the user has previously been registered in the system.
The user data userData.OrderNumber and userData.Id are unique and belong to only one user.
bool _isSpecialData - to indicate that the user is special (set before this code).
private void UpdateAccount(UserModel userData)
{
Account accountById = _accountController.GetAccountById(userData.Id);
Account accountByNumber = _accountController.GetAccountByNumber(userData.Number);
bool _isSpecialData = accountByNumber != null ? accountByNumber.Vip : false;
if (accountById != null)
{
if (accountByNumber != null)
{
if (accountById.Id == accountByNumber.Id)
{
if (_isSpecialData)
{
AddPartUserDataToAccount(userData, accountByNumber);
if (userData.Status == Blocked) return;
}
}
else
{
_log.Error($"User data can not be in different accounts");
return;
}
}
else
{
AddPartUserDataToAccount(userData, accountById);
}
}
else
{
if (accountByNumber != null)
{
if (accountByNumber.Id == null)
{
accountByNumber.Id = userData.Id;
if (_isSpecialData)
{
AddPartUserDataToAccount(userData, accountByNumber);
if (userData.Status == Blocked) return;
}
}
else
{
_log.Error($"accountByNumber.Id can be just with the same value as userData.Id or with null (because it was not set in first place)");
return;
}
}
else
{
CreateNewAccount(userData);
}
}
UpdateAllUserDataToAccount(userData);
}
accountById
,accountByNumber
etc. are. This could be useful. At least post the entire method. \$\endgroup\$ – Emaro Sep 22 '20 at 12:41accountByNumber
can be null,bool _isSpecialData = accountByNumber.Vip;
would throw an exception in such a case. \$\endgroup\$ – BCdotWEB Sep 22 '20 at 15:54