From b7c772185f529be8de0b064f9828d1a1469d6cdb Mon Sep 17 00:00:00 2001 From: popovspiridon99 Date: Fri, 14 Nov 2025 17:05:57 +0900 Subject: [PATCH] server: update fuel; update general proper city GET --- server/src/app.module.ts | 15 +++++ server/src/fuel/dto/get-boilers.ts | 18 ++++++ server/src/fuel/dto/get-fuels.ts | 18 ++++++ server/src/fuel/fuel.controller.ts | 24 +++++-- server/src/fuel/fuel.service.ts | 81 +++++++++++++++++++++--- server/src/general/dto/get-cities.ts | 6 +- server/src/general/general.controller.ts | 4 +- server/src/general/general.service.ts | 19 +++--- 8 files changed, 159 insertions(+), 26 deletions(-) create mode 100644 server/src/fuel/dto/get-boilers.ts create mode 100644 server/src/fuel/dto/get-fuels.ts diff --git a/server/src/app.module.ts b/server/src/app.module.ts index 20073ab..87ef057 100644 --- a/server/src/app.module.ts +++ b/server/src/app.module.ts @@ -63,6 +63,21 @@ import { GisModule } from './gis/gis.module'; autoLoadEntities: true, name: 'generalConnection' }), + TypeOrmModule.forRoot({ + type: 'mssql', + host: process.env.WORLDSTONE_DB_HOST, + port: Number(process.env.WORLDSTONE_DB_PORT) || 1433, + username: process.env.WORLDSTONE_DB_USER, + password: process.env.WORLDSTONE_DB_PASSWORD, + database: process.env.WORLDSTONE_DB_NAME, + options: { + encrypt: false, + trustServerCertificate: true + }, + synchronize: false, + autoLoadEntities: true, + name: 'worldstoneConnection' + }), // TypeOrmModule.forRoot({ // type: 'postgres', // host: process.env.PG_HOST, diff --git a/server/src/fuel/dto/get-boilers.ts b/server/src/fuel/dto/get-boilers.ts new file mode 100644 index 0000000..2cea012 --- /dev/null +++ b/server/src/fuel/dto/get-boilers.ts @@ -0,0 +1,18 @@ +import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger" +import { IsNumberString, IsOptional } from "class-validator" + +export class GetBoilersDTO { + @ApiProperty() + @IsNumberString() + city_id: number + + @ApiPropertyOptional() + @IsNumberString() + @IsOptional() + offset?: number + + @ApiPropertyOptional() + @IsNumberString() + @IsOptional() + limit?: number +} \ No newline at end of file diff --git a/server/src/fuel/dto/get-fuels.ts b/server/src/fuel/dto/get-fuels.ts new file mode 100644 index 0000000..dfdeeb5 --- /dev/null +++ b/server/src/fuel/dto/get-fuels.ts @@ -0,0 +1,18 @@ +import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger" +import { IsNumberString, IsOptional } from "class-validator" + +export class GetFuelsDTO { + @ApiProperty() + @IsNumberString() + id_fuels: number + + @ApiPropertyOptional() + @IsNumberString() + @IsOptional() + offset?: number + + @ApiPropertyOptional() + @IsNumberString() + @IsOptional() + limit?: number +} \ No newline at end of file diff --git a/server/src/fuel/fuel.controller.ts b/server/src/fuel/fuel.controller.ts index 2273ecb..4a7d4bd 100644 --- a/server/src/fuel/fuel.controller.ts +++ b/server/src/fuel/fuel.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Get, Post } from '@nestjs/common' +import { Body, Controller, Get, Post, Query } from '@nestjs/common' import { FuelService } from './fuel.service' import { CreateExpenseDto } from './dto/create-expense' import { CreateLimitDto } from './dto/create-limit' @@ -7,6 +7,8 @@ import { FuelLimitDto } from './dto/limit' import { ApiOkResponse } from '@nestjs/swagger' import { FuelExpenseDto } from './dto/expense' import { FuelTransferDto } from './dto/transfer' +import { GetBoilersDTO } from './dto/get-boilers' +import { GetFuelsDTO } from './dto/get-fuels' @Controller('fuel') export class FuelController { @@ -17,7 +19,22 @@ export class FuelController { return this.fuelService.getColumnDataTypes('BoilersFuelsExpenses') } - + @Get('/boilers')//✅ + async getBoilers(@Query() getBoilersDTO: GetBoilersDTO) { + const { city_id, offset, limit } = getBoilersDTO + return this.fuelService.getBoilers(city_id, offset, limit) + } + + @Get('/fuel-types') + async getFuelTypes() { + return this.fuelService.getFuelTypes() + } + + @Get('/fuels') + async getFuels(@Query() getFuelsDTO: GetFuelsDTO) { + const { id_fuels, offset, limit } = getFuelsDTO + return this.fuelService.getFuels(id_fuels) + } // Fuel limits @Get('/limits') @@ -35,8 +52,6 @@ export class FuelController { return this.fuelService.addBoilersFuelsLimit(createLimitDto) } - - // Fuel expenses @Get('/expenses') @ApiOkResponse({ @@ -54,7 +69,6 @@ export class FuelController { } - // Fuel transfer @Get('/transfer') @ApiOkResponse({ diff --git a/server/src/fuel/fuel.service.ts b/server/src/fuel/fuel.service.ts index 74b9319..77e97d1 100644 --- a/server/src/fuel/fuel.service.ts +++ b/server/src/fuel/fuel.service.ts @@ -4,13 +4,18 @@ import { DataSource } from 'typeorm'; import { CreateExpenseDto } from './dto/create-expense'; import { CreateLimitDto } from './dto/create-limit'; import { CreateTransferDto } from './dto/create-transfer'; +import { GetFuelsDTO } from './dto/get-fuels'; @Injectable() export class FuelService { constructor( - @InjectDataSource('fuelConnection') private dataSource: DataSource + @InjectDataSource('fuelConnection') private dataSource: DataSource, + @InjectDataSource('generalConnection') private generalDataSource: DataSource, + @InjectDataSource('worldstoneConnection') private wsDataSource: DataSource ) { } + // GET + async getColumnDataTypes(table_name: string): Promise { const result = await this.dataSource.query(` SELECT @@ -22,6 +27,61 @@ export class FuelService { return result } + async getFuelTypes() { + const result = await this.wsDataSource.query(` + SELECT * FROM TFuels WHERE NOT id = 0 + `) + return result + } + + async getFuels(id_fuels: GetFuelsDTO['id_fuels']) { + const result = await this.wsDataSource.query(` + SELECT * FROM dFuelsParameters + WHERE id_fuels = ${id_fuels} + `) + return result + } + + async getBoilers(city_id: number, offset?: number, limit?: number): Promise { + // const result = await this.wsDataSource.query(` + // SELECT * FROM isWorldstone..vBoilers" + // WHERE id_city = ${city_id} + // ORDER BY id_object + // OFFSET ${offset || 0} ROWS + // FETCH NEXT ${limit || 100} ROWS ONLY + // `) + + // return result + + const result = await this.wsDataSource.query(` + SELECT + b.*, + COALESCE( + (SELECT fp.* + FROM isFuels.dbo.BoilersFuels bf + INNER JOIN isWorldstone.dbo.dFuelsParameters fp ON bf.id_fuels = fp.id + WHERE bf.id_boilers = b.id_object + FOR JSON PATH), + '[]' + ) AS id_fuels + FROM isWorldstone.dbo.vBoilers b + WHERE id_city = @0 + ORDER BY id_object + OFFSET @1 ROWS + FETCH NEXT @2 ROWS ONLY + `, [city_id, Number(offset || 0), Number(limit || 100)]) + + if (Array.isArray(result)) { + return result.map(row => ({ + ...row, + id_fuels: JSON.parse(row.id_fuels) + })) + } else { + return result + } + + } + async getBoilersFuelsExpenses(): Promise { const result = await this.dataSource.query(` SELECT * @@ -31,13 +91,21 @@ export class FuelService { } async getBoilersFuelsLimits(): Promise { - const result = await this.dataSource.query(` + const result = await this.wsDataSource.query(` SELECT * - FROM "BoilersFuelsLimits"; + FROM "vBoilerLimits"; `) return result } + async getFuelsTransfer(): Promise { + const result = await this.dataSource.query(` + SELECT * FROM "FuelsTransfer"; + `) + return result + } + + // ADD async addBoilersFuelsExpense(createExpenseDto: CreateExpenseDto): Promise { const result = await this.dataSource.query(` INSERT INTO dbo.BoilersFuelsExpenses (id_boiler, id_fuel, date, value) VALUES ($1, $2, $3, $4) @@ -52,13 +120,6 @@ export class FuelService { return result } - async getFuelsTransfer(): Promise { - const result = await this.dataSource.query(` - SELECT * FROM "FuelsTransfer"; - `) - return result - } - async addFuelsTransfer(createTransferDto: CreateTransferDto): Promise { const result = await this.dataSource.query(` INSERT INTO dbo.FuelsTransfer (id_out, id_in, id_fuel, date, value) VALUES ($1, $2, $3, $4, $5) diff --git a/server/src/general/dto/get-cities.ts b/server/src/general/dto/get-cities.ts index 552112d..0b173cd 100644 --- a/server/src/general/dto/get-cities.ts +++ b/server/src/general/dto/get-cities.ts @@ -1,7 +1,11 @@ -import { ApiPropertyOptional } from "@nestjs/swagger" +import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger" import { IsNumberString, IsOptional, IsString } from "class-validator" export class GetCitiesDTO { + @ApiProperty() + @IsNumberString() + region_id: number + @ApiPropertyOptional() @IsNumberString() @IsOptional() diff --git a/server/src/general/general.controller.ts b/server/src/general/general.controller.ts index b90dc05..c9e9a3e 100644 --- a/server/src/general/general.controller.ts +++ b/server/src/general/general.controller.ts @@ -19,9 +19,9 @@ export class GeneralController { @Get('/cities')//✅ async getCities(@Query() getCitiesDTO: GetCitiesDTO) { - const { offset, limit, search, id } = getCitiesDTO + const { region_id, offset, limit, search } = getCitiesDTO - return this.generalService.getCities(offset, limit, search, id) + return this.generalService.getCities(region_id, offset, limit, search) } @Get('/types')//✅ diff --git a/server/src/general/general.service.ts b/server/src/general/general.service.ts index e392a7a..1d5e821 100644 --- a/server/src/general/general.service.ts +++ b/server/src/general/general.service.ts @@ -9,13 +9,16 @@ export class GeneralService { private dataSource: DataSource, @InjectDataSource('generalConnection') - private generalDataSource: DataSource + private generalDataSource: DataSource, + + @InjectDataSource('worldstoneConnection') + private wsDataSource: DataSource ) { } async getRegions(): Promise { - const generalDatabase = '_isGeneral' + const generalDatabase = 'isWorldstone' - const result = await this.generalDataSource.query(` + const result = await this.wsDataSource.query(` SELECT r.*, CASE WHEN r.capital IS NOT NULL THEN c.longitude END AS capital_longitude, @@ -39,12 +42,12 @@ export class GeneralService { return result } - async getCities(offset?: number, limit?: number, search?: string, id?: number): Promise { - const generalDatabase = '_isGeneral' + async getCities(id_region: number, offset?: number, limit?: number, search?: string): Promise { + const generalDatabase = 'isWorldstone' - const result = await this.generalDataSource.query(` - SELECT * FROM ${generalDatabase}..Cities - ${id ? `WHERE id = ${id}` : ''} + const result = await this.wsDataSource.query(` + SELECT * FROM ${generalDatabase}..vCities + ${id_region ? `WHERE id_region = ${id_region}` : ''} ${search ? `WHERE name LIKE '%${search || ''}%'` : ''} ORDER BY id OFFSET ${Number(offset) || 0} ROWS