Skip to content

Commit

Permalink
return also number of updated establishments
Browse files Browse the repository at this point in the history
  • Loading branch information
JeromeBu committed Dec 17, 2024
1 parent 8ec216c commit 093fc7b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,11 @@ describe("SQL queries, independent from PgEstablishmentAggregateRepository", ()
);

// Run the deactivation query
await deactivateUnresponsiveEstablishmentsQuery(db);
const updatedEstablishments =
await deactivateUnresponsiveEstablishmentsQuery(db);

// Verify the count of updated establishments
expectToEqual(updatedEstablishments.length, 1);

// Verify the establishment was deactivated using direct DB query
const result = await db
Expand Down Expand Up @@ -290,7 +294,11 @@ describe("SQL queries, independent from PgEstablishmentAggregateRepository", ()
);

// Run the deactivation query
await deactivateUnresponsiveEstablishmentsQuery(db);
const updatedEstablishments =
await deactivateUnresponsiveEstablishmentsQuery(db);

// Verify no establishments were updated
expectToEqual(updatedEstablishments.length, 0);

// Verify the establishment was not deactivated using direct DB query
const result = await db
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,5 @@ export const deactivateUnresponsiveEstablishmentsQuery = (db: KyselyDb) =>
),
),
)
.returningAll()
.execute();
13 changes: 10 additions & 3 deletions back/src/scripts/triggerDeactivateUnresponsiveEstablishments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ import { handleCRONScript } from "./handleCRONScript";
const logger = createLogger(__filename);
const config = AppConfig.createFromEnv();

type Report = { status: "success" } | { status: "error"; message: string };
type Report =
| { status: "success"; updatedCount: number }
| { status: "error"; message: string };

const deactivateUnresponsiveEstablishments = async (): Promise<Report> => {
const pool = new Pool({ connectionString: config.pgImmersionDbUrl });
const db = makeKyselyDb(pool);

return deactivateUnresponsiveEstablishmentsQuery(db)
.then((): Report => ({ status: "success" }))
.then(
(updatedEstablishments): Report => ({
status: "success",
updatedCount: updatedEstablishments.length,
}),
)
.catch(
(e): Report => ({
status: "error",
Expand All @@ -31,7 +38,7 @@ handleCRONScript(
deactivateUnresponsiveEstablishments,
(report) =>
report.status === "success"
? "Unresponsive establishments deactivated successfully"
? `${report.updatedCount} unresponsive establishments deactivated successfully`
: `Error deactivating unresponsive establishments: ${report.message}`,
logger,
);

0 comments on commit 093fc7b

Please sign in to comment.