diff --git a/index.ts b/index.ts index b20070b8b9681994fd1329764731bb109227c204..99992628ba1076fe07e41d42a7256cf7caae4c39 100644 --- a/index.ts +++ b/index.ts @@ -57,7 +57,7 @@ program.command('main') }) break case 'ADMIN_ASSOCIATION': - questions[0].choices = ['Créer un token', 'Bruler des tokens', 'Voir mon portefeuille'] + questions[0].choices = ['Créer un token', 'Bruler des tokens', 'Résoudre une proposition', 'Clôturer une proposition', 'Voir les propositions', 'Voir mon portefeuille'] await inquirer.prompt(questions).then(async (answers: { choice: string }) => { await handleAdminAssociationChoice(answers.choice, tezos) }) diff --git a/src/features/proposal/closeProposal.ts b/src/features/proposal/closeProposal.ts new file mode 100644 index 0000000000000000000000000000000000000000..da2edd1d3c2e1910a32a8c0687d4b80d8613c231 --- /dev/null +++ b/src/features/proposal/closeProposal.ts @@ -0,0 +1,19 @@ +import { type TezosToolkit } from '@taquito/taquito' +import chalk from 'chalk' +import { handleGetProposals, handleCloseProposal } from '../../handlers/proposal/proposalHandlers.js' + +async function closeProposal (tezos: TezosToolkit): Promise<void> { + await handleGetProposals(tezos).then(async (response) => { + const proposalsByTitle: string[] = response + + await handleCloseProposal(proposalsByTitle, tezos).then(() => { + console.log(chalk.bgGreenBright('\nVous avez clôturé la proposition !!\n')) + }).catch((error) => { + console.log(chalk.bgRed(`\n${error.message}\n`)) + }) + }).catch((error) => { + console.log(chalk.bgRed(`\n${error.message}\n`)) + }) +} + +export { closeProposal } diff --git a/src/features/proposal/resolveProposal.ts b/src/features/proposal/resolveProposal.ts new file mode 100644 index 0000000000000000000000000000000000000000..0f1703c54b13930df3d3b82e8fbf818e38f51819 --- /dev/null +++ b/src/features/proposal/resolveProposal.ts @@ -0,0 +1,19 @@ +import { type TezosToolkit } from '@taquito/taquito' +import chalk from 'chalk' +import { handleGetProposals, handleResolveProposal } from '../../handlers/proposal/proposalHandlers.js' + +async function resolveProposal (tezos: TezosToolkit): Promise<void> { + await handleGetProposals(tezos).then(async (response) => { + const proposalsByTitle: string[] = response + + await handleResolveProposal(proposalsByTitle, tezos).then(() => { + console.log(chalk.bgGreenBright('\nVous avez résolue la proposition !!\n')) + }).catch((error) => { + console.log(chalk.bgRed(`\n${error.message}\n`)) + }) + }).catch((error) => { + console.log(chalk.bgRed(`\n${error.message}\n`)) + }) +} + +export { resolveProposal } diff --git a/src/features/proposal/showProposals.ts b/src/features/proposal/showProposals.ts new file mode 100644 index 0000000000000000000000000000000000000000..7cb71f9b8ced3b0c40dc0996c8ac48f46c9f0da4 --- /dev/null +++ b/src/features/proposal/showProposals.ts @@ -0,0 +1,14 @@ +import { type TezosToolkit } from '@taquito/taquito' +import chalk from 'chalk' +import { handleGetProposals } from '../../handlers/proposal/proposalHandlers.js' + +async function showProposals (tezos: TezosToolkit): Promise<void> { + await handleGetProposals(tezos).then((response) => { + const proposalsByTitle = response + proposalsByTitle.forEach(title => { console.log(chalk.yellow(title)) }) + }).catch((error) => { + console.log(chalk.bgRed(`\n${error.message}\n`)) + }) +} + +export { showProposals } diff --git a/src/handlers/proposal/proposalHandlers.ts b/src/handlers/proposal/proposalHandlers.ts index edf78e041b539feb9721d19d241da005984f3a8f..892cfd67714aaed005a29b4233f08b9141d8753f 100644 --- a/src/handlers/proposal/proposalHandlers.ts +++ b/src/handlers/proposal/proposalHandlers.ts @@ -5,7 +5,7 @@ import { type TezosToolkit } from '@taquito/taquito' import { type Proposal } from '../../types/Proposal.js' // IMPORT SERVICES -import { createProposal } from '../../services/proposal.service.js' +import { closeProposal, createProposal, getProposals, resolveProposal } from '../../services/proposal.service.js' import inquirer from 'inquirer' /** @@ -61,4 +61,75 @@ async function handleCreateProposal (tezos: TezosToolkit): Promise<void> { } } -export { handleCreateProposal } +/** + * Handles the process of resolve proposal. + * @param {string[]} proposals - A list of proposal title. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<void>} A promise resolved once the resolve process is complete. + */ +async function handleResolveProposal (proposals: string[], tezos: TezosToolkit): Promise<void> { + const questions = [ + { + type: 'list', + name: 'choice', + message: 'Quelle proposition voulez-vous résoudre ?', + choices: proposals + } + ] + + let associationName: string + + await inquirer.prompt(questions).then(async (answers: { choice: string }) => { + associationName = answers.choice + }) + + try { + await resolveProposal(associationName, tezos) + } catch (error) { + throw new Error(`${error.lastError.with.string}`) + } +} + +/** + * Handles the process of close proposal. + * @param {string[]} proposals - A list of proposal title. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<void>} A promise resolved once the close process is complete. + */ +async function handleCloseProposal (proposals: string[], tezos: TezosToolkit): Promise<void> { + const questions = [ + { + type: 'list', + name: 'choice', + message: 'Quelle proposition voulez-vous clôturé ?', + choices: proposals + } + ] + + let associationName: string + + await inquirer.prompt(questions).then(async (answers: { choice: string }) => { + associationName = answers.choice + }) + + try { + await closeProposal(associationName, tezos) + } catch (error) { + throw new Error(`${error.lastError.with.string}`) + } +} + +/** + * Handles the process of listing proposals. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<string[]>} A promise with a string of proposal title. + */ +async function handleGetProposals (tezos: TezosToolkit): Promise<string[]> { + try { + return await getProposals(tezos) + } catch (error) { + throw new Error(`${error.lastError.with.string}`) + } +} + +export { handleCreateProposal, handleResolveProposal, handleGetProposals, handleCloseProposal } diff --git a/src/handlers/roleHandlers.ts b/src/handlers/roleHandlers.ts index 2a37892313181fa4b36547830424cd8ee470e63f..0a6e981350b3a642a7fea46c61b3b80286edf5ba 100644 --- a/src/handlers/roleHandlers.ts +++ b/src/handlers/roleHandlers.ts @@ -8,6 +8,9 @@ import { createAssociation } from '../features/association/createAssociation.js' import { burnToken } from '../features/token/burnToken.js' import { createToken } from '../features/token/createToken.js' import { createProposal } from '../features/proposal/createProposal.js' +import { resolveProposal } from '../features/proposal/resolveProposal.js' +import { showProposals } from '../features/proposal/showProposals.js' +import { closeProposal } from '../features/proposal/closeProposal.js' /** * Handles fa token administrator actions based on the specified choice. @@ -49,6 +52,15 @@ async function handleAdminAssociationChoice (choice: string, tezos: TezosToolkit case 'Bruler des tokens': await burnToken(tezos) break + case 'Résoudre une proposition': + await resolveProposal(tezos) + break + case 'Clôturer une proposition': + await closeProposal(tezos) + break + case 'Voir les propositions': + await showProposals(tezos) + break case 'Voir mon portefeuille': await showBalance(tezos) break diff --git a/src/services/proposal.service.ts b/src/services/proposal.service.ts index ca105ce3bb9a61c2a9f7d26c7c023619c492b78b..d4a562c0ce82c60f1a334c37f78026d7978a8e9d 100644 --- a/src/services/proposal.service.ts +++ b/src/services/proposal.service.ts @@ -4,6 +4,17 @@ import { type Proposal } from '../types/Proposal' // NEED UPDATE ADDRESS !! (SMART CONTRACT 2: Factory de DAO) const address = 'KT1QZJzhSPQ89K4eC59tmQYCt44qph7wXoJu' +const mockProposals: Proposal[] = [ + { + title: 'Proposal 1', + description: 'Ceci est la proposition 1' + }, + { + title: 'Proposal 2', + description: 'Ceci est la proposition 2' + } +] + // NEED UPDATE ENTRYPOINT !! async function createProposal (proposal: Proposal, tezos: TezosToolkit): Promise<void> { // const contract = await tezos.contract.at(address) @@ -49,4 +60,15 @@ async function viewVoteInProgressProposal (proposalName: string, tezos: TezosToo // await op.confirmation() } -export { createProposal, voteForProposal, viewVoteInProgressProposal, closeProposal, resolveProposal } +// NEED UPDATE ENTRYPOINT !! +async function getProposals (tezos: TezosToolkit): Promise<string[]> { + // const contract = await tezos.contract.at(address) + // const op: Operation = await contract.methodsObject.resolveProposal(proposalName).send() + + // await op.confirmation() + + // MOCK + return mockProposals.map(association => association.title) +} + +export { createProposal, voteForProposal, viewVoteInProgressProposal, closeProposal, resolveProposal, getProposals } diff --git a/src/utils/getRole.ts b/src/utils/getRole.ts index e21034dc158515d6afc37015b9999c909fb262e2..aa9df95e99233cc2088046047594a962290cb2c3 100644 --- a/src/utils/getRole.ts +++ b/src/utils/getRole.ts @@ -9,5 +9,5 @@ export function getRole (tezos: TezosToolkit): Role { // ELSE -> CONNECTED // TEMPORARY - return 'ADMIN_FA_TOKEN' + return 'ADMIN_ASSOCIATION' } diff --git a/test/handlers/roleHandlers.spec.ts b/test/handlers/roleHandlers.spec.ts index 985ec19ac6dbf924931327f6eda4843569e6224f..548583809da244e38295b5e334cb13710268a869 100644 --- a/test/handlers/roleHandlers.spec.ts +++ b/test/handlers/roleHandlers.spec.ts @@ -9,6 +9,9 @@ import { showBalance } from '../../src/features/balance/showBalance' import { createProposal } from '../../src/features/proposal/createProposal' import { joinAssociation } from '../../src/features/association/joinAssociation' import { showAssociations } from '../../src/features/association/showAssociations' +import { resolveProposal } from '../../src/features/proposal/resolveProposal' +import { showProposals } from '../../src/features/proposal/showProposals' +import { closeProposal } from '../../src/features/proposal/closeProposal' vi.mock('../../src/features/association/createAssociation', () => ({ createAssociation: vi.fn() @@ -42,24 +45,16 @@ vi.mock('../../src/features/balance/showBalance', () => ({ showBalance: vi.fn() })) -vi.mock('../../src/handlers/proposal/proposalHandlers', () => ({ - handleCreateProposal: vi.fn() +vi.mock('../../src/features/proposal/resolveProposal', () => ({ + resolveProposal: vi.fn() })) -vi.mock('../../src/handlers/association/associationHandlers', () => ({ - handleCreateAssociation: vi.fn(), - handleJoinAssociation: vi.fn(), - handleGetAssociations: vi.fn().mockResolvedValue([{}]), - handleGetAssociationDetails: vi.fn() +vi.mock('../../src/features/proposal/closeProposal', () => ({ + closeProposal: vi.fn() })) -vi.mock('../../src/handlers/token/tokenHandlers', () => ({ - handleCreateToken: vi.fn(), - handleBurnToken: vi.fn() -})) - -vi.mock('../../src/handlers/balance/balanceHandlers', () => ({ - handleGetBalance: vi.fn() +vi.mock('../../src/features/proposal/showProposals', () => ({ + showProposals: vi.fn() })) const mockedTezosToolkit = {} as unknown as TezosToolkit @@ -71,7 +66,7 @@ describe('roleHandlers', () => { describe('handleAdminFATokenChoice', () => { describe('when choice is "Créer une association"', () => { - it('should call handleCreateAssociation', async () => { + it('should call createAssociation', async () => { await handleAdminFATokenChoice('Créer une association', mockedTezosToolkit) expect(createAssociation).toBeCalled() @@ -79,7 +74,7 @@ describe('roleHandlers', () => { }) describe('when choice is "Créer un token"', () => { - it('should call handleCreateToken', async () => { + it('should call createToken', async () => { await handleAdminFATokenChoice('Créer un token', mockedTezosToolkit) expect(createToken).toBeCalled() @@ -87,7 +82,7 @@ describe('roleHandlers', () => { }) describe('when choice is "Bruler des tokens"', () => { - it('should call handleBurnToken', async () => { + it('should call burnToken', async () => { await handleAdminFATokenChoice('Bruler des tokens', mockedTezosToolkit) expect(burnToken).toBeCalled() @@ -95,7 +90,7 @@ describe('roleHandlers', () => { }) describe('when choice is "Voir mon portefeuille"', () => { - it('should call handleGetBalance', async () => { + it('should call showBalance', async () => { await handleAdminFATokenChoice('Voir mon portefeuille', mockedTezosToolkit) expect(showBalance).toBeCalled() @@ -114,7 +109,7 @@ describe('roleHandlers', () => { describe('handleAdminAssociationChoice', () => { describe('when choice is "Créer un token"', () => { - it('should call handleCreateToken', async () => { + it('should call createToken', async () => { await handleAdminAssociationChoice('Créer un token', mockedTezosToolkit) expect(createToken).toBeCalled() @@ -122,15 +117,39 @@ describe('roleHandlers', () => { }) describe('when choice is "Bruler des tokens"', () => { - it('should call handleBurnToken', async () => { + it('should call burnToken', async () => { await handleAdminAssociationChoice('Bruler des tokens', mockedTezosToolkit) expect(burnToken).toBeCalled() }) }) + describe('when choice is "Résoudre une proposition"', () => { + it('should call resolveProposal', async () => { + await handleAdminAssociationChoice('Résoudre une proposition', mockedTezosToolkit) + + expect(resolveProposal).toBeCalled() + }) + }) + + describe('when choice is "Voir les propositions"', () => { + it('should call showProposals', async () => { + await handleAdminAssociationChoice('Voir les propositions', mockedTezosToolkit) + + expect(showProposals).toBeCalled() + }) + }) + + describe('when choice is "Clôturer une proposition"', () => { + it('should call closeProposal', async () => { + await handleAdminAssociationChoice('Clôturer une proposition', mockedTezosToolkit) + + expect(closeProposal).toBeCalled() + }) + }) + describe('when choice is "Voir mon portefeuille"', () => { - it('should call handleGetBalance', async () => { + it('should call showBalance', async () => { await handleAdminAssociationChoice('Voir mon portefeuille', mockedTezosToolkit) expect(showBalance).toBeCalled() @@ -150,7 +169,7 @@ describe('roleHandlers', () => { describe('handleAdherentChoice', () => { describe('when choice is "Faire une proposition"', () => { - it('should call handleCreateProposal', async () => { + it('should call createProposal', async () => { await handleAdherentChoice('Faire une proposition', mockedTezosToolkit) expect(createProposal).toBeCalled() @@ -158,7 +177,7 @@ describe('roleHandlers', () => { }) describe('when choice is "Créer un token"', () => { - it('should call handleCreateToken', async () => { + it('should call createToken', async () => { await handleAdherentChoice('Créer un token', mockedTezosToolkit) expect(createToken).toBeCalled() @@ -166,7 +185,7 @@ describe('roleHandlers', () => { }) describe('when choice is "Voir mon portefeuille"', () => { - it('should call handleGetBalance', async () => { + it('should call showBalance', async () => { await handleAdherentChoice('Voir mon portefeuille', mockedTezosToolkit) expect(showBalance).toBeCalled() @@ -185,7 +204,7 @@ describe('roleHandlers', () => { describe('handleConnectedChoice', () => { describe('when choice is "Rejoindre une association"', () => { - it('should call handleJoinAssociation', async () => { + it('should call joinAssociation', async () => { await handleConnectedChoice('Rejoindre une association', mockedTezosToolkit) expect(joinAssociation).toBeCalled() @@ -193,7 +212,7 @@ describe('roleHandlers', () => { }) describe('when choice is "Créer un token"', () => { - it('should call handleCreateToken', async () => { + it('should call createToken', async () => { await handleConnectedChoice('Créer un token', mockedTezosToolkit) expect(createToken).toBeCalled() @@ -201,7 +220,7 @@ describe('roleHandlers', () => { }) describe('when choice is "Voir les associations"', () => { - it('should call handleGetAssociations', async () => { + it('should call showAssociations', async () => { await handleConnectedChoice('Voir les associations', mockedTezosToolkit) expect(showAssociations).toBeCalled() @@ -209,7 +228,7 @@ describe('roleHandlers', () => { }) describe('when choice is "Voir mon portefeuille"', () => { - it('should call handleGetBalance', async () => { + it('should call showBalance', async () => { await handleConnectedChoice('Voir mon portefeuille', mockedTezosToolkit) expect(showBalance).toBeCalled()