Create registers in DB
This commit is contained in:
parent
fbac3b1f90
commit
f7b18b05f1
@ -1,8 +1,9 @@
|
||||
import { Controller, Delete, Get, InternalServerErrorException, Param } from "@nestjs/common";
|
||||
import { Body, Controller, Delete, Get, InternalServerErrorException, Param, Post } from "@nestjs/common";
|
||||
import { renderToString } from "solid-js/web";
|
||||
import { Certs } from "../../views/Certs";
|
||||
import { template } from "./certificate.template";
|
||||
import { CertificateService } from "./certificate.service";
|
||||
import { CertificateDto } from "./certificate.dto";
|
||||
|
||||
@Controller("certificate")
|
||||
export class CertificateController {
|
||||
@ -21,6 +22,12 @@ export class CertificateController {
|
||||
}
|
||||
|
||||
|
||||
@Post()
|
||||
async create(@Body() subjectDto: CertificateDto) {
|
||||
await this.certificateService.create(subjectDto);
|
||||
}
|
||||
|
||||
|
||||
@Delete(":id")
|
||||
async delete(@Param() param: {id: number}) {
|
||||
console.log(param);
|
||||
|
5
src/controller/certificate/certificate.dto.ts
Normal file
5
src/controller/certificate/certificate.dto.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export class CertificateDto {
|
||||
personId: number;
|
||||
subjectId: number;
|
||||
date: string;
|
||||
}
|
@ -1,15 +1,23 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { BadRequestException, Injectable, InternalServerErrorException } from "@nestjs/common";
|
||||
import { DataSource, Repository } from "typeorm";
|
||||
import { RegistroGIE } from "../../model/RegistroGIE/registroGIE.entity";
|
||||
import { RegisterReturn } from "../../types/RegisterReturn";
|
||||
import { CertificateDto } from "./certificate.dto";
|
||||
import { Persona } from "../../model/Persona/persona.entity";
|
||||
import { CursoGIE } from "../../model/CursoGIE/cursoGIE.entity";
|
||||
import { data } from "autoprefixer";
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class CertificateService {
|
||||
registroGIERepository: Repository<RegistroGIE>;
|
||||
personGIERepository: Repository<Persona>;
|
||||
subjectRepo: Repository<CursoGIE>;
|
||||
|
||||
constructor(private dataSource: DataSource) {
|
||||
this.registroGIERepository = dataSource.getRepository(RegistroGIE);
|
||||
this.personGIERepository = dataSource.getRepository(Persona);
|
||||
this.subjectRepo = dataSource.getRepository(CursoGIE);
|
||||
}
|
||||
|
||||
async getByDni(dni: string): Promise<Array<RegisterReturn>> {
|
||||
@ -31,6 +39,70 @@ export class CertificateService {
|
||||
}));
|
||||
}
|
||||
|
||||
async create(data: CertificateDto) {
|
||||
const certificate = new RegistroGIE();
|
||||
|
||||
// Get data for legacy fields
|
||||
const person = await this.personGIERepository.findOneBy({id: data.personId});
|
||||
const subject = await this.subjectRepo.findOneBy({id: data.subjectId});
|
||||
|
||||
if (person === null) {
|
||||
throw new BadRequestException("personId es invalido");
|
||||
}
|
||||
if (subject === null) {
|
||||
throw new BadRequestException("subjectId es invalido");
|
||||
}
|
||||
|
||||
certificate.dni = person.dni;
|
||||
// For some reason this is `last names` `names`
|
||||
certificate.nombre = `${person.apellidoPaterno} ${person.apellidoMaterno} ${person.nombres}`;
|
||||
certificate.curso = data.subjectId;
|
||||
certificate.codigo = await this.getNextRegisterCode(data.subjectId);
|
||||
certificate.fecha_actual = new Date();
|
||||
certificate.fecha_inscripcion = new Date(data.date);
|
||||
certificate.curso_nombre = subject.nombre;
|
||||
|
||||
certificate.persona = person;
|
||||
certificate.cursoGIE = subject;
|
||||
|
||||
try {
|
||||
await this.registroGIERepository.save(certificate);
|
||||
} catch (e) {
|
||||
throw new InternalServerErrorException("Error creando registro");
|
||||
}
|
||||
}
|
||||
|
||||
async getNextRegisterCode(subjectId: number): Promise<number> {
|
||||
|
||||
// Matpel 1, 2 & 3 share codes
|
||||
// TODO: Get actual ids from db instead of hard coding them?
|
||||
if (subjectId === 10 || subjectId === 11 || subjectId === 12) {
|
||||
const res = await this.registroGIERepository
|
||||
.createQueryBuilder("registro")
|
||||
.select("MAX(registro.codigo)")
|
||||
.where("registro.curso = :m1", {m1: 10})
|
||||
.orWhere("registro.curso = :m2", {m2: 11})
|
||||
.orWhere("registro.curso = :m3", {m3: 12})
|
||||
.getRawOne();
|
||||
|
||||
const {"MAX(`registro`.`codigo`)": nextRegisterCode} = res;
|
||||
|
||||
// TODO?: Make atomic to avoid race conditions?
|
||||
return nextRegisterCode + 1;
|
||||
} else {
|
||||
const res = await this.registroGIERepository
|
||||
.createQueryBuilder("registro")
|
||||
.select("MAX(registro.codigo)")
|
||||
.where("registro.curso = :subjectId", {subjectId})
|
||||
.getRawOne();
|
||||
|
||||
const {"MAX(`registro`.`codigo`)": nextRegisterCode} = res;
|
||||
|
||||
// TODO?: Make atomic to avoid race conditions?
|
||||
return nextRegisterCode + 1;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteById(id: number) {
|
||||
// TODO: Should do logical deletion
|
||||
const certificate = await this.registroGIERepository.findOneBy({
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Controller, Get } from "@nestjs/common";
|
||||
import { Body, Controller, Get, Param, Post } from "@nestjs/common";
|
||||
import { SubjectService } from "./subject.service";
|
||||
import { CertificateDto } from "../certificate/certificate.dto";
|
||||
|
||||
@Controller("subject")
|
||||
export class SubjectController {
|
||||
@ -10,4 +11,5 @@ export class SubjectController {
|
||||
async getAll() {
|
||||
return this.subjectService.getAll();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ export function Certs() {
|
||||
</h1>
|
||||
<Search setPerson={setPerson}/>
|
||||
<Registers person={person()} />
|
||||
<NewRegister />
|
||||
<NewRegister personId={person()?.id ?? -1} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user