-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor(backend): Move poll service and controller * refactor(backend): Rename PollService to PollActionsService * refactor(backend): Add participant module * refactor(backend): Move participant endpoints to ParticipantController * refactor(backend): Add poll-event module * fix(backend): Poll security - isAdmin now checks createdBy for registered users. - Non-owners can no longer edit poll events * refactor(backend): Use @NotFound * style(backend): PollController indent * style(backend): poll-actions.service.ts imports * chore: Fix lint problems * test(backend): Fix broken tests * feat(backend): Show own polls via createdBy --------- Co-authored-by: Clashsoft <Clashsoft@users.noreply.github.com>
- Loading branch information
Showing
14 changed files
with
367 additions
and
231 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {CreateParticipantDto, Participant, ReadParticipantDto, UpdateParticipantDto} from '@apollusia/types'; | ||
import {AuthUser, UserToken} from '@mean-stream/nestx/auth'; | ||
import {ObjectIdPipe} from '@mean-stream/nestx/ref'; | ||
import {Body, Controller, Delete, Get, Headers, Param, Post, Put, UseGuards} from '@nestjs/common'; | ||
import {Types} from 'mongoose'; | ||
|
||
import {OptionalAuthGuard} from '../auth/optional-auth.guard'; | ||
import {PollActionsService} from '../poll/poll-actions.service'; | ||
|
||
@Controller('poll/:poll/participate') | ||
export class ParticipantController { | ||
constructor( | ||
private pollService: PollActionsService, | ||
) { | ||
} | ||
|
||
@Get() | ||
async findAll( | ||
@Param('poll', ObjectIdPipe) poll: Types.ObjectId, | ||
@Headers('Participant-Token') token: string, | ||
): Promise<ReadParticipantDto[]> { | ||
return this.pollService.getParticipants(poll, token); | ||
} | ||
|
||
@Post() | ||
@UseGuards(OptionalAuthGuard) | ||
async create( | ||
@Param('poll', ObjectIdPipe) poll: Types.ObjectId, | ||
@Body() dto: CreateParticipantDto, | ||
@AuthUser() user?: UserToken, | ||
): Promise<Participant> { | ||
return this.pollService.postParticipation(poll, dto, user); | ||
} | ||
|
||
@Put(':participant') | ||
async update( | ||
@Param('poll', ObjectIdPipe) poll: Types.ObjectId, | ||
@Param('participant', ObjectIdPipe) participant: Types.ObjectId, | ||
@Headers('Participant-Token') token: string, | ||
@Body() dto: UpdateParticipantDto, | ||
): Promise<ReadParticipantDto | null> { | ||
return this.pollService.editParticipation(poll, participant, token, dto); | ||
} | ||
|
||
@Delete(':participant') | ||
async delete( | ||
@Param('poll', ObjectIdPipe) poll: Types.ObjectId, | ||
@Param('participant', ObjectIdPipe) participant: Types.ObjectId, | ||
): Promise<ReadParticipantDto | null> { | ||
return this.pollService.deleteParticipation(poll, participant); | ||
} | ||
} |
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,25 @@ | ||
import {Participant, ParticipantSchema} from '@apollusia/types'; | ||
import {Module} from '@nestjs/common'; | ||
import {MongooseModule} from '@nestjs/mongoose'; | ||
|
||
import {ParticipantController} from './participant.controller'; | ||
import {ParticipantService} from './participant.service'; | ||
import {MailModule} from '../mail/mail.module'; | ||
import {PollModule} from '../poll/poll.module'; | ||
import {PushModule} from '../push/push.module'; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([ | ||
{name: Participant.name, schema: ParticipantSchema}, | ||
]), | ||
MailModule, | ||
PushModule, | ||
PollModule, | ||
], | ||
providers: [ParticipantService], | ||
exports: [ParticipantService], | ||
controllers: [ParticipantController], | ||
}) | ||
export class ParticipantModule { | ||
} |
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,14 @@ | ||
import {Participant} from '@apollusia/types'; | ||
import {MongooseRepository} from '@mean-stream/nestx/resource'; | ||
import {Injectable} from '@nestjs/common'; | ||
import {InjectModel} from '@nestjs/mongoose'; | ||
import {Model} from 'mongoose'; | ||
|
||
@Injectable() | ||
export class ParticipantService extends MongooseRepository<Participant> { | ||
constructor( | ||
@InjectModel(Participant.name) model: Model<Participant>, | ||
) { | ||
super(model); | ||
} | ||
} |
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,51 @@ | ||
import {PollEvent, PollEventDto, ReadPollEventDto} from '@apollusia/types'; | ||
import {AuthUser, UserToken} from '@mean-stream/nestx/auth'; | ||
import {notFound} from '@mean-stream/nestx/not-found'; | ||
import {ObjectIdPipe} from '@mean-stream/nestx/ref'; | ||
import { | ||
Body, | ||
Controller, | ||
ForbiddenException, | ||
Get, | ||
Headers, | ||
Param, | ||
ParseArrayPipe, | ||
Post, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import {Types} from 'mongoose'; | ||
|
||
import {OptionalAuthGuard} from '../auth/optional-auth.guard'; | ||
import {PollActionsService} from '../poll/poll-actions.service'; | ||
|
||
@Controller('poll/:poll/events') | ||
export class PollEventController { | ||
constructor( | ||
private pollService: PollActionsService, | ||
) { | ||
} | ||
|
||
@Get() | ||
async getEvents( | ||
@Param('poll', ObjectIdPipe) poll: Types.ObjectId, | ||
): Promise<ReadPollEventDto[]> { | ||
await this.pollService.find(poll) ?? notFound(poll); | ||
return this.pollService.getEvents(poll); | ||
} | ||
|
||
@Post() | ||
@UseGuards(OptionalAuthGuard) | ||
async postEvents( | ||
@Param('poll', ObjectIdPipe) poll: Types.ObjectId, | ||
@Body(new ParseArrayPipe({items: PollEventDto})) pollEvents: PollEventDto[], | ||
@Headers('Participant-Token') token?: string, | ||
@AuthUser() user?: UserToken, | ||
): Promise<PollEvent[]> { | ||
const pollDoc = await this.pollService.find(poll) ?? notFound(poll); | ||
if (!this.pollService.isAdmin(pollDoc, token, user?.sub)) { | ||
throw new ForbiddenException('You are not allowed to edit events for this poll'); | ||
} | ||
return this.pollService.postEvents(poll, pollEvents); | ||
} | ||
|
||
} |
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,25 @@ | ||
import {PollEvent, PollEventSchema} from '@apollusia/types'; | ||
import {Module} from '@nestjs/common'; | ||
import {MongooseModule} from '@nestjs/mongoose'; | ||
|
||
import {PollEventController} from './poll-event.controller'; | ||
import {PollEventService} from './poll-event.service'; | ||
import {MailModule} from '../mail/mail.module'; | ||
import {PollModule} from '../poll/poll.module'; | ||
import {PushModule} from '../push/push.module'; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([ | ||
{name: PollEvent.name, schema: PollEventSchema}, | ||
]), | ||
MailModule, | ||
PushModule, | ||
PollModule, | ||
], | ||
providers: [PollEventService], | ||
exports: [PollEventService], | ||
controllers: [PollEventController], | ||
}) | ||
export class PollEventModule { | ||
} |
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,14 @@ | ||
import {PollEvent} from '@apollusia/types'; | ||
import {MongooseRepository} from '@mean-stream/nestx/resource'; | ||
import {Injectable} from '@nestjs/common'; | ||
import {InjectModel} from '@nestjs/mongoose'; | ||
import {Model} from 'mongoose'; | ||
|
||
@Injectable() | ||
export class PollEventService extends MongooseRepository<PollEvent> { | ||
constructor( | ||
@InjectModel(PollEvent.name) model: Model<PollEvent>, | ||
) { | ||
super(model); | ||
} | ||
} |
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
Oops, something went wrong.