Skip to content

Commit

Permalink
feat: Add slug in entities
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Sep 9, 2024
1 parent 907e369 commit d290cd5
Show file tree
Hide file tree
Showing 77 changed files with 6,706 additions and 5,898 deletions.
510 changes: 272 additions & 238 deletions apps/api/src/api-key/api-key.e2e.spec.ts

Large diffs are not rendered by default.

24 changes: 15 additions & 9 deletions apps/api/src/api-key/controller/api-key.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,24 @@ export class ApiKeyController {
return this.apiKeyService.createApiKey(user, dto)
}

@Put(':id')
@Put(':apiKeySlug')
@RequiredApiKeyAuthorities(Authority.UPDATE_API_KEY)
async updateApiKey(
@CurrentUser() user: User,
@Body() dto: UpdateApiKey,
@Param('id') id: string
@Param('apiKeySlug') apiKeySlug: string
) {
return this.apiKeyService.updateApiKey(user, id, dto)
return this.apiKeyService.updateApiKey(user, apiKeySlug, dto)
}

@Delete(':id')
@Delete(':apiKeySlug')
@RequiredApiKeyAuthorities(Authority.DELETE_API_KEY)
@HttpCode(204)
async deleteApiKey(@CurrentUser() user: User, @Param('id') id: string) {
return this.apiKeyService.deleteApiKey(user, id)
async deleteApiKey(
@CurrentUser() user: User,
@Param('apiKeySlug') apiKeySlug: string
) {
return this.apiKeyService.deleteApiKey(user, apiKeySlug)
}

@Get('/')
Expand All @@ -63,10 +66,13 @@ export class ApiKeyController {
)
}

@Get(':id')
@Get(':apiKeySlug')
@RequiredApiKeyAuthorities(Authority.READ_API_KEY)
async getApiKey(@CurrentUser() user: User, @Param('id') id: string) {
return this.apiKeyService.getApiKeyById(user, id)
async getApiKey(
@CurrentUser() user: User,
@Param('apiKeySlug') apiKeySlug: string
) {
return this.apiKeyService.getApiKeyBySlug(user, apiKeySlug)
}

@Get('/access/live-updates')
Expand Down
72 changes: 34 additions & 38 deletions apps/api/src/api-key/service/api-key.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@ import {
} from '@nestjs/common'
import { PrismaService } from '@/prisma/prisma.service'
import { CreateApiKey } from '../dto/create.api-key/create.api-key'
import { addHoursToDate } from '@/common/add-hours-to-date'
import { generateApiKey } from '@/common/api-key-generator'
import { toSHA256 } from '@/common/to-sha256'
import { UpdateApiKey } from '../dto/update.api-key/update.api-key'
import { ApiKey, User } from '@prisma/client'
import { limitMaxItemsPerPage } from '@/common/limit-max-items-per-page'
import generateEntitySlug from '@/common/slug-generator'
import { generateApiKey, toSHA256 } from '@/common/cryptography'
import { addHoursToDate, limitMaxItemsPerPage } from '@/common/util'

@Injectable()
export class ApiKeyService {
private readonly logger = new Logger(ApiKeyService.name)

constructor(private readonly prisma: PrismaService) {}

private apiKeySelect = {
id: true,
expiresAt: true,
name: true,
slug: true,
authorities: true,
createdAt: true,
updatedAt: true
}

async createApiKey(user: User, dto: CreateApiKey) {
await this.isApiKeyUnique(user, dto.name)

Expand All @@ -27,6 +36,7 @@ export class ApiKeyService {
const apiKey = await this.prisma.apiKey.create({
data: {
name: dto.name,
slug: await generateEntitySlug(dto.name, 'API_KEY', this.prisma),
value: hashedApiKey,
authorities: dto.authorities
? {
Expand All @@ -50,15 +60,19 @@ export class ApiKeyService {
}
}

async updateApiKey(user: User, apiKeyId: string, dto: UpdateApiKey) {
async updateApiKey(
user: User,
apiKeySlug: ApiKey['slug'],
dto: UpdateApiKey
) {
await this.isApiKeyUnique(user, dto.name)

const apiKey = await this.prisma.apiKey.findUnique({
where: {
id: apiKeyId,
userId: user.id
slug: apiKeySlug
}
})
const apiKeyId = apiKey.id

if (!apiKey) {
throw new NotFoundException(`API key with id ${apiKeyId} not found`)
Expand All @@ -71,61 +85,50 @@ export class ApiKeyService {
},
data: {
name: dto.name,
slug: dto.name
? await generateEntitySlug(dto.name, 'API_KEY', this.prisma)
: apiKey.slug,
authorities: {
set: dto.authorities ? dto.authorities : apiKey.authorities
},
expiresAt: dto.expiresAfter
? addHoursToDate(dto.expiresAfter)
: undefined
},
select: {
id: true,
expiresAt: true,
name: true,
authorities: true,
createdAt: true,
updatedAt: true
}
select: this.apiKeySelect
})

this.logger.log(`User ${user.id} updated API key ${apiKeyId}`)

return updatedApiKey
}

async deleteApiKey(user: User, apiKeyId: string) {
async deleteApiKey(user: User, apiKeySlug: ApiKey['slug']) {
try {
await this.prisma.apiKey.delete({
where: {
id: apiKeyId,
slug: apiKeySlug,
userId: user.id
}
})
} catch (error) {
throw new NotFoundException(`API key with id ${apiKeyId} not found`)
throw new NotFoundException(`API key with id ${apiKeySlug} not found`)
}

this.logger.log(`User ${user.id} deleted API key ${apiKeyId}`)
this.logger.log(`User ${user.id} deleted API key ${apiKeySlug}`)
}

async getApiKeyById(user: User, apiKeyId: string) {
async getApiKeyBySlug(user: User, apiKeySlug: ApiKey['slug']) {
const apiKey = await this.prisma.apiKey.findUnique({
where: {
id: apiKeyId,
slug: apiKeySlug,
userId: user.id
},
select: {
id: true,
expiresAt: true,
name: true,
authorities: true,
createdAt: true,
updatedAt: true
}
select: this.apiKeySelect
})

if (!apiKey) {
throw new NotFoundException(`API key with id ${apiKeyId} not found`)
throw new NotFoundException(`API key with id ${apiKeySlug} not found`)
}

return apiKey
Expand All @@ -151,14 +154,7 @@ export class ApiKeyService {
orderBy: {
[sort]: order
},
select: {
id: true,
expiresAt: true,
name: true,
authorities: true,
createdAt: true,
updatedAt: true
}
select: this.apiKeySelect
})
}

Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/auth/controller/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import { GoogleOAuthStrategyFactory } from '@/config/factory/google/google-strat
import { GitlabOAuthStrategyFactory } from '@/config/factory/gitlab/gitlab-strategy.factory'
import { Response } from 'express'
import { AuthProvider } from '@prisma/client'
import setCookie from '@/common/set-cookie'
import {
sendOAuthFailureRedirect,
sendOAuthSuccessRedirect
} from '@/common/redirect'
import { setCookie } from '@/common/util'

@Controller('auth')
export class AuthController {
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/auth/guard/auth/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { IS_PUBLIC_KEY } from '@/decorators/public.decorator'
import { PrismaService } from '@/prisma/prisma.service'
import { ONBOARDING_BYPASSED } from '@/decorators/bypass-onboarding.decorator'
import { AuthenticatedUserContext } from '../../auth.types'
import { toSHA256 } from '@/common/to-sha256'
import { EnvSchema } from '@/common/env/env.schema'
import { CacheService } from '@/cache/cache.service'
import { toSHA256 } from '@/common/cryptography'

const X_E2E_USER_EMAIL = 'x-e2e-user-email'
const X_KEYSHADE_TOKEN = 'x-keyshade-token'
Expand All @@ -25,8 +25,8 @@ export class AuthGuard implements CanActivate {
constructor(
private readonly jwtService: JwtService,
private readonly prisma: PrismaService,
private reflector: Reflector,
private cache: CacheService
private readonly reflector: Reflector,
private readonly cache: CacheService
) {}

async canActivate(context: ExecutionContext): Promise<boolean> {
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/auth/service/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { Cron, CronExpression } from '@nestjs/schedule'
import { UserAuthenticatedResponse } from '../auth.types'
import { IMailService, MAIL_SERVICE } from '@/mail/services/interface.service'
import { PrismaService } from '@/prisma/prisma.service'
import createUser from '@/common/create-user'
import { AuthProvider } from '@prisma/client'
import generateOtp from '@/common/generate-otp'
import { CacheService } from '@/cache/cache.service'
import { generateOtp } from '@/common/util'
import { createUser } from '@/common/user'

@Injectable()
export class AuthService {
Expand Down
7 changes: 0 additions & 7 deletions apps/api/src/common/add-hours-to-date.ts

This file was deleted.

4 changes: 0 additions & 4 deletions apps/api/src/common/api-key-generator.ts

This file was deleted.

Loading

0 comments on commit d290cd5

Please sign in to comment.