base de datos terminada
This commit is contained in:
Generated
+449
-75
File diff suppressed because it is too large
Load Diff
@@ -21,16 +21,20 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/common": "^11.0.1",
|
||||
"@nestjs/config": "^4.0.1",
|
||||
"@nestjs/core": "^11.0.1",
|
||||
"@nestjs/platform-express": "^11.0.1",
|
||||
"@nestjs/swagger": "^11.0.5",
|
||||
"@nestjs/typeorm": "^11.0.0",
|
||||
"csv-parser": "^3.2.0",
|
||||
"dotenv": "^16.4.7",
|
||||
"fs": "^0.0.1-security",
|
||||
"multer": "^1.4.5-lts.1",
|
||||
"mysql2": "^3.13.0",
|
||||
"nodemailer": "^6.10.0",
|
||||
"reflect-metadata": "^0.2.2",
|
||||
"rxjs": "^7.8.1",
|
||||
"typeorm": "^0.3.21",
|
||||
"xlsx": "^0.18.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -3,11 +3,33 @@ import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { MailModule } from './mail/mail.module';
|
||||
import {ExcelModule} from './excel/excel.module'
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
|
||||
|
||||
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot(),
|
||||
TypeOrmModule.forRoot({
|
||||
type: 'mysql',
|
||||
host: process.env.db_host,
|
||||
username: process.env.db_username,
|
||||
database: process.env.db_database,
|
||||
password: process.env.db_password,
|
||||
port: 3306,
|
||||
synchronize: true,
|
||||
dropSchema: true, // elimina la base de datos
|
||||
// logging: true, // Habilita los logs para depuración
|
||||
autoLoadEntities: true, // Carga automáticamente las entidades
|
||||
|
||||
}),
|
||||
|
||||
MailModule,
|
||||
ExcelModule,
|
||||
|
||||
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
|
||||
@@ -23,10 +23,10 @@ export class MailController {
|
||||
|
||||
// Endpoint para enviar correos a través de una solicitud POST
|
||||
@Post('send')
|
||||
async sendMail(@Body() body: { to: string; subject: string; text: string }) {
|
||||
const { to, subject, text } = body;
|
||||
async sendMail(@Body() body: { to: string; subject: string; text: string; fecha_recibido: Date }) {
|
||||
const { to, subject, text, fecha_recibido } = body;
|
||||
|
||||
const result = await this.mailService.sendMail(to, subject, text);
|
||||
const result = await this.mailService.sendMail(to, subject, text, fecha_recibido);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -34,9 +34,11 @@ export class MailController {
|
||||
async sendMailExcel() {
|
||||
|
||||
const emails=this.excelService.readEmailsFromExcel('./src/excel_Usuarios/usuarios_fake.xlsx');
|
||||
let fechaActual: Date = new Date();
|
||||
|
||||
for (const email of emails) {
|
||||
try {
|
||||
await this.mailService.sendMail(email, "subject", "text");
|
||||
await this.mailService.sendMail(email, "subject", "text", fechaActual);
|
||||
console.log(`📧 Enviado a: ${email}`);
|
||||
} catch (error) {
|
||||
console.error(`❌ Error enviando a ${email}:`, error.message);
|
||||
|
||||
+12
-1
@@ -2,10 +2,21 @@ import { Module } from '@nestjs/common';
|
||||
import { MailService } from './mail.service';
|
||||
import { MailController } from './mail.controller';
|
||||
import { ExcelModule } from 'src/excel/excel.module';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import {
|
||||
Status,
|
||||
Correo,
|
||||
}from 'src/typeorm/votacionesPrueba.entity'
|
||||
|
||||
@Module({
|
||||
providers: [MailService],
|
||||
imports: [ExcelModule],
|
||||
imports: [
|
||||
ExcelModule,
|
||||
TypeOrmModule.forFeature([
|
||||
Status,
|
||||
Correo,
|
||||
]),
|
||||
],
|
||||
controllers: [MailController],
|
||||
exports: [MailService], // Exportar si otros módulos necesitan usar el MailService
|
||||
})
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import * as nodemailer from 'nodemailer';
|
||||
import * as dotenv from 'dotenv';
|
||||
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Correo, Status } from 'src/typeorm/votacionesPrueba.entity';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
@@ -9,7 +11,12 @@ dotenv.config();
|
||||
export class MailService {
|
||||
private transporter;
|
||||
|
||||
constructor() {
|
||||
constructor(
|
||||
@InjectRepository(Correo) private readonly correoRepository: Repository<Correo>,
|
||||
@InjectRepository(Status) private readonly statusRepository: Repository<Status>,
|
||||
|
||||
) {
|
||||
|
||||
|
||||
|
||||
this.transporter = nodemailer.createTransport({
|
||||
@@ -59,7 +66,7 @@ export class MailService {
|
||||
}
|
||||
}
|
||||
|
||||
async sendMail(to: string, subject: string, text: string) {
|
||||
async sendMail(to: string, subject: string, text: string, fecha_recibido: Date) {
|
||||
const mailOptions = {
|
||||
from: process.env.USER_GMAIL,
|
||||
to,
|
||||
@@ -67,7 +74,35 @@ export class MailService {
|
||||
text,
|
||||
};
|
||||
|
||||
return this.transporter.sendMail(mailOptions);
|
||||
let resMail = await this.transporter.sendMail(mailOptions);
|
||||
const statusTexto = resMail.accepted.length > 0 ? "Enviado" : "Fallido";
|
||||
|
||||
|
||||
let status = await this.statusRepository.findOne({ where: { status: statusTexto } });
|
||||
|
||||
|
||||
if (!status) {
|
||||
status = this.statusRepository.create({ status: statusTexto });
|
||||
await this.statusRepository.save(status);
|
||||
}
|
||||
|
||||
const info= this.correoRepository.create({
|
||||
id_status: status,
|
||||
|
||||
fecha_recibido: fecha_recibido,
|
||||
fecha_enviado: new Date(),
|
||||
destinatario: to,
|
||||
remitente: process.env.USER_GMAIL,
|
||||
|
||||
});
|
||||
if(!resMail){
|
||||
throw new Error("fallo")
|
||||
}
|
||||
this.correoRepository.save(info);
|
||||
|
||||
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
async sendBulkMailsOld(emails: string[], subject: string, text: string) {
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
export class Status {
|
||||
@PrimaryGeneratedColumn()
|
||||
id_status: number;
|
||||
|
||||
@Column({ type: 'varchar', length: 50, unique:true})
|
||||
status: string;
|
||||
|
||||
correos: Correo[];
|
||||
}
|
||||
|
||||
@Entity()
|
||||
export class Sistema {
|
||||
@PrimaryGeneratedColumn()
|
||||
id_sistema: number;
|
||||
|
||||
@Column({ type: 'varchar', length: 50 })
|
||||
refresh_token: string;
|
||||
|
||||
@Column({ type: 'varchar', length: 50})
|
||||
clave: string;
|
||||
|
||||
@Column({ type: 'varchar', length: 50})
|
||||
nombre_sistema: string;
|
||||
|
||||
correos: Correo[];
|
||||
}
|
||||
|
||||
@Entity()
|
||||
export class Correo {
|
||||
@PrimaryGeneratedColumn()
|
||||
id_correo: number;
|
||||
|
||||
@ManyToOne(() => Status, (status) => status.correos)
|
||||
id_status: Status;
|
||||
|
||||
|
||||
|
||||
@Column({ type: 'datetime'})
|
||||
fecha_recibido: Date;
|
||||
|
||||
@Column({ type: 'datetime'})
|
||||
fecha_enviado: Date;
|
||||
|
||||
@Column({ type: 'varchar', length: 50 })
|
||||
destinatario: string;
|
||||
|
||||
@Column({ type: 'varchar', length: 50 })
|
||||
remitente: string;
|
||||
}
|
||||
Reference in New Issue
Block a user