96 lines
2.3 KiB
JavaScript
96 lines
2.3 KiB
JavaScript
const admin = require("firebase-admin");
|
|
const moment = require("moment");
|
|
const { onCall, HttpsError } = require("firebase-functions/v2/https");
|
|
const functions = require("firebase-functions/v1");
|
|
|
|
const { generateDocument } = require("./utils/helpers.js");
|
|
const { logoBase64 } = require("./data/logoBase64.js");
|
|
|
|
const documentTemplateList = {
|
|
QUOTE: {
|
|
path: "./components/Quote.js",
|
|
},
|
|
INVOICE: {
|
|
path: "./components/Invoice.js",
|
|
},
|
|
CONTRACT: {
|
|
path: "./components/Contract.js",
|
|
},
|
|
};
|
|
|
|
const appConfig = {
|
|
company: {
|
|
logoBase64,
|
|
name: "SAS StarsClick",
|
|
address: "8 rue de la Paix",
|
|
postalCode: "75002",
|
|
city: "Paris",
|
|
identifier: "123456789",
|
|
RCS: "123 456 789",
|
|
taxIdentifier: "FR 12 345 678 912",
|
|
phoneNumber: "01 23 45 67 89",
|
|
supportEmail: "contact@starsclick.fr",
|
|
},
|
|
};
|
|
|
|
exports.generateDocumentFromTemplate = onCall(
|
|
async ({ auth = {}, data = {} }) => {
|
|
try {
|
|
const { type, documentData = {} } = data;
|
|
|
|
const { path } = documentTemplateList[type] || {};
|
|
|
|
if (!path) {
|
|
throw new Error("Invalid document type");
|
|
}
|
|
|
|
const { default: DocumentImport } = await import(path);
|
|
|
|
const { downloadURL } = await generateDocument({
|
|
template: DocumentImport,
|
|
data: {
|
|
...documentData,
|
|
creationTimestamp: moment().format("DD/MM/YYYY"),
|
|
},
|
|
filePath: `documents/${moment().format("DD-MM-YYYY-HH-mm-ss")}.pdf`,
|
|
});
|
|
|
|
console.log(`PDF URL: ${downloadURL}`);
|
|
|
|
return { documentUrl: downloadURL };
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
}
|
|
);
|
|
|
|
exports.testDocumentGeneration = functions
|
|
.runWith({ memory: "512MB" })
|
|
.https.onRequest(async (req, res) => {
|
|
try {
|
|
// const { type } = req.query;
|
|
|
|
const type = "INVOICE";
|
|
|
|
const { path } = documentTemplateList[type] || {};
|
|
|
|
if (!path) {
|
|
throw new Error("Invalid document type");
|
|
}
|
|
|
|
const { default: DocumentTemplate } = await import(path);
|
|
|
|
const { downloadURL } = await generateDocument({
|
|
template: DocumentTemplate,
|
|
data: {
|
|
appConfig,
|
|
},
|
|
filePath: `documents/${moment().format("DD-MM-YYYY-HH-mm-ss")}.pdf`,
|
|
});
|
|
|
|
console.log(`PDF URL: ${downloadURL}`);
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
});
|