feat(environment): add environment CRUD module and wire into auth login

This commit is contained in:
2026-04-07 13:00:54 +03:00
parent 49dd1aa14c
commit de48a912d4
12 changed files with 208 additions and 24 deletions
+1 -1
View File
@@ -21,6 +21,6 @@ export class AuthController {
@ApiResponse({ status: 400, description: 'Key not found or invalid' })
@ApiResponse({ status: 500, description: 'Automation failed' })
login(@Body() dto: LoginDto): Promise<{ token: string; sessionName: string }> {
return this.authService.login(dto.key, dto.sessionName);
return this.authService.login(dto.key, dto.environmentName, dto.sessionName);
}
}
+2 -1
View File
@@ -2,9 +2,10 @@ import { Module } from '@nestjs/common';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { SessionModule } from '../session/session.module';
import { EnvironmentModule } from '../environment/environment.module';
@Module({
imports: [SessionModule],
imports: [SessionModule, EnvironmentModule],
controllers: [AuthController],
providers: [AuthService],
exports: [AuthService],
+16 -15
View File
@@ -3,6 +3,7 @@ import {
BadRequestException,
InternalServerErrorException,
Logger,
NotFoundException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { chromium } from 'playwright';
@@ -10,6 +11,7 @@ import * as fs from 'fs';
import * as path from 'path';
import * as crypto from 'crypto';
import { SessionService } from '../session/session.service';
import { EnvironmentService } from '../environment/environment.service';
interface KeyDescriptor {
keyFile: string;
@@ -20,24 +22,15 @@ interface KeyDescriptor {
export class AuthService {
private readonly logger = new Logger(AuthService.name);
private readonly keysDir: string;
private readonly loginUrl: string;
private readonly cabinetUrl: string;
constructor(
private readonly config: ConfigService,
private readonly sessionService: SessionService,
private readonly environmentService: EnvironmentService,
) {
this.keysDir = path.resolve(
this.config.get<string>('KEYS_DIR', 'keys'),
);
this.loginUrl = this.config.get<string>(
'ID_LOGIN_URL',
'https://id-liquio-diia-stg.kitsoft.ua/',
);
this.cabinetUrl = this.config.get<string>(
'CABINET_URL',
'https://cabinet-liquio-diia-stg.kitsoft.ua/messages',
);
}
listKeys(): string[] {
@@ -47,9 +40,17 @@ export class AuthService {
.map(f => path.basename(f, '.json'));
}
async login(keyId: string, sessionName?: string): Promise<{ token: string; sessionName: string }> {
async login(keyId: string, environmentName: string, sessionName?: string): Promise<{ token: string; sessionName: string }> {
const resolvedSession = sessionName ?? crypto.randomUUID();
const env = await this.environmentService.findAll().then(all => all.find(e => e.name === environmentName));
if (!env) throw new NotFoundException(`Environment "${environmentName}" not found`);
const loginUrl = env.urls.id_url;
const cabinetUrl = env.urls.cabinet_url;
if (!loginUrl) throw new BadRequestException(`Environment "${environmentName}" is missing id_url`);
if (!cabinetUrl) throw new BadRequestException(`Environment "${environmentName}" is missing cabinet_url`);
const keyJsonPath = path.join(this.keysDir, `${keyId}.json`);
if (!fs.existsSync(keyJsonPath)) {
@@ -73,8 +74,8 @@ export class AuthService {
const context = await browser.newContext();
const page = await context.newPage();
this.logger.log(`Navigating to ${this.loginUrl}`);
await page.goto(this.loginUrl, { waitUntil: 'networkidle' });
this.logger.log(`Navigating to ${loginUrl}`);
await page.goto(loginUrl, { waitUntil: 'networkidle' });
// Click "Файловий ключ" button
await page.getByText('Файловий ключ').click();
@@ -92,8 +93,8 @@ export class AuthService {
await page.locator('#id-app-login-file-key-sign-button').click();
// Wait until redirected to cabinet
this.logger.log(`Waiting for redirect to ${this.cabinetUrl}`);
await page.waitForURL(this.cabinetUrl, { timeout: 30000 });
this.logger.log(`Waiting for redirect to ${cabinetUrl}`);
await page.waitForURL(cabinetUrl, { timeout: 30000 });
// Extract token from localStorage
const token = await page.evaluate(() => localStorage.getItem('token'));
+10 -1
View File
@@ -1,5 +1,5 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsOptional, IsString } from 'class-validator';
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
export class LoginDto {
@ApiProperty({
@@ -7,8 +7,17 @@ export class LoginDto {
example: '3273334361',
})
@IsString()
@IsNotEmpty()
key: string;
@ApiProperty({
description: 'Environment name to resolve login/cabinet URLs from',
example: 'liquio-diia-stg',
})
@IsString()
@IsNotEmpty()
environmentName: string;
@ApiPropertyOptional({
description: 'Session name to store credentials under. Auto-generated UUID if omitted.',
example: 'my-test-session',