mirror of https://github.com/ghostfolio/ghostfolio
99 changed files with 1883 additions and 350 deletions
File diff suppressed because it is too large
@ -1,24 +1,125 @@ |
|||
import { PlatformService } from '@ghostfolio/api/app/platform/platform.service'; |
|||
import { AllowDuringImpersonation } from '@ghostfolio/api/decorators/allow-during-impersonation.decorator'; |
|||
import { HasPermission } from '@ghostfolio/api/decorators/has-permission.decorator'; |
|||
import { HasPermissionGuard } from '@ghostfolio/api/guards/has-permission.guard'; |
|||
import { PlatformsResponse } from '@ghostfolio/common/interfaces'; |
|||
import { permissions } from '@ghostfolio/common/permissions'; |
|||
import { PlatformService } from '@ghostfolio/api/services/platform/platform.service'; |
|||
import { CreatePlatformDto, UpdatePlatformDto } from '@ghostfolio/common/dtos'; |
|||
import { |
|||
PlatformResponse, |
|||
PlatformsResponse |
|||
} from '@ghostfolio/common/interfaces'; |
|||
import { hasPermission, permissions } from '@ghostfolio/common/permissions'; |
|||
import type { RequestWithUser } from '@ghostfolio/common/types'; |
|||
|
|||
import { Controller, Get, UseGuards } from '@nestjs/common'; |
|||
import { |
|||
Body, |
|||
Controller, |
|||
Delete, |
|||
Get, |
|||
HttpException, |
|||
Inject, |
|||
Param, |
|||
Post, |
|||
Put, |
|||
UseGuards |
|||
} from '@nestjs/common'; |
|||
import { REQUEST } from '@nestjs/core'; |
|||
import { AuthGuard } from '@nestjs/passport'; |
|||
import { StatusCodes, getReasonPhrase } from 'http-status-codes'; |
|||
import { pick } from 'lodash-es'; |
|||
|
|||
@AllowDuringImpersonation() |
|||
@Controller('platforms') |
|||
export class PlatformsController { |
|||
public constructor(private readonly platformService: PlatformService) {} |
|||
public constructor( |
|||
private readonly platformService: PlatformService, |
|||
@Inject(REQUEST) private readonly request: RequestWithUser |
|||
) {} |
|||
|
|||
@HasPermission(permissions.createPlatform) |
|||
@Post() |
|||
@UseGuards(AuthGuard('jwt'), HasPermissionGuard) |
|||
public async createPlatform( |
|||
@Body() data: CreatePlatformDto |
|||
): Promise<PlatformResponse> { |
|||
const platform = await this.platformService.createPlatform(data); |
|||
|
|||
return pick(platform, ['id', 'name', 'url']); |
|||
} |
|||
|
|||
@Delete(':id') |
|||
@HasPermission(permissions.deletePlatform) |
|||
@UseGuards(AuthGuard('jwt'), HasPermissionGuard) |
|||
public async deletePlatform(@Param('id') id: string): Promise<void> { |
|||
const originalPlatform = await this.platformService.getPlatform({ |
|||
id |
|||
}); |
|||
|
|||
if (!originalPlatform) { |
|||
throw new HttpException( |
|||
getReasonPhrase(StatusCodes.FORBIDDEN), |
|||
StatusCodes.FORBIDDEN |
|||
); |
|||
} |
|||
|
|||
await this.platformService.deletePlatform({ id }); |
|||
} |
|||
|
|||
@Get() |
|||
@HasPermission(permissions.readPlatforms) |
|||
@UseGuards(AuthGuard('jwt'), HasPermissionGuard) |
|||
public async getPlatforms(): Promise<PlatformsResponse> { |
|||
if ( |
|||
hasPermission( |
|||
this.request.user.permissions, |
|||
permissions.readPlatformsWithAccountCount |
|||
) |
|||
) { |
|||
const platformsWithAccountCount = |
|||
await this.platformService.getPlatformsWithAccountCount({ |
|||
orderBy: { name: 'asc' } |
|||
}); |
|||
|
|||
return { platforms: platformsWithAccountCount }; |
|||
} |
|||
|
|||
const platforms = await this.platformService.getPlatforms({ |
|||
orderBy: { name: 'asc' } |
|||
}); |
|||
|
|||
return { platforms }; |
|||
return { |
|||
platforms: platforms.map((platform) => { |
|||
return pick(platform, ['id', 'name', 'url']); |
|||
}) |
|||
}; |
|||
} |
|||
|
|||
@HasPermission(permissions.updatePlatform) |
|||
@Put(':id') |
|||
@UseGuards(AuthGuard('jwt'), HasPermissionGuard) |
|||
public async updatePlatform( |
|||
@Param('id') id: string, |
|||
@Body() data: UpdatePlatformDto |
|||
): Promise<PlatformResponse> { |
|||
const originalPlatform = await this.platformService.getPlatform({ |
|||
id |
|||
}); |
|||
|
|||
if (!originalPlatform) { |
|||
throw new HttpException( |
|||
getReasonPhrase(StatusCodes.FORBIDDEN), |
|||
StatusCodes.FORBIDDEN |
|||
); |
|||
} |
|||
|
|||
const platform = await this.platformService.updatePlatform({ |
|||
data: { |
|||
...data |
|||
}, |
|||
where: { |
|||
id |
|||
} |
|||
}); |
|||
|
|||
return pick(platform, ['id', 'name', 'url']); |
|||
} |
|||
} |
|||
|
|||
@ -1,11 +1,12 @@ |
|||
import { PlatformModule } from '@ghostfolio/api/app/platform/platform.module'; |
|||
import { PlatformModule } from '@ghostfolio/api/services/platform/platform.module'; |
|||
|
|||
import { Module } from '@nestjs/common'; |
|||
|
|||
import { PlatformController } from './platform.controller'; |
|||
import { PlatformsController } from './platforms.controller'; |
|||
|
|||
@Module({ |
|||
controllers: [PlatformsController], |
|||
controllers: [PlatformController, PlatformsController], |
|||
imports: [PlatformModule] |
|||
}) |
|||
export class PlatformsModule {} |
|||
|
|||
@ -0,0 +1,6 @@ |
|||
import type { Platform } from '@ghostfolio/prisma/browser'; |
|||
|
|||
export interface PlatformResponse extends Pick< |
|||
Platform, |
|||
'id' | 'name' | 'url' |
|||
> {} |
|||
@ -1,5 +1,5 @@ |
|||
import type { Platform } from '@ghostfolio/prisma/browser'; |
|||
import type { PlatformResponse } from './platform-response.interface'; |
|||
|
|||
export interface PlatformsResponse { |
|||
platforms: Platform[]; |
|||
platforms: (PlatformResponse & { accountCount?: number })[]; |
|||
} |
|||
|
|||
Loading…
Reference in new issue