-
Notifications
You must be signed in to change notification settings - Fork 0
Feat: Daymail routes #92
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
Open
TeddyRoncin
wants to merge
7
commits into
dev
Choose a base branch
from
feat/daymail
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7ff1fea
New route POST /assos/:assoId/daymail and decorator HasSomeAmong is n…
TeddyRoncin 7d76f37
Added routes GET, PATCH and DELETE for daymails, and tests for GET route
TeddyRoncin 3f2a56f
Tests for PATCH and DELETE route + added another check to the GET rou…
TeddyRoncin 3985716
Review, daymails are now weeklies
TeddyRoncin 3b64627
Renamed daymail to weekly
TeddyRoncin b394c9d
Put weeklies into their own controllers / services
TeddyRoncin abf5327
Review part 2 (I forgot to finish it ^^')
TeddyRoncin 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
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
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
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,28 @@ | ||
| import { Prisma, PrismaClient } from '@prisma/client'; | ||
| import { generateCustomModel } from '../../prisma/prisma.service'; | ||
| import { pick, translationSelect } from '../../utils'; | ||
| import { Translation } from '../../prisma/types'; | ||
|
|
||
| const ASSO_WEEKLY_SELECT_FILTER = { | ||
| select: { | ||
| id: true, | ||
| assoId: true, | ||
| createdAt: true, | ||
| titleTranslation: translationSelect, | ||
| bodyTranslation: translationSelect, | ||
| date: true, | ||
| }, | ||
| orderBy: { date: 'asc'} | ||
| } as const satisfies Prisma.AssoWeeklyFindManyArgs; | ||
|
|
||
| export type UnformattedAssoWeekly = Prisma.AssoWeeklyGetPayload<typeof ASSO_WEEKLY_SELECT_FILTER>; | ||
| export type AssoWeekly = Pick<UnformattedAssoWeekly, 'id' | 'assoId' | 'createdAt' | 'date'> & { title: Translation, message: Translation } | ||
|
|
||
| export const generateCustomAssoWeeklyModel = (prisma: PrismaClient) => | ||
| generateCustomModel(prisma, 'assoWeekly', ASSO_WEEKLY_SELECT_FILTER, formatAssoWeekly); | ||
|
|
||
| function formatAssoWeekly(_: PrismaClient, r: UnformattedAssoWeekly): AssoWeekly { | ||
| return { | ||
| ...pick(r, 'id', 'assoId', 'createdAt', 'date'), title: r.titleTranslation, message: r.bodyTranslation | ||
| } | ||
| } |
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,22 @@ | ||
| import { Type } from 'class-transformer'; | ||
| import { IsDate, IsNotEmpty, ValidateNested } from 'class-validator'; | ||
| import { TranslationReqDto } from '../../../../app.dto'; | ||
| import { IsWeekDate } from '../../../../validation'; | ||
|
|
||
| export default class WeeklyReqDto { | ||
| @ValidateNested() | ||
| @IsNotEmpty() | ||
| @Type(() => TranslationReqDto) | ||
| title: TranslationReqDto | ||
|
|
||
| @ValidateNested() | ||
| @IsNotEmpty() | ||
| @Type(() => TranslationReqDto) | ||
| message: TranslationReqDto; | ||
|
|
||
| @IsDate() | ||
| @IsNotEmpty() | ||
| @Type(() => Date) | ||
| @IsWeekDate() | ||
| date: Date; | ||
| } | ||
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,17 @@ | ||
| import { IsDate, IsInt, IsOptional } from 'class-validator'; | ||
| import { Type } from 'class-transformer'; | ||
|
|
||
| export default class WeeklySearchReqDto { | ||
| @IsDate() | ||
| @Type(() => Date) | ||
| from?: Date; | ||
|
|
||
| @IsOptional() | ||
| @IsDate() | ||
| @Type(() => Date) | ||
| to?: Date; | ||
|
|
||
| @IsOptional() | ||
| @IsInt() | ||
| page: number = 1; | ||
| } |
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,4 @@ | ||
| export default class WeeklyInfoResDto { | ||
| sendDay: number; | ||
| sendHour: number; | ||
| } |
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,10 @@ | ||
| import { Translation } from '../../../../prisma/types'; | ||
|
|
||
| export default class WeeklyResDto { | ||
| id: string; | ||
| assoId: string; | ||
| createdAt: Date; | ||
| title: Translation; | ||
| message: Translation; | ||
| date: Date; | ||
| } |
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,127 @@ | ||
| import { Body, Controller, Delete, Get, Patch, Post, Query } from '@nestjs/common'; | ||
| import { ApiCreatedResponse, ApiOkResponse, ApiOperation, ApiQuery } from '@nestjs/swagger'; | ||
| import WeeklyResDto from './dto/res/weekly-res.dto'; | ||
| import { ApiAppErrorResponse, paginatedResponseDto } from '../../app.dto'; | ||
| import { AppException, ERROR_CODE } from '../../exceptions'; | ||
| import { ParamAsso } from '../decorator/get-asso'; | ||
| import { Asso } from '../interfaces/asso.interface'; | ||
| import AssoGetWeeklyReqDto from './dto/req/weekly-search-req.dto'; | ||
| import { GetUser } from '../../auth/decorator'; | ||
| import { User } from '../../users/interfaces/user.interface'; | ||
| import AssosPostWeeklyReqDto from './dto/req/weekly-req.dto'; | ||
| import { UUIDParam } from '../../app.pipe'; | ||
| import { AssoWeekly } from '../interfaces/weekly.interface'; | ||
| import { ConfigModule } from '../../config/config.module'; | ||
| import WeeklyService from './weekly.service'; | ||
| import { AssosService } from '../assos.service'; | ||
|
|
||
| @Controller('assos/:assoId/weekly') | ||
| export class WeeklyWithAssoIdController { | ||
| constructor(readonly weeklyService: WeeklyService, readonly assosService: AssosService, readonly config: ConfigModule) {} | ||
|
|
||
| @Get() | ||
| @ApiOperation({ description: 'Get weeklies from query parameter `from` to query parameter `to`.' }) | ||
| @ApiQuery({ name: 'from', type: String }) | ||
| @ApiQuery({ name: 'to', type: String, default: `\`from\` + env.PAGINATION_PAGE_SIZE days`, required: false }) | ||
| @ApiOkResponse({ type: WeeklyResDto }) | ||
| @ApiAppErrorResponse(ERROR_CODE.PARAM_DATE_MUST_BE_AFTER, '`to` must come after `from` (or be equal)') | ||
| @ApiAppErrorResponse( | ||
| ERROR_CODE.FORBIDDEN_ASSOS_PERMISSIONS, | ||
| 'The user issuing the request does not have the permission weekly', | ||
| ) | ||
| async searchWeeklies( | ||
| @ParamAsso() asso: Asso, | ||
| @Query() { from, to, page }: AssoGetWeeklyReqDto, | ||
| @GetUser() user: User, | ||
| ): Promise<Pagination<WeeklyResDto>> { | ||
| from = from.dropTime(); | ||
| to = to?.dropTime(); | ||
| if (to && from > to) | ||
| throw new AppException(ERROR_CODE.PARAM_DATE_MUST_BE_AFTER, to.toISOString(), from.toISOString()); | ||
| if (!(await this.assosService.hasSomeAssoPermission(asso, user.id, 'weekly'))) | ||
| throw new AppException(ERROR_CODE.FORBIDDEN_ASSOS_PERMISSIONS, asso.id, 'weekly'); | ||
| const { weeklies, count } = await this.weeklyService.searchWeeklies(asso.id, from, to, page); | ||
| return { | ||
| items: weeklies, | ||
| itemCount: count, | ||
| itemsPerPage: this.config.PAGINATION_PAGE_SIZE, | ||
| }; | ||
| } | ||
|
|
||
| @Post() | ||
| @ApiOperation({ description: 'Create a weekly for the given association.' }) | ||
| @ApiCreatedResponse({ type: WeeklyResDto }) | ||
| @ApiAppErrorResponse( | ||
| ERROR_CODE.FORBIDDEN_ASSOS_PERMISSIONS, | ||
| 'The user issuing the request does not have the permission weekly', | ||
| ) | ||
| @ApiAppErrorResponse(ERROR_CODE.WEEKLY_ALREADY_SENT_FOR_WEEK, 'The weekly was already sent for the specified week') | ||
| @ApiAppErrorResponse(ERROR_CODE.WEEKLY_ALREADY_PLANNED_FOR_WEEK, 'The asso already has a weekly planned for the requested week') | ||
| async createWeekly( | ||
| @ParamAsso() asso: Asso, | ||
| @Body() dto: AssosPostWeeklyReqDto, | ||
| @GetUser() user: User, | ||
| ): Promise<WeeklyResDto> { | ||
| if (!(await this.assosService.hasSomeAssoPermission(asso, user.id, 'weekly'))) | ||
| throw new AppException(ERROR_CODE.FORBIDDEN_ASSOS_PERMISSIONS, asso.id, 'weekly'); | ||
| if (this.weeklyService.getWeeklySendDate(dto.date) < new Date()) | ||
| throw new AppException(ERROR_CODE.WEEKLY_ALREADY_SENT_FOR_WEEK, dto.date.toISOString()); | ||
| if (await this.weeklyService.hasWeekly(asso.id, dto.date)) | ||
| throw new AppException(ERROR_CODE.WEEKLY_ALREADY_PLANNED_FOR_WEEK); | ||
| return this.weeklyService.addWeekly(asso.id, dto.title, dto.message, dto.date); | ||
| } | ||
|
|
||
| @Patch('/:weeklyId') | ||
| @ApiOperation({ description: 'Update a weekly for the given association.' }) | ||
| @ApiOkResponse({ type: paginatedResponseDto(WeeklyResDto) }) | ||
| @ApiAppErrorResponse(ERROR_CODE.NO_SUCH_ASSO) | ||
| @ApiAppErrorResponse( | ||
| ERROR_CODE.FORBIDDEN_ASSOS_PERMISSIONS, | ||
| 'The user issuing the request does not have the permission weekly', | ||
| ) | ||
| @ApiAppErrorResponse(ERROR_CODE.NO_SUCH_WEEKLY, 'The weekly does not exist for the specified asso') | ||
| @ApiAppErrorResponse(ERROR_CODE.WEEKLY_ALREADY_SENT, 'The weekly that is beeing modified was already sent') | ||
| @ApiAppErrorResponse(ERROR_CODE.WEEKLY_ALREADY_SENT_FOR_WEEK, 'The weekly was already sent for the specified week') | ||
| @ApiAppErrorResponse(ERROR_CODE.WEEKLY_ALREADY_PLANNED_FOR_WEEK, 'The asso already has a weekly planned for the requested week') | ||
| async updateWeekly( | ||
| @ParamAsso() asso: Asso, | ||
| @UUIDParam('weeklyId') weeklyId: string, | ||
| @Body() dto: AssosPostWeeklyReqDto, | ||
| @GetUser() user: User, | ||
| ): Promise<WeeklyResDto> { | ||
| if (!(await this.assosService.hasSomeAssoPermission(asso, user.id, 'weekly'))) | ||
| throw new AppException(ERROR_CODE.FORBIDDEN_ASSOS_PERMISSIONS, asso.id, 'weekly'); | ||
| const weekly: AssoWeekly = await this.weeklyService.getWeekly(weeklyId, asso.id); | ||
| if (!weekly) throw new AppException(ERROR_CODE.NO_SUCH_WEEKLY, weeklyId); | ||
| if (this.weeklyService.getWeeklySendDate(weekly.date) < new Date()) | ||
| throw new AppException(ERROR_CODE.WEEKLY_ALREADY_SENT); | ||
| if (this.weeklyService.getWeeklySendDate(dto.date) < new Date()) | ||
| throw new AppException(ERROR_CODE.WEEKLY_ALREADY_SENT_FOR_WEEK, dto.date.toISOString()); | ||
| if (await this.weeklyService.hasWeekly(asso.id, weekly.date, weekly.id)) | ||
| throw new AppException(ERROR_CODE.WEEKLY_ALREADY_PLANNED_FOR_WEEK); | ||
| return this.weeklyService.updateWeekly(weeklyId, dto); | ||
| } | ||
|
|
||
| @Delete('/:weeklyId') | ||
| @ApiOperation({ description: 'Delete a weekly for the given association.' }) | ||
| @ApiOkResponse({ type: WeeklyResDto }) | ||
| @ApiAppErrorResponse( | ||
| ERROR_CODE.FORBIDDEN_ASSOS_PERMISSIONS, | ||
| 'The user issuing the request does not have the permission weekly', | ||
| ) | ||
| @ApiAppErrorResponse(ERROR_CODE.NO_SUCH_WEEKLY) | ||
| @ApiAppErrorResponse(ERROR_CODE.WEEKLY_ALREADY_SENT, 'The weekly that is beeing modified was already sent') | ||
| async deleteWeekly( | ||
| @ParamAsso() asso: Asso, | ||
| @UUIDParam('weeklyId') weeklyId: string, | ||
| @GetUser() user: User, | ||
| ): Promise<WeeklyResDto> { | ||
| if (!(await this.assosService.hasSomeAssoPermission(asso, user.id, 'weekly'))) | ||
| throw new AppException(ERROR_CODE.FORBIDDEN_ASSOS_PERMISSIONS, asso.id, 'weekly'); | ||
| const weekly: AssoWeekly = await this.weeklyService.getWeekly(weeklyId, asso.id); | ||
| if (!weekly) throw new AppException(ERROR_CODE.NO_SUCH_WEEKLY, weeklyId); | ||
| if (this.weeklyService.getWeeklySendDate(weekly.date) < new Date()) | ||
| throw new AppException(ERROR_CODE.WEEKLY_ALREADY_SENT); | ||
| return this.weeklyService.deleteWeekly(weeklyId); | ||
| } | ||
| } |
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,20 @@ | ||
| import { Controller, Get } from '@nestjs/common'; | ||
| import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger'; | ||
| import WeeklyInfoResDto from './dto/res/weekly-info-res.dto'; | ||
| import { ConfigModule } from '../../config/config.module'; | ||
|
|
||
| @Controller('assos/weekly') | ||
| @ApiTags('Weekly') | ||
| export default class WeeklyWithoutAssoIdController { | ||
| constructor(readonly config: ConfigModule) {} | ||
|
|
||
| @Get('/info') | ||
| @ApiOperation({ description: 'Returns information about weeklies.' }) | ||
| @ApiOkResponse({ type: WeeklyInfoResDto }) | ||
| getWeeklyInfo(): WeeklyInfoResDto { | ||
| return { | ||
| sendDay: this.config.WEEKLY_SEND_DAY, | ||
| sendHour: this.config.WEEKLY_SEND_HOUR, | ||
| }; | ||
| } | ||
| } |
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.