Skip to content

Commit

Permalink
feat: ajout du stockage des jobs effectués
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasDetre committed Dec 20, 2024
1 parent e696323 commit 7eb6b86
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 30 deletions.
6 changes: 2 additions & 4 deletions .infra/ansible/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,11 @@

- name: "Installation du cron d'anonymisation des utilisateurs"
ansible.builtin.cron:
name: "Anonymisation des utilisateurs"
name: "anonymize-users"
minute: "0"
hour: "2"
weekday: "1"
job: "/opt/app/scripts/anonymise-users.sh"
user: "root"
cron_file: "anonymize-users"
job: "sudo /opt/app/scripts/anonymize-users.sh"

- name: "Verification des certificats SSL"
shell:
Expand Down
2 changes: 1 addition & 1 deletion .infra/files/scripts/anonymise-users.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
set -euo pipefail

/opt/app/tools/docker-compose.sh run --rm --no-deps server yarn cli anonymiseUsers
/opt/app/tools/docker-compose.sh run --rm --no-deps server yarn cli anonymizeUsers
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dev": "tsup-node --env.TSUP_WATCH true",
"build": "tsup-node --env.NODE_ENV production",
"typecheck": "NODE_OPTIONS='--max-old-space-size=8192' tsc --noEmit",
"kysely": "npx kysely-codegen --schema public --out-file=./src/db/schema.ts --dialect postgres && yarn prettier:fix",
"generate:seed": "./seed/generate-seed.sh"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions server/seed/generate-seed.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ echo ""

pg_dump $DB_URL --format=custom --data-only \
--exclude-table='public."changeLog"' \
--exclude-table='public."job"' \
--exclude-table='public."changementStatut"' \
--exclude-table='public."demande"' \
--exclude-table='public."intention"' \
Expand Down
Binary file modified server/seed/seed_data.dump
Binary file not shown.
Binary file modified server/seed/seed_schema.dump
Binary file not shown.
20 changes: 15 additions & 5 deletions server/src/commands.product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { PERMISSIONS } from "shared";
import { z } from "zod";

import { basepath } from "./basepath";
import { anonymiseUsers } from "./modules/core/usecases/anonymiseUsers/anonymiseUsers.usecase";
import { createJob } from "./modules/core/queries/createJob";
import { anonymizeUsers } from "./modules/core/usecases/anonymizeUsers/anonymizeUsers.usecase";
import { createUser } from "./modules/core/usecases/createUser/createUser.usecase";
import type { LineTypes } from "./modules/import/repositories/rawData.repository";
import { Schemas } from "./modules/import/repositories/rawData.repository";
Expand Down Expand Up @@ -93,10 +94,11 @@ export function productCommands(cli: Command) {
console.log(`${user.email} failed`, (e as Error).message);
}
}
await createJob({ name: "importUsers" });
});

cli
.command("create-user")
.command("createUser")
.requiredOption("--email <string>")
.requiredOption("--firstname <string>")
.requiredOption("--lastname <string>")
Expand All @@ -105,6 +107,7 @@ export function productCommands(cli: Command) {
.action(
async (options: { email: string; firstname: string; lastname: string; role: Role; codeRegion?: string }) => {
await createUser({ body: options });
await createJob({ name: "createUser" });
}
);

Expand Down Expand Up @@ -280,6 +283,7 @@ export function productCommands(cli: Command) {
}
}
}
await createJob({ name: "importFiles", sub: filename });
});

cli
Expand Down Expand Up @@ -314,14 +318,16 @@ export function productCommands(cli: Command) {
}
}
await refreshViews();
await createJob({ name: "importTables", sub: usecaseName });
});

cli.command("refreshViews").action(async () => {
await refreshViews();
});

cli.command("importIJ").action(async () => {
await importIJData();
await importIJData().then();
await createJob({ name: "importIJ" });
});

cli
Expand All @@ -341,6 +347,7 @@ export function productCommands(cli: Command) {
}
}
await refreshViews();
await createJob({ name: "importFormations", sub: usecaseName });
});

cli
Expand All @@ -361,19 +368,22 @@ export function productCommands(cli: Command) {
await usecase();
}
}
await createJob({ name: "importTensionFranceTravail", sub: usecaseName });
});

cli
.command("importPositionsQuadrant")
.description("Calcul des positions quadrants")
.action(async () => {
await importPositionsQuadrant();
await createJob({ name: "importPositionsQuadrant" });
});

cli
.command("anonymiseUsers")
.command("anonymizeUsers")
.description("Anonymisation des users inactifs depuis 2 ans")
.action(async () => {
await anonymiseUsers();
await anonymizeUsers();
await createJob({ name: "anonymizeUsers" });
});
}
7 changes: 7 additions & 0 deletions server/src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,12 @@ export interface IntentionAccessLog {
updatedAt: Timestamp | null;
}

export interface Job {
name: string | null;
sub: string | null;
createdAt: Generated<Timestamp | null>;
}

export interface LatestDemandeIntentionNonMaterializedView {
isIntention: boolean | null;
numero: string | null;
Expand Down Expand Up @@ -939,6 +945,7 @@ export interface DB {
indicateurSortie: IndicateurSortie;
intention: Intention;
intentionAccessLog: IntentionAccessLog;
job: Job;
latestDemandeIntentionNonMaterializedView: LatestDemandeIntentionNonMaterializedView;
latestDemandeNonMaterializedView: LatestDemandeNonMaterializedView;
latestIntentionNonMaterializedView: LatestIntentionNonMaterializedView;
Expand Down
2 changes: 2 additions & 0 deletions server/src/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ import * as migration_1733743141284 from "./migration_1733743141284";
import * as migration_1734527074984 from "./migration_1734527074984";
import * as migration_1734527078957 from "./migration_1734527078957";
import * as migration_1734618351550 from "./migration_1734618351550";
import * as migration_1734620499618 from "./migration_1734620499618";

type Migration = {
up: (db: Kysely<any>) => Promise<void>;
Expand Down Expand Up @@ -203,4 +204,5 @@ export const migrations: Migrations = {
migration_1734527074984,
migration_1734527078957,
migration_1734618351550,
migration_1734620499618,
};
15 changes: 15 additions & 0 deletions server/src/migrations/migration_1734620499618.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Kysely } from "kysely";
import { sql } from "kysely";

export const up = async (db: Kysely<unknown>) => {
await db.schema
.createTable("job")
.addColumn("name", "varchar")
.addColumn("sub", "varchar")
.addColumn("createdAt", "timestamptz", (c) => c.defaultTo(sql`NOW()`))
.execute();
};

export const down = async (db: Kysely<unknown>) => {
await db.schema.dropTable("job").execute();
};
5 changes: 5 additions & 0 deletions server/src/modules/core/queries/createJob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { getKbdClient } from "@/db/db";

export const createJob = async ({ name, sub }: { name: string; sub?: string }) => {
return getKbdClient().insertInto("job").values({ name, sub }).execute();
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { sql } from "kysely";

import { getKbdClient } from "@/db/db";

export const countUsersToAnonymiseQuery = async () =>
export const countUsersToAnonymizeQuery = async () =>
await getKbdClient()
.selectFrom("user")
.select((eb) =>
Expand All @@ -22,7 +22,7 @@ export const countUsersToAnonymiseQuery = async () =>
)
.executeTakeFirstOrThrow();

export const anonymiseUsersQuery = async () => {
export const anonymizeUsersQuery = async () => {
await sql`
UPDATE ${sql.table("user")}
SET
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// eslint-disable-next-line import/no-extraneous-dependencies, n/no-extraneous-import
import { inject } from "injecti";

import logger from "@/services/logger";

import { anonymizeUsersQuery, countUsersToAnonymizeQuery } from "./anonymizeUsers.dep";

export const [anonymizeUsers, anonymizeUsersFactory] = inject(
{
anonymizeUsersQuery,
countUsersToAnonymizeQuery,
},
(deps) => async () => {
const { count: nbOfUsers } = await deps.countUsersToAnonymizeQuery();
logger.info(`Nombre d'utilisateurs à anonymizer : ${nbOfUsers}`);
await deps.anonymizeUsersQuery();
}
);

0 comments on commit 7eb6b86

Please sign in to comment.