Commit 2762c416 authored by mechtelm's avatar mechtelm
Browse files

Add principal relation and api

parent 4687e473
......@@ -6,12 +6,15 @@ import Config from "./entities/config.entity";
import {ConfigService} from "./services/config.service";
import Staff from "./entities/staff-member.entity";
import {StaffService} from "./services/staff.service";
import Principal from "./entities/principal.entity";
import {PrincipalService} from "./services/principal.service";
@Module({
imports: [
User,
Config,
Staff,
Principal,
TypeOrmModule.forRootAsync({
useFactory: () => ({
"type": "postgres",
......@@ -20,25 +23,28 @@ import {StaffService} from "./services/staff.service";
"username": process.env.POSTGRES_USER,
"password": process.env.POSTGRES_PASSWORD,
"database": process.env.POSTGRES_DB,
"entities": [User, Config, Staff],
"entities": [User, Config, Staff, Principal],
"synchronize": false
})
}
),
TypeOrmModule.forFeature([User, Config, Staff])
TypeOrmModule.forFeature([User, Config, Staff, Principal])
],
providers: [
UsersService,
StaffService,
ConfigService
ConfigService,
PrincipalService
],
exports: [
User,
Config,
Staff,
Principal,
UsersService,
StaffService,
ConfigService
ConfigService,
PrincipalService
]
})
export class DatabaseModule {}
import { Test, TestingModule } from '@nestjs/testing';
import { PrincipalService } from './principal.service';
describe('PrincipalService', () => {
let service: PrincipalService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PrincipalService],
}).compile();
service = module.get<PrincipalService>(PrincipalService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
import { Injectable } from '@nestjs/common';
import {InjectRepository} from "@nestjs/typeorm";
import {Repository} from "typeorm";
import Principal from "../entities/principal.entity";
import {PrincipalRelationDto} from "@lib/dto";
@Injectable()
export class PrincipalService {
constructor(
@InjectRepository(Principal)
private principalRepository: Repository<Principal>,
) {}
async findAllPrincipals(subId: number): Promise<PrincipalRelationDto[]> {
const principalData = await this.principalRepository.createQueryBuilder('principal')
.where(`principal.sub_id = ${subId}`)
.innerJoinAndSelect('staff', 'staff', 'principal.id = staff.id')
.select(['staff.id as id', 'staff.name as name', 'staff.first_name as first_name'])
.execute();
const returnPrincipals: PrincipalRelationDto[] = [];
principalData.forEach(principal => {
returnPrincipals.push(<PrincipalRelationDto>{
id: principal.id,
name: principal.name,
firstName: principal.first_name
})
});
return returnPrincipals;
}
async findAllSubs(id: number): Promise<PrincipalRelationDto[]> {
const principalData = await this.principalRepository.createQueryBuilder('principal')
.where(`principal.id = ${id}`)
.innerJoinAndSelect('staff', 'staff', 'principal.sub_id = staff.id')
.select(['staff.id as id', 'staff.name as name', 'staff.first_name as first_name'])
.execute();
const returnPrincipals: PrincipalRelationDto[] = [];
principalData.forEach(principal => {
returnPrincipals.push(<PrincipalRelationDto>{
id: principal.id,
name: principal.name,
firstName: principal.first_name
})
});
return returnPrincipals;
}
async create(id: number, subId: number): Promise<void> {
const principalToCreate = await this.principalRepository.findOne({
where: {id: id, subId: subId}
});
if (!principalToCreate) {
await this.principalRepository.createQueryBuilder()
.insert()
.values([
{ id: id, subId: subId }
])
.execute();
}
}
async remove(id: number, subId: number): Promise<void> {
await this.principalRepository.createQueryBuilder()
.where('subId = :subId', {subId})
.andWhere('id = :id', {id})
.delete()
.execute();
}
}
......@@ -12,7 +12,12 @@ export class StaffService {
) {}
async findAll(): Promise<StaffMemberInListDto[]> {
const staffMembers: Staff[] = await this.staffRepository.find({order: {name: 'ASC'}});
const staffMembers: Staff[] = await this.staffRepository.find({
order: {
name: 'ASC',
firstName: 'ASC'
}
});
const returnStaff: StaffMemberInListDto[] = [];
staffMembers.forEach(staffMember => {
returnStaff.push(<StaffMemberInListDto>{
......
import {Body, Controller, Get, Param, Patch, Post, UseGuards} from '@nestjs/common';
import {Body, Controller, Delete, Get, Param, Patch, Post, UseGuards} from '@nestjs/common';
import {JwtAuthGuard} from "../auth/jwt-auth.guard";
import {ApiBearerAuth, ApiCreatedResponse, ApiTags} from "@nestjs/swagger";
import {StaffMemberCreateDto, StaffMemberInListDto, StaffMemberMainDto} from "@lib/dto";
import {PrincipalRelationDto, StaffMemberCreateDto, StaffMemberInListDto, StaffMemberMainDto} from "@lib/dto";
import {StaffService} from "../database/services/staff.service";
import {PrincipalService} from "../database/services/principal.service";
@Controller('staff')
export class StaffController {
constructor(
private staffService: StaffService
private staffService: StaffService,
private principalService: PrincipalService
) {}
@Get()
......@@ -51,4 +53,42 @@ export class StaffController {
async create(@Body() staffMemberCreateDto: StaffMemberCreateDto) {
return this.staffService.create(staffMemberCreateDto)
}
@Get(':id/principals')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiCreatedResponse({
type: [PrincipalRelationDto],
})
@ApiTags('staff')
async findOnesPrincipals(@Param('id') id: number): Promise<PrincipalRelationDto[]> {
return this.principalService.findAllPrincipals(id);
}
@Get(':id/subs')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiCreatedResponse({
type: [PrincipalRelationDto],
})
@ApiTags('staff')
async findOnesSubs(@Param('id') id: number): Promise<PrincipalRelationDto[]> {
return this.principalService.findAllSubs(id);
}
@Post(':subId/principals/:id')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiTags('staff')
async createPrincipal(@Param('subId') subId: number, @Param('id') id: number) {
return this.principalService.create(id, subId);
}
@Delete(':subId/principals/:id')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiTags('staff')
async remove(@Param('subId') subId: number, @Param('id') id: number): Promise<void> {
return this.principalService.remove(id, subId);
}
}
export {StaffMemberCreateDto} from "./lib/staff/staff-member-create-dto";
export {UserInListDto} from './lib/user/user-in-list-dto';
export {UserFullDto} from './lib/user/user-full-dto';
export {StaffMemberMainDto} from "./lib/staff/staff-member-main-dto";
export {StaffMemberMainDto, PrincipalRelationDto} from "./lib/staff/staff-member-main-dto";
export {StaffMemberInListDto} from "./lib/staff/staff-member-in-list-dto";
export {StaffMemberVacationDataDto} from "./lib/staff/staff-member-vacation-data-dto";
export {AuthDataDto} from "./lib/auth-data-dto";
......
......@@ -43,3 +43,14 @@ export class StaffMemberMainDto {
@ApiProperty()
title?: string;
}
export class PrincipalRelationDto {
@ApiProperty()
id!: number;
@ApiProperty()
name!: string;
@ApiProperty()
firstName!: string;
}
......@@ -55,3 +55,17 @@ CREATE TABLE public.app_config
content jsonb,
PRIMARY KEY (key)
);
create table public.principal
(
id integer not null
constraint principal_staff_id_fk
references staff
on delete cascade,
sub_id integer not null
constraint principal_staff_id_fk_2
references staff
on delete cascade,
constraint principal_pk
unique (id, sub_id)
);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment