-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Profile:SetAttribute Functionality
- Loading branch information
Hamid HK
committed
Jun 11, 2023
1 parent
2142f5f
commit 8395624
Showing
2 changed files
with
95 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
services/blockchain-indexer/shared/indexer/transactionProcessor/profile/setAttribute.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
const { | ||
Logger, | ||
MySQL: { getTableInstance }, | ||
} = require('lisk-service-framework'); | ||
const BluebirdPromise = require('bluebird'); | ||
const config = require('../../../../config'); | ||
const { getLisk32AddressFromPublicKey } = require('../../../utils/account'); | ||
|
||
const logger = Logger(); | ||
|
||
const MYSQL_ENDPOINT = config.endpoints.mysql; | ||
const profilesTableSchema = require('../../../database/schema/profiles'); | ||
const socialAccountsTableSchema = require('../../../database/schema/socialAccounts'); | ||
|
||
const getProfilesTable = () => getTableInstance( | ||
profilesTableSchema.tableName, | ||
profilesTableSchema, | ||
MYSQL_ENDPOINT, | ||
); | ||
const getSocialAccountsTable = () => getTableInstance( | ||
socialAccountsTableSchema.tableName, | ||
socialAccountsTableSchema, | ||
MYSQL_ENDPOINT, | ||
); | ||
|
||
// Command specific constants | ||
const COMMAND_NAME = 'setAttributes'; | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
const applyTransaction = async (blockHeader, tx, events, dbTrx) => { | ||
const profilesTable = await getProfilesTable(); | ||
const socialAccountsTable = await getSocialAccountsTable(); | ||
const senderAddress = getLisk32AddressFromPublicKey(tx.senderPublicKey); | ||
|
||
const account = { | ||
address: senderAddress, | ||
}; | ||
logger.trace(`Indexing profiles with address ${account.address}.`); | ||
const [existingProfile] = await profilesTable.find( | ||
{ profileID: tx.params.profileID }, | ||
['profileID', 'name', 'nickName', 'description', 'avatarHash', 'avatarSignature', 'bannerHash', 'bannerSignature', 'creatorAddress'], | ||
dbTrx, | ||
); | ||
if (!existingProfile) { | ||
throw new Error(`Profile with ID ${tx.params.profileID} does not exist.`); | ||
} | ||
|
||
const newProfile = { | ||
// Copy existing profile properties | ||
...tx.params, | ||
// Use the existing creatorAddress, prevent changing it | ||
}; | ||
newProfile.profileID = existingProfile.profileID; | ||
newProfile.creatorAddress = existingProfile.creatorAddress; | ||
|
||
// // Copy properties from tx.params.profile dynamically | ||
// Object.keys(tx.params.profile).forEach((property) => { | ||
// if (property !== 'profileID' && property !== 'creatorAddress') { | ||
// profile[property] = tx.params.profile[property]; | ||
// } | ||
// }); | ||
// Delete existing social account records | ||
await socialAccountsTable.delete({ profileID: existingProfile.profileID }, dbTrx); | ||
|
||
// Insert new social account records | ||
await BluebirdPromise.map( | ||
tx.params.socialAccounts, | ||
async (socialAccount) => { | ||
const socialInfo = { | ||
profileID: existingProfile.profileID, | ||
username: socialAccount.username, | ||
platform: socialAccount.platform, | ||
}; | ||
logger.trace(`Updating social accounts for the profile with ID ${existingProfile.profileID}.`); | ||
await socialAccountsTable.upsert(socialInfo, dbTrx); | ||
logger.debug(`Updating social accounts for the profile with ID ${existingProfile.profileID}.`); | ||
return true; | ||
}, | ||
{ concurrency: tx.params.socialAccounts.length }, | ||
); | ||
|
||
// Update the profile record | ||
|
||
await profilesTable.upsert(newProfile, dbTrx); | ||
logger.debug(`Updated profile with ID ${existingProfile.profileID}.`); | ||
}; | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
const revertTransaction = async (blockHeader, tx, events, dbTrx) => {}; | ||
|
||
module.exports = { | ||
COMMAND_NAME, | ||
applyTransaction, | ||
revertTransaction, | ||
}; |