From 8395624acc940a113eab4b79df42b62e1da64013 Mon Sep 17 00:00:00 2001 From: Hamid HK Date: Sun, 11 Jun 2023 22:56:16 +0330 Subject: [PATCH] Add Profile:SetAttribute Functionality --- .../shared/database/schema/profiles.js | 10 -- .../profile/setAttribute.js | 95 +++++++++++++++++++ 2 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 services/blockchain-indexer/shared/indexer/transactionProcessor/profile/setAttribute.js diff --git a/services/blockchain-indexer/shared/database/schema/profiles.js b/services/blockchain-indexer/shared/database/schema/profiles.js index 6b1903e70..54acb4120 100644 --- a/services/blockchain-indexer/shared/database/schema/profiles.js +++ b/services/blockchain-indexer/shared/database/schema/profiles.js @@ -6,16 +6,6 @@ module.exports = { name: { type: 'string', null: true, defaultValue: null }, nickName: { type: 'string', null: true, defaultValue: null }, description: { type: 'string', null: true, defaultValue: null }, - // @TODO: Modify Schema in order to support social media accounts - // socialAccounts: { type: 'array', - // items: { - // type: 'object', - // props: { - // username: { type: 'string', null: true, defaultValue: null }, - // platform: { type: 'integer', null: true, defaultValue: null }, - // }, - // }, - // }, avatarHash: { type: 'string', null: true, defaultValue: null }, avatarSignature: { type: 'string', null: true, defaultValue: null }, bannerHash: { type: 'string', null: true, defaultValue: null }, diff --git a/services/blockchain-indexer/shared/indexer/transactionProcessor/profile/setAttribute.js b/services/blockchain-indexer/shared/indexer/transactionProcessor/profile/setAttribute.js new file mode 100644 index 000000000..20f8f7ac5 --- /dev/null +++ b/services/blockchain-indexer/shared/indexer/transactionProcessor/profile/setAttribute.js @@ -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, +};