Skip to content

Commit

Permalink
fix score calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
JeromeBu committed Sep 25, 2024
1 parent 3c51c86 commit 121ecc9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { subMonths } from "date-fns";
import { Pool } from "pg";
import { DiscussionBuilder, Exchange, expectToEqual } from "shared";
import {
AgencyDtoBuilder,
ConventionDtoBuilder,
DiscussionBuilder,
Exchange,
expectToEqual,
} from "shared";
import { KyselyDb, makeKyselyDb } from "../../../config/pg/kysely/kyselyUtils";
import { getTestPgPool } from "../../../config/pg/pgUtils";
import { PgAgencyRepository } from "../../agency/adapters/PgAgencyRepository";
import { PgConventionRepository } from "../../convention/adapters/PgConventionRepository";
import { EstablishmentAggregateBuilder } from "../helpers/EstablishmentBuilders";
import { PgDiscussionRepository } from "./PgDiscussionRepository";
import { PgEstablishmentAggregateRepository } from "./PgEstablishmentAggregateRepository";
Expand All @@ -20,6 +28,9 @@ describe("SQL queries, independent from PgEstablishmentAggregateRepository", ()
beforeEach(async () => {
await db.deleteFrom("establishments").execute();
await db.deleteFrom("discussions").execute();
await db.deleteFrom("conventions").execute();
await db.deleteFrom("agency_groups__agencies").execute();
await db.deleteFrom("agencies").execute();
});

afterAll(async () => {
Expand All @@ -32,12 +43,26 @@ describe("SQL queries, independent from PgEstablishmentAggregateRepository", ()
new PgEstablishmentAggregateRepository(db);

const pgDiscussionRepository = new PgDiscussionRepository(db);
const pgConventionRepository = new PgConventionRepository(db);
const pgAgencyRepository = new PgAgencyRepository(db);

const establishment = new EstablishmentAggregateBuilder()
.withScore(0)
.build();
const { siret } = establishment.establishment;

const agency = new AgencyDtoBuilder().build();
await pgAgencyRepository.insert(agency);

const convention = new ConventionDtoBuilder()
.withSiret(siret)
.withStatus("ACCEPTED_BY_VALIDATOR")
.withAgencyId(agency.id)
.withDateSubmission(new Date().toISOString())
.build();

await pgConventionRepository.save(convention);

const initialExchange: Exchange = {
subject: "Hello",
message: "Initial message",
Expand Down Expand Up @@ -106,7 +131,11 @@ describe("SQL queries, independent from PgEstablishmentAggregateRepository", ()
siret,
);

expectToEqual(establishmentAfter?.establishment.score, 110);
const minimunScore = 10;
const discussionScore = 100 * (1 / 2);
const conventionScore = 10 * 1;
const expectedScore = minimunScore + discussionScore + conventionScore;
expectToEqual(establishmentAfter?.establishment.score, expectedScore);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,31 @@ export const updateAllEstablishmentScoresQuery = async (
.selectFrom("discussions as d")
.innerJoin("exchanges", "d.id", "exchanges.discussion_id")
.where("d.created_at", ">=", sql<Date>`NOW() - INTERVAL '1 year'`)
.where("exchanges.sender", "=", "establishment")
.select(["siret", sql`COUNT(DISTINCT d.id)`.as("discussion_count")])
.select([
"siret",
sql`COUNT(DISTINCT d.id)`.as("total_discussions"),
sql`COUNT(DISTINCT CASE WHEN exchanges.sender = 'establishment'::exchange_role THEN d.id END)`.as(
"answered_discussions",
),
])
.groupBy("siret"),
)
.updateTable("establishments as e")
.set({
score: sql`${minimumScore} +
COALESCE((SELECT convention_count * ${conventionCountCoefficient} FROM convention_counts WHERE siret = e.siret), 0) +
COALESCE((SELECT discussion_count * ${discussionCountCoefficient} FROM discussion_counts WHERE siret = e.siret), 0)`,
score: sql`ROUND(
${minimumScore}
+ COALESCE((SELECT convention_count * ${conventionCountCoefficient} FROM convention_counts WHERE siret = e.siret), 0)
+ COALESCE((
SELECT
CASE
WHEN total_discussions > 0
THEN (answered_discussions::float / total_discussions) * ${discussionCountCoefficient}
ELSE 0
END
FROM discussion_counts
WHERE siret = e.siret
), 0)
)`,
})
.execute();
};

0 comments on commit 121ecc9

Please sign in to comment.