Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Submission verification endpoint #29

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions apps/scv-gateway/src/contracts/contracts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import {
HttpException,
HttpStatus,
Param,
Res,
} from '@nestjs/common';
import { Response } from 'express';
import { FilesInterceptor } from '@nestjs/platform-express';
import { ContractsService } from './contracts.service';
import { ContractSubmissionDto } from './dto/contract-submission.dto';
import { ApiBody, ApiConsumes, ApiParam, ApiResponse } from '@nestjs/swagger';
import { ContractFilesValidator } from './validators/contract-files.validator';
import { ContractIdDto } from './dto/contract-id.dto';
import { ContractSubmissionStatusDto } from './dto/contract-submission-status.dto';
import { VerificationStatus } from './entities/contract-submission.entity';
import { ContractSubmissionStatusRequestDto } from './dto/contract-submission-status-request.dto';

@Controller('contracts')
export class ContractsController {
Expand Down Expand Up @@ -81,13 +86,39 @@ export class ContractsController {
);
}

@Get()
findAll() {
return this.contractsService.findAll();
}
@Get(':id/check/:submissionId')
@ApiParam({ name: 'id', type: String })
@ApiParam({ name: 'submissionId', type: String })
@ApiResponse({
status: HttpStatus.OK,
description: 'Contract is still pending or verified successfully',
type: ContractSubmissionStatusDto,
})
@ApiResponse({
status: HttpStatus.NOT_FOUND,
description: 'Contract or submission matching that contract not found',
})
@ApiResponse({
status: HttpStatus.UNPROCESSABLE_ENTITY,
description: 'Contract verification failed',
type: ContractSubmissionStatusDto,
})
async checkSubmissionStatus(
@Param() params: ContractSubmissionStatusRequestDto,
@Res() res: Response,
) {
const verificationStatus =
await this.contractsService.checkSubmissionStatus(
params.id,
params.submissionId,
);

// @Get(':id')
// findOne(@Param('id') id: string) {
// return this.contractsService.findOne(+id);
// }
if (verificationStatus.status === VerificationStatus.FAIL) {
res.status(HttpStatus.UNPROCESSABLE_ENTITY);
} else {
res.status(HttpStatus.OK);
}

res.json(verificationStatus);
}
}
34 changes: 28 additions & 6 deletions apps/scv-gateway/src/contracts/contracts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ContractSubmissionDto } from './dto/contract-submission.dto';
import { ContractSubmission } from './entities/contract-submission.entity';
import {
ContractSubmission,
VerificationStatus,
} from './entities/contract-submission.entity';
import { ContractSubmissionSourceFile } from './entities/contract-submission-source-file.entity';
import { ContractSubmissionResponseDto } from './dto/contract-submission-response.dto';
import { SubmissionNotFoundException } from './exceptions/submission-not-found.exception';
import { ContractSubmissionStatusDto } from './dto/contract-submission-status.dto';

@Injectable()
export class ContractsService {
Expand Down Expand Up @@ -40,11 +45,28 @@ export class ContractsService {
return contractSubmissionResponseDto;
}

findAll() {
return this.contractSubmissionsRepository.find();
}
async checkSubmissionStatus(
contractId: string,
submissionId: string,
): Promise<ContractSubmissionStatusDto> {
const submission = await this.contractSubmissionsRepository.findOne({
where: {
id: submissionId,
contractId,
},
});

if (!submission) {
throw new SubmissionNotFoundException(submissionId, contractId);
}

const contractSubmissionStatusDto = new ContractSubmissionStatusDto();
contractSubmissionStatusDto.status = submission.status;

if (submission.status === VerificationStatus.FAIL) {
contractSubmissionStatusDto.message = submission.result;
}

findOne(id: string) {
return `This action returns a #${id} contract`;
return contractSubmissionStatusDto;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IsNotEmpty, IsUUID, Validate } from 'class-validator';
import { ContractIdValidator } from '../validators/contract-id.validator';

export class ContractSubmissionStatusRequestDto {
@IsNotEmpty()
@Validate(ContractIdValidator)
id: string;

@IsUUID()
submissionId: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ApiProperty } from '@nestjs/swagger';
import { VerificationStatus } from '../entities/contract-submission.entity';

export class ContractSubmissionStatusDto {
@ApiProperty({
enum: VerificationStatus,
})
status: VerificationStatus;

@ApiProperty({
required: false,
description: 'Error message if status is FAIL',
})
message?: string;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Entity, Column, PrimaryGeneratedColumn, OneToMany } from 'typeorm';
import { ContractSubmissionSourceFile } from './contract-submission-source-file.entity';

export enum VerificationStatus {
PENDING = 'pending',
SUCCESS = 'success',
FAIL = 'fail',
}

@Entity('contract_submissions')
export class ContractSubmission {
@PrimaryGeneratedColumn('uuid')
Expand All @@ -18,6 +24,19 @@ export class ContractSubmission {
@Column()
entryFile: string;

@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
submittedAt: Date;

@Column({
type: 'enum',
enum: VerificationStatus,
default: VerificationStatus.PENDING,
})
status: VerificationStatus;

@Column({ nullable: true })
result: string;

@OneToMany(() => ContractSubmissionSourceFile, (file) => file.submission, {
cascade: true,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { HttpException, HttpStatus } from '@nestjs/common';

export class SubmissionNotFoundException extends HttpException {
constructor(submissionId: string, contractId: string | undefined) {
if (!contractId) {
super(`Submission ${submissionId} was not found`, HttpStatus.NOT_FOUND);
return;
}
super(
`Submission ${submissionId} was not found for contract ${contractId}`,
HttpStatus.NOT_FOUND,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { isAddressValid, Encoding } from '@aeternity/aepp-sdk';
@ValidatorConstraint({ name: 'contractId', async: false })
export class ContractIdValidator implements ValidatorConstraintInterface {
validate(text: string) {
console.log('validating', text);
return isAddressValid(text, Encoding.ContractAddress);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class SubmissionVerification1696408787963 implements MigrationInterface {
name = 'SubmissionVerification1696408787963';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "contract_submissions" ADD "submitted_at" TIMESTAMP NOT NULL DEFAULT now()`,
);
await queryRunner.query(
`CREATE TYPE "public"."contract_submissions_status_enum" AS ENUM('pending', 'success', 'fail')`,
);
await queryRunner.query(
`ALTER TABLE "contract_submissions" ADD "status" "public"."contract_submissions_status_enum" NOT NULL DEFAULT 'pending'`,
);
await queryRunner.query(
`ALTER TABLE "contract_submissions" ADD "result" character varying`,
);
await queryRunner.query(
`ALTER TABLE "contract_source_files" DROP COLUMN "file_path"`,
);
await queryRunner.query(
`ALTER TABLE "contract_source_files" ADD "file_path" character varying NOT NULL`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "contract_source_files" DROP COLUMN "file_path"`,
);
await queryRunner.query(
`ALTER TABLE "contract_source_files" ADD "file_path" character varying(127) NOT NULL`,
);
await queryRunner.query(
`ALTER TABLE "contract_submissions" DROP COLUMN "result"`,
);
await queryRunner.query(
`ALTER TABLE "contract_submissions" DROP COLUMN "status"`,
);
await queryRunner.query(
`DROP TYPE "public"."contract_submissions_status_enum"`,
);
await queryRunner.query(
`ALTER TABLE "contract_submissions" DROP COLUMN "submitted_at"`,
);
}
}
Loading