diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 4eb3086b..98c4b48d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -36,7 +36,7 @@ jobs: uses: actions/checkout@v3 with: repository: CirclesUBI/circles-docker.git - ref: main + ref: test-api-profile-migration path: circles-docker - name: Setup docker repo diff --git a/src/user.js b/src/user.js index 31abc78f..46393f60 100644 --- a/src/user.js +++ b/src/user.js @@ -346,5 +346,108 @@ export default function createUserModule(web3, contracts, utils) { } return null; }, + + /** + * Update the profile migration consent of a user, connected (or not) to a deployed Safe address. + * + * @namespace core.user.updateProfileMigrationConsent + * + * @param {Object} account - web3 account instance + * @param {Object} userOptions - options + * @param {string} userOptions.safeAddress - owned Safe address + * @param {boolean} userOptions.profileMigrationConsent - consent (true or false) + * + * @return {boolean} - Returns true when successful + */ + updateProfileMigrationConsent: async (account, userOptions) => { + checkAccount(web3, account); + + const options = checkOptions(userOptions, { + safeAddress: { + type: web3.utils.checkAddressChecksum, + }, + profileMigrationConsent: { + type: 'boolean', + }, + }); + + const { address } = account; + const { safeAddress, profileMigrationConsent } = options; + const { signature } = web3.eth.accounts.sign( + [address, safeAddress, profileMigrationConsent].join(''), + account.privateKey, + ); + + await utils.requestAPI({ + path: ['users', safeAddress, 'update-profile-migration-consent'], + method: 'POST', + data: { + address: account.address, + signature, + data: { + safeAddress, + profileMigrationConsent, + }, + }, + }); + + return true; + }, + + /** + * Get the profile migration consent value of a user. + * + * @namespace core.user.getProfileMigrationConsent + * + * @param {Object} account - web3 account instance + * @param {Object} userOptions - options + * @param {string} userOptions.safeAddress - owned Safe address + * + * @return {profileMigrationConsent} - Whether or not the user has given their consent + */ + getProfileMigrationConsent: async (account, userOptions) => { + checkAccount(web3, account); + + const options = checkOptions(userOptions, { + safeAddress: { + type: web3.utils.checkAddressChecksum, + }, + }); + + const { address } = account; + const { safeAddress } = options; + const { signature } = web3.eth.accounts.sign( + [address, safeAddress].join(''), + account.privateKey, + ); + + try { + const response = await utils.requestAPI({ + path: ['users', safeAddress, 'get-profile-migration-consent'], + method: 'POST', + data: { + address: account.address, + signature, + }, + }); + + if ( + response && + response.data && + response.data.profileMigrationConsent + ) { + return response.data.profileMigrationConsent; + } + } catch (error) { + // Do nothing when not found or denied access ... + if ( + !error.request || + (error.request.status !== 404 && error.request.status !== 403) + ) { + throw error; + } + } + return null; + }, }; } diff --git a/test/user.test.js b/test/user.test.js index efb0be9e..1b973bc6 100644 --- a/test/user.test.js +++ b/test/user.test.js @@ -158,3 +158,44 @@ describe('User - delete', () => { }); }); }); + +describe('User - update profile migration consent', () => { + beforeAll(async () => { + account = getAccount(3); + + // The Safe must be deployed and signedup to the Hub before trying to change the consent + deployedSafe = await deploySafeAndToken(core, account); + safeAddress = deployedSafe.safeAddress; + username = `catty${new Date().getTime()}`; + email = 'catt@yyy.com'; + + // This update acts as a register + await core.user.update(account, { + email, + safeAddress, + username, + }); + }); + + describe('when a new user registers its Safe address', () => { + it('should return profile migration consent', async () => { + expect( + await core.user.getProfileMigrationConsent(account, { + safeAddress, + }), + ).toBe(false); + }); + + it('should return correct value of profile migration consent after updatins', async () => { + await core.user.updateProfileMigrationConsent(account, { + safeAddress, + profileMigrationConsent: true, + }), + expect( + await core.user.getProfileMigrationConsent(account, { + safeAddress, + }), + ).toBe(true); + }); + }); +});