Handout management
This commit is contained in:
20
api/src/modules/customer/customer.controller.ts
Normal file
20
api/src/modules/customer/customer.controller.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common';
|
||||
import { AuthGuard } from 'src/core/guards/auth.guard';
|
||||
import { AuthenticatedRequest } from 'src/model/interface/authenticated-request.interface';
|
||||
import { CustomerService } from './customer.service';
|
||||
|
||||
@Controller('customer')
|
||||
@UseGuards(AuthGuard)
|
||||
export class CustomerController {
|
||||
constructor(private service: CustomerService) {}
|
||||
|
||||
@Get()
|
||||
getCustomers(@Req() req: AuthenticatedRequest) {
|
||||
return this.service.getCustomers(req.user);
|
||||
}
|
||||
|
||||
@Post()
|
||||
createCustomer(@Req() req: AuthenticatedRequest, @Body() body: any) {
|
||||
return this.service.createCustomer(req.user, body);
|
||||
}
|
||||
}
|
||||
12
api/src/modules/customer/customer.module.ts
Normal file
12
api/src/modules/customer/customer.module.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { CustomerController } from './customer.controller';
|
||||
import { CustomerService } from './customer.service';
|
||||
import { DatabaseModule } from 'src/shared/database/database.module';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
|
||||
@Module({
|
||||
controllers: [CustomerController],
|
||||
providers: [CustomerService],
|
||||
imports: [DatabaseModule, AuthModule],
|
||||
})
|
||||
export class CustomerModule {}
|
||||
24
api/src/modules/customer/customer.service.ts
Normal file
24
api/src/modules/customer/customer.service.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { Customer } from 'src/model/entitites';
|
||||
import { IUser } from 'src/model/interface';
|
||||
import { CustomerRepository } from 'src/model/repositories';
|
||||
|
||||
@Injectable()
|
||||
export class CustomerService {
|
||||
constructor(private customerRepository: CustomerRepository) {}
|
||||
|
||||
createCustomer(user: IUser, data: any) {
|
||||
if (!user || !data.name || data.name.length == 0 || !data.system) {
|
||||
throw new HttpException('invalid', HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
return this.customerRepository.save(this.customerRepository.create(data));
|
||||
}
|
||||
|
||||
getCustomers(user: IUser): Promise<Customer[]> {
|
||||
return this.customerRepository.find({
|
||||
where: { system: { managers: { id: user.id } } },
|
||||
order: { name: { direction: 'ASC' } },
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
Put,
|
||||
Req,
|
||||
@@ -12,6 +13,7 @@ import { AuthenticatedRequest } from 'src/model/interface/authenticated-request.
|
||||
import { AuthGuard } from 'src/core/guards/auth.guard';
|
||||
import { Key } from 'src/model/entitites';
|
||||
import { CreateKeySystemDto } from 'src/model/dto/create-key-system.dto';
|
||||
import { HandoverKeyDto } from 'src/model/dto';
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
@Controller('key')
|
||||
@@ -34,7 +36,24 @@ export class KeyController {
|
||||
}
|
||||
|
||||
@Post('system')
|
||||
createKeySystem(@Req() req: AuthenticatedRequest, @Body() body: CreateKeySystemDto) {
|
||||
createKeySystem(
|
||||
@Req() req: AuthenticatedRequest,
|
||||
@Body() body: CreateKeySystemDto,
|
||||
) {
|
||||
return this.service.createKeySystem(req.user, body);
|
||||
}
|
||||
|
||||
@Post(':id/handover')
|
||||
handoutKey(
|
||||
@Req() req: AuthenticatedRequest,
|
||||
@Body() body: any,
|
||||
@Param('id') id: string,
|
||||
) {
|
||||
return this.service.handoverKey(req.user, body, id);
|
||||
}
|
||||
|
||||
@Get(':id/handover')
|
||||
getKeyHandouts(@Req() req: AuthenticatedRequest, @Param('id') id: string) {
|
||||
return this.service.getKeyHandovers(req.user, id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { CreateKeySystemDto } from 'src/model/dto';
|
||||
import { Cylinder, Key, User } from 'src/model/entitites';
|
||||
import { IUser } from 'src/model/interface';
|
||||
import {
|
||||
CylinderRepository,
|
||||
KeyActivityRepository,
|
||||
KeyRepository,
|
||||
KeySystemRepository,
|
||||
} from 'src/model/repositories';
|
||||
import { KeyHandoutRepository } from 'src/model/repositories/key-handout.repository';
|
||||
|
||||
@Injectable()
|
||||
export class KeyService {
|
||||
@@ -15,12 +17,13 @@ export class KeyService {
|
||||
private readonly cylinderRepository: CylinderRepository,
|
||||
private readonly systemRepo: KeySystemRepository,
|
||||
private activityRepo: KeyActivityRepository,
|
||||
private handoverRepo: KeyHandoutRepository,
|
||||
) {}
|
||||
|
||||
async getUsersKeys(user: User): Promise<Key[]> {
|
||||
return this.keyrepository.find({
|
||||
where: { cylinder: { system: { managers: { id: user.id } } } },
|
||||
relations: ['cylinder', 'cylinder.system'],
|
||||
relations: ['cylinder', 'cylinder.system', 'customer'],
|
||||
});
|
||||
}
|
||||
|
||||
@@ -74,4 +77,35 @@ export class KeyService {
|
||||
throw new HttpException(e.code, HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
}
|
||||
|
||||
async handoverKey(user: IUser, data: any, keyID: string) {
|
||||
const key: Key = await this.keyrepository.findOneOrFail({
|
||||
where: { id: keyID, cylinder: { system: { managers: { id: user.id } } } },
|
||||
});
|
||||
|
||||
key.handedOut = data.direction == 'out';
|
||||
this.keyrepository.save(key);
|
||||
|
||||
return this.handoverRepo.save(
|
||||
this.handoverRepo.create({
|
||||
customer: data.customer,
|
||||
direction: data.direction,
|
||||
timestamp: data.timestamp,
|
||||
key: key,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
getKeyHandovers(user: User, keyID: string) {
|
||||
return this.handoverRepo.find({
|
||||
where: {
|
||||
key: { cylinder: { system: { managers: { id: user.id } } }, id: keyID },
|
||||
},
|
||||
order: {
|
||||
timestamp: { direction: 'DESC' },
|
||||
created: { direction: 'DESC' },
|
||||
},
|
||||
relations: ['customer'],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { RoleController } from './role.controller';
|
||||
|
||||
describe('RoleController', () => {
|
||||
let controller: RoleController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [RoleController],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<RoleController>(RoleController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -1,18 +0,0 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { RoleService } from './role.service';
|
||||
|
||||
describe('RoleService', () => {
|
||||
let service: RoleService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [RoleService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<RoleService>(RoleService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user