This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
339a29d
commit e4e78e1
Showing
4 changed files
with
145 additions
and
0 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
58 changes: 58 additions & 0 deletions
58
src/modules/approved-subnets/approved-subnets.controller.ts
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,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)); | ||
} | ||
} |
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,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 {} |
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,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); | ||
} | ||
} |