Skip to content

Commit

Permalink
improve logs of pdf generation, to include conventionId (for debug pu…
Browse files Browse the repository at this point in the history
…rposes)
  • Loading branch information
JeromeBu committed Jan 23, 2025
1 parent cba358a commit 59fb691
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const createTechnicalRouter = (
async (req, res) =>
sendHttpResponse(req, res, () =>
deps.useCases.htmlToPdf.execute(
req.body.htmlContent,
req.body,
req.payloads?.inclusion ?? req.payloads?.convention,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,16 @@ describe("technical router", () => {
const response = await httpClient.htmlToPdf({
body: {
htmlContent: "<p>some html content</p>",
conventionId: "convention-id",
},
headers: {
authorization: validatorJwt,
},
});
expectToEqual(response.body, 'PDF_OF >> "<p>some html content</p>"');
expectToEqual(
response.body,
'PDF_OF convention convention-id >> "<p>some html content</p>"',
);

expectToEqual(response.status, 200);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { HtmlToPdfRequest } from "shared";
import { PdfGeneratorGateway } from "../ports/PdfGeneratorGateway";

export class InMemoryPdfGeneratorGateway implements PdfGeneratorGateway {
public async make(htmlContent: string): Promise<string> {
return `PDF_OF >> "${htmlContent}"`;
public async make({
htmlContent,
conventionId,
}: HtmlToPdfRequest): Promise<string> {
return `PDF_OF convention ${conventionId} >> "${htmlContent}"`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ describe("ScalingoPdfGeneratorGateway", () => {
uuidGenerator,
);

const pdfBase64 = await pdfGeneratorGateway.make("TEST");
const pdfBase64 = await pdfGeneratorGateway.make({
htmlContent: "TEST",
conventionId: "osef",
});
expect(pdfBase64).toBeDefined();
expect(pdfBase64.length).toBeGreaterThan(0);
expect(Buffer.from(pdfBase64, "base64").toString("binary")).toContain(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { AbsoluteUrl, withAuthorizationHeaders } from "shared";
import {
AbsoluteUrl,
HtmlToPdfRequest,
withAuthorizationHeaders,
} from "shared";
import { HttpClient, defineRoute, defineRoutes } from "shared-routes";
import { z } from "zod";
import { UuidGenerator } from "../../uuid-generator/ports/UuidGenerator";
Expand Down Expand Up @@ -46,7 +50,10 @@ export class ScalingoPdfGeneratorGateway implements PdfGeneratorGateway {
this.#uuidGenerator = uuidGenerator;
}

public async make(htmlContent: string): Promise<string> {
public async make({
htmlContent,
conventionId,
}: HtmlToPdfRequest): Promise<string> {
const requestId = this.#uuidGenerator.new();

const response = await this.#httpClient.generate({
Expand All @@ -59,7 +66,7 @@ export class ScalingoPdfGeneratorGateway implements PdfGeneratorGateway {

if (response.status !== 200)
throw new Error(
`[requestId : ${requestId}] Pdf generation failed with status ${response.status} - ${response.body}`,
`[requestId : ${requestId}, conventionId : ${conventionId}] Pdf generation failed with status ${response.status} - ${response.body}`,
);

return response.body;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { HtmlToPdfRequest } from "shared";

export interface PdfGeneratorGateway {
make(htmlContent: string): Promise<string>;
make(params: HtmlToPdfRequest): Promise<string>;
}
15 changes: 10 additions & 5 deletions back/src/domains/core/pdf-generation/use-cases/HtmlToPdf.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import {
ConventionJwtPayload,
ConventionRelatedJwtPayload,
HtmlToPdfRequest,
errors,
zStringMinLength1,
htmlToPdfRequestSchema,
} from "shared";
import { UseCase } from "../../UseCase";
import { PdfGeneratorGateway } from "../ports/PdfGeneratorGateway";

export class HtmlToPdf extends UseCase<string, string, ConventionJwtPayload> {
protected inputSchema = zStringMinLength1;
export class HtmlToPdf extends UseCase<
HtmlToPdfRequest,
string,
ConventionJwtPayload
> {
protected inputSchema = htmlToPdfRequestSchema;

constructor(protected pdfGeneratorGateway: PdfGeneratorGateway) {
super();
}

protected async _execute(
htmlContent: string,
params: HtmlToPdfRequest,
jwtPayload?: ConventionRelatedJwtPayload,
): Promise<string> {
if (!jwtPayload) throw errors.user.unauthorized();
return this.pdfGeneratorGateway.make(htmlContent);
return this.pdfGeneratorGateway.make(params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,35 @@ const jwtPayload: ConventionJwtPayload = {
const htmlContent = "<h1>Hello world</h1><p>test</p>";

describe("HtmlToPdf", () => {
const conventionId = "convention-id";
let htmlToPdf: HtmlToPdf;
beforeEach(() => {
htmlToPdf = new HtmlToPdf(new InMemoryPdfGeneratorGateway());
});

it("renders a pdf from a html content", async () => {
const base64Pdf = await htmlToPdf.execute(htmlContent, jwtPayload);
expectToEqual(base64Pdf, `PDF_OF >> "${htmlContent}"`);
const base64Pdf = await htmlToPdf.execute(
{ htmlContent, conventionId },
jwtPayload,
);
expectToEqual(
base64Pdf,
`PDF_OF convention convention-id >> "${htmlContent}"`,
);
});

it("returns an error if the html content is empty", async () => {
await expectPromiseToFailWithError(
htmlToPdf.execute("", jwtPayload),
htmlToPdf.execute({ htmlContent: "", conventionId }, jwtPayload),
errors.inputs.badSchema({
flattenErrors: ["Obligatoire"],
flattenErrors: ["htmlContent : Obligatoire"],
}),
);
});

it("returns an error if no JWT is provided", async () => {
await expectPromiseToFailWithError(
htmlToPdf.execute(htmlContent),
htmlToPdf.execute({ htmlContent, conventionId }),
errors.user.unauthorized(),
);
});
Expand Down
7 changes: 6 additions & 1 deletion front/src/app/pages/convention/ConventionDocumentPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,12 @@ export const ConventionDocumentPage = ({
setIsPdfLoading(true);
const pdfContent =
await outOfReduxDependencies.technicalGateway.htmlToPdf(
prepareContentForPdfGenerator(document.documentElement.outerHTML),
{
htmlContent: prepareContentForPdfGenerator(
document.documentElement.outerHTML,
),
conventionId: convention.id,
},
jwt,
);
const downloadLink = document.createElement("a");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ConventionSupportedJwt,
Email,
FeatureFlags,
HtmlToPdfRequest,
TechnicalRoutes,
ValidateEmailFeedback,
uploadFileRoute,
Expand Down Expand Up @@ -42,12 +43,12 @@ export class HttpTechnicalGateway implements TechnicalGateway {
}

public htmlToPdf(
htmlContent: string,
params: HtmlToPdfRequest,
jwt: ConventionSupportedJwt,
): Promise<string> {
return this.httpClient
.htmlToPdf({
body: { htmlContent },
body: params,
headers: { authorization: jwt },
})
.then((response) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ConventionSupportedJwt,
Email,
FeatureFlags,
HtmlToPdfRequest,
ValidateEmailFeedback,
sleep,
} from "shared";
Expand All @@ -15,7 +16,7 @@ export class SimulatedTechnicalGateway implements TechnicalGateway {
of(makeStubFeatureFlags());

public htmlToPdf = (
_htmlContent: string,
_htmlContent: HtmlToPdfRequest,
_jwt: ConventionSupportedJwt,
): Promise<string> => Promise.resolve("YWJjZA==");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ConventionSupportedJwt,
Email,
FeatureFlags,
HtmlToPdfRequest,
ValidateEmailFeedback,
} from "shared";
import { TechnicalGateway } from "src/core-logic/ports/TechnicalGateway";
Expand All @@ -15,7 +16,7 @@ export class TestTechnicalGateway implements TechnicalGateway {
public getAllFeatureFlags$ = () => this.featureFlags$;

public htmlToPdf = (
_htmlContent: string,
_htmlContent: HtmlToPdfRequest,
_jwt: ConventionSupportedJwt,
): Promise<string> => Promise.resolve("YWJjZA==");

Expand Down
6 changes: 5 additions & 1 deletion front/src/core-logic/ports/TechnicalGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import {
ConventionSupportedJwt,
Email,
FeatureFlags,
HtmlToPdfRequest,
ValidateEmailFeedback,
} from "shared";

export interface TechnicalGateway {
getAllFeatureFlags$(): Observable<FeatureFlags>;
uploadFile(file: File, renameFileToId: boolean): Promise<AbsoluteUrl>;
htmlToPdf(htmlContent: string, jwt: ConventionSupportedJwt): Promise<string>;
htmlToPdf(
params: HtmlToPdfRequest,
jwt: ConventionSupportedJwt,
): Promise<string>;
getEmailStatus(email: Email): Promise<ValidateEmailFeedback>;
}
9 changes: 8 additions & 1 deletion shared/src/routes/technicalRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineRoute, defineRoutes } from "shared-routes";
import { z } from "zod";
import { ConventionId } from "../convention/convention.dto";
import {
validateEmailInputSchema,
validateEmailResponseSchema,
Expand All @@ -14,8 +15,14 @@ import {
zStringMinLength1,
} from "../zodUtils";

const htmlToPdfRequestSchema = z.object({
export type HtmlToPdfRequest = {
htmlContent: string;
conventionId: ConventionId;
};

export const htmlToPdfRequestSchema = z.object({
htmlContent: zStringMinLength1,
conventionId: z.string(),
});

// @TODO: This should be a proper OpenAPI schema
Expand Down

0 comments on commit 59fb691

Please sign in to comment.