Skip to content

Commit

Permalink
Add Profile:SetAttribute Functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamid HK committed Jun 11, 2023
1 parent 2142f5f commit 8395624
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 10 deletions.
10 changes: 0 additions & 10 deletions services/blockchain-indexer/shared/database/schema/profiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
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,
};

0 comments on commit 8395624

Please sign in to comment.