changes
This commit is contained in:
parent
43b9dd941b
commit
04aa26288f
@ -54,8 +54,12 @@ import {
|
|||||||
import {
|
import {
|
||||||
StorageCreateRequestDto,
|
StorageCreateRequestDto,
|
||||||
StorageUpdateRequestDto,
|
StorageUpdateRequestDto,
|
||||||
|
StorageWithSetsQueryDto,
|
||||||
} from './dto/storage-request.dto';
|
} from './dto/storage-request.dto';
|
||||||
import { StorageResponseDto } from './dto/storage-response.dto';
|
import {
|
||||||
|
StorageResponseDto,
|
||||||
|
StorageResponseWithItemCountDto,
|
||||||
|
} from './dto/storage-response.dto';
|
||||||
import {
|
import {
|
||||||
StorageSetCreateRequestDto,
|
StorageSetCreateRequestDto,
|
||||||
StorageSetUpdateRequestDto,
|
StorageSetUpdateRequestDto,
|
||||||
@ -76,10 +80,10 @@ export class AppStorageController {
|
|||||||
@Get('storages/:storageId')
|
@Get('storages/:storageId')
|
||||||
@ApiParam({ name: 'storageId', description: 'Storage ID' })
|
@ApiParam({ name: 'storageId', description: 'Storage ID' })
|
||||||
@ApiOperation({ summary: 'Get storage by ID' })
|
@ApiOperation({ summary: 'Get storage by ID' })
|
||||||
@ApiOkResponse({ type: StorageResponseDto })
|
@ApiOkResponse({ type: StorageResponseWithItemCountDto })
|
||||||
async getStorage(
|
async getStorage(
|
||||||
@CurrentStorage() storage: Storage,
|
@CurrentStorage() storage: Storage,
|
||||||
): Promise<StorageResponseDto> {
|
): Promise<StorageResponseWithItemCountDto> {
|
||||||
return this.service.getStorageWithItems(storage);
|
return this.service.getStorageWithItems(storage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,8 +151,11 @@ export class AppStorageController {
|
|||||||
@ApiParam({ name: 'roomId', description: 'Room ID' })
|
@ApiParam({ name: 'roomId', description: 'Room ID' })
|
||||||
@ApiOperation({ summary: 'Get storages in room' })
|
@ApiOperation({ summary: 'Get storages in room' })
|
||||||
@ApiOkResponse({ type: StorageResponseDto, isArray: true })
|
@ApiOkResponse({ type: StorageResponseDto, isArray: true })
|
||||||
async getStorages(@CurrentRoom() room: Room) {
|
async getStorages(
|
||||||
return this.service.getStoragesInRoom(room.id);
|
@CurrentRoom() room: Room,
|
||||||
|
@Query() { includeWithSets }: StorageWithSetsQueryDto,
|
||||||
|
) {
|
||||||
|
return this.service.getStoragesInRoom(room.id, includeWithSets);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get('set/room/:roomId')
|
@Get('set/room/:roomId')
|
||||||
|
@ -39,6 +39,7 @@ import {
|
|||||||
import {
|
import {
|
||||||
StorageActorResponse,
|
StorageActorResponse,
|
||||||
StorageResponseDto,
|
StorageResponseDto,
|
||||||
|
StorageResponseWithItemCountDto,
|
||||||
} from './dto/storage-response.dto';
|
} from './dto/storage-response.dto';
|
||||||
import {
|
import {
|
||||||
StorageSetCreateRequestDto,
|
StorageSetCreateRequestDto,
|
||||||
@ -53,8 +54,11 @@ export class AppStorageService {
|
|||||||
private readonly storageService: StorageService,
|
private readonly storageService: StorageService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async getStoragesInRoom(roomId: number) {
|
async getStoragesInRoom(roomId: number, includeWithSets = false) {
|
||||||
const storages = await this.storageService.getStoragesInRoom(roomId);
|
const storages = await this.storageService.getStoragesInRoom(
|
||||||
|
roomId,
|
||||||
|
includeWithSets,
|
||||||
|
);
|
||||||
return storages.map((storage) => this.formatStorageNoItems(storage));
|
return storages.map((storage) => this.formatStorageNoItems(storage));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -358,13 +362,13 @@ export class AppStorageService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getStorageWithItems(storage: Storage) {
|
async getStorageWithItems(storage: Storage) {
|
||||||
storage = await this.storageService.getStorageById(storage.id, [
|
const withItemCount = await this.storageService.getStorageWithItemCount(
|
||||||
'items',
|
storage.id,
|
||||||
'items.addedBy',
|
);
|
||||||
'items.item',
|
|
||||||
]);
|
|
||||||
|
|
||||||
return this.formatStorageWithItems(storage);
|
return this.formatStorageNoItems(
|
||||||
|
withItemCount,
|
||||||
|
) as StorageResponseWithItemCountDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
private formatActor(input: User): StorageActorResponse {
|
private formatActor(input: User): StorageActorResponse {
|
||||||
@ -373,7 +377,7 @@ export class AppStorageService {
|
|||||||
|
|
||||||
private formatStorageNoItems(storage: Storage): StorageResponseDto {
|
private formatStorageNoItems(storage: Storage): StorageResponseDto {
|
||||||
return {
|
return {
|
||||||
...omit(storage, ['room', 'set', 'items']),
|
...omit(storage, ['room', 'items']),
|
||||||
addedBy: storage.addedBy && this.formatActor(storage.addedBy),
|
addedBy: storage.addedBy && this.formatActor(storage.addedBy),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger';
|
import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger';
|
||||||
|
import { Transform } from 'class-transformer';
|
||||||
import {
|
import {
|
||||||
|
IsBoolean,
|
||||||
IsEnum,
|
IsEnum,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
IsString,
|
IsString,
|
||||||
@ -36,3 +38,11 @@ export class StorageCreateRequestDto {
|
|||||||
export class StorageUpdateRequestDto extends PartialType(
|
export class StorageUpdateRequestDto extends PartialType(
|
||||||
StorageCreateRequestDto,
|
StorageCreateRequestDto,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
export class StorageWithSetsQueryDto {
|
||||||
|
@ApiPropertyOptional()
|
||||||
|
@IsBoolean()
|
||||||
|
@IsOptional()
|
||||||
|
@Transform(({ value }) => value === 'true' || value === true)
|
||||||
|
includeWithSets?: boolean;
|
||||||
|
}
|
||||||
|
@ -1,4 +1,9 @@
|
|||||||
import { ApiPropertyOptional, OmitType, PickType } from '@nestjs/swagger';
|
import {
|
||||||
|
ApiProperty,
|
||||||
|
ApiPropertyOptional,
|
||||||
|
OmitType,
|
||||||
|
PickType,
|
||||||
|
} from '@nestjs/swagger';
|
||||||
import { Storage } from 'src/objects/storage/entities/storage.entity';
|
import { Storage } from 'src/objects/storage/entities/storage.entity';
|
||||||
import { User } from 'src/objects/user/user.entity';
|
import { User } from 'src/objects/user/user.entity';
|
||||||
import { StorageStoredItemResponseDto } from './storage-item-response.dto';
|
import { StorageStoredItemResponseDto } from './storage-item-response.dto';
|
||||||
@ -25,3 +30,16 @@ export class StorageResponseDto extends OmitType(Storage, [
|
|||||||
})
|
})
|
||||||
items?: StorageStoredItemResponseDto[];
|
items?: StorageStoredItemResponseDto[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class StorageResponseWithItemCountDto extends OmitType(Storage, [
|
||||||
|
'room',
|
||||||
|
'items',
|
||||||
|
'set',
|
||||||
|
'addedBy',
|
||||||
|
]) {
|
||||||
|
@ApiPropertyOptional({ type: StorageActorResponse })
|
||||||
|
addedBy: StorageActorResponse;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
itemCount: number;
|
||||||
|
}
|
||||||
|
@ -7,6 +7,9 @@ import { AppModule } from './app.module';
|
|||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create(AppModule);
|
const app = await NestFactory.create(AppModule);
|
||||||
const configService = app.get(ConfigService);
|
const configService = app.get(ConfigService);
|
||||||
|
app.enableCors({
|
||||||
|
origin: configService.get('FRONTEND_URL'),
|
||||||
|
});
|
||||||
const config = new DocumentBuilder()
|
const config = new DocumentBuilder()
|
||||||
.setTitle('Home Manager')
|
.setTitle('Home Manager')
|
||||||
.setDescription('Home manager API')
|
.setDescription('Home manager API')
|
||||||
|
@ -26,7 +26,7 @@ export class Floor {
|
|||||||
number: number;
|
number: number;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@Column()
|
@Column({ type: 'text' })
|
||||||
plan: string;
|
plan: string;
|
||||||
|
|
||||||
@ApiProperty({ type: () => Building })
|
@ApiProperty({ type: () => Building })
|
||||||
|
@ -29,7 +29,7 @@ export class Room {
|
|||||||
displayName: string;
|
displayName: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@Column()
|
@Column({ type: 'text' })
|
||||||
plan: string;
|
plan: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { ILike, Repository } from 'typeorm';
|
import { ILike, IsNull, Repository } from 'typeorm';
|
||||||
import { StoredItemTransaction } from './entities/item-transaction.entity';
|
import { StoredItemTransaction } from './entities/item-transaction.entity';
|
||||||
import { Item } from './entities/item.entity';
|
import { Item } from './entities/item.entity';
|
||||||
import { StorageSet } from './entities/storage-set.entity';
|
import { StorageSet } from './entities/storage-set.entity';
|
||||||
@ -31,6 +31,15 @@ export class StorageService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getStorageWithItemCount(storageId: number) {
|
||||||
|
return this.storageRepository
|
||||||
|
.createQueryBuilder('storage')
|
||||||
|
.leftJoinAndSelect('storage.addedBy', 'addedBy')
|
||||||
|
.loadRelationCountAndMap('storage.itemCount', 'storage.items')
|
||||||
|
.where('storage.id = :storageId', { storageId })
|
||||||
|
.getOne();
|
||||||
|
}
|
||||||
|
|
||||||
async getStorageByIdAndSub(id: number, sub: string, relations = []) {
|
async getStorageByIdAndSub(id: number, sub: string, relations = []) {
|
||||||
return this.storageRepository.findOne({
|
return this.storageRepository.findOne({
|
||||||
where: {
|
where: {
|
||||||
@ -127,26 +136,38 @@ export class StorageService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async getStoragesInRoom(roomId: number, relations = []) {
|
async getStoragesInRoom(roomId: number, includeWithSets = false) {
|
||||||
return this.storageRepository.find({
|
const qb = this.storageRepository
|
||||||
where: {
|
.createQueryBuilder('storage')
|
||||||
room: {
|
.leftJoin('storage.room', 'storage.room')
|
||||||
id: roomId,
|
.leftJoin('storage.set', 'storage.set')
|
||||||
},
|
.loadRelationCountAndMap('storage.itemCount', 'storage.items')
|
||||||
},
|
.where('storage.room.id = :roomId', { roomId });
|
||||||
relations,
|
|
||||||
});
|
if (!includeWithSets) {
|
||||||
|
qb.andWhere({ set: IsNull() });
|
||||||
}
|
}
|
||||||
|
|
||||||
async getStorageSetsInRoom(roomId: number, relations = []) {
|
qb.addSelect(
|
||||||
return this.storageSetRepository.find({
|
'CASE WHEN storage.set IS NULL THEN FALSE ELSE TRUE END',
|
||||||
where: {
|
'storage.hasSet',
|
||||||
room: {
|
);
|
||||||
id: roomId,
|
|
||||||
},
|
return qb.getMany();
|
||||||
},
|
}
|
||||||
relations: ['storages', 'storages.addedBy', ...relations],
|
|
||||||
});
|
async getStorageSetsInRoom(roomId: number) {
|
||||||
|
return this.storageSetRepository
|
||||||
|
.createQueryBuilder('storageSet')
|
||||||
|
.leftJoin('storageSet.room', 'storageSet.room')
|
||||||
|
.leftJoinAndMapMany(
|
||||||
|
'storageSet.storages',
|
||||||
|
'storageSet.storages',
|
||||||
|
'storages',
|
||||||
|
)
|
||||||
|
.loadRelationCountAndMap('storages.itemCount', 'storages.items')
|
||||||
|
.where('storageSet.room.id = :roomId', { roomId })
|
||||||
|
.getMany();
|
||||||
}
|
}
|
||||||
|
|
||||||
async getItemsInStorage(storageId: number, relations = []) {
|
async getItemsInStorage(storageId: number, relations = []) {
|
||||||
|
@ -34,7 +34,7 @@ export class User {
|
|||||||
@Column({ default: false })
|
@Column({ default: false })
|
||||||
emailVerified: boolean;
|
emailVerified: boolean;
|
||||||
|
|
||||||
@Column()
|
@Column({ type: 'text' })
|
||||||
@Exclude()
|
@Exclude()
|
||||||
password: string;
|
password: string;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user