| 1 | import { Body, Controller, Delete, Get, Param, Post, Query } from '@nestjs/common' |
| 2 | import { ApiTags } from '@nestjs/swagger' |
| 3 | import { GetToken, TokenInfo } from '@yikart/aitoearn-auth' |
| 4 | import { ApiDoc } from '@yikart/common' |
| 5 | import { AnalyticsService } from '../analytics/analytics.service' |
| 6 | import { ChannelAccountAnalyticsVo } from '../analytics/analytics.vo' |
| 7 | import { PublishOptionCreateBodyDto, PublishOptionValuesQueryDto } from '../platforms/platforms.dto' |
| 8 | import { PlatformsService } from '../platforms/platforms.service' |
| 9 | import { PublishOptionCreatedValueVo, PublishOptionValuesVo } from '../platforms/platforms.vo' |
| 10 | import { |
| 11 | ChannelAccountAnalyticsQueryDto, |
| 12 | ChannelAccountCreateDto, |
| 13 | ChannelAccountDeleteQueryDto, |
| 14 | ChannelAccountListQueryDto, |
| 15 | } from './account.dto' |
| 16 | import { AccountService } from './account.service' |
| 17 | import { |
| 18 | ChannelAccountAuthStatusVo, |
| 19 | ChannelAccountListVo, |
| 20 | ChannelAccountVo, |
| 21 | } from './account.vo' |
| 22 | |
| 23 | @ApiTags('Channels/Accounts') |
| 24 | @Controller({ path: '/channels/accounts', version: '2' }) |
| 25 | export class AccountsController { |
| 26 | constructor( |
| 27 | private readonly accountService: AccountService, |
| 28 | private readonly analyticsService: AnalyticsService, |
| 29 | private readonly platformsService: PlatformsService, |
| 30 | ) {} |
| 31 | |
| 32 | @ApiDoc({ |
| 33 | summary: '账号列表', |
| 34 | query: ChannelAccountListQueryDto.schema, |
| 35 | response: ChannelAccountListVo, |
| 36 | }) |
| 37 | @Get('/') |
| 38 | async list( |
| 39 | @GetToken() token: TokenInfo, |
| 40 | @Query() query: ChannelAccountListQueryDto, |
| 41 | ) { |
| 42 | return ChannelAccountListVo.create(await this.accountService.list(token.id, query)) |
| 43 | } |
| 44 | |
| 45 | @ApiDoc({ |
| 46 | summary: '批量删除账号', |
| 47 | query: ChannelAccountDeleteQueryDto.schema, |
| 48 | }) |
| 49 | @Delete('/') |
| 50 | async deleteMany( |
| 51 | @GetToken() token: TokenInfo, |
| 52 | @Query() query: ChannelAccountDeleteQueryDto, |
| 53 | ) { |
| 54 | return this.accountService.deleteMany(token.id, query.ids) |
| 55 | } |
| 56 | |
| 57 | @ApiDoc({ |
| 58 | summary: '账号详情', |
| 59 | response: ChannelAccountVo, |
| 60 | }) |
| 61 | @Get('/:accountId') |
| 62 | async getById( |
| 63 | @GetToken() token: TokenInfo, |
| 64 | @Param('accountId') accountId: string, |
| 65 | ) { |
| 66 | return ChannelAccountVo.create(await this.accountService.getById(token.id, accountId)) |
| 67 | } |
| 68 | |
| 69 | @ApiDoc({ |
| 70 | summary: '删除账号', |
| 71 | }) |
| 72 | @Delete('/:accountId') |
| 73 | async delete( |
| 74 | @GetToken() token: TokenInfo, |
| 75 | @Param('accountId') accountId: string, |
| 76 | ) { |
| 77 | return this.accountService.delete(token.id, accountId) |
| 78 | } |
| 79 | |
| 80 | @ApiDoc({ |
| 81 | summary: '账号授权状态', |
| 82 | response: ChannelAccountAuthStatusVo, |
| 83 | }) |
| 84 | @Get('/:accountId/auth-status') |
| 85 | async authStatus( |
| 86 | @GetToken() token: TokenInfo, |
| 87 | @Param('accountId') accountId: string, |
| 88 | ) { |
| 89 | return ChannelAccountAuthStatusVo.create(await this.accountService.getAuthStatus(token.id, accountId)) |
| 90 | } |
| 91 | |
| 92 | @ApiDoc({ |
| 93 | summary: '账号数据', |
| 94 | query: ChannelAccountAnalyticsQueryDto.schema, |
| 95 | response: ChannelAccountAnalyticsVo, |
| 96 | }) |
| 97 | @Get('/:accountId/analytics') |
| 98 | async accountAnalytics( |
| 99 | @GetToken() token: TokenInfo, |
| 100 | @Param('accountId') accountId: string, |
| 101 | @Query() query: ChannelAccountAnalyticsQueryDto, |
| 102 | ) { |
| 103 | return ChannelAccountAnalyticsVo.create( |
| 104 | await this.analyticsService.fetchAccountAnalytics(token.id, accountId, { |
| 105 | since: query.since, |
| 106 | until: query.until, |
| 107 | }), |
| 108 | ) |
| 109 | } |
| 110 | |
| 111 | @ApiDoc({ |
| 112 | summary: '获取账号平台动态发布选项取值', |
| 113 | query: PublishOptionValuesQueryDto.schema, |
| 114 | response: PublishOptionValuesVo, |
| 115 | }) |
| 116 | @Get('/:accountId/publish-options/:field/values') |
| 117 | async publishOptionValues( |
| 118 | @GetToken() token: TokenInfo, |
| 119 | @Param('accountId') accountId: string, |
| 120 | @Param('field') field: string, |
| 121 | @Query() query: PublishOptionValuesQueryDto, |
| 122 | ) { |
| 123 | return PublishOptionValuesVo.create( |
| 124 | await this.platformsService.getAccountValues(token.id, accountId, field, query), |
| 125 | ) |
| 126 | } |
| 127 | |
| 128 | @ApiDoc({ |
| 129 | summary: '创建账号平台动态发布选项取值', |
| 130 | body: PublishOptionCreateBodyDto.schema, |
| 131 | response: PublishOptionCreatedValueVo, |
| 132 | }) |
| 133 | @Post('/:accountId/publish-options/:field/values') |
| 134 | async createPublishOptionValue( |
| 135 | @GetToken() token: TokenInfo, |
| 136 | @Param('accountId') accountId: string, |
| 137 | @Param('field') field: string, |
| 138 | @Body() data: PublishOptionCreateBodyDto, |
| 139 | ) { |
| 140 | return PublishOptionCreatedValueVo.create( |
| 141 | await this.platformsService.createAccountValue(token.id, accountId, field, data), |
| 142 | ) |
| 143 | } |
| 144 | |
| 145 | @ApiDoc({ |
| 146 | summary: '创建账号', |
| 147 | description: '仅支持插件授权平台创建账号', |
| 148 | body: ChannelAccountCreateDto.schema, |
| 149 | }) |
| 150 | @Post('/') |
| 151 | async create( |
| 152 | @GetToken() token: TokenInfo, |
| 153 | @Body() data: ChannelAccountCreateDto, |
| 154 | ) { |
| 155 | return ChannelAccountVo.create(await this.accountService.addAccount(token.id, data)) |
| 156 | } |
| 157 | } |
| 158 |