Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { AssetCategory } from './asset-categories/asset-category.entity';
import { Department } from './departments/department.entity';
import { User } from './users/entities/user.entity';
import { FileUpload } from './file-uploads/entities/file-upload.entity';
import { Asset } from './assets/entities/assest.entity';
import { Asset } from './assets/entities/asset.entity';
import { Supplier } from './suppliers/entities/supplier.entity';
import { AssetCategoriesModule } from './asset-categories/asset-categories.module';
import { DepartmentsModule } from './departments/departments.module';
Expand Down
40 changes: 40 additions & 0 deletions backend/src/asset-categories/asset-category.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
Entity,
Column,
PrimaryGeneratedColumn,
CreateDateColumn,
UpdateDateColumn,
DeleteDateColumn,
Index,
} from 'typeorm';

@Entity('asset_categories')
@Index(['code'], { unique: true })
export class AssetCategory {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ length: 100, unique: true })
code: string;

@Column({ length: 200 })
name: string;

@Column({ type: 'text', nullable: true })
description: string;

@Column({ type: 'decimal', precision: 5, scale: 2, default: 0 })
depreciationRate: number;

@Column({ default: true })
isActive: boolean;

@CreateDateColumn({ name: 'created_at' })
createdAt: Date;

@UpdateDateColumn({ name: 'updated_at' })
updatedAt: Date;

@DeleteDateColumn({ name: 'deleted_at' })
deletedAt: Date;
}
14 changes: 8 additions & 6 deletions backend/src/assets/assets.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { AssetsService } from './assets.service';
import { AssetsController } from './assets.controller';
import { Asset } from './entities/asset.entity';
// import { AssetCategory } from '../asset-categories/asset-category.entity';
// import { Department } from '../departments/department.entity';
// import { User } from '../users/entities/user.entity';
import { AssetCategory } from '../asset-categories/asset-category.entity';
import { Department } from '../departments/department.entity';
import { Location } from '../locations/location.entity';
import { User } from '../users/entities/user.entity';

@Module({
imports: [
TypeOrmModule.forFeature([
Asset,
// AssetCategory,
// Department,
// User,
AssetCategory,
Department,
Location,
User,
]),
],
controllers: [AssetsController],
Expand Down
37 changes: 31 additions & 6 deletions backend/src/assets/assets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Repository, DataSource } from 'typeorm';
import { Asset, AssetStatus } from './entities/asset.entity';
import { AssetCategory } from '../asset-categories/asset-category.entity';
import { Department } from '../departments/department.entity';
import { Location } from '../locations/location.entity';
import { User } from '../users/entities/user.entity';
import { CreateAssetDto, BulkCreateAssetDto } from './dto/create-asset.dto';
import {
Expand Down Expand Up @@ -38,6 +39,8 @@ export class AssetsService {
private readonly categoryRepository: Repository<AssetCategory>,
@InjectRepository(Department)
private readonly departmentRepository: Repository<Department>,
@InjectRepository(Location)
private readonly locationRepository: Repository<Location>,
@InjectRepository(User)
private readonly userRepository: Repository<User>,
private readonly dataSource: DataSource,
Expand Down Expand Up @@ -67,6 +70,18 @@ export class AssetsService {
);
}

let location = null;
if (createAssetDto.locationId) {
location = await this.locationRepository.findOne({
where: { id: createAssetDto.locationId },
});
if (!location) {
throw new NotFoundException(
`Location with ID ${createAssetDto.locationId} not found`,
);
}
}

let assignedUser = null;
if (createAssetDto.assignedToId) {
assignedUser = await this.userRepository.findOne({
Expand Down Expand Up @@ -123,7 +138,7 @@ export class AssetsService {
assetId,
category,
department,
location: createAssetDto.location,
location,
assignedTo: assignedUser,
currentValue,
createdBy: creatingUser,
Expand Down Expand Up @@ -154,7 +169,7 @@ export class AssetsService {
sortOrder = 'DESC',
categoryId,
departmentId,
location,
locationId,
assignedToId,
status,
condition,
Expand All @@ -169,6 +184,7 @@ export class AssetsService {
.createQueryBuilder('asset')
.leftJoinAndSelect('asset.category', 'category')
.leftJoinAndSelect('asset.department', 'department')
.leftJoinAndSelect('asset.location', 'location')
.leftJoinAndSelect('asset.assignedTo', 'assignedTo')
.leftJoinAndSelect('asset.createdBy', 'createdBy')
.leftJoinAndSelect('asset.updatedBy', 'updatedBy');
Expand All @@ -188,10 +204,8 @@ export class AssetsService {
queryBuilder.andWhere('asset.departmentId = :departmentId', { departmentId });
}

if (location) {
queryBuilder.andWhere('asset.location ILIKE :location', {
location: `%${location}%`,
});
if (locationId) {
queryBuilder.andWhere('asset.locationId = :locationId', { locationId });
}

if (assignedToId) {
Expand Down Expand Up @@ -302,6 +316,17 @@ export class AssetsService {
}
}

if (updateAssetDto.locationId) {
const location = await this.locationRepository.findOne({
where: { id: updateAssetDto.locationId },
});
if (!location) {
throw new NotFoundException(
`Location with ID ${updateAssetDto.locationId} not found`,
);
}
}

if (updateAssetDto.assignedToId) {
const assignedUser = await this.userRepository.findOne({
where: { id: updateAssetDto.assignedToId },
Expand Down
4 changes: 2 additions & 2 deletions backend/src/assets/dto/asset-query.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export class AssetQueryDto {
departmentId?: string;

@IsOptional()
@IsString()
location?: string;
@IsUUID()
locationId?: string;

@IsOptional()
@IsUUID()
Expand Down
5 changes: 2 additions & 3 deletions backend/src/assets/dto/create-asset.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ export class CreateAssetDto {
departmentId: string;

@IsOptional()
@IsString()
@MaxLength(200)
location?: string;
@IsUUID()
locationId?: string;

@IsOptional()
@IsUUID()
Expand Down
6 changes: 4 additions & 2 deletions backend/src/assets/entities/asset.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from 'typeorm';
import { AssetCategory } from '../../asset-categories/asset-category.entity';
import { Department } from '../../departments/department.entity';
import { Location } from '../../locations/location.entity';
import { User } from '../../users/entities/user.entity';

export enum AssetStatus {
Expand Down Expand Up @@ -96,8 +97,9 @@ export class Asset {
@JoinColumn({ name: 'department_id' })
department: Department;

@Column({ name: 'location', length: 200, nullable: true })
location: string;
@ManyToOne(() => Location, { eager: true, nullable: true })
@JoinColumn({ name: 'location_id' })
location: Location;

@ManyToOne(() => User, { nullable: true, eager: true })
@JoinColumn({ name: 'assigned_to_id' })
Expand Down
37 changes: 37 additions & 0 deletions backend/src/departments/department.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
Entity,
Column,
PrimaryGeneratedColumn,
CreateDateColumn,
UpdateDateColumn,
DeleteDateColumn,
Index,
} from 'typeorm';

@Entity('departments')
@Index(['code'], { unique: true })
export class Department {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ length: 100, unique: true })
code: string;

@Column({ length: 200 })
name: string;

@Column({ type: 'text', nullable: true })
description: string;

@Column({ default: true })
isActive: boolean;

@CreateDateColumn({ name: 'created_at' })
createdAt: Date;

@UpdateDateColumn({ name: 'updated_at' })
updatedAt: Date;

@DeleteDateColumn({ name: 'deleted_at' })
deletedAt: Date;
}
43 changes: 43 additions & 0 deletions backend/src/locations/location.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
Entity,
Column,
PrimaryGeneratedColumn,
CreateDateColumn,
UpdateDateColumn,
DeleteDateColumn,
Index,
} from 'typeorm';

@Entity('locations')
@Index(['code'], { unique: true })
export class Location {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ length: 100, unique: true })
code: string;

@Column({ length: 200 })
name: string;

@Column({ type: 'text', nullable: true })
description: string;

@Column({ length: 100, nullable: true })
building: string;

@Column({ length: 100, nullable: true })
floor: string;

@Column({ default: true })
isActive: boolean;

@CreateDateColumn({ name: 'created_at' })
createdAt: Date;

@UpdateDateColumn({ name: 'updated_at' })
updatedAt: Date;

@DeleteDateColumn({ name: 'deleted_at' })
deletedAt: Date;
}
41 changes: 41 additions & 0 deletions backend/src/users/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
Entity,
Column,
PrimaryGeneratedColumn,
CreateDateColumn,
UpdateDateColumn,
DeleteDateColumn,
} from 'typeorm';

@Entity('users')
export class User {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ length: 100, unique: true })
username: string;

@Column({ length: 255, unique: true })
email: string;

@Column({ length: 100 })
firstName: string;

@Column({ length: 100 })
lastName: string;

@Column({ select: false, nullable: true })
password: string;

@Column({ default: true })
isActive: boolean;

@CreateDateColumn({ name: 'created_at' })
createdAt: Date;

@UpdateDateColumn({ name: 'updated_at' })
updatedAt: Date;

@DeleteDateColumn({ name: 'deleted_at' })
deletedAt: Date;
}
Loading