-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add signalement module * feat: add route to fetch signalement author * review
- Loading branch information
Showing
45 changed files
with
1,699 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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
18 changes: 18 additions & 0 deletions
18
apps/api/src/modules/signalement/dto/update-signalement-dto.ts
This file contains 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,18 @@ | ||
import { Signalement } from '@/shared/openapi-signalement'; | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { ArrayMinSize, IsEnum } from 'class-validator'; | ||
|
||
export class UpdateSignalementDTO { | ||
@ApiProperty({ | ||
type: String, | ||
required: true, | ||
nullable: false, | ||
isArray: true, | ||
}) | ||
@ArrayMinSize(1) | ||
ids: string[]; | ||
|
||
@ApiProperty({ required: true, nullable: false, enum: Signalement.status }) | ||
@IsEnum(Signalement.status) | ||
status: Signalement.status; | ||
} |
32 changes: 32 additions & 0 deletions
32
apps/api/src/modules/signalement/openAPI-signalement.service.ts
This file contains 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,32 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
import { | ||
SignalementsService, | ||
OpenAPI as OpenAPISignalement, | ||
UpdateSignalementDTO, | ||
} from '@/shared/openapi-signalement'; | ||
|
||
@Injectable() | ||
export class OpenAPISignalementService { | ||
constructor(private configService: ConfigService) { | ||
OpenAPISignalement.BASE = this.configService.get('API_SIGNALEMENT_URL'); | ||
OpenAPISignalement.TOKEN = this.configService.get( | ||
'API_SIGNALEMENT_CLIENT_SECRET', | ||
); | ||
} | ||
|
||
getSignalementById(signalementId: string) { | ||
return SignalementsService.getSignalementById(signalementId); | ||
} | ||
|
||
async updateSignalement( | ||
signalementId: string, | ||
updateSignalementDTO: UpdateSignalementDTO, | ||
) { | ||
return SignalementsService.updateSignalement( | ||
signalementId, | ||
updateSignalementDTO, | ||
); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
apps/api/src/modules/signalement/signalement.controller.ts
This file contains 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,84 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Get, | ||
HttpStatus, | ||
Inject, | ||
Param, | ||
Put, | ||
Req, | ||
Res, | ||
UseGuards, | ||
forwardRef, | ||
} from '@nestjs/common'; | ||
import { | ||
ApiBearerAuth, | ||
ApiBody, | ||
ApiOperation, | ||
ApiParam, | ||
ApiResponse, | ||
ApiTags, | ||
} from '@nestjs/swagger'; | ||
import { Response } from 'express'; | ||
|
||
import { AdminGuard } from '@/lib/guards/admin.guard'; | ||
import { CustomRequest } from '@/lib/types/request.type'; | ||
import { SignalementService } from './signalement.service'; | ||
import { UpdateSignalementDTO } from './dto/update-signalement-dto'; | ||
|
||
@ApiTags('signalements') | ||
@Controller('signalements') | ||
export class SignalementController { | ||
constructor( | ||
@Inject(forwardRef(() => SignalementService)) | ||
private signalementService: SignalementService, | ||
) {} | ||
|
||
@Put(':baseLocaleId') | ||
@ApiOperation({ | ||
summary: 'Update signalements', | ||
operationId: 'updateSignalements', | ||
}) | ||
@ApiParam({ name: 'baseLocaleId', required: true, type: String }) | ||
@ApiBody({ type: UpdateSignalementDTO, required: true }) | ||
@ApiResponse({ | ||
status: HttpStatus.OK, | ||
type: Boolean, | ||
}) | ||
@ApiBearerAuth('admin-token') | ||
@UseGuards(AdminGuard) | ||
async update( | ||
@Req() req: CustomRequest, | ||
@Body() updateSignalementDTO: UpdateSignalementDTO, | ||
@Res() res: Response, | ||
) { | ||
await this.signalementService.updateMany( | ||
req.baseLocale, | ||
updateSignalementDTO, | ||
); | ||
res.status(HttpStatus.OK).json(true); | ||
} | ||
|
||
@Get('/:baseLocaleId/:idSignalement/author') | ||
@ApiOperation({ | ||
summary: 'Get author by signalement id', | ||
operationId: 'getAuthor', | ||
}) | ||
@ApiParam({ name: 'baseLocaleId', required: true, type: String }) | ||
@ApiParam({ name: 'idSignalement', required: true, type: String }) | ||
@ApiBearerAuth('admin-token') | ||
@UseGuards(AdminGuard) | ||
@ApiResponse({ | ||
status: HttpStatus.OK, | ||
}) | ||
async getAuthor( | ||
@Req() req: Request, | ||
@Res() res: Response, | ||
@Param('idSignalement') idSignalement: string, | ||
) { | ||
const signalement = | ||
await this.signalementService.findOneOrFail(idSignalement); | ||
|
||
res.status(HttpStatus.OK).json(signalement.author); | ||
} | ||
} |
This file contains 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,20 @@ | ||
import { Module, MiddlewareConsumer, forwardRef } from '@nestjs/common'; | ||
|
||
import { BaseLocaleMiddleware } from '@/modules/base_locale/base_locale.middleware'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import { BaseLocaleModule } from '../base_locale/base_locale.module'; | ||
import { SignalementService } from './signalement.service'; | ||
import { SignalementController } from './signalement.controller'; | ||
import { OpenAPISignalementService } from './openAPI-signalement.service'; | ||
|
||
@Module({ | ||
imports: [ConfigModule, forwardRef(() => BaseLocaleModule)], | ||
providers: [SignalementService, OpenAPISignalementService], | ||
controllers: [SignalementController], | ||
exports: [SignalementService], | ||
}) | ||
export class SignalementModule { | ||
configure(consumer: MiddlewareConsumer) { | ||
consumer.apply(BaseLocaleMiddleware).forRoutes(SignalementController); | ||
} | ||
} |
This file contains 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,57 @@ | ||
import { | ||
BaseLocale, | ||
StatusBaseLocalEnum, | ||
} from '@/shared/entities/base_locale.entity'; | ||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; | ||
import { UpdateSignalementDTO } from './dto/update-signalement-dto'; | ||
import { OpenAPISignalementService } from './openAPI-signalement.service'; | ||
|
||
@Injectable() | ||
export class SignalementService { | ||
constructor(private openAPISignalementService: OpenAPISignalementService) {} | ||
|
||
async findOneOrFail(signalementId: string) { | ||
const fetchedSignalement = | ||
await this.openAPISignalementService.getSignalementById(signalementId); | ||
|
||
if (!fetchedSignalement) { | ||
throw new HttpException( | ||
`Signalement ${signalementId} not found`, | ||
HttpStatus.NOT_FOUND, | ||
); | ||
} | ||
|
||
return fetchedSignalement; | ||
} | ||
|
||
async updateMany( | ||
baseLocale: BaseLocale, | ||
updateSignalementDTO: UpdateSignalementDTO, | ||
) { | ||
const { ids, status } = updateSignalementDTO; | ||
|
||
if (baseLocale.status !== StatusBaseLocalEnum.PUBLISHED) { | ||
throw new HttpException( | ||
'BaseLocale is not published', | ||
HttpStatus.PRECONDITION_FAILED, | ||
); | ||
} | ||
|
||
for (const signalementId of ids) { | ||
const fetchedSignalement = await this.findOneOrFail(signalementId); | ||
|
||
if (baseLocale.commune !== fetchedSignalement.codeCommune) { | ||
throw new HttpException( | ||
`Communes do not match for signalement ${signalementId}`, | ||
HttpStatus.PRECONDITION_FAILED, | ||
); | ||
} | ||
|
||
await this.openAPISignalementService.updateSignalement(signalementId, { | ||
status, | ||
}); | ||
} | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.