90 lines
2.6 KiB
TypeScript
90 lines
2.6 KiB
TypeScript
import {
|
|||
|
|
Body,
|
||
|
|
Controller,
|
||
|
|
Get,
|
||
|
|
Inject,
|
||
|
|
Param,
|
||
|
|
Post,
|
||
|
|
Put,
|
||
|
|
Query,
|
||
|
|
Req,
|
||
|
|
ValidationPipe,
|
||
|
|
} from '@nestjs/common';
|
||
|
|
|
||
|
|
import type {
|
||
|
|
ActivityRegistrationResponse,
|
||
|
|
PaginatedActivityRegistrationsResponse,
|
||
|
|
VolunteerProfileResponse,
|
||
|
|
} from '@lehuo/contracts';
|
||
|
|
|
||
|
|
import { RequireSessionChannel } from '../auth/decorators/require-session-channel.decorator';
|
||
|
|
import { CurrentAuth } from '../common/decorators/current-auth.decorator';
|
||
|
|
import type { AuthContext, RequestContext } from '../common/request-context';
|
||
|
|
import { SessionChannel } from '../generated/prisma/enums';
|
||
|
|
import { ActivitiesService } from './activities.service';
|
||
|
|
import {
|
||
|
|
ApplyVolunteerDto,
|
||
|
|
RegisterActivityDto,
|
||
|
|
RegistrationQueryDto,
|
||
|
|
} from './dto/participation.dto';
|
||
|
|
import { VolunteersService } from './volunteers.service';
|
||
|
|
|
||
|
|
const strict = (expectedType: new () => object) =>
|
||
|
|
new ValidationPipe({
|
||
|
|
transform: true,
|
||
|
|
whitelist: true,
|
||
|
|
forbidNonWhitelisted: true,
|
||
|
|
expectedType,
|
||
|
|
});
|
||
|
|
|
||
|
|
@Controller('api/v1')
|
||
|
|
@RequireSessionChannel(SessionChannel.WECHAT)
|
||
|
|
export class MeParticipationController {
|
||
|
|
constructor(
|
||
|
|
@Inject(VolunteersService) private readonly volunteers: VolunteersService,
|
||
|
|
@Inject(ActivitiesService) private readonly activities: ActivitiesService,
|
||
|
|
) {}
|
||
|
|
|
||
|
|
@Get('me/volunteer')
|
||
|
|
mine(@CurrentAuth() actor: AuthContext): Promise<VolunteerProfileResponse> {
|
||
|
|
return this.volunteers.mine(actor);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Put('me/volunteer')
|
||
|
|
apply(
|
||
|
|
@Body(strict(ApplyVolunteerDto)) input: ApplyVolunteerDto,
|
||
|
|
@CurrentAuth() actor: AuthContext,
|
||
|
|
@Req() request: RequestContext,
|
||
|
|
): Promise<VolunteerProfileResponse> {
|
||
|
|
return this.volunteers.apply(input, actor, request);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Get('me/activity-registrations')
|
||
|
|
registrations(
|
||
|
|
@Query(strict(RegistrationQueryDto)) query: RegistrationQueryDto,
|
||
|
|
@CurrentAuth() actor: AuthContext,
|
||
|
|
): Promise<PaginatedActivityRegistrationsResponse> {
|
||
|
|
return this.activities.myRegistrations(query, actor);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Post('activities/:publicId/registrations')
|
||
|
|
register(
|
||
|
|
@Param('publicId') publicId: string,
|
||
|
|
@Body(strict(RegisterActivityDto)) input: RegisterActivityDto,
|
||
|
|
@CurrentAuth() actor: AuthContext,
|
||
|
|
@Req() request: RequestContext,
|
||
|
|
): Promise<ActivityRegistrationResponse> {
|
||
|
|
return this.activities.register(publicId, input, actor, request);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Post('me/activity-registrations/:publicId/cancel')
|
||
|
|
cancel(
|
||
|
|
@Param('publicId') publicId: string,
|
||
|
|
@Body(strict(RegisterActivityDto)) input: RegisterActivityDto,
|
||
|
|
@CurrentAuth() actor: AuthContext,
|
||
|
|
@Req() request: RequestContext,
|
||
|
|
): Promise<ActivityRegistrationResponse> {
|
||
|
|
return this.activities.cancel(publicId, input, actor, request);
|
||
|
|
}
|
||
|
|
}
|