Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Add approved subnets endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 30, 2020
1 parent 339a29d commit e4e78e1
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { ScheduleModule } from '@nestjs/schedule';
import { RateLimiterInterceptor, RateLimiterModule } from 'nestjs-rate-limiter';
import configuration from './config/configuration';
import { AccessTokensModule } from './modules/access-tokens/access-tokens.module';
import { ApprovedSubnetsModule } from './modules/approved-subnets/approved-subnets.module';
import { AuthModule } from './modules/auth/auth.module';
import { JwtAuthGuard } from './modules/auth/jwt-auth.guard';
import { ScopesGuard } from './modules/auth/scope.guard';
import { EmailModule } from './modules/email/email.module';
import { EmailsModule } from './modules/emails/emails.module';
import { GroupsModule } from './modules/groups/groups.module';
import { MultiFactorAuthenticationModule } from './modules/multi-factor-authentication/multi-factor-authentication.module';
import { PrismaModule } from './modules/prisma/prisma.module';
import { SessionsModule } from './modules/sessions/sessions.module';
import { TasksModule } from './modules/tasks/tasks.module';
Expand All @@ -35,6 +37,8 @@ import { UsersModule } from './modules/users/users.module';
AccessTokensModule,
EmailsModule,
GroupsModule,
MultiFactorAuthenticationModule,
ApprovedSubnetsModule,
],
providers: [
{
Expand Down
58 changes: 58 additions & 0 deletions src/modules/approved-subnets/approved-subnets.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {
Controller,
Delete,
Get,
Param,
ParseIntPipe,
Query,
} from '@nestjs/common';
import { approvedLocations } from '@prisma/client';
import { Expose } from 'src/modules/prisma/prisma.interface';
import { CursorPipe } from 'src/pipes/cursor.pipe';
import { OptionalIntPipe } from 'src/pipes/optional-int.pipe';
import { OrderByPipe } from 'src/pipes/order-by.pipe';
import { WherePipe } from 'src/pipes/where.pipe';
import { Scopes } from '../auth/scope.decorator';
import { ApprovedSubnetsService } from './approved-subnets.service';

@Controller('users/:userId/approved-subnets')
export class ApprovedSubnetController {
constructor(private approvedSubnetsService: ApprovedSubnetsService) {}

@Get()
@Scopes('user-{userId}:read-approved-location')
async getAll(
@Param('userId', ParseIntPipe) userId: number,
@Query('skip', OptionalIntPipe) skip?: number,
@Query('take', OptionalIntPipe) take?: number,
@Query('cursor', CursorPipe) cursor?: Record<string, number | string>,
@Query('where', WherePipe) where?: Record<string, number | string>,
@Query('orderBy', OrderByPipe) orderBy?: Record<string, 'asc' | 'desc'>,
): Promise<Expose<approvedLocations>[]> {
return this.approvedSubnetsService.getApprovedSubnets(userId, {
skip,
take,
orderBy,
cursor,
where,
});
}

@Get(':id')
@Scopes('user-{userId}:read-approved-location-{id}')
async get(
@Param('userId', ParseIntPipe) userId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<approvedLocations>> {
return this.approvedSubnetsService.getApprovedSubnet(userId, Number(id));
}

@Delete(':id')
@Scopes('user-{userId}:delete-approved-location-{id}')
async remove(
@Param('userId', ParseIntPipe) userId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<approvedLocations>> {
return this.approvedSubnetsService.deleteApprovedSubnet(userId, Number(id));
}
}
11 changes: 11 additions & 0 deletions src/modules/approved-subnets/approved-subnets.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { PrismaModule } from '../prisma/prisma.module';
import { ApprovedSubnetController } from './approved-subnets.controller';
import { ApprovedSubnetsService } from './approved-subnets.service';

@Module({
imports: [PrismaModule],
controllers: [ApprovedSubnetController],
providers: [ApprovedSubnetsService],
})
export class ApprovedSubnetsModule {}
72 changes: 72 additions & 0 deletions src/modules/approved-subnets/approved-subnets.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {
HttpException,
HttpStatus,
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import {
approvedLocations,
approvedLocationsOrderByInput,
approvedLocationsWhereInput,
approvedLocationsWhereUniqueInput,
} from '@prisma/client';
import { Expose } from 'src/modules/prisma/prisma.interface';
import { PrismaService } from '../prisma/prisma.service';

@Injectable()
export class ApprovedSubnetsService {
constructor(private prisma: PrismaService) {}
async getApprovedSubnets(
userId: number,
params: {
skip?: number;
take?: number;
cursor?: approvedLocationsWhereUniqueInput;
where?: approvedLocationsWhereInput;
orderBy?: approvedLocationsOrderByInput;
},
): Promise<Expose<approvedLocations>[]> {
const { skip, take, cursor, where, orderBy } = params;
const approvedLocations = await this.prisma.approvedLocations.findMany({
skip,
take,
cursor,
where: { ...where, user: { id: userId } },
orderBy,
});
return approvedLocations.map(user =>
this.prisma.expose<approvedLocations>(user),
);
}

async getApprovedSubnet(
userId: number,
id: number,
): Promise<Expose<approvedLocations> | null> {
const approvedLocation = await this.prisma.approvedLocations.findOne({
where: { id },
});
if (!approvedLocation)
throw new HttpException('ApprovedSubnet not found', HttpStatus.NOT_FOUND);
if (approvedLocation.userId !== userId) throw new UnauthorizedException();
if (!approvedLocation)
throw new HttpException('ApprovedSubnet not found', HttpStatus.NOT_FOUND);
return this.prisma.expose<approvedLocations>(approvedLocation);
}

async deleteApprovedSubnet(
userId: number,
id: number,
): Promise<Expose<approvedLocations>> {
const testApprovedSubnet = await this.prisma.approvedLocations.findOne({
where: { id },
});
if (!testApprovedSubnet)
throw new HttpException('ApprovedSubnet not found', HttpStatus.NOT_FOUND);
if (testApprovedSubnet.userId !== userId) throw new UnauthorizedException();
const approvedLocation = await this.prisma.approvedLocations.delete({
where: { id },
});
return this.prisma.expose<approvedLocations>(approvedLocation);
}
}

0 comments on commit e4e78e1

Please sign in to comment.