249 lines
6.0 KiB
TypeScript
249 lines
6.0 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { ILike, Repository } from 'typeorm';
|
|
import { BuildingService } from '../building/building.service';
|
|
import { GroupService } from '../group/group.service';
|
|
import { UserService } from '../user/user.service';
|
|
import { StoredItemTransaction } from './entities/item-transaction.entity';
|
|
import { Item } from './entities/item.entity';
|
|
import { StorageSet } from './entities/storage-set.entity';
|
|
import { Storage } from './entities/storage.entity';
|
|
import { StoredItem } from './entities/stored-item.entity';
|
|
|
|
@Injectable()
|
|
export class StorageService {
|
|
constructor(
|
|
@InjectRepository(Item)
|
|
private readonly itemRepository: Repository<Item>,
|
|
@InjectRepository(Storage)
|
|
private readonly storageRepository: Repository<Storage>,
|
|
@InjectRepository(StorageSet)
|
|
private readonly storageSetRepository: Repository<StorageSet>,
|
|
@InjectRepository(StoredItem)
|
|
private readonly storedItemRepository: Repository<StoredItem>,
|
|
@InjectRepository(StoredItemTransaction)
|
|
private readonly transactionRepository: Repository<StoredItemTransaction>,
|
|
private readonly groupService: GroupService,
|
|
private readonly userService: UserService,
|
|
private readonly buildingService: BuildingService,
|
|
) {}
|
|
|
|
async getStorageByIdAndSub(id: number, sub: string, relations = []) {
|
|
return this.storageRepository.findOne({
|
|
where: {
|
|
id,
|
|
room: {
|
|
building: {
|
|
groups: {
|
|
members: {
|
|
sub,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
relations,
|
|
});
|
|
}
|
|
|
|
async getStorageByIdAndBuilding(
|
|
id: number,
|
|
buildingId: number,
|
|
relations = [],
|
|
) {
|
|
return this.storageRepository.findOne({
|
|
where: {
|
|
id,
|
|
room: {
|
|
building: {
|
|
id: buildingId,
|
|
},
|
|
},
|
|
},
|
|
relations,
|
|
});
|
|
}
|
|
|
|
async getStorageByIdAndRoom(id: number, roomId: number, relations = []) {
|
|
return this.storageRepository.findOne({
|
|
where: {
|
|
id,
|
|
room: {
|
|
id: roomId,
|
|
},
|
|
},
|
|
relations,
|
|
});
|
|
}
|
|
|
|
async getStorageSetByIdAndSub(id: number, sub: string, relations = []) {
|
|
return this.storageSetRepository.findOne({
|
|
where: {
|
|
id,
|
|
room: {
|
|
building: {
|
|
groups: {
|
|
members: {
|
|
sub,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
relations: ['storages', 'storages.addedBy', ...relations],
|
|
});
|
|
}
|
|
|
|
async getStorageSetByIdAndBuilding(
|
|
id: number,
|
|
buildingId: number,
|
|
relations = [],
|
|
) {
|
|
return this.storageSetRepository.findOne({
|
|
where: {
|
|
id,
|
|
room: {
|
|
building: {
|
|
id: buildingId,
|
|
},
|
|
},
|
|
},
|
|
relations: ['storages', 'storages.addedBy', ...relations],
|
|
});
|
|
}
|
|
|
|
async getStorageSetByIdAndRoom(id: number, roomId: number, relations = []) {
|
|
return this.storageSetRepository.findOne({
|
|
where: {
|
|
id,
|
|
room: {
|
|
id: roomId,
|
|
},
|
|
},
|
|
relations: ['storages', 'storages.addedBy', ...relations],
|
|
});
|
|
}
|
|
|
|
async getStoragesInRoom(roomId: number, relations = []) {
|
|
return this.storageRepository.find({
|
|
where: {
|
|
room: {
|
|
id: roomId,
|
|
},
|
|
},
|
|
relations,
|
|
});
|
|
}
|
|
|
|
async getStorageSetsInRoom(roomId: number, relations = []) {
|
|
return this.storageSetRepository.find({
|
|
where: {
|
|
room: {
|
|
id: roomId,
|
|
},
|
|
},
|
|
relations: ['storages', 'storages.addedBy', ...relations],
|
|
});
|
|
}
|
|
|
|
async getItemsInStorage(storageId: number, relations = []) {
|
|
return this.storedItemRepository.find({
|
|
where: {
|
|
storage: {
|
|
id: storageId,
|
|
},
|
|
},
|
|
relations: ['item', ...relations],
|
|
});
|
|
}
|
|
|
|
async searchForItem(searchTerm: string, sub: string) {
|
|
const displayName = ILike(`%${searchTerm}%`);
|
|
return this.itemRepository.find({
|
|
where: [
|
|
{
|
|
displayName,
|
|
addedBy: {
|
|
sub,
|
|
},
|
|
},
|
|
{
|
|
displayName,
|
|
public: true,
|
|
},
|
|
{
|
|
displayName,
|
|
addedBy: {
|
|
groups: {
|
|
members: {
|
|
sub,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
],
|
|
take: 10,
|
|
select: ['id', 'displayName', 'type', 'barcode', 'image', 'createdAt'],
|
|
});
|
|
}
|
|
|
|
async searchForItemByBarcode(barcode: string, sub: string) {
|
|
return this.itemRepository.find({
|
|
where: [
|
|
{
|
|
barcode,
|
|
addedBy: {
|
|
sub,
|
|
},
|
|
},
|
|
{
|
|
barcode,
|
|
public: true,
|
|
},
|
|
{
|
|
barcode,
|
|
addedBy: {
|
|
groups: {
|
|
members: {
|
|
sub,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
],
|
|
take: 10,
|
|
select: ['id', 'displayName', 'type', 'barcode', 'image', 'createdAt'],
|
|
});
|
|
}
|
|
|
|
async saveStorage(data: Partial<Storage>) {
|
|
const newStorage = new Storage();
|
|
Object.assign(newStorage, data);
|
|
return this.storageRepository.save(newStorage);
|
|
}
|
|
|
|
async saveItem(data: Partial<Item>) {
|
|
const newItem = new Item();
|
|
Object.assign(newItem, data);
|
|
return this.itemRepository.save(newItem);
|
|
}
|
|
|
|
async saveStoredItem(data: Partial<StoredItem>) {
|
|
const newStoredItem = new StoredItem();
|
|
Object.assign(newStoredItem, data);
|
|
return this.storedItemRepository.save(newStoredItem);
|
|
}
|
|
|
|
async saveStorageSet(data: Partial<StorageSet>) {
|
|
const newStorageSet = new StorageSet();
|
|
Object.assign(newStorageSet, data);
|
|
return this.storageSetRepository.save(newStorageSet);
|
|
}
|
|
|
|
async saveStoredItemTransaction(data: Partial<StoredItemTransaction>) {
|
|
const newStoredItemTransaction = new StoredItemTransaction();
|
|
Object.assign(newStoredItemTransaction, data);
|
|
return this.transactionRepository.save(newStoredItemTransaction);
|
|
}
|
|
}
|