#TypeError: this.proposal is not a constructor

3 messages · Page 1 of 1 (latest)

zealous saffron
#

I am testing my service and getting this error

  ● ProposalService › createProposal › should create a proposal

    TypeError: this.proposal is not a constructor

      78 |     // TODO: Check if tx `txHash` was sent to a chain and was successful (status=1)
      79 |
    > 80 |     proposal = new this.proposal({
         |                ^
      81 |       txHash: txHash.toLowerCase(),
      82 |       title,
      83 |       description,

      at ProposalService.createProposal (proposal/proposal.service.ts:80:16)
      at Object.<anonymous> (proposal/proposal.controller.spec.ts:108:31)
#

Here is my service

import { ConflictException, Injectable, UnauthorizedException } from '@nestjs/common';
import { verifyMessage } from 'viem';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { IProposal } from '../proposal/interfaces/proposal.interface';
import { Proposal } from '../proposal/schemas/proposal.schema';
import { Deanonymization } from '../deanonymization/schemas/deanonymization.schema';
import { IDeanonymization } from '../deanonymization/interfaces/deanonymization.interface';
import { ConfigService } from '@nestjs/config';

declare let process: {
  env: {
    REVOKER_ADDRESS: `0x${string}`;
  };
};

@Injectable()
export class ProposalService {

  constructor(
    @InjectModel(Proposal.name)
    private readonly proposal: Model<IProposal>,
    @InjectModel(Deanonymization.name)
    private readonly deanonymization: Model<IDeanonymization>,

    private configService: ConfigService,
  ) {}

  async createProposal(
    txHash: `0x${string}`,
    title: string,
    description: string,
    signature: `0x${string}`,
  ) {
    let proposal = await this.proposal.findOne({ txHash: txHash.toLowerCase() });

    const revokerAddress = this.configService.get<`0x${string}`>('REVOKER_ADDRESS');

    const isValid = await verifyMessage({
      address: revokerAddress,
      message: {raw: txHash},
      signature: signature,
    });

    console.log(isValid)

    if (!isValid) {
      throw new UnauthorizedException('Invalid signature');
    }

    proposal = new this.proposal({ // GETTING ERR HERE
      txHash: txHash.toLowerCase(),
      title,
      description,
      signature,
    });


    const numContributionsPerGuardian = 2;

    const deanon = new this.deanonymization({
      txHash,
      numContributionsPerGuardian,
    });

    await proposal.save();
    await deanon.save();

    return proposal;
  }
}

#

Here is my test:

  describe('createProposal', () => {
    it('should create a proposal', async () => {
      const txHash: `0x${string}` = "0x6b2b9b59f967d74a503fa10cbd77aeed1a7e94060086ae2361b344755dd13cb2"
      const signature: `0x${string}` = "0xcebe9078bcf5f1e1672c4d503970f51acdc57f6a33a1b1e042eee84097003d9d7ed31726bb1285e8fc5ecc5f8e5ecf76273def48d571daff35277072e4676a701c"
      const mockProposal = {
        txHash: txHash,
        title: 'Test Proposal',
        description: 'This is a test proposal',
        signature: signature,
      };

      const mockConfigValue = '0xf41228dC5259Df28ef04EcEd2B63EBac820E736e';

      jest.spyOn(configService, 'get').mockReturnValue(mockConfigValue);

      const createdProposal = await service.createProposal(
        mockProposal.txHash,
        mockProposal.title,
        mockProposal.description,
        mockProposal.signature,
      );

      expect(createdProposal).toEqual(mockProposal);
    });