feat: add swagger docs for responses

This commit is contained in:
Ahmed Bouhuolia
2025-06-16 13:50:30 +02:00
parent 88ef60ef28
commit c4668d7d22
30 changed files with 1363 additions and 64 deletions

View File

@@ -1,4 +1,10 @@
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import {
ApiOperation,
ApiTags,
ApiResponse,
getSchemaPath,
ApiExtraModels,
} from '@nestjs/swagger';
import {
Body,
Controller,
@@ -9,20 +15,28 @@ import {
Put,
} from '@nestjs/common';
import { BankRulesApplication } from './BankRulesApplication';
import { BankRule } from './models/BankRule';
import { CreateBankRuleDto } from './dtos/BankRule.dto';
import { EditBankRuleDto } from './dtos/BankRule.dto';
import { BankRuleResponseDto } from './dtos/BankRuleResponse.dto';
@Controller('banking/rules')
@ApiTags('Bank Rules')
@ApiExtraModels(BankRuleResponseDto)
export class BankRulesController {
constructor(private readonly bankRulesApplication: BankRulesApplication) {}
@Post()
@ApiOperation({ summary: 'Create a new bank rule.' })
@ApiResponse({
status: 201,
description: 'The bank rule has been successfully created.',
schema: {
$ref: getSchemaPath(BankRuleResponseDto),
},
})
async createBankRule(
@Body() createRuleDTO: CreateBankRuleDto,
): Promise<BankRule> {
): Promise<BankRuleResponseDto> {
return this.bankRulesApplication.createBankRule(createRuleDTO);
}
@@ -37,19 +51,36 @@ export class BankRulesController {
@Delete(':id')
@ApiOperation({ summary: 'Delete the given bank rule.' })
@ApiResponse({
status: 200,
description: 'The bank rule has been successfully deleted.',
})
async deleteBankRule(@Param('id') ruleId: number): Promise<void> {
return this.bankRulesApplication.deleteBankRule(ruleId);
}
@Get(':id')
@ApiOperation({ summary: 'Retrieves the bank rule details.' })
async getBankRule(@Param('id') ruleId: number): Promise<any> {
@ApiResponse({
status: 200,
description: 'The bank rule details have been successfully retrieved.',
schema: { $ref: getSchemaPath(BankRuleResponseDto) },
})
async getBankRule(@Param('id') ruleId: number): Promise<BankRuleResponseDto> {
return this.bankRulesApplication.getBankRule(ruleId);
}
@Get()
@ApiOperation({ summary: 'Retrieves the bank rules.' })
async getBankRules(): Promise<any> {
@ApiResponse({
status: 200,
description: 'The bank rules have been successfully retrieved.',
schema: {
type: 'array',
items: { $ref: getSchemaPath(BankRuleResponseDto) },
},
})
async getBankRules(): Promise<BankRuleResponseDto[]> {
return this.bankRulesApplication.getBankRules();
}
}

View File

@@ -0,0 +1,140 @@
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { BankRuleAssignCategory, BankRuleConditionType } from '../types';
class BankRuleConditionResponseDto {
@ApiProperty({
description: 'The unique identifier of the bank rule condition',
example: 1,
})
id: number;
@ApiProperty({
description: 'The field to check in the condition',
example: 'description',
enum: ['description', 'amount'],
})
field: string;
@ApiProperty({
description: 'The comparison operator to use',
example: 'contains',
enum: [
'equals',
'equal',
'contains',
'not_contain',
'bigger',
'bigger_or_equal',
'smaller',
'smaller_or_equal',
],
})
comparator: string;
@ApiProperty({
description: 'The value to compare against',
example: 'Salary',
})
value: string;
}
export class BankRuleResponseDto {
@ApiProperty({
description: 'The unique identifier of the bank rule',
example: 1,
})
id: number;
@ApiProperty({
description: 'The name of the bank rule',
example: 'Monthly Salary',
})
name: string;
@ApiProperty({
description: 'The order in which the rule should be applied',
example: 1,
})
order: number;
@ApiProperty({
description: 'The account ID to apply the rule if',
example: 1,
})
applyIfAccountId: number;
@ApiProperty({
description: 'The transaction type to apply the rule if',
example: 'deposit',
enum: ['deposit', 'withdrawal'],
})
applyIfTransactionType: string;
@ApiProperty({
description: 'The conditions type to apply the rule if',
example: 'and',
enum: ['and', 'or'],
})
conditionsType: BankRuleConditionType;
@ApiProperty({
description: 'The conditions to apply the rule if',
type: [BankRuleConditionResponseDto],
example: [
{
id: 1,
field: 'description',
comparator: 'contains',
value: 'Salary',
},
],
})
@Type(() => BankRuleConditionResponseDto)
conditions: BankRuleConditionResponseDto[];
@ApiProperty({
description: 'The category to assign the rule if',
example: 'InterestIncome',
enum: [
'InterestIncome',
'OtherIncome',
'Deposit',
'Expense',
'OwnerDrawings',
],
})
assignCategory: BankRuleAssignCategory;
@ApiProperty({
description: 'The account ID to assign the rule if',
example: 1,
})
assignAccountId: number;
@ApiProperty({
description: 'The payee to assign the rule if',
example: 'Employer Inc.',
required: false,
})
assignPayee?: string;
@ApiProperty({
description: 'The memo to assign the rule if',
example: 'Monthly Salary',
required: false,
})
assignMemo?: string;
@ApiProperty({
description: 'The creation timestamp of the bank rule',
example: '2024-03-20T10:00:00Z',
})
createdAt: string;
@ApiProperty({
description: 'The last update timestamp of the bank rule',
example: '2024-03-20T10:00:00Z',
})
updatedAt: string;
}

View File

@@ -2,8 +2,9 @@ import { BaseModel } from '@/models/Model';
import { Model } from 'objection';
import { BankRuleCondition } from './BankRuleCondition';
import { BankRuleAssignCategory, BankRuleConditionType } from '../types';
import { TenantBaseModel } from '@/modules/System/models/TenantBaseModel';
export class BankRule extends BaseModel {
export class BankRule extends TenantBaseModel {
public readonly id!: number;
public readonly name!: string;
public readonly order!: number;
@@ -17,6 +18,9 @@ export class BankRule extends BaseModel {
public readonly conditions!: BankRuleCondition[];
public readonly createdAt: string;
public readonly updatedAt: string;
/**
* Table name
*/
@@ -28,7 +32,7 @@ export class BankRule extends BaseModel {
* Timestamps columns.
*/
static get timestamps() {
return ['created_at', 'updated_at'];
return ['createdAt', 'updatedAt'];
}
/**