I am using nodemailer to send emails, and inside I am attaching pdf files, to handle the attachments and the limit of these made certain validations.
Context
I have a validation that takes care of splitting an array that carries files into a smaller one. For example, if I have 20 files but those 20 files weigh more than an established limit of MB's, perform the validation.
I have another array where I have all these files without filtering, without validation, all in one where I return an ID (correlative_request) so to speak and the content (buffer) of the file
So as I needed to get from the array that divides the information of the file (content) and get from the array without dividing the ID (correlative_request) I filtered this array, where I told it that if the content of the divided array is equal to the content of I place matrix B in a new array with the information I need for each time I divide the matrix and I get something like this:
Now that I have the info just as I need it, I want that if the content of the divided array is equal to the content of the new array that it creates (ans) for each element of the array I want it to take out the fileName and place them where they belong
Problem *
My problem is that every time I divide the array that contains the content of the attachment into a smaller one, I go and assign the email a new BODY that goes in the body, for example, imagine that I have 20 files to send and those 20 files exceed the limit of 25 MB. If so, it comes and removes those attachments and converts them into a smaller matrix that does not exceed the size
{
EmailAttachment {
filename: 'B-22-1-03 - Ana Lucia Márquez Hernández.pdf',
content: <Buffer 25 50 44 46 2d 31 2e 34 0a 25 d3 eb e9 e1 0a 31 20 30 20 6f 62 6a 0a 3c 3c 2f 43 72 65 61 74 6f 72 20 28 43 68 72 6f 6d 69 75 6d 29 0a 2f 50 72 6f 64 ... 8208120 more bytes>,
path: null,
contentType: null,
cid: null
},
EmailAttachment {
filename: 'G-22-1-021 - Katerin Rivera.pdf',
content: <Buffer 25 50 44 46 2d 31 2e 34 0a 25 d3 eb e9 e1 0a 31 20 30 20 6f 62 6a 0a 3c 3c 2f 43 72 65 61 74 6f 72 20 28 43 68 72 6f 6d 69 75 6d 29 0a 2f 50 72 6f 64 ... 277114 more bytes>,
path: null,
contentType: null,
cid: null
}
]
Then when it has already divided them, it will go and compare the content (buffer) and if it is equal to the matrix that brings the information of the new Body, it will place it there where it belongs.
In my logic that would be the best way, but instead of getting what I want I just get an element from the new Body
It only returns to me in the body what I mark in the image, when if I put eight files in the attachment, it returns all eightfileName
Do you think I'm handling arrays incorrectly? I leave you the code, comment it so that more or less make me understand a little
// numero en megas del attachment (archivo adjunto)
const totalSize: number = this.getSizeFromAttachments(attachments);
// obtener si es mayor
const chunkSplit = Math.floor(isNaN(totalSize) ? 1 : totalSize / this.LIMIT_ATTACHMENTS) + 1;
// funcion lodash para dividir la matriz
const attachmentsChunk: any[][] = _.chunk(attachments, chunkSplit);
// dentro de los attachmente que se dividieron buscar content
const result = attachment.map(element => this.getCantidad.find(y => element.content === y.content))
// trae el nombre de los paciente y el correlativo de una consulta a solicitudes
const aux = this.namePatient
// nuevo arreglo
const ans = []
// por cada elemento del array que ya filtro
result.forEach(ele => {
// saca la llave del arreglo correlativo_solicitud
const expected_key = ele["correlativo_solicitud"];
// comprueba si la key anterior está presente en aux
if (aux[expected_key]) {
// crear una copia de algunos elementos en result
const newItem = { ...ele };
// Agregamos la nueva llave requerida, valor
newItem["name_Patient"] = aux[expected_key]
newItem["fileName"] = `Saludos se envía adjunto el resultado para la solicitud: ${expected_key}${aux[expected_key] ? ' - ' + aux[expected_key] : null}`
// Agregamos el nuevo item a otro nuevo arreglo ans
ans.push(newItem)
}
});
console.log('newItem', ans);
let newBody = '';
// Por cada elemento del array saca el content del arreglo ans
ans.forEach(element => {
// por cada elemento del arreglo saca el content del attachment que divide los archivos
attachment.forEach(item => {
// si el conten del attachment == al del arreglo ans entonces extrae el nuevo cuerpo
if (element.content === item.content) {
newBody = element.fileName;
}
});
});
// console.log('newBody', newBody);
const link = `Estimado Usuario, la siguente solicitud le será enviado en diferentes correos. Este es el correo (${index + 1}/${attachmentsChunk?.length})`;
const newContext = {
getCurrent: link,
newBody: newBody,
...context
}
return this.prepareEmail({
para: to,
asunto: `HOLAAA2 ${subject} (Correo ${index + 1}/${attachmentsChunk?.length - 1})`,
plantilla: template,
contexto: newContext,
}, attachment);
}
})
I know that the problem is not so easy, but I already have a large part underway, I would only like if you can advise how I can assign the new body according to the divided matrix
Thank you very much to everyone who takes the time to read me
In the end I managed to solve it, I only performed a simple validation by accessing the new array that "ans" created by filtering it, and I told it to filter the root elements that included the same content and if so, I put it in a new array where I pass the new Body. Something like that:
Result