feat: complete backend swagger annotations, admin pages pagination, and frontend application fixes
This commit is contained in:
1 parent
24137beb32
commit
2851ef8a25
41 files changed
+478
-128
No files matched your search
@@ -0,0 +1,39 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsOptional, IsInt, Min, IsString, IsEnum } from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
|
||||
export enum SortOrder {
|
||||
ASC = 'asc',
|
||||
DESC = 'desc',
|
||||
}
|
||||
|
||||
export class PaginationDto {
|
||||
@ApiPropertyOptional({ description: 'شماره صفحه (شروع از ۱)', minimum: 1, default: 1 })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
page?: number = 1;
|
||||
|
||||
@ApiPropertyOptional({ description: 'تعداد آیتمها در هر صفحه', minimum: 1, default: 10 })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
limit?: number = 10;
|
||||
|
||||
@ApiPropertyOptional({ description: 'فیلد برای مرتبسازی', default: 'createdAt' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
sortBy?: string = 'createdAt';
|
||||
|
||||
@ApiPropertyOptional({ description: 'جهت مرتبسازی (asc/desc)', enum: SortOrder, default: SortOrder.DESC })
|
||||
@IsOptional()
|
||||
@IsEnum(SortOrder)
|
||||
sortOrder?: SortOrder = SortOrder.DESC;
|
||||
|
||||
@ApiPropertyOptional({ description: 'عبارت جستجو' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
search?: string;
|
||||
}
|
||||
@@ -9,10 +9,27 @@ export class HttpExceptionFilter implements ExceptionFilter {
|
||||
const status = exception.getStatus();
|
||||
const exceptionResponse: any = exception.getResponse();
|
||||
|
||||
let message = typeof exceptionResponse === 'string' ? exceptionResponse : (exceptionResponse.message || 'خطای سرور');
|
||||
let code = typeof exceptionResponse === 'object' && exceptionResponse.error ? exceptionResponse.error : (status === 400 ? 'BAD_REQUEST' : 'ERROR');
|
||||
|
||||
// Convert array of class-validator errors to a generic Farsi message if it's a 400
|
||||
if (Array.isArray(message) && status === 400) {
|
||||
message = 'اطلاعات وارد شده نامعتبر است. لطفاً فرم را بررسی کنید.';
|
||||
} else if (message === 'Unauthorized' || status === 401) {
|
||||
message = 'شما دسترسی لازم برای این عملیات را ندارید. لطفاً وارد شوید.';
|
||||
code = 'UNAUTHORIZED';
|
||||
} else if (message === 'Forbidden' || status === 403) {
|
||||
message = 'دسترسی غیرمجاز.';
|
||||
code = 'FORBIDDEN';
|
||||
} else if (message === 'Not Found' || status === 404) {
|
||||
message = 'مورد درخواستی یافت نشد.';
|
||||
code = 'NOT_FOUND';
|
||||
}
|
||||
|
||||
response.status(status).json({
|
||||
success: false,
|
||||
message: typeof exceptionResponse === 'string' ? exceptionResponse : (exceptionResponse.message || 'خطای سرور'),
|
||||
code: exceptionResponse.error || (status === 400 ? 'BAD_REQUEST' : 'ERROR'),
|
||||
message,
|
||||
code,
|
||||
details: typeof exceptionResponse === 'object' ? exceptionResponse : {}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class ApiErrorResponse {
|
||||
@ApiProperty({ description: 'موفقیتآمیز بودن درخواست', example: false })
|
||||
success: boolean;
|
||||
|
||||
@ApiProperty({ description: 'پیام خطا', example: 'اطلاعات وارد شده نامعتبر است. لطفاً فرم را بررسی کنید.' })
|
||||
message: string;
|
||||
|
||||
@ApiProperty({ description: 'کد خطا', example: 'BAD_REQUEST' })
|
||||
code: string;
|
||||
|
||||
@ApiProperty({ description: 'جزئیات خطا (در صورت وجود)', required: false, example: {} })
|
||||
details?: any;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class PaginationMeta {
|
||||
@ApiProperty({ description: 'تعداد کل آیتمها', example: 100 })
|
||||
total: number;
|
||||
|
||||
@ApiProperty({ description: 'شماره صفحه فعلی', example: 1 })
|
||||
page: number;
|
||||
|
||||
@ApiProperty({ description: 'تعداد کل صفحات', example: 10 })
|
||||
lastPage: number;
|
||||
|
||||
@ApiProperty({ description: 'تعداد آیتمها در هر صفحه', example: 10 })
|
||||
limit: number;
|
||||
}
|
||||
|
||||
export class PaginatedResponse<T> {
|
||||
@ApiProperty({ description: 'آرایه آیتمها' })
|
||||
data: T[];
|
||||
|
||||
@ApiProperty({ description: 'اطلاعات صفحهبندی' })
|
||||
meta: PaginationMeta;
|
||||
}
|
||||
|
||||
export function createPaginatedSchema(dtoModel: any) {
|
||||
return {
|
||||
schema: {
|
||||
allOf: [
|
||||
{
|
||||
properties: {
|
||||
data: {
|
||||
type: 'array',
|
||||
items: { $ref: `#/components/schemas/${dtoModel.name}` },
|
||||
},
|
||||
meta: {
|
||||
$ref: '#/components/schemas/PaginationMeta',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user