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

Commit

Permalink
♻️ Change approved location -> approved subnet
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 30, 2020
1 parent ed452de commit 3fb490f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/modules/approved-subnets/approved-subnets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ParseIntPipe,
Query,
} from '@nestjs/common';
import { approvedLocations } from '@prisma/client';
import { approvedSubnets } 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';
Expand All @@ -28,7 +28,7 @@ export class ApprovedSubnetController {
@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>[]> {
): Promise<Expose<approvedSubnets>[]> {
return this.approvedSubnetsService.getApprovedSubnets(userId, {
skip,
take,
Expand All @@ -43,7 +43,7 @@ export class ApprovedSubnetController {
async get(
@Param('userId', ParseIntPipe) userId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<approvedLocations>> {
): Promise<Expose<approvedSubnets>> {
return this.approvedSubnetsService.getApprovedSubnet(userId, Number(id));
}

Expand All @@ -52,7 +52,7 @@ export class ApprovedSubnetController {
async remove(
@Param('userId', ParseIntPipe) userId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<approvedLocations>> {
): Promise<Expose<approvedSubnets>> {
return this.approvedSubnetsService.deleteApprovedSubnet(userId, Number(id));
}
}
46 changes: 23 additions & 23 deletions src/modules/approved-subnets/approved-subnets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
UnauthorizedException,
} from '@nestjs/common';
import {
approvedLocations,
approvedLocationsOrderByInput,
approvedLocationsWhereInput,
approvedLocationsWhereUniqueInput,
approvedSubnets,
approvedSubnetsOrderByInput,
approvedSubnetsWhereInput,
approvedSubnetsWhereUniqueInput,
} from '@prisma/client';
import { Expose } from 'src/modules/prisma/prisma.interface';
import { PrismaService } from '../prisma/prisma.service';
Expand All @@ -22,64 +22,64 @@ export class ApprovedSubnetsService {
params: {
skip?: number;
take?: number;
cursor?: approvedLocationsWhereUniqueInput;
where?: approvedLocationsWhereInput;
orderBy?: approvedLocationsOrderByInput;
cursor?: approvedSubnetsWhereUniqueInput;
where?: approvedSubnetsWhereInput;
orderBy?: approvedSubnetsOrderByInput;
},
): Promise<Expose<approvedLocations>[]> {
): Promise<Expose<approvedSubnets>[]> {
const { skip, take, cursor, where, orderBy } = params;
const approvedLocations = await this.prisma.approvedLocations.findMany({
const approvedSubnets = await this.prisma.approvedSubnets.findMany({
skip,
take,
cursor,
where: { ...where, user: { id: userId } },
orderBy,
});
return approvedLocations.map(user =>
this.prisma.expose<approvedLocations>(user),
return approvedSubnets.map(user =>
this.prisma.expose<approvedSubnets>(user),
);
}

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

async deleteApprovedSubnet(
userId: number,
id: number,
): Promise<Expose<approvedLocations>> {
const testApprovedSubnet = await this.prisma.approvedLocations.findOne({
): Promise<Expose<approvedSubnets>> {
const testApprovedSubnet = await this.prisma.approvedSubnets.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({
const approvedSubnet = await this.prisma.approvedSubnets.delete({
where: { id },
});
return this.prisma.expose<approvedLocations>(approvedLocation);
return this.prisma.expose<approvedSubnets>(approvedSubnet);
}

async approveNewSubnet(userId: number, ipAddress: string) {
const subnet = anonymize(ipAddress);
const approved = await this.prisma.approvedLocations.create({
const approved = await this.prisma.approvedSubnets.create({
data: {
user: { connect: { id: userId } },
subnet,
createdAt: new Date(),
},
});
return this.prisma.expose<approvedLocations>(approved);
return this.prisma.expose<approvedSubnets>(approved);
}
}
2 changes: 1 addition & 1 deletion src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export class AuthService {
): Promise<void> {
if (!checkLocationOnLogin) return;
const subnet = anonymize(ipAddress);
const isApproved = await this.prisma.approvedLocations.findFirst({
const isApproved = await this.prisma.approvedSubnets.findFirst({
where: { user: { id }, subnet },
});
if (!isApproved) {
Expand Down

0 comments on commit 3fb490f

Please sign in to comment.