diff --git a/package-lock.json b/package-lock.json index 84e8a8e852af7287b213a6ea78e2b1e6e1cc0fe6..2fe18e317f551d7d1061d8b63ae8566743790f5e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ "eslint-import-resolver-typescript": "^3.5.2", "fastify": "^4.9.2", "mailgun-js": "^0.22.0", + "moment": "^2.29.4", "mongoose": "^6.7.2", "multer": "^1.4.5-lts.1", "node-fetch": "^2.6.9", @@ -10573,6 +10574,14 @@ "mkdirp": "bin/cmd.js" } }, + "node_modules/moment": { + "version": "2.29.4", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz", + "integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==", + "engines": { + "node": "*" + } + }, "node_modules/mongodb": { "version": "4.11.0", "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-4.11.0.tgz", @@ -22398,6 +22407,11 @@ "minimist": "^1.2.6" } }, + "moment": { + "version": "2.29.4", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz", + "integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==" + }, "mongodb": { "version": "4.11.0", "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-4.11.0.tgz", diff --git a/package.json b/package.json index 245e3d9b0abdf63325cc774c2341abb30aa89393..8767b4080704c7838b841752a8453941c46621fc 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "eslint-import-resolver-typescript": "^3.5.2", "fastify": "^4.9.2", "mailgun-js": "^0.22.0", + "moment": "^2.29.4", "mongoose": "^6.7.2", "multer": "^1.4.5-lts.1", "node-fetch": "^2.6.9", diff --git a/src/internships/internships.service.ts b/src/internships/internships.service.ts index 038980ddc6a1b13dac43167d1b5565c4937618f3..7dd16917e18d06232e93a1b00f4d0d7b296727d7 100644 --- a/src/internships/internships.service.ts +++ b/src/internships/internships.service.ts @@ -8,6 +8,7 @@ import { join } from 'path'; + @Injectable() export class InternshipService { constructor(private readonly _internshipsDao: InternshipDao) {} @@ -22,46 +23,285 @@ export class InternshipService { this._internshipsDao.save(internship); async generatePDF (id: string): Promise<InternshipEntity>{ + var information = await this.findOne(id).then( (data: InternshipEntity) => + data.information); + + //on recupere les informations de l'entreprise + var nomEntreprise = information.company.companyName; + + var adresse = information.company.address; + var adresseString = adresse.street + ', ' + adresse.postalCode + ', ' + + adresse.city + ', ' + adresse.country; + + var telOrganisme = '+33 6 63 12 84 65'; + + var melOrganisme = information.company.hrContactEmail; + + var reprsentePar = information.company.ceoName; + + var qualiteRepresentant = 'CEO'; + + var nomService = information.affectation.service; + + var lieuStage = information.company.address; + var lieuStageString = lieuStage.street + ', ' + lieuStage.postalCode + ', ' + lieuStage.city + ', ' + lieuStage.country; + + //on recupere les informations de l'etudiant + var etudiant = information.student; + + var nomEtudiant = etudiant.completeName; + + var dateNaissanceEtudiant = etudiant.birthDate.toDateString(); + + var adresseEtudiant = etudiant.address.street + ', ' + etudiant.address.postalCode + ', ' + etudiant.address.city + ', ' + etudiant.address.country; - console.log("generation pdf"); + var telEtudiant = etudiant.phone; + + var melEtudiant = etudiant.email; + + //on recupere les informations du stage + var sujetStage = 'NULL'; + + var dateDebutStage = information.affectation.dateStart.toDateString(); + + var dateFinStage = information.affectation.dateEnd.toDateString(); + + const diffInMs = information.affectation.dateEnd.getTime() - information.affectation.dateStart.getTime(); + const diffInWeeks = diffInMs / 1000 / 60 / 60 / 24 / 7; + var dureeStageEnSemaines = Math.ceil(diffInWeeks); + + var dureeStageEnJours = dureeStageEnSemaines * 5; + + //on recupere les informations du tuteur du stage + var nomCompletTuteur = information.affectation.responsibleName; + + var fonctionTuteur = information.affectation.responsibleFunction; + + var telTuteur = information.affectation.responsiblePhone; + + var melTuteur = information.affectation.responsibleEmail; const filePath = join(__dirname, '..', '..', 'files', 'convention_a_modifier.pdf'); const existingPdfBytes = readFileSync(filePath); const pdfDoc = await PDFDocument.load(existingPdfBytes); - // Embed the Helvetica font - const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica); + // Embed the Helvetica font + const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica); - // Get the first page of the document - const pages = pdfDoc.getPages(); - console.log("nombres de pages : ", pages.length ) - const firstPage = pages[0]; + // Get the first page of the document + const pages = pdfDoc.getPages(); + const firstPage = pages[0]; - // Get the width and height of the first page - const { width, height } = firstPage.getSize(); + // on remplit les informations de l'entreprise + firstPage.drawText(nomEntreprise, { + x: 61, + y: 564, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); - // Draw a string of text diagonally across the first page - firstPage.drawText('This text was added with JavaScript!', { - x: 5, - y: height / 2 + 300, - size: 50, + firstPage.drawText(adresseString, { + x: 73, + y: 542, + size: 9, font: helveticaFont, color: rgb(0.95, 0.1, 0.1), - rotate: degrees(-45), - }); + rotate: degrees(0), + }); + + firstPage.drawText(telOrganisme, { + x: 52, + y: 522, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(melOrganisme, { + x: 149, + y: 522, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(reprsentePar, { + x: 98, + y: 501, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(qualiteRepresentant, { + x: 130, + y: 480, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(nomService, { + x: 230, + y: 459, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(lieuStageString, { + x: 98, + y: 438, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + // on remplit les informations de l'entreprise + firstPage.drawText(nomEtudiant, { + x: 98, + y: 396, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(dateNaissanceEtudiant, { + x: 137, + y: 374, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(adresseEtudiant, { + x: 73, + y: 353, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(telEtudiant, { + x: 52, + y: 331, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(melEtudiant, { + x: 149, + y: 331, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(sujetStage, { + x: 102, + y: 231, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(dateDebutStage, { + x: 119, + y: 214, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(dateFinStage, { + x: 221, + y: 214, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(dureeStageEnSemaines.toString(), { + x: 200, + y: 197, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(dureeStageEnJours.toString(), { + x: 276, + y: 197, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(nomCompletTuteur, { + x: 380, + y: 131, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(fonctionTuteur, { + x: 360, + y: 110, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(telTuteur, { + x: 345, + y: 89, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + firstPage.drawText(melTuteur, { + x: 345, + y: 69, + size: 9, + font: helveticaFont, + color: rgb(0.95, 0.1, 0.1), + rotate: degrees(0), + }); + + + - // Serialize the PDFDocument to bytes (a Uint8Array) - const pdfBytes = await pdfDoc.save(); - const newFileName = 'convention-' + id; - const newFilePath = join(__dirname, '..', '..', 'files', newFileName + '.pdf'); - writeFileSync(newFilePath, pdfBytes); + // Serialize the PDFDocument to bytes (a Uint8Array) + const pdfBytes = await pdfDoc.save(); + const newFileName = 'convention-' + id; + const newFilePath = join(__dirname, '..', '..', 'files', newFileName + '.pdf'); + writeFileSync(newFilePath, pdfBytes); - //on recupere les informations de l'entreprise - var nomEntreprise = 'capgemini'; - var adresse = '99 rue CEDEX, Courbevoie'; - return null; }