From fee7f8f6437cc9d44de5407d8daed138ed988df2 Mon Sep 17 00:00:00 2001 From: Nawfel Senoussi <nawfelsen@mbp-de-nawfel.home> Date: Thu, 28 Mar 2024 23:34:49 +0100 Subject: [PATCH] add close, resolve and get proposal --- index.ts | 2 +- src/features/proposal/closeProposal.ts | 19 ++++++ src/features/proposal/resolveProposal.ts | 19 ++++++ src/features/proposal/showProposals.ts | 14 +++++ src/handlers/proposal/proposalHandlers.ts | 75 ++++++++++++++++++++++- src/handlers/roleHandlers.ts | 12 ++++ src/services/proposal.service.ts | 24 +++++++- src/utils/getRole.ts | 2 +- test/handlers/roleHandlers.spec.ts | 75 ++++++++++++++--------- 9 files changed, 209 insertions(+), 33 deletions(-) create mode 100644 src/features/proposal/closeProposal.ts create mode 100644 src/features/proposal/resolveProposal.ts create mode 100644 src/features/proposal/showProposals.ts diff --git a/index.ts b/index.ts index b20070b..9999262 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 0000000..da2edd1 --- /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 0000000..0f1703c --- /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 0000000..7cb71f9 --- /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 edf78e0..892cfd6 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 2a37892..0a6e981 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 ca105ce..d4a562c 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 e21034d..aa9df95 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 985ec19..5485838 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() -- GitLab