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
11 changes: 8 additions & 3 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import { AssetsModule } from './assets/assets.module';
import { QueueModule } from './queue/queue.module';
import { StorageModule } from './storage/storage.module';
import { CacheService } from './cache/cache.service';
import { ContractsModule } from './contracts/contracts.module';
import { LicensesModule } from './licenses/licenses.module';
import { PurchaseOrdersModule } from './purchase-orders/purchase-orders.module';
import { TasksModule } from './tasks/tasks.module';

@Module({
imports: [
// Global environment configuration provider
ConfigModule.forRoot({ isGlobal: true }),

// Asynchronous Database Configuration Management
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
Expand All @@ -40,7 +42,6 @@ import { CacheService } from './cache/cache.service';
inject: [ConfigService],
}),

// #878 [BE-05] Asynchronous Redis Cache Layer Registration
CacheModule.registerAsync({
isGlobal: true,
imports: [ConfigModule],
Expand Down Expand Up @@ -73,6 +74,10 @@ import { CacheService } from './cache/cache.service';
StorageModule,
UsersModule,
AuthModule,
ContractsModule,
LicensesModule,
PurchaseOrdersModule,
TasksModule,
],
controllers: [AppController],
providers: [
Expand Down
52 changes: 52 additions & 0 deletions backend/src/contracts/contracts.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Controller, Get, Post, Put, Delete, Param, Body, Query, Req, UseGuards, UseInterceptors, UploadedFile } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { FileInterceptor } from '@nestjs/platform-express';
import { ContractsService } from './contracts.service';
import { CreateContractDto } from './dtos/create-contract.dto';
import { UpdateContractDto } from './dtos/update-contract.dto';
import { ContractQueryDto } from './dtos/contract-query.dto';
import { StorageService } from '../storage/storage.service';

@Controller('contracts')
@UseGuards(AuthGuard('jwt'))
export class ContractsController {
constructor(
private readonly contractsService: ContractsService,
private readonly storageService: StorageService,
) {}

@Post()
async create(@Body() dto: CreateContractDto, @Req() req: any) {
return this.contractsService.create(dto, req.user?.id);
}

@Post('upload')
@UseInterceptors(FileInterceptor('file'))
async upload(@UploadedFile() file: Express.Multer.File, @Req() req: any) {
const key = `contracts/${Date.now()}-${file.originalname}`;
await this.storageService.upload(file, key);
const url = await this.storageService.getSignedUrl(key);
return { key, url };
}

@Get()
async findAll(@Query() query: ContractQueryDto) {
return this.contractsService.findAll(query);
}

@Get(':id')
async findOne(@Param('id') id: string) {
return this.contractsService.findById(id);
}

@Put(':id')
async update(@Param('id') id: string, @Body() dto: UpdateContractDto, @Req() req: any) {
return this.contractsService.update(id, dto, req.user?.id);
}

@Delete(':id')
async remove(@Param('id') id: string) {
await this.contractsService.remove(id);
return { message: 'Contract deleted successfully' };
}
}
14 changes: 14 additions & 0 deletions backend/src/contracts/contracts.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Contract } from './entities/contract.entity';
import { ContractsService } from './contracts.service';
import { ContractsController } from './contracts.controller';
import { StorageModule } from '../storage/storage.module';

@Module({
imports: [TypeOrmModule.forFeature([Contract]), StorageModule],
controllers: [ContractsController],
providers: [ContractsService],
exports: [ContractsService],
})
export class ContractsModule {}
73 changes: 73 additions & 0 deletions backend/src/contracts/contracts.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { Contract } from './entities/contract.entity';
import { CreateContractDto } from './dtos/create-contract.dto';
import { UpdateContractDto } from './dtos/update-contract.dto';
import { ContractQueryDto } from './dtos/contract-query.dto';

@Injectable()
export class ContractsService {
private nextNumber: number;

constructor(
@InjectRepository(Contract)
private readonly contractRepository: Repository<Contract>,
private readonly configService: ConfigService,
) {
this.nextNumber = parseInt(configService.get<string>('CONTRACT_ID_START', '500'), 10);
}

async create(dto: CreateContractDto, userId?: string): Promise<Contract> {
const prefix = this.configService.get<string>('CONTRACT_ID_PREFIX', 'CTR');
const contractId = `${prefix}-${this.nextNumber++}`;
const contract = this.contractRepository.create({
...dto,
contractId,
createdById: userId,
});
return this.contractRepository.save(contract);
}

async findAll(query: ContractQueryDto): Promise<{ data: Contract[]; total: number }> {
const { search, status, vendor, assignedToId, page, limit } = query;
const qb = this.contractRepository.createQueryBuilder('contract')
.leftJoinAndSelect('contract.createdBy', 'createdBy')
.leftJoinAndSelect('contract.assignedTo', 'assignedTo');

if (search) {
qb.andWhere(
'(contract.title ILIKE :search OR contract.vendor ILIKE :search OR contract.contractId ILIKE :search)',
{ search: `%${search}%` },
);
}
if (status) qb.andWhere('contract.status = :status', { status });
if (vendor) qb.andWhere('contract.vendor ILIKE :vendor', { vendor: `%${vendor}%` });
if (assignedToId) qb.andWhere('contract.assignedToId = :assignedToId', { assignedToId });

qb.orderBy('contract.createdAt', 'DESC');
qb.skip((page - 1) * limit).take(limit);
return qb.getManyAndCount().then(([data, total]) => ({ data, total }));
}

async findById(id: string): Promise<Contract> {
const contract = await this.contractRepository.findOne({
where: { id },
relations: ['createdBy', 'assignedTo'],
});
if (!contract) throw new NotFoundException('Contract not found');
return contract;
}

async update(id: string, dto: UpdateContractDto, userId?: string): Promise<Contract> {
const contract = await this.findById(id);
Object.assign(contract, dto);
return this.contractRepository.save(contract);
}

async remove(id: string): Promise<void> {
const contract = await this.findById(id);
await this.contractRepository.softDelete(contract.id);
}
}
33 changes: 33 additions & 0 deletions backend/src/contracts/dtos/contract-query.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { IsOptional, IsString, IsInt, Min, Max } from 'class-validator';
import { Type } from 'class-transformer';

export class ContractQueryDto {
@IsOptional()
@IsString()
search?: string;

@IsOptional()
@IsString()
status?: string;

@IsOptional()
@IsString()
vendor?: string;

@IsOptional()
@IsString()
assignedToId?: string;

@IsOptional()
@Type(() => Number)
@IsInt()
@Min(1)
page?: number = 1;

@IsOptional()
@Type(() => Number)
@IsInt()
@Min(1)
@Max(100)
limit?: number = 20;
}
37 changes: 37 additions & 0 deletions backend/src/contracts/dtos/create-contract.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { IsString, IsOptional, IsNumber } from 'class-validator';

export class CreateContractDto {
@IsString()
title: string;

@IsString()
vendor: string;

@IsOptional()
@IsString()
startDate?: string;

@IsOptional()
@IsString()
endDate?: string;

@IsOptional()
@IsNumber()
value?: number;

@IsOptional()
@IsString()
status?: string;

@IsOptional()
@IsString()
description?: string;

@IsOptional()
@IsString()
documentUrl?: string;

@IsOptional()
@IsString()
assignedToId?: string;
}
39 changes: 39 additions & 0 deletions backend/src/contracts/dtos/update-contract.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { IsString, IsOptional, IsNumber } from 'class-validator';

export class UpdateContractDto {
@IsOptional()
@IsString()
title?: string;

@IsOptional()
@IsString()
vendor?: string;

@IsOptional()
@IsString()
startDate?: string;

@IsOptional()
@IsString()
endDate?: string;

@IsOptional()
@IsNumber()
value?: number;

@IsOptional()
@IsString()
status?: string;

@IsOptional()
@IsString()
description?: string;

@IsOptional()
@IsString()
documentUrl?: string;

@IsOptional()
@IsString()
assignedToId?: string;
}
55 changes: 55 additions & 0 deletions backend/src/contracts/entities/contract.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne, JoinColumn } from 'typeorm';
import { User } from '../../users/entities/user.entity';

@Entity('contracts')
export class Contract {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ unique: true })
contractId: string;

@Column()
title: string;

@Column()
vendor: string;

@Column({ type: 'date', nullable: true })
startDate: string;

@Column({ type: 'date', nullable: true })
endDate: string;

@Column({ type: 'decimal', precision: 12, scale: 2, nullable: true })
value: number;

@Column({ default: 'DRAFT' })
status: string;

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

@Column({ nullable: true })
documentUrl: string;

@Column({ nullable: true })
createdById: string;

@ManyToOne(() => User, { nullable: true })
@JoinColumn({ name: 'createdById' })
createdBy: User;

@Column({ nullable: true })
assignedToId: string;

@ManyToOne(() => User, { nullable: true })
@JoinColumn({ name: 'assignedToId' })
assignedTo: User;

@CreateDateColumn()
createdAt: Date;

@UpdateDateColumn()
updatedAt: Date;
}
46 changes: 46 additions & 0 deletions backend/src/licenses/dtos/create-license.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { IsString, IsOptional, IsNumber, IsInt } from 'class-validator';

export class CreateLicenseDto {
@IsString()
name: string;

@IsOptional()
@IsString()
softwareName?: string;

@IsOptional()
@IsString()
vendor?: string;

@IsOptional()
@IsInt()
totalSeats?: number;

@IsOptional()
@IsInt()
usedSeats?: number;

@IsOptional()
@IsString()
purchaseDate?: string;

@IsOptional()
@IsString()
expiryDate?: string;

@IsOptional()
@IsNumber()
cost?: number;

@IsOptional()
@IsString()
status?: string;

@IsOptional()
@IsString()
notes?: string;

@IsOptional()
@IsString()
assignedToId?: string;
}
Loading
Loading