Skip to content
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
3 changes: 2 additions & 1 deletion prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ async function createDefaultProject() {
console.log('## CREATING DEFAULT PROJECT ##');
console.log('##############################');
console.log('');
console.log(`Project name ${project.name}`);
console.log(`Project key: ${project.id}`);
console.log(`Project name ${project.name}`);
console.log(`Project name ${project.mainBranchName}`);
});
}
}
10 changes: 3 additions & 7 deletions src/projects/dto/create-project.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';
import { PickType } from '@nestjs/swagger';
import { ProjectDto } from './project.dto';

export class CreateProjectDto {
@ApiProperty()
@IsString()
readonly name: string;
}
export class CreateProjectDto extends PickType(ProjectDto, ['name', 'mainBranchName']) {}
23 changes: 23 additions & 0 deletions src/projects/dto/project.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsUUID, IsString } from 'class-validator';
import { Project } from '@prisma/client';

export class ProjectDto implements Project {
@ApiProperty()
@IsUUID()
readonly id: string;

@ApiProperty()
@IsString()
readonly name: string;

@ApiProperty()
@IsString()
readonly mainBranchName: string;

@ApiProperty()
readonly createdAt: Date;

@ApiProperty()
readonly updatedAt: Date;
}
26 changes: 14 additions & 12 deletions src/projects/projects.controller.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import {
Controller,
Get,
UseGuards,
Body,
Post,
Param,
ParseUUIDPipe,
Delete,
} from '@nestjs/common';
import { Controller, Get, UseGuards, Body, Post, Param, ParseUUIDPipe, Delete, Put } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOkResponse, ApiParam } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/auth.guard';
import { ProjectsService } from './projects.service';
import { CreateProjectDto } from './dto/create-project.dto';
import { Project } from '@prisma/client';
import { ProjectDto } from './dto/project.dto';

@Controller('projects')
@ApiTags('projects')
Expand All @@ -22,22 +14,32 @@ export class ProjectsController {
@Get()
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ApiOkResponse({ type: [ProjectDto] })
getAll(): Promise<Project[]> {
return this.projectsService.findAll();
}

@Post()
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ApiOkResponse({ type: ProjectDto })
create(@Body() createProjectDto: CreateProjectDto): Promise<Project> {
return this.projectsService.create(createProjectDto);
}

@Put()
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ApiOkResponse({ type: ProjectDto })
update(@Body() projectDto: ProjectDto): Promise<Project> {
return this.projectsService.update(projectDto);
}

@Delete(':id')
@ApiParam({ name: 'id', required: true })
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ApiOkResponse({ type: Number })
@ApiOkResponse({ type: ProjectDto })
@ApiParam({ name: 'id', required: true })
remove(@Param('id', new ParseUUIDPipe()) id: string): Promise<Project> {
return this.projectsService.remove(id);
}
Expand Down
16 changes: 14 additions & 2 deletions src/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BuildsService } from '../builds/builds.service';
import { TestVariationsService } from '../test-variations/test-variations.service';
import { PrismaService } from '../prisma/prisma.service';
import { Project } from '@prisma/client';
import { ProjectDto } from './dto/project.dto';

@Injectable()
export class ProjectsService {
Expand All @@ -21,6 +22,17 @@ export class ProjectsService {
return this.prismaService.project.create({
data: {
name: createProjectDto.name,
mainBranchName: createProjectDto.mainBranchName,
},
});
}

async update(projectDto: ProjectDto): Promise<Project> {
return this.prismaService.project.update({
where: { id: projectDto.id },
data: {
name: projectDto.name,
mainBranchName: projectDto.mainBranchName,
},
});
}
Expand All @@ -35,9 +47,9 @@ export class ProjectsService {
});

try {
await Promise.all(project.builds.map((build) => this.buildsService.remove(build.id)));
await Promise.all(project.builds.map(build => this.buildsService.remove(build.id)));
await Promise.all(
project.testVariations.map((testVariation) => this.testVariationsService.remove(testVariation.id))
project.testVariations.map(testVariation => this.testVariationsService.remove(testVariation.id))
);
} catch (err) {
console.log(err);
Expand Down
2 changes: 1 addition & 1 deletion test/builds.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Builds (e2e)', () => {

beforeEach(async () => {
user = await haveUserLogged(usersService);
project = await projecstService.create({ name: 'E2E test' });
project = await projecstService.create({ name: 'E2E test', mainBranchName: 'master' });
});

afterEach(async () => {
Expand Down
13 changes: 13 additions & 0 deletions test/projects.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const projectServiceMock = {
findAll: () => ['test'],
create: () => project,
remove: () => project,
update: () => project,
};

describe('Projects (e2e)', () => {
Expand Down Expand Up @@ -91,4 +92,16 @@ describe('Projects (e2e)', () => {
await requestWithAuth(app, 'delete', `/projects/${project.id}`, {}, 'asd').expect(401);
});
});

describe('PUT /', () => {
it('can edit', async () => {
const res = await requestWithAuth(app, 'put', `/projects`, project, loggedUser.token).expect(200);

expect(res.body).toStrictEqual(projectServiceMock.update());
});

it('not valid token', async () => {
await requestWithAuth(app, 'put', `/projects`, project, 'asd').expect(401);
});
});
});