Skip to content

Commit

Permalink
improve query in siggest establishment update
Browse files Browse the repository at this point in the history
  • Loading branch information
JeromeBu committed Oct 7, 2024
1 parent 462f33a commit e39d217
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Pool } from "pg";
import { expectToEqual } from "shared";
import { KyselyDb, makeKyselyDb } from "../../../config/pg/kysely/kyselyUtils";
import { getTestPgPool } from "../../../config/pg/pgUtils";
import { PgOutboxRepository } from "../../core/events/adapters/PgOutboxRepository";
import { PgNotificationRepository } from "../../core/notifications/adapters/PgNotificationRepository";
import { EstablishmentAggregateBuilder } from "../helpers/EstablishmentBuilders";
import { PgEstablishmentAggregateRepository } from "./PgEstablishmentAggregateRepository";
Expand All @@ -13,13 +12,11 @@ describe("PgScriptsQueries", () => {
let pool: Pool;
let db: KyselyDb;
let pgEstablishmentAggregateRepository: PgEstablishmentAggregateRepository;
let pgOutboxRepository: PgOutboxRepository;
let pgNotificationRepository: PgNotificationRepository;

beforeAll(async () => {
pool = getTestPgPool();
db = makeKyselyDb(pool);
pgOutboxRepository = new PgOutboxRepository(db);
pgNotificationRepository = new PgNotificationRepository(db);
pgEstablishmentAggregateRepository = new PgEstablishmentAggregateRepository(
db,
Expand Down Expand Up @@ -53,35 +50,6 @@ describe("PgScriptsQueries", () => {
.withLocationId("aaaaaaaa-aaaa-4000-aaaa-aaaaaaaaaaaa")
.build();

// <<<<<----------- this is the legacy behavior, we keep it until we reach the 6 months.
// We can remove this part of the code, and the FormEstablishmentEditLinkSent events in january 2024

const establishmentWithLinkSentEvent = new EstablishmentAggregateBuilder()
.withEstablishmentSiret("22220000222200")
.withEstablishmentUpdatedAt(toUpdateDate)
.withContactId("22222222-2222-4000-2222-222222222222")
.withLocationId("aaaaaaaa-aaaa-4000-bbbb-bbbbbbbbbbbb")
.build();

await pgOutboxRepository.save({
id: "22222222-2222-4000-2222-000000000000",
topic: "FormEstablishmentEditLinkSent",
payload: {
siret: establishmentWithLinkSentEvent.establishment.siret,
version: 1,
triggeredBy: {
kind: "establishment-magic-link",
siret: establishmentWithLinkSentEvent.establishment.siret,
},
},
occurredAt: addDays(before, 1).toISOString(),
publications: [],
status: "never-published",
wasQuarantined: false,
});

// end of legacy ----------->>>>>>

const eventWithNotificationSavedButLongAgo =
new EstablishmentAggregateBuilder()
.withEstablishmentSiret("33330000333300")
Expand Down Expand Up @@ -147,7 +115,6 @@ describe("PgScriptsQueries", () => {
establishmentToUpdate,
eventWithNotificationSavedButLongAgo,
eventWithRecentNotificationSaved,
establishmentWithLinkSentEvent,
recentlyUpdatedEstablishment,
].map((aggregate) =>
pgEstablishmentAggregateRepository.insertEstablishmentAggregate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,6 @@ export class PgEstablishmentAggregateRepository
.select("establishments.siret")
.distinct()
.where("establishments.update_date", "<", before)
.where(({ not, exists, selectFrom }) =>
not(
exists(
selectFrom("outbox as o")
.select(sql`1`.as("_"))
.where("o.topic", "=", "FormEstablishmentEditLinkSent")
.whereRef(sql`o.payload ->> 'siret'`, "=", "establishments.siret")
.where("o.occurred_at", ">", before)
.limit(1),
),
),
)
.where(({ not, exists, selectFrom }) =>
not(
exists(
Expand Down

0 comments on commit e39d217

Please sign in to comment.