search stored items

This commit is contained in:
Evert Prants 2023-01-24 22:37:45 +02:00
parent 8ba00eb9e2
commit 69e0bac7b3
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
5 changed files with 98 additions and 16 deletions

View File

@ -149,17 +149,6 @@ export class AppStorageController {
return this.service.removeFromSet(set, storage); return this.service.removeFromSet(set, storage);
} }
@Get('room/:roomId')
@ApiParam({ name: 'roomId', description: 'Room ID' })
@ApiOperation({ summary: 'Get storages in room' })
@ApiOkResponse({ type: StorageResponseDto, isArray: true })
async getStorages(
@CurrentRoom() room: Room,
@Query() { includeWithSets }: StorageWithSetsQueryDto,
) {
return this.service.getStoragesInRoom(room.id, includeWithSets);
}
@Get('set/room/:roomId') @Get('set/room/:roomId')
@ApiParam({ name: 'roomId', description: 'Room ID' }) @ApiParam({ name: 'roomId', description: 'Room ID' })
@ApiOperation({ summary: 'Get storage sets in room' }) @ApiOperation({ summary: 'Get storage sets in room' })
@ -181,6 +170,17 @@ export class AppStorageController {
return this.service.createStorageSet(user, room, body); return this.service.createStorageSet(user, room, body);
} }
@Get('room/:roomId')
@ApiParam({ name: 'roomId', description: 'Room ID' })
@ApiOperation({ summary: 'Get storages in room' })
@ApiOkResponse({ type: StorageResponseDto, isArray: true })
async getStorages(
@CurrentRoom() room: Room,
@Query() { includeWithSets }: StorageWithSetsQueryDto,
) {
return this.service.getStoragesInRoom(room.id, includeWithSets);
}
@Post('room/:roomId') @Post('room/:roomId')
@ApiParam({ name: 'roomId', description: 'Room ID' }) @ApiParam({ name: 'roomId', description: 'Room ID' })
@ApiBody({ type: StorageCreateRequestDto }) @ApiBody({ type: StorageCreateRequestDto })
@ -203,7 +203,33 @@ export class AppStorageController {
return this.service.getExpiringOrExpiredItems(user); return this.service.getExpiringOrExpiredItems(user);
} }
@Get('expiring/building/:buildingId') @Get('search')
@ApiOperation({
summary: 'Search for stored items',
})
@ApiOkResponse({ type: StorageStoredItemResponseDto, isArray: true })
async searchForStoredItems(
@LoggedInUser() user: User,
@Query() query: StorageItemRequestQueryDto,
) {
return this.service.searchForStoredItems(query, user);
}
@Get('building/:buildingId/search')
@ApiParam({ name: 'buildingId', description: 'Building ID' })
@ApiOperation({
summary: 'Search for stored items in building',
})
@ApiOkResponse({ type: StorageStoredItemResponseDto, isArray: true })
async searchForStoredItemsInBuilding(
@LoggedInUser() user: User,
@CurrentBuilding() building: Building,
@Query() query: StorageItemRequestQueryDto,
) {
return this.service.searchForStoredItems(query, user, building.id);
}
@Get('building/:buildingId/expiring')
@ApiParam({ name: 'buildingId', description: 'Building ID' }) @ApiParam({ name: 'buildingId', description: 'Building ID' })
@ApiOperation({ summary: 'Get expiring and expired items in building' }) @ApiOperation({ summary: 'Get expiring and expired items in building' })
@ApiOkResponse({ type: StorageStoredItemResponseDto, isArray: true }) @ApiOkResponse({ type: StorageStoredItemResponseDto, isArray: true })

View File

@ -159,6 +159,21 @@ export class AppStorageService {
); );
} }
async searchForStoredItems(
search: StorageItemRequestQueryDto,
user: User,
buildingId?: number,
) {
const foundItems = await this.storageService.searchForStoredItem(
search,
user.sub,
buildingId,
);
return foundItems.map((storedItem) =>
this.formatStoredItem(storedItem, true),
);
}
async createStoredItem( async createStoredItem(
user: User, user: User,
item: Item, item: Item,

View File

@ -1,14 +1,16 @@
import { ApiPropertyOptional } from '@nestjs/swagger'; import { ApiPropertyOptional } from '@nestjs/swagger';
import { IsString, ValidateIf } from 'class-validator'; import { IsString, MinLength, ValidateIf } from 'class-validator';
export class StorageItemRequestQueryDto { export class StorageItemRequestQueryDto {
@ApiPropertyOptional() @ApiPropertyOptional()
@IsString() @IsString()
@MinLength(2)
@ValidateIf((obj) => !obj.barcode) @ValidateIf((obj) => !obj.barcode)
searchTerm?: string; searchTerm?: string;
@ApiPropertyOptional() @ApiPropertyOptional()
@IsString() @IsString()
@MinLength(2)
@ValidateIf((obj) => !obj.searchTerm) @ValidateIf((obj) => !obj.searchTerm)
barcode?: string; barcode?: string;
} }

View File

@ -62,15 +62,15 @@ export class StoredItem {
transactions?: StoredItemTransaction[]; transactions?: StoredItemTransaction[];
@ApiPropertyOptional() @ApiPropertyOptional()
@Column({ nullable: true, type: 'datetime' }) @Column({ nullable: true, type: 'datetime', default: null })
expiresAt?: Date; expiresAt?: Date;
@ApiPropertyOptional() @ApiPropertyOptional()
@Column({ nullable: true, type: 'datetime' }) @Column({ nullable: true, type: 'datetime', default: null })
acquiredAt?: Date; acquiredAt?: Date;
@ApiPropertyOptional() @ApiPropertyOptional()
@Column({ nullable: true, type: 'datetime' }) @Column({ nullable: true, type: 'datetime', default: null })
consumedAt?: Date; consumedAt?: Date;
@ApiProperty() @ApiProperty()

View File

@ -173,6 +173,7 @@ export class StorageService {
async getItemsInStorage(storageId: number, relations = []) { async getItemsInStorage(storageId: number, relations = []) {
return this.storedItemRepository.find({ return this.storedItemRepository.find({
where: { where: {
consumedAt: null,
storage: { storage: {
id: storageId, id: storageId,
}, },
@ -181,6 +182,42 @@ export class StorageService {
}); });
} }
async searchForStoredItem(
search: { searchTerm?: string; barcode?: string },
sub: string,
buildingId?: number,
) {
return this.storedItemRepository.find({
where: {
consumedAt: null,
item: {
[search.barcode ? 'barcode' : 'displayName']:
search.barcode || ILike(`%${search.searchTerm}%`),
},
...(buildingId
? {
building: { id: buildingId },
}
: {
building: {
groups: {
members: {
sub,
},
},
},
}),
},
take: 10,
relations: [
'item',
'storage',
'storage.room',
...(!buildingId ? ['building'] : []),
],
});
}
async searchForItem(searchTerm: string, sub: string) { async searchForItem(searchTerm: string, sub: string) {
const displayName = ILike(`%${searchTerm}%`); const displayName = ILike(`%${searchTerm}%`);
return this.itemRepository.find({ return this.itemRepository.find({
@ -283,6 +320,7 @@ export class StorageService {
// 8 days // 8 days
return this.storedItemRepository.find({ return this.storedItemRepository.find({
where: { where: {
consumedAt: null,
expiresAt: LessThanOrEqual(new Date(Date.now() + 691200000)), expiresAt: LessThanOrEqual(new Date(Date.now() + 691200000)),
building: { building: {
id: buildingId, id: buildingId,
@ -300,6 +338,7 @@ export class StorageService {
// 8 days // 8 days
return this.storedItemRepository.find({ return this.storedItemRepository.find({
where: { where: {
consumedAt: null,
expiresAt: LessThanOrEqual(new Date(Date.now() + 691200000)), expiresAt: LessThanOrEqual(new Date(Date.now() + 691200000)),
building: { building: {
groups: { groups: {