refactor: 统一代码缩进与引号风格,新增CORS配置
1. 修正所有文件的缩进为4空格,将单引号统一改为双引号 2. 新增CORS_ORIGIN环境变量配置并从env读取CORS允许源 3. 新增prettier配置文件统一代码格式 4. 为main.ts添加启动日志输出
This commit is contained in:
3
.env
3
.env
@@ -12,4 +12,7 @@ DB_USERNAME=root
|
|||||||
DB_PASSWORD=zhxiao1124..
|
DB_PASSWORD=zhxiao1124..
|
||||||
DB_DATABASE=onion_tiktok
|
DB_DATABASE=onion_tiktok
|
||||||
|
|
||||||
|
# CORS settings
|
||||||
|
CORS_ORIGIN=http://localhost:5173,http://127.0.0.1:5173
|
||||||
|
|
||||||
DB_SYNCHRONIZE = true
|
DB_SYNCHRONIZE = true
|
||||||
|
|||||||
@@ -12,4 +12,7 @@ DB_USERNAME=root
|
|||||||
DB_PASSWORD=
|
DB_PASSWORD=
|
||||||
DB_DATABASE=cth_tk_backend
|
DB_DATABASE=cth_tk_backend
|
||||||
|
|
||||||
|
# CORS settings
|
||||||
|
CORS_ORIGIN=http://localhost:5173,http://127.0.0.1:5173
|
||||||
|
|
||||||
DB_SYNCHRONIZE = true
|
DB_SYNCHRONIZE = true
|
||||||
|
|||||||
9
.prettierrc
Normal file
9
.prettierrc
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"tabWidth": 4,
|
||||||
|
"singleQuote": false,
|
||||||
|
"trailingComma": "all",
|
||||||
|
"printWidth": 390,
|
||||||
|
"eslintIntegration": true,
|
||||||
|
"bracketSpacing": true,
|
||||||
|
"stylelintIntegration": true
|
||||||
|
}
|
||||||
@@ -1,15 +1,15 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from "@nestjs/common";
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from "@nestjs/config";
|
||||||
import { AdminModule } from './modules/admin/admin.module';
|
import { AdminModule } from "./modules/admin/admin.module";
|
||||||
import { AuthModule } from './modules/auth/auth.module';
|
import { AuthModule } from "./modules/auth/auth.module";
|
||||||
import { DataModule } from './modules/data/data.module';
|
import { DataModule } from "./modules/data/data.module";
|
||||||
import { DatabaseModule } from './modules/database/database.module';
|
import { DatabaseModule } from "./modules/database/database.module";
|
||||||
import { DramasModule } from './modules/dramas/dramas.module';
|
import { DramasModule } from "./modules/dramas/dramas.module";
|
||||||
import { HealthModule } from './modules/health/health.module';
|
import { HealthModule } from "./modules/health/health.module";
|
||||||
import { LogsModule } from './modules/logs/logs.module';
|
import { LogsModule } from "./modules/logs/logs.module";
|
||||||
import { MediaModule } from './modules/media/media.module';
|
import { MediaModule } from "./modules/media/media.module";
|
||||||
import { OrdersModule } from './modules/orders/orders.module';
|
import { OrdersModule } from "./modules/orders/orders.module";
|
||||||
import { PlayerModule } from './modules/player/player.module';
|
import { PlayerModule } from "./modules/player/player.module";
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
|
|||||||
@@ -9,10 +9,7 @@ import { LogsService } from "./modules/logs/logs.service";
|
|||||||
|
|
||||||
export function bootstrapApp(app: INestApplication) {
|
export function bootstrapApp(app: INestApplication) {
|
||||||
app.enableCors({
|
app.enableCors({
|
||||||
origin: process.env.CORS_ORIGIN?.split(',') ?? [
|
origin: process.env.CORS_ORIGIN?.split(","),
|
||||||
'http://localhost:5173',
|
|
||||||
'http://127.0.0.1:5173',
|
|
||||||
],
|
|
||||||
credentials: true,
|
credentials: true,
|
||||||
});
|
});
|
||||||
app.use(RequestIdMiddleware);
|
app.use(RequestIdMiddleware);
|
||||||
@@ -25,18 +22,13 @@ export function bootstrapApp(app: INestApplication) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const logsService = app.get(LogsService);
|
const logsService = app.get(LogsService);
|
||||||
app.useGlobalFilters(new HttpExceptionFilter(logsService));
|
|
||||||
app.useGlobalInterceptors(
|
|
||||||
new RequestLoggingInterceptor(logsService, app.get(Reflector)),
|
|
||||||
new ResponseInterceptor(),
|
|
||||||
);
|
|
||||||
|
|
||||||
const config = new DocumentBuilder()
|
app.useGlobalFilters(new HttpExceptionFilter(logsService));
|
||||||
.setTitle("TikTok Short Drama Backend")
|
app.useGlobalInterceptors(new RequestLoggingInterceptor(logsService, app.get(Reflector)), new ResponseInterceptor());
|
||||||
.setDescription("RESTful API for TikTok short drama backend services.")
|
|
||||||
.setVersion("1.0")
|
const config = new DocumentBuilder().setTitle("TikTok Short Drama Backend").setDescription("RESTful API for TikTok short drama backend services.").setVersion("1.0").addBearerAuth().build();
|
||||||
.addBearerAuth()
|
|
||||||
.build();
|
|
||||||
const document = SwaggerModule.createDocument(app, config);
|
const document = SwaggerModule.createDocument(app, config);
|
||||||
|
|
||||||
SwaggerModule.setup("/api/docs", app, document);
|
SwaggerModule.setup("/api/docs", app, document);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
import 'dotenv/config';
|
import "dotenv/config";
|
||||||
import { NestFactory } from '@nestjs/core';
|
import { NestFactory } from "@nestjs/core";
|
||||||
import { AppModule } from './app.module';
|
import { AppModule } from "./app.module";
|
||||||
import { bootstrapApp } from './bootstrap';
|
import { bootstrapApp } from "./bootstrap";
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create(AppModule);
|
const app = await NestFactory.create(AppModule);
|
||||||
bootstrapApp(app);
|
bootstrapApp(app);
|
||||||
await app.listen(process.env.PORT ?? 3000);
|
await app.listen(process.env.PORT ?? 3000);
|
||||||
|
console.log(`Application is running on: ${await app.getUrl()}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
void bootstrap();
|
void bootstrap();
|
||||||
|
|||||||
@@ -1,151 +1,117 @@
|
|||||||
import {
|
import { Body, Controller, Get, Param, Patch, Post, Query, UseGuards } from "@nestjs/common";
|
||||||
Body,
|
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
|
||||||
Controller,
|
import { RequirePermissions } from "../admin/decorators/require-permissions.decorator";
|
||||||
Get,
|
import { AdminJwtAuthGuard } from "../admin/guards/admin-jwt-auth.guard";
|
||||||
Param,
|
import { PermissionsGuard } from "../admin/guards/permissions.guard";
|
||||||
Patch,
|
import { ConfigureEpisodesDto } from "./dto/configure-episodes.dto";
|
||||||
Post,
|
import { CreateDramaDto } from "./dto/create-drama.dto";
|
||||||
Query,
|
import { CreateEpisodeDto } from "./dto/create-episode.dto";
|
||||||
UseGuards,
|
import { UpdateDramaDto } from "./dto/update-drama.dto";
|
||||||
} from '@nestjs/common';
|
import { UpdateEpisodeDto } from "./dto/update-episode.dto";
|
||||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
import { DramasService } from "./dramas.service";
|
||||||
import { RequirePermissions } from '../admin/decorators/require-permissions.decorator';
|
import { PublishStatus } from "./drama-status.enum";
|
||||||
import { AdminJwtAuthGuard } from '../admin/guards/admin-jwt-auth.guard';
|
|
||||||
import { PermissionsGuard } from '../admin/guards/permissions.guard';
|
|
||||||
import { ConfigureEpisodesDto } from './dto/configure-episodes.dto';
|
|
||||||
import { CreateDramaDto } from './dto/create-drama.dto';
|
|
||||||
import { CreateEpisodeDto } from './dto/create-episode.dto';
|
|
||||||
import { UpdateDramaDto } from './dto/update-drama.dto';
|
|
||||||
import { UpdateEpisodeDto } from './dto/update-episode.dto';
|
|
||||||
import { DramasService } from './dramas.service';
|
|
||||||
import { PublishStatus } from './drama-status.enum';
|
|
||||||
|
|
||||||
@ApiTags('Dramas')
|
@ApiTags("Dramas")
|
||||||
@Controller()
|
@Controller()
|
||||||
export class DramasController {
|
export class DramasController {
|
||||||
constructor(private readonly dramasService: DramasService) {}
|
constructor(private readonly dramasService: DramasService) {}
|
||||||
|
|
||||||
@Post('/api/admin/v1/dramas')
|
@Post("/api/admin/v1/dramas")
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||||
@RequirePermissions('drama:create')
|
@RequirePermissions("drama:create")
|
||||||
createDrama(@Body() dto: CreateDramaDto) {
|
createDrama(@Body() dto: CreateDramaDto) {
|
||||||
return this.dramasService.createDrama(dto);
|
return this.dramasService.createDrama(dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get('/api/admin/v1/dramas')
|
@Get("/api/admin/v1/dramas")
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||||
@RequirePermissions('drama:read')
|
@RequirePermissions("drama:read")
|
||||||
listAdminDramas(@Query('page') page?: string, @Query('pageSize') pageSize?: string) {
|
listAdminDramas(@Query("page") page?: string, @Query("pageSize") pageSize?: string) {
|
||||||
return this.dramasService.listAdminDramas({
|
return this.dramasService.listAdminDramas({
|
||||||
page: Number(page) || 1,
|
page: Number(page) || 1,
|
||||||
pageSize: Number(pageSize) || 20,
|
pageSize: Number(pageSize) || 20,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get('/api/admin/v1/dramas/:id')
|
@Get("/api/admin/v1/dramas/:id")
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||||
@RequirePermissions('drama:read')
|
@RequirePermissions("drama:read")
|
||||||
getDrama(@Param('id') id: string) {
|
getDrama(@Param("id") id: string) {
|
||||||
return this.dramasService.getDramaOrThrow(Number(id));
|
return this.dramasService.getDramaOrThrow(Number(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch('/api/admin/v1/dramas/:id')
|
@Patch("/api/admin/v1/dramas/:id")
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||||
@RequirePermissions('drama:update')
|
@RequirePermissions("drama:update")
|
||||||
updateDrama(@Param('id') id: string, @Body() dto: UpdateDramaDto) {
|
updateDrama(@Param("id") id: string, @Body() dto: UpdateDramaDto) {
|
||||||
return this.dramasService.updateDrama(Number(id), dto);
|
return this.dramasService.updateDrama(Number(id), dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post('/api/admin/v1/dramas/:id/episodes/batch')
|
@Post("/api/admin/v1/dramas/:id/episodes/batch")
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||||
@RequirePermissions('episode:create')
|
@RequirePermissions("episode:create")
|
||||||
configureEpisodes(
|
configureEpisodes(@Param("id") id: string, @Body() dto: ConfigureEpisodesDto) {
|
||||||
@Param('id') id: string,
|
|
||||||
@Body() dto: ConfigureEpisodesDto,
|
|
||||||
) {
|
|
||||||
return this.dramasService.configureEpisodes(Number(id), dto);
|
return this.dramasService.configureEpisodes(Number(id), dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get('/api/admin/v1/dramas/:id/episodes')
|
@Get("/api/admin/v1/dramas/:id/episodes")
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||||
@RequirePermissions('episode:update')
|
@RequirePermissions("episode:update")
|
||||||
listAdminEpisodes(
|
listAdminEpisodes(@Param("id") id: string, @Query("page") page?: string, @Query("pageSize") pageSize?: string) {
|
||||||
@Param('id') id: string,
|
|
||||||
@Query('page') page?: string,
|
|
||||||
@Query('pageSize') pageSize?: string,
|
|
||||||
) {
|
|
||||||
return this.dramasService.listAdminEpisodes(Number(id), {
|
return this.dramasService.listAdminEpisodes(Number(id), {
|
||||||
page: Number(page) || 1,
|
page: Number(page) || 1,
|
||||||
pageSize: Number(pageSize) || 20,
|
pageSize: Number(pageSize) || 20,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post('/api/admin/v1/episodes')
|
@Post("/api/admin/v1/episodes")
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||||
@RequirePermissions('episode:create')
|
@RequirePermissions("episode:create")
|
||||||
createEpisode(@Body() dto: CreateEpisodeDto) {
|
createEpisode(@Body() dto: CreateEpisodeDto) {
|
||||||
return this.dramasService.createEpisode(dto);
|
return this.dramasService.createEpisode(dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch('/api/admin/v1/episodes/:id')
|
@Patch("/api/admin/v1/episodes/:id")
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||||
@RequirePermissions('episode:update')
|
@RequirePermissions("episode:update")
|
||||||
updateEpisode(@Param('id') id: string, @Body() dto: UpdateEpisodeDto) {
|
updateEpisode(@Param("id") id: string, @Body() dto: UpdateEpisodeDto) {
|
||||||
return this.dramasService.updateEpisode(Number(id), dto);
|
return this.dramasService.updateEpisode(Number(id), dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch('/api/admin/v1/dramas/:dramaId/episodes/:episodeId')
|
@Patch("/api/admin/v1/dramas/:dramaId/episodes/:episodeId")
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||||
@RequirePermissions('episode:update')
|
@RequirePermissions("episode:update")
|
||||||
updateDramaEpisode(
|
updateDramaEpisode(@Param("dramaId") dramaId: string, @Param("episodeId") episodeId: string, @Body() dto: UpdateEpisodeDto) {
|
||||||
@Param('dramaId') dramaId: string,
|
return this.dramasService.updateDramaEpisode(Number(dramaId), Number(episodeId), dto);
|
||||||
@Param('episodeId') episodeId: string,
|
|
||||||
@Body() dto: UpdateEpisodeDto,
|
|
||||||
) {
|
|
||||||
return this.dramasService.updateDramaEpisode(
|
|
||||||
Number(dramaId),
|
|
||||||
Number(episodeId),
|
|
||||||
dto,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post('/api/admin/v1/dramas/:id/submit-review')
|
@Post("/api/admin/v1/dramas/:id/submit-review")
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||||
@RequirePermissions('drama:update')
|
@RequirePermissions("drama:update")
|
||||||
submitReview(@Param('id') id: string) {
|
submitReview(@Param("id") id: string) {
|
||||||
return this.dramasService.submitReview(Number(id));
|
return this.dramasService.submitReview(Number(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch('/api/admin/v1/episodes/:id/publish-status')
|
@Patch("/api/admin/v1/episodes/:id/publish-status")
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||||
@RequirePermissions('episode:update')
|
@RequirePermissions("episode:update")
|
||||||
updatePublishStatus(
|
updatePublishStatus(@Param("id") id: string, @Body() dto: { publishStatus: PublishStatus }) {
|
||||||
@Param('id') id: string,
|
return this.dramasService.updateEpisodePublishStatus(Number(id), dto.publishStatus);
|
||||||
@Body() dto: { publishStatus: PublishStatus },
|
|
||||||
) {
|
|
||||||
return this.dramasService.updateEpisodePublishStatus(
|
|
||||||
Number(id),
|
|
||||||
dto.publishStatus,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get('/api/app/v1/dramas')
|
@Get("/api/app/v1/dramas")
|
||||||
listPublishedDramas(
|
listPublishedDramas(@Query("page") page?: string, @Query("pageSize") pageSize?: string, @Query("title") title?: string) {
|
||||||
@Query('page') page?: string,
|
|
||||||
@Query('pageSize') pageSize?: string,
|
|
||||||
@Query('title') title?: string,
|
|
||||||
) {
|
|
||||||
return this.dramasService.listPublishedDramas({
|
return this.dramasService.listPublishedDramas({
|
||||||
page: Number(page) || 1,
|
page: Number(page) || 1,
|
||||||
pageSize: Number(pageSize) || 10,
|
pageSize: Number(pageSize) || 10,
|
||||||
@@ -153,20 +119,16 @@ export class DramasController {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get('/api/app/v1/dramas/:id/episodes')
|
@Get("/api/app/v1/dramas/:id/episodes")
|
||||||
listPublishedEpisodes(
|
listPublishedEpisodes(@Param("id") id: string, @Query("page") page?: string, @Query("pageSize") pageSize?: string) {
|
||||||
@Param('id') id: string,
|
|
||||||
@Query('page') page?: string,
|
|
||||||
@Query('pageSize') pageSize?: string,
|
|
||||||
) {
|
|
||||||
return this.dramasService.listPublishedEpisodes(Number(id), {
|
return this.dramasService.listPublishedEpisodes(Number(id), {
|
||||||
page: Number(page) || 1,
|
page: Number(page) || 1,
|
||||||
pageSize: Number(pageSize) || 20,
|
pageSize: Number(pageSize) || 20,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get('/api/app/v1/dramas/:id')
|
@Get("/api/app/v1/dramas/:id")
|
||||||
getPublishedDrama(@Param('id') id: string) {
|
getPublishedDrama(@Param("id") id: string) {
|
||||||
return this.dramasService.getPublishedDramaOrThrow(Number(id));
|
return this.dramasService.getPublishedDramaOrThrow(Number(id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,4 @@
|
|||||||
import {
|
import { Body, Controller, Get, Param, Patch, Post, Query, UseGuards } from "@nestjs/common";
|
||||||
Body,
|
|
||||||
Controller,
|
|
||||||
Get,
|
|
||||||
Param,
|
|
||||||
Patch,
|
|
||||||
Post,
|
|
||||||
Query,
|
|
||||||
UseGuards,
|
|
||||||
} from "@nestjs/common";
|
|
||||||
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
|
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
|
||||||
import { RequirePermissions } from "../admin/decorators/require-permissions.decorator";
|
import { RequirePermissions } from "../admin/decorators/require-permissions.decorator";
|
||||||
import { AdminJwtAuthGuard } from "../admin/guards/admin-jwt-auth.guard";
|
import { AdminJwtAuthGuard } from "../admin/guards/admin-jwt-auth.guard";
|
||||||
@@ -68,10 +59,7 @@ export class MediaController {
|
|||||||
|
|
||||||
@Get("/upload-jobs")
|
@Get("/upload-jobs")
|
||||||
@RequirePermissions("media:read")
|
@RequirePermissions("media:read")
|
||||||
listUploadJobs(
|
listUploadJobs(@Query("page") page?: string, @Query("pageSize") pageSize?: string) {
|
||||||
@Query("page") page?: string,
|
|
||||||
@Query("pageSize") pageSize?: string,
|
|
||||||
) {
|
|
||||||
return this.mediaService.listUploadJobs({
|
return this.mediaService.listUploadJobs({
|
||||||
page: Number(page) || 1,
|
page: Number(page) || 1,
|
||||||
pageSize: Number(pageSize) || 20,
|
pageSize: Number(pageSize) || 20,
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { Injectable, NotFoundException, Optional } from '@nestjs/common';
|
import { Injectable, NotFoundException, Optional } from "@nestjs/common";
|
||||||
import { InjectDataSource } from '@nestjs/typeorm';
|
import { InjectDataSource } from "@nestjs/typeorm";
|
||||||
import { DataSource } from 'typeorm';
|
import { DataSource } from "typeorm";
|
||||||
import { PaginatedResponse } from '../../common/dto/paginated-response.dto';
|
import { PaginatedResponse } from "../../common/dto/paginated-response.dto";
|
||||||
import { User } from './entities/user.entity';
|
import { User } from "./entities/user.entity";
|
||||||
import { UserRecord } from './interfaces/user-record.interface';
|
import { UserRecord } from "./interfaces/user-record.interface";
|
||||||
|
|
||||||
interface UpsertTikTokUserInput {
|
interface UpsertTikTokUserInput {
|
||||||
tiktokOpenId: string;
|
tiktokOpenId: string;
|
||||||
@@ -48,15 +48,13 @@ export class UsersService {
|
|||||||
tiktokUnionId: input.tiktokUnionId,
|
tiktokUnionId: input.tiktokUnionId,
|
||||||
nickname: input.nickname,
|
nickname: input.nickname,
|
||||||
avatarUrl: input.avatarUrl,
|
avatarUrl: input.avatarUrl,
|
||||||
status: 'active',
|
status: "active",
|
||||||
});
|
});
|
||||||
const saved = await repository.save(user);
|
const saved = await repository.save(user);
|
||||||
return { user: this.toRecord(saved), isNewUser: true };
|
return { user: this.toRecord(saved), isNewUser: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
const existing = this.users.find(
|
const existing = this.users.find((user) => user.tiktokOpenId === input.tiktokOpenId);
|
||||||
(user) => user.tiktokOpenId === input.tiktokOpenId,
|
|
||||||
);
|
|
||||||
const now = new Date().toISOString();
|
const now = new Date().toISOString();
|
||||||
|
|
||||||
if (existing) {
|
if (existing) {
|
||||||
@@ -73,7 +71,7 @@ export class UsersService {
|
|||||||
tiktokUnionId: input.tiktokUnionId,
|
tiktokUnionId: input.tiktokUnionId,
|
||||||
nickname: input.nickname,
|
nickname: input.nickname,
|
||||||
avatarUrl: input.avatarUrl,
|
avatarUrl: input.avatarUrl,
|
||||||
status: 'active',
|
status: "active",
|
||||||
createdAt: now,
|
createdAt: now,
|
||||||
updatedAt: now,
|
updatedAt: now,
|
||||||
};
|
};
|
||||||
@@ -99,7 +97,7 @@ export class UsersService {
|
|||||||
const pageSize = Math.min(Math.max(pagination.pageSize, 1), 100);
|
const pageSize = Math.min(Math.max(pagination.pageSize, 1), 100);
|
||||||
const repository = this.dataSource.getRepository(User);
|
const repository = this.dataSource.getRepository(User);
|
||||||
const [records, total] = await repository.findAndCount({
|
const [records, total] = await repository.findAndCount({
|
||||||
order: { id: 'DESC' },
|
order: { id: "DESC" },
|
||||||
skip: (page - 1) * pageSize,
|
skip: (page - 1) * pageSize,
|
||||||
take: pageSize,
|
take: pageSize,
|
||||||
});
|
});
|
||||||
@@ -116,18 +114,18 @@ export class UsersService {
|
|||||||
async getUserOrThrow(id: number) {
|
async getUserOrThrow(id: number) {
|
||||||
const user = await this.findById(id);
|
const user = await this.findById(id);
|
||||||
if (!user) {
|
if (!user) {
|
||||||
throw new NotFoundException('User not found');
|
throw new NotFoundException("User not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateStatus(id: number, status: 'active' | 'disabled') {
|
async updateStatus(id: number, status: "active" | "disabled") {
|
||||||
if (this.hasDatabase()) {
|
if (this.hasDatabase()) {
|
||||||
const repository = this.dataSource.getRepository(User);
|
const repository = this.dataSource.getRepository(User);
|
||||||
const user = await repository.findOne({ where: { id } });
|
const user = await repository.findOne({ where: { id } });
|
||||||
if (!user) {
|
if (!user) {
|
||||||
throw new NotFoundException('User not found');
|
throw new NotFoundException("User not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
user.status = status;
|
user.status = status;
|
||||||
@@ -151,16 +149,13 @@ export class UsersService {
|
|||||||
tiktokUnionId: user.tiktokUnionId,
|
tiktokUnionId: user.tiktokUnionId,
|
||||||
nickname: user.nickname,
|
nickname: user.nickname,
|
||||||
avatarUrl: user.avatarUrl,
|
avatarUrl: user.avatarUrl,
|
||||||
status: user.status as 'active' | 'disabled',
|
status: user.status as "active" | "disabled",
|
||||||
createdAt: user.createdAt?.toISOString?.() ?? new Date().toISOString(),
|
createdAt: user.createdAt?.toISOString?.() ?? new Date().toISOString(),
|
||||||
updatedAt: user.updatedAt?.toISOString?.() ?? new Date().toISOString(),
|
updatedAt: user.updatedAt?.toISOString?.() ?? new Date().toISOString(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private paginate<T>(
|
private paginate<T>(records: T[], input: PaginationInput): PaginatedResponse<T> {
|
||||||
records: T[],
|
|
||||||
input: PaginationInput,
|
|
||||||
): PaginatedResponse<T> {
|
|
||||||
const page = Math.max(input.page, 1);
|
const page = Math.max(input.page, 1);
|
||||||
const pageSize = Math.min(Math.max(input.pageSize, 1), 100);
|
const pageSize = Math.min(Math.max(input.pageSize, 1), 100);
|
||||||
const start = (page - 1) * pageSize;
|
const start = (page - 1) * pageSize;
|
||||||
|
|||||||
Reference in New Issue
Block a user