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

Commit

Permalink
✨ Add API key users controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 8, 2020
1 parent 89af802 commit bf9976d
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import {
Query,
} from '@nestjs/common';
import { apiKeys } from '@prisma/client';
import { Expose } from '../../modules/prisma/prisma.interface';
import { CursorPipe } from '../../pipes/cursor.pipe';
import { OptionalIntPipe } from '../../pipes/optional-int.pipe';
import { OrderByPipe } from '../../pipes/order-by.pipe';
import { WherePipe } from '../../pipes/where.pipe';
import { AuditLog } from '../audit-logs/audit-log.decorator';
import { Scopes } from '../auth/scope.decorator';
import { Expose } from '../prisma/prisma.interface';
import {
CreateApiKeyDto,
ReplaceApiKeyDto,
Expand All @@ -26,7 +26,7 @@ import {
import { ApiKeysService } from './api-keys.service';

@Controller('groups/:groupId/api-keys')
export class ApiKeyController {
export class ApiKeyGroupController {
constructor(private apiKeysService: ApiKeysService) {}

@Post()
Expand Down
109 changes: 109 additions & 0 deletions src/modules/api-keys/api-keys-user.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import {
Body,
Controller,
Delete,
Get,
Param,
ParseIntPipe,
Patch,
Post,
Put,
Query,
} from '@nestjs/common';
import { apiKeys } from '@prisma/client';
import { CursorPipe } from '../../pipes/cursor.pipe';
import { OptionalIntPipe } from '../../pipes/optional-int.pipe';
import { OrderByPipe } from '../../pipes/order-by.pipe';
import { WherePipe } from '../../pipes/where.pipe';
import { AuditLog } from '../audit-logs/audit-log.decorator';
import { Scopes } from '../auth/scope.decorator';
import { Expose } from '../prisma/prisma.interface';
import {
CreateApiKeyDto,
ReplaceApiKeyDto,
UpdateApiKeyDto,
} from './api-keys.dto';
import { ApiKeysService } from './api-keys.service';

@Controller('users/:userId/api-keys')
export class ApiKeyUserController {
constructor(private apiKeysService: ApiKeysService) {}

@Post()
@AuditLog('create-api-key')
@Scopes('user-{userId}:write-api-key-*')
async create(
@Param('userId', ParseIntPipe) userId: number,
@Body() data: CreateApiKeyDto,
): Promise<Expose<apiKeys>> {
return this.apiKeysService.createApiKeyForUser(userId, data);
}

@Get()
@Scopes('user-{userId}:read-api-key-*')
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<apiKeys>[]> {
return this.apiKeysService.getApiKeysForUser(userId, {
skip,
take,
orderBy,
cursor,
where,
});
}

@Get('scopes')
@Scopes('user-{userId}:write-api-key-*')
async scopes(
@Param('userId', ParseIntPipe) userId: number,
): Promise<Record<string, string>> {
return this.apiKeysService.getApiKeyScopesForUser(userId);
}

@Get(':id')
@Scopes('user-{userId}:read-api-key-{id}')
async get(
@Param('userId', ParseIntPipe) userId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<apiKeys>> {
return this.apiKeysService.getApiKeyForUser(userId, Number(id));
}

@Patch(':id')
@AuditLog('update-api-key')
@Scopes('user-{userId}:write-api-key-{id}')
async update(
@Body() data: UpdateApiKeyDto,
@Param('userId', ParseIntPipe) userId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<apiKeys>> {
return this.apiKeysService.updateApiKeyForUser(userId, Number(id), data);
}

@Put(':id')
@AuditLog('update-api-key')
@Scopes('user-{userId}:write-api-key-{id}')
async replace(
@Body() data: ReplaceApiKeyDto,
@Param('userId', ParseIntPipe) userId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<apiKeys>> {
return this.apiKeysService.updateApiKeyForUser(userId, Number(id), data);
}

@Delete(':id')
@AuditLog('delete-api-key')
@Scopes('user-{userId}:delete-api-key-{id}')
async remove(
@Param('userId', ParseIntPipe) userId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<apiKeys>> {
return this.apiKeysService.deleteApiKeyForUser(userId, Number(id));
}
}
5 changes: 3 additions & 2 deletions src/modules/api-keys/api-keys.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { Module } from '@nestjs/common';
import { PrismaModule } from '../prisma/prisma.module';
import { StripeModule } from '../stripe/stripe.module';
import { TokensModule } from '../tokens/tokens.module';
import { ApiKeyController } from './api-keys.controller';
import { ApiKeyGroupController } from './api-keys-group.controller';
import { ApiKeyUserController } from './api-keys-user.controller';
import { ApiKeysService } from './api-keys.service';

@Module({
imports: [PrismaModule, TokensModule, StripeModule],
controllers: [ApiKeyController],
controllers: [ApiKeyGroupController, ApiKeyUserController],
providers: [ApiKeysService],
})
export class ApiKeysModule {}

0 comments on commit bf9976d

Please sign in to comment.