Repository navigation
refactor(zone): migrer le module vers server-nestjs #2487
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c119bcb
refactor(zone): name the zone record type in service specs
shikanime 822f9d6
refactor(zone): drop event-helper comment
shikanime 95dce81
refactor(zone): map records to contract in controller
shikanime 621288d
fix(zone): accept null log projectId and validate zone UUID params
shikanime b78eb6d
refactor(zone): name the update body schema in shared
shikanime File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
apps/server-nestjs/src/modules/admin-role/admin-role-testing.utils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import type { Cluster, Prisma } from '@prisma/client' | ||
|
|
||
| export const zoneSelect = { | ||
| id: true, | ||
| slug: true, | ||
| label: true, | ||
| argocdUrl: true, | ||
| description: true, | ||
| createdAt: true, | ||
| updatedAt: true, | ||
| } satisfies Prisma.ZoneSelect | ||
|
|
||
| export type Zone = Prisma.ZoneGetPayload<{ | ||
| select: typeof zoneSelect | ||
| }> | ||
|
|
||
| export function getZoneById(tx: Prisma.TransactionClient, id: Zone['id']) { | ||
| return tx.zone.findUnique({ where: { id }, select: zoneSelect }) | ||
| } | ||
|
|
||
| export function listZones(tx: Prisma.TransactionClient) { | ||
| return tx.zone.findMany({ select: zoneSelect }) | ||
| } | ||
|
|
||
| export function createZone( | ||
| tx: Prisma.TransactionClient, | ||
| data: Pick<Zone, 'slug' | 'label' | 'argocdUrl' | 'description'>, | ||
| ) { | ||
| return tx.zone.create({ data, select: zoneSelect }) | ||
| } | ||
|
|
||
| export function updateZone( | ||
| tx: Prisma.TransactionClient, | ||
| zoneId: Zone['id'], | ||
| data: Pick<Zone, 'label' | 'argocdUrl' | 'description'>, | ||
| ) { | ||
| return tx.zone.update({ where: { id: zoneId }, data, select: zoneSelect }) | ||
| } | ||
|
|
||
| export function linkZoneToClusters(tx: Prisma.TransactionClient, zoneId: Zone['id'], clusterIds: Array<Cluster['id']>) { | ||
| return tx.zone.update({ | ||
| where: { id: zoneId }, | ||
| data: { clusters: { connect: clusterIds.map(id => ({ id })) } }, | ||
| select: zoneSelect, | ||
| }) | ||
| } | ||
|
|
||
| export async function createZoneWithClusters( | ||
| tx: Prisma.TransactionClient, | ||
| data: Pick<Zone, 'slug' | 'label' | 'argocdUrl' | 'description'>, | ||
| clusterIds?: Array<Cluster['id']>, | ||
| ) { | ||
| const zone = await createZone(tx, data) | ||
| if (clusterIds?.length) { | ||
| await linkZoneToClusters(tx, zone.id, clusterIds) | ||
| } | ||
| return zone | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import type { Zone } from './zone-queries.utils' | ||
| import { faker } from '@faker-js/faker' | ||
|
|
||
| export function makeZone(overrides: Partial<Zone> = {}): Zone { | ||
| return { | ||
| id: faker.string.uuid(), | ||
| slug: faker.helpers.slugify(faker.word.sample(5)).toLowerCase(), | ||
| label: faker.word.sample(10), | ||
| argocdUrl: `https://argocd.example.com/${faker.helpers.slugify(faker.word.sample(5))}`, | ||
| description: faker.lorem.sentence(), | ||
| createdAt: faker.date.past(), | ||
| updatedAt: faker.date.past(), | ||
| ...overrides, | ||
| } satisfies Zone | ||
| } |
65 changes: 65 additions & 0 deletions
65
apps/server-nestjs/src/modules/zone/zone.controller.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import type { NestFastifyApplication } from '@nestjs/platform-fastify' | ||
| import type { TestingModule } from '@nestjs/testing' | ||
| import { FastifyAdapter } from '@nestjs/platform-fastify' | ||
| import { Test } from '@nestjs/testing' | ||
| import { beforeEach, describe, expect, it } from 'vitest' | ||
| import { mock } from 'vitest-mock-extended' | ||
| import { UserGuard } from '../infrastructure/permission/user/user.guard' | ||
| import { makeZone } from './zone-testing.utils' | ||
| import { ZoneController } from './zone.controller' | ||
| import { ZoneService } from './zone.service' | ||
| import { toZone } from './zone.utils' | ||
|
|
||
| describe('zoneController', () => { | ||
| let module: TestingModule | ||
| let controller: ZoneController | ||
| let service: MockProxy<ZoneService> | ||
|
|
||
| const zone = makeZone({ slug: 'paris', label: 'Paris' }) | ||
|
|
||
| beforeEach(async () => { | ||
| service = mock<ZoneService>() | ||
|
|
||
| module = await Test.createTestingModule({ | ||
| controllers: [ZoneController], | ||
| providers: [ | ||
| { provide: ZoneService, useValue: service }, | ||
| ], | ||
| }) | ||
| .overrideGuard(UserGuard) | ||
| .useValue({ canActivate: () => true }) | ||
| .compile() | ||
|
|
||
| controller = module.get<ZoneController>(ZoneController) | ||
| }) | ||
|
|
||
| it('should be defined', () => { | ||
| expect(controller).toBeDefined() | ||
| }) | ||
|
|
||
| it('serves GET list without authentication, matching the legacy public route', async () => { | ||
| const app = module.createNestApplication<NestFastifyApplication>(new FastifyAdapter()) | ||
| await app.init() | ||
| service.list.mockResolvedValue([zone]) | ||
|
|
||
| const response = await app.inject({ | ||
| method: 'GET', | ||
| url: '/api/v1/zones', | ||
| }) | ||
|
|
||
| expect(response.statusCode).toEqual(200) | ||
| expect(response.json()).toEqual([toZone(zone)]) | ||
| await app.close() | ||
| }) | ||
|
|
||
| it('rejects non-UUID zone params with 400 before reaching the service', async () => { | ||
| const app = module.createNestApplication<NestFastifyApplication>(new FastifyAdapter()) | ||
| await app.init() | ||
|
|
||
| for (const method of ['PUT', 'DELETE'] as const) { | ||
| const response = await app.inject({ method, url: '/api/v1/zones/not-an-uuid' }) | ||
| expect(response.statusCode).toEqual(400) | ||
| } | ||
| await app.close() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import type { CreateZoneBody, UpdateZoneBody, Zone } from '@cpn-console/shared' | ||
| import type { FastifyRequest } from 'fastify' | ||
| import type { UserContext } from '../infrastructure/auth/auth-user.decorator' | ||
| import { zoneContract } from '@cpn-console/shared' | ||
| import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Inject, Param, ParseUUIDPipe, Post, Put, Req, UseGuards } from '@nestjs/common' | ||
| import { AuthUser } from '../infrastructure/auth/auth-user.decorator' | ||
| import { RequireAdminPermission } from '../infrastructure/permission/user/user-admin-permission.decorator' | ||
| import { RequireUserType } from '../infrastructure/permission/user/user-type.decorator' | ||
| import { UserGuard } from '../infrastructure/permission/user/user.guard' | ||
| import { ZodValidationPipe } from '../infrastructure/pipe/zod-validation.pipe' | ||
| import { ZoneService } from './zone.service' | ||
| import { toZone, toZones } from './zone.utils' | ||
|
|
||
| @Controller('api/v1/zones') | ||
| export class ZoneController { | ||
| constructor(@Inject(ZoneService) private readonly zoneService: ZoneService) {} | ||
|
|
||
| @Get() | ||
| async list(): Promise<Zone[]> { | ||
| return toZones(await this.zoneService.list()) | ||
| } | ||
|
|
||
| @Post() | ||
| @HttpCode(HttpStatus.CREATED) | ||
| @UseGuards(UserGuard) | ||
| @RequireUserType('human') | ||
| @RequireAdminPermission('ManageZones') | ||
| async create( | ||
| @Body(new ZodValidationPipe(zoneContract.createZone.body)) body: CreateZoneBody, | ||
| @AuthUser() user: UserContext, | ||
| @Req() request: FastifyRequest, | ||
| ): Promise<Zone> { | ||
| return toZone(await this.zoneService.create(body, user.userId, request.id)) | ||
| } | ||
|
|
||
| @Put(':zoneId') | ||
|
shikanime marked this conversation as resolved.
|
||
| @UseGuards(UserGuard) | ||
| @RequireUserType('human') | ||
| @RequireAdminPermission('ManageZones') | ||
| async update( | ||
| @Param('zoneId', ParseUUIDPipe) zoneId: string, | ||
| @Body(new ZodValidationPipe(zoneContract.updateZone.body)) body: UpdateZoneBody, | ||
| @AuthUser() user: UserContext, | ||
| @Req() request: FastifyRequest, | ||
| ): Promise<Zone> { | ||
| return toZone(await this.zoneService.update(zoneId, body, user.userId, request.id)) | ||
| } | ||
|
|
||
| @Delete(':zoneId') | ||
| @HttpCode(HttpStatus.NO_CONTENT) | ||
| @UseGuards(UserGuard) | ||
| @RequireUserType('human') | ||
| @RequireAdminPermission('ManageZones') | ||
| async delete( | ||
| @Param('zoneId', ParseUUIDPipe) zoneId: string, | ||
| @AuthUser() user: UserContext, | ||
| @Req() request: FastifyRequest, | ||
| ): Promise<void> { | ||
| return this.zoneService.delete(zoneId, user.userId, request.id) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { Module } from '@nestjs/common' | ||
| import { AppEventsModule } from '../events/app-events.module' | ||
| import { InfrastructureModule } from '../infrastructure/infrastructure.module' | ||
| import { ZoneController } from './zone.controller' | ||
| import { ZoneService } from './zone.service' | ||
|
|
||
| @Module({ | ||
| imports: [ | ||
| AppEventsModule, | ||
| InfrastructureModule, | ||
| ], | ||
| controllers: [ZoneController], | ||
| providers: [ZoneService], | ||
| exports: [ZoneService], | ||
| }) | ||
| export class ZoneModule {} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.