Skip to content

Commit

Permalink
fix rebase issues
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoE105 committed Dec 27, 2024
1 parent b2c1693 commit 37f0731
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { nanoId } from 'minifaker';

import { AiFreeCreditsRequestDto } from '../ai-free-credits-request.dto';
import 'minifaker/locales/en';

describe('AiChatRequestDto', () => {
it('should succeed if projectId is a valid nanoid', () => {
const validRequest = {
projectId: nanoId.nanoid(),
};

const result = AiFreeCreditsRequestDto.safeParse(validRequest);

expect(result.success).toBe(true);
});

it('should succeed if no projectId is sent', () => {
const result = AiFreeCreditsRequestDto.safeParse({});

expect(result.success).toBe(true);
});

it('should fail is projectId invalid value', () => {
const validRequest = {
projectId: '',
};

const result = AiFreeCreditsRequestDto.safeParse(validRequest);

expect(result.success).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { z } from 'zod';
import { Z } from 'zod-class';

export class AiFreeCreditsRequestDto extends Z.class({
projectId: z.string().min(1).optional(),
}) {}
1 change: 1 addition & 0 deletions packages/@n8n/api-types/src/dto/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { AiAskRequestDto } from './ai/ai-ask-request.dto';
export { AiChatRequestDto } from './ai/ai-chat-request.dto';
export { AiApplySuggestionRequestDto } from './ai/ai-apply-suggestion-request.dto';
export { AiFreeCreditsRequestDto } from './ai/ai-free-credits-request.dto';

export { LoginRequestDto } from './auth/login-request.dto';
export { ResolveSignupTokenQueryDto } from './auth/resolve-signup-token-query.dto';
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/controllers/__tests__/ai.controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import { AiController, type FlushableResponse } from '../ai.controller';

describe('AiController', () => {
const aiService = mock<AiService>();
const controller = new AiController(aiService);

const controller = new AiController(aiService, mock(), mock());

const request = mock<AuthenticatedRequest>({
user: { id: 'user123' },
Expand Down
13 changes: 11 additions & 2 deletions packages/cli/src/controllers/ai.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { AiChatRequestDto, AiApplySuggestionRequestDto, AiAskRequestDto } from '@n8n/api-types';
import {
AiChatRequestDto,
AiApplySuggestionRequestDto,
AiAskRequestDto,
AiFreeCreditsRequestDto,
} from '@n8n/api-types';
import type { AiAssistantSDK } from '@n8n_io/ai-assistant-sdk';
import { Response } from 'express';
import { strict as assert } from 'node:assert';
import { WritableStream } from 'node:stream/web';

import { FREE_AI_CREDITS_CREDENTIAL_NAME, OPEN_AI_API_CREDENTIAL_TYPE } from '@/constants';
import { CredentialsService } from '@/credentials/credentials.service';
import { Body, Post, RestController } from '@/decorators';
import { InternalServerError } from '@/errors/response-errors/internal-server.error';
import type { CredentialRequest } from '@/requests';
import { AuthenticatedRequest } from '@/requests';
import { AiService } from '@/services/ai.service';
import { UserService } from '@/services/user.service';
Expand Down Expand Up @@ -71,7 +79,7 @@ export class AiController {
}

@Post('/free-credits')
async aiCredits(req: AiAssistantRequest.FreeAiCreditsPayload) {
async aiCredits(req: AuthenticatedRequest, _: Response, @Body payload: AiFreeCreditsRequestDto) {
try {
const aiCredits = await this.aiService.createFreeAiCredits(req.user);

Expand All @@ -83,6 +91,7 @@ export class AiController {
url: aiCredits.url,
},
isManaged: true,
projectId: payload?.projectId,
};

const newCredential = await this.credentialsService.createCredential(
Expand Down

0 comments on commit 37f0731

Please sign in to comment.