Skip to content

Commit

Permalink
fix(export): correction de l'erreur d'affichage (#1443)
Browse files Browse the repository at this point in the history
* fix: erreur

* fix: test

* fix: test

* fix: test

* fix: test

* fix: test

* fix: test

* fix: test

* fix: test

* fix: test

* fix: test

* fix: test

* fix: test
  • Loading branch information
maxgfr authored Jul 11, 2024
1 parent d597467 commit 3f32c4c
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 183 deletions.
11 changes: 11 additions & 0 deletions targets/export-elasticsearch/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
extends: ["@shared/eslint-config"],
rules: {
"@typescript-eslint/naming-convention": "off",
},
parserOptions: {
project: "tsconfig.json",
sourceType: "module",
tsconfigRootDir: __dirname,
},
};
8 changes: 0 additions & 8 deletions targets/export-elasticsearch/.eslintrc.json

This file was deleted.

4 changes: 2 additions & 2 deletions targets/export-elasticsearch/request.http
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ POST http://localhost:8787/export
content-type: application/json

{
"environment": "production",
"userId": "148a6a2b-c565-4ce2-a093-ad1e74a1c722"
"environment": "preproduction",
"userId": "8babda41-6001-4665-96f5-c430fddb0c53"
}

###
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,8 @@ describe("ExportController /export", () => {
environment: Environment.preproduction,
userId: "890ca91b-f150-4957-9bb2-8500940815f0",
});
expect(res.statusCode).toEqual(202);
expect(res.body).toEqual({
created_at: "2022-03-24T10:09:10.000Z",
environment: Environment.preproduction,
id: "1",
status: Status.running,
updated_at: "2022-03-24T10:09:10.000Z",
user_id: "890ca91b-f150-4957-9bb2-8500940815f0",
isRunning: true,
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,18 @@ export class FakeExportService {
},
];
}

async getRunningExport(): Promise<ExportEsStatus[]> {
await wait(100);
return [];
}

async verifyAndCleanPreviousExport(
runningResult: ExportEsStatus[],

Check warning on line 58 in targets/export-elasticsearch/src/controllers/__test__/fake/export.ts

View workflow job for this annotation

GitHub Actions / Lint (export-elasticsearch)

'runningResult' is defined but never used
environment: Environment,

Check warning on line 59 in targets/export-elasticsearch/src/controllers/__test__/fake/export.ts

View workflow job for this annotation

GitHub Actions / Lint (export-elasticsearch)

'environment' is defined but never used
minutes: number

Check warning on line 60 in targets/export-elasticsearch/src/controllers/__test__/fake/export.ts

View workflow job for this annotation

GitHub Actions / Lint (export-elasticsearch)

'minutes' is defined but never used
): Promise<void> {
await wait(100);
return;
}
}
23 changes: 13 additions & 10 deletions targets/export-elasticsearch/src/controllers/export.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { ExportEsStatus } from "@socialgouv/cdtn-types";
import { Environment } from "@socialgouv/cdtn-types";
import { Request, Response } from "express";
import { ExportEsStatus, Environment } from "@socialgouv/cdtn-types";
import { Request } from "express";
import { inject } from "inversify";
import type { interfaces } from "inversify-express-utils";
import {
Expand All @@ -9,7 +8,6 @@ import {
httpPost,
queryParam,
request,
response,
} from "inversify-express-utils";

import { ExportService } from "../services/export";
Expand All @@ -25,13 +23,18 @@ export class ExportController implements interfaces.Controller {
) {}

@httpPost("/", getName(ExportEsRunMiddleware))
async run(
@request() req: Request,
@response() res: Response
): Promise<ExportEsStatus> {
async run(@request() req: Request): Promise<{ isRunning: true }> {
const body: ValidatorCreateExportEsStatusType = req.body;
res.status(202);
return this.service.runExport(body.userId, body.environment);
const runningResult = await this.service.getRunningExport();
await this.service.verifyAndCleanPreviousExport(
runningResult,
body.environment,
process.env.DISABLE_LIMIT_EXPORT ? 0 : 15
);
this.service.runExport(body.userId, body.environment);

Check warning on line 34 in targets/export-elasticsearch/src/controllers/export.ts

View workflow job for this annotation

GitHub Actions / Lint (export-elasticsearch)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
return {
isRunning: true,
};
}

@httpGet("/")
Expand Down
12 changes: 6 additions & 6 deletions targets/export-elasticsearch/src/ingester/types/GraphQL.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export type GraphQLResponseRoot<Data> = {
export interface GraphQLResponseRoot<Data> {
data?: Data;
errors?: GraphQLResponseError[];
};
}

export type GraphQLResponseError = {
export interface GraphQLResponseError {
message: string;
locations?: GraphQLResponseErrorLocation[];
};
}

export type GraphQLResponseErrorLocation = {
export interface GraphQLResponseErrorLocation {
line: number;
column: number;
};
}
83 changes: 53 additions & 30 deletions targets/export-elasticsearch/src/services/__test__/export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { SitemapService } from "../sitemap";
import { FakeCopyService } from "./fake/copy";
import { FakeExportRepository } from "./fake/export";
import { FakeSitemapService } from "./fake/sitemap";
import { FakeAgreementsService } from "./fake/agreements";
import { AgreementsService } from "../agreements";

jest.mock("../../workers", () => {
return {
Expand Down Expand Up @@ -40,6 +42,10 @@ describe("ExportService", () => {
.bind<FakeSitemapService>(getName(SitemapService))
.to(FakeSitemapService)
.inSingletonScope();
container
.bind<FakeAgreementsService>(getName(AgreementsService))
.to(FakeAgreementsService)
.inSingletonScope();
service = container.get<ExportService>(getName(ExportService));
mockRepository = container.get<ExportRepository>(getName(ExportRepository));
jest.clearAllMocks();
Expand Down Expand Up @@ -92,25 +98,11 @@ describe("ExportService", () => {
.runExport("ABC", Environment.preproduction)
.catch((e: Error) => {
// eslint-disable-next-line jest/no-conditional-expect
expect(e.message).toBe("There is already a running job");
expect(e.message).toBe(
"Il y a déjà un export en cours qui a été lancé il y a moins de 15 minutes..."
);
});
});

it("should not clean previous job", async () => {
const date = new Date();
const spy = jest.spyOn(mockRepository, "updateOne");
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
await service.cleanPreviousExport({
created_at: date,
environment: Environment.preproduction,
id: "1",
status: Status.running,
updated_at: date,
user_id: "userId",
});
expect(spy).toHaveBeenCalledTimes(0);
});
});

describe("Outdated job has run", () => {
Expand All @@ -129,29 +121,60 @@ describe("ExportService", () => {
);
const spy = jest.spyOn(mockRepository, "updateOne");
await service.runExport("ABC", Environment.preproduction);
expect(spy).toHaveBeenCalledTimes(2);
expect(spy).toHaveBeenCalledTimes(1);
expect(runWorkerIngesterPreproduction).toHaveBeenCalledTimes(1);
});

it("should clean previous job because launched > 1h", async () => {
it("should clean previous job because launched > 15min", async () => {
const oldDate = new Date();
const expiryDate = new Date(
new Date().setHours(new Date().getHours() + 2)
new Date().setHours(new Date().getMinutes() + 20)
);
timekeeper.travel(expiryDate);
const spy = jest.spyOn(mockRepository, "updateOne");
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
await service.cleanPreviousExport({
created_at: oldDate,
environment: Environment.preproduction,
id: "1",
status: Status.running,
updated_at: oldDate,
user_id: "userId",
});
await service.verifyAndCleanPreviousExport(
[
{
created_at: oldDate,
environment: Environment.preproduction,
id: "1",
status: Status.running,
updated_at: oldDate,
user_id: "userId",
},
],
Environment.preproduction,
15
);
expect(spy).toHaveBeenCalledTimes(1);
});

it("should cancel because there are already an export in an other env", async () => {
const spy = jest.spyOn(mockRepository, "updateOne");
await service
.verifyAndCleanPreviousExport(
[
{
created_at: new Date("2020-01-01"),
environment: Environment.preproduction,
id: "1",
status: Status.running,
updated_at: new Date("2020-01-01"),
user_id: "userId",
},
],
Environment.production,
15
)
.catch((e: Error) => {
// eslint-disable-next-line jest/no-conditional-expect
expect(e.message).toBe(
"Il y a déjà un export en cours sur un autre environnement..."
);
});

expect(spy).toHaveBeenCalledTimes(0);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { injectable } from "inversify";

import { wait } from "../../../utils";
import { Environment } from "@socialgouv/cdtn-types";

@injectable()
export class FakeAgreementsService {
async uploadAgreements(
environment: Environment,
destinationFolder: string,
destinationName: string
): Promise<void> {
await wait(100);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { injectable } from "inversify";

import { wait } from "../../../utils";
import { Environment } from "@socialgouv/cdtn-types";

@injectable()
export class FakeSitemapService {
async uploadSitemap(
environment: Environment,
sitemapEndpoint: string,
destinationContainer: string,
destinationName: string
destinationFolder: string,
sitemapName: string
): Promise<void> {
await wait(100);
}
Expand Down
Loading

0 comments on commit 3f32c4c

Please sign in to comment.