diff --git a/src/app.module.ts b/src/app.module.ts index 5a7d70d..0de352c 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -10,6 +10,7 @@ import { PersonService } from "./controller/person/person.service"; import { SubjectController } from "./controller/subject/subject.controller"; import { SubjectService } from "./controller/subject/subject.service"; import * as dotenv from "dotenv"; +import { MigratorController } from "./controller/migrator/migrator.controller"; // Must be done before initializing DB. console.log(dotenv.config()); @@ -31,7 +32,7 @@ console.log(dotenv.config()); synchronize: false, }), ], - controllers: [CertificateController, PersonController, SubjectController], + controllers: [CertificateController, PersonController, SubjectController, MigratorController], providers: [CertificateService, PersonService, SubjectService], }) export class AppModule {} diff --git a/src/controller/certificate/certificate.controller.ts b/src/controller/certificate/certificate.controller.ts index bbd570a..0c27112 100644 --- a/src/controller/certificate/certificate.controller.ts +++ b/src/controller/certificate/certificate.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Delete, Get, InternalServerErrorException, Param, Post, Res, StreamableFile } from "@nestjs/common"; +import { Body, Controller, Delete, Get, InternalServerErrorException, Param, Post, Res } from "@nestjs/common"; import { renderToString } from "solid-js/web"; import { Certs } from "../../views/Certs"; import { template } from "./certificate.template"; @@ -19,7 +19,7 @@ export class CertificateController { } @Get(":dni") - getByDni(@Param() param: {dni: string}) { + getByDni(@Param() param: { dni: string }) { return this.certificateService.getByDni(param.dni); } @@ -30,7 +30,7 @@ export class CertificateController { } @Post("generate/:id") - async generate(@Param() param: {id: string}, @Res({ passthrough: true }) res: Response) { + async generate(@Param() param: { id: string }, @Res({ passthrough: true }) res: Response) { const [stream, filename] = await this.certificateService.generate(parseInt(param.id, 10)); const str = res.writeHead(201, { @@ -42,7 +42,7 @@ export class CertificateController { } @Delete(":id") - async delete(@Param() param: {id: number}) { + async delete(@Param() param: { id: number }) { console.log(param); const result = await this.certificateService.deleteById(param.id); if (!result) { diff --git a/src/controller/migrator/migrator.controller.ts b/src/controller/migrator/migrator.controller.ts new file mode 100644 index 0000000..b1a7d8c --- /dev/null +++ b/src/controller/migrator/migrator.controller.ts @@ -0,0 +1,162 @@ +import { Controller, Get } from "@nestjs/common"; +import { CursoGIE } from "src/model/CursoGIE/cursoGIE.entity"; +import { Persona } from "src/model/Persona/persona.entity"; +import { RegistroGIE } from "src/model/RegistroGIE/registroGIE.entity"; +import { DataSource, Repository } from "typeorm"; +import { PersonService } from "../person/person.service"; + +@Controller("migrator") +export class MigratorController { + registroGIERepository: Repository; + personGIERepository: Repository; + subjectRepo: Repository; + + constructor(dataSource: DataSource, private personService: PersonService) { + this.registroGIERepository = dataSource.getRepository(RegistroGIE); + this.personGIERepository = dataSource.getRepository(Persona); + this.subjectRepo = dataSource.getRepository(CursoGIE); + } + + @Get() + async migrate() { + const registers = await this.registroGIERepository.find({ + // @ts-ignore + select: { + cursoGIE: null, + persona: null, + }, + }); + + // Try to create Persons for each user + for (const register of registers) { + const {curso, persona, cursoGIE, dni} = register; + + // If this register doesn't link to a Person + if (!persona) { + // Try to get the person by DNI + try { + await this.personService.getByDni(dni); + + // Update this register + register.persona = (await this.personGIERepository.findOneBy({dni}))!; + } catch (e) { + // Return creation form to the user + return ` + + +
+

1.1 Registrar persona manualmente

+

${register.nombre}

+
+ +
+ + +
+ +
+
+ +
+
+ +
+ +
+
+ +
+ +
+
+
+ + +
+ +
+ + +
+ + + `; + return `NOT FOUND IN DB NOR SUNAT: ${JSON.stringify(register)}`; + } + } + + // If this register doesn't link to a Curso + if (!cursoGIE) { + // Get Curso and update + const cursoGIE = await this.subjectRepo.findOneBy({id: curso}); + + register.cursoGIE = cursoGIE!; + } + + // Save person + if (!persona || !cursoGIE) { + await this.registroGIERepository.save(register); + } + } + + return "All registers updated (?) :D"; + } +} + diff --git a/src/controller/person/person.service.ts b/src/controller/person/person.service.ts index 788c4d4..4046ceb 100644 --- a/src/controller/person/person.service.ts +++ b/src/controller/person/person.service.ts @@ -15,7 +15,7 @@ interface SunatPerson { export class PersonService { personaRepository: Repository; - constructor(private dataSource: DataSource) { + constructor(dataSource: DataSource) { this.personaRepository = dataSource.getRepository(Persona); } diff --git a/src/model/RegistroGIE/registroGIE.entity.ts b/src/model/RegistroGIE/registroGIE.entity.ts index d7adf05..1298d66 100644 --- a/src/model/RegistroGIE/registroGIE.entity.ts +++ b/src/model/RegistroGIE/registroGIE.entity.ts @@ -36,9 +36,9 @@ export class RegistroGIE { /* New fields */ - @ManyToOne(() => Persona, (persona) => persona.registros) + @ManyToOne(() => Persona,(persona) => persona.registros, {nullable: true}) persona: Persona; - @ManyToOne(() => CursoGIE) + @ManyToOne(() => CursoGIE, {nullable: true}) cursoGIE: CursoGIE; } diff --git a/src/views/components/Registers.tsx b/src/views/components/Registers.tsx index c9affdb..04908f8 100644 --- a/src/views/components/Registers.tsx +++ b/src/views/components/Registers.tsx @@ -41,7 +41,7 @@ export function Registers(props: { person: Person | null, lastUpdate: number })
Nombres y Apellidos -
+
void}) { +function Register(props: { cert: RegisterReturn, onUpdate: () => void }) { const [deleteConfirmation, setDeleteConfirmation] = createSignal(false); const [deleteText, setDeleteText] = createSignal("Eliminar"); const deleteRegister = async() => { if (deleteConfirmation()) { // Actually delete from DB - const response = await fetch(`/certificate/${props.cert.id}`,{ + const response = await fetch(`/certificate/${props.cert.id}`, { method: "DELETE", }); @@ -113,7 +113,7 @@ function Register(props: {cert: RegisterReturn, onUpdate: () => void}) { }; const getCertificate = async() => { - const response = await fetch(`/certificate/generate/${props.cert.id}`,{ + const response = await fetch(`/certificate/generate/${props.cert.id}`, { method: "POST", }); @@ -145,7 +145,8 @@ function Register(props: {cert: RegisterReturn, onUpdate: () => void}) { return; } else { - alert("Error downloading..."); + const json = await response.json(); + alert(json); } }; @@ -158,10 +159,10 @@ function Register(props: {cert: RegisterReturn, onUpdate: () => void}) {