Skip to content

Commit

Permalink
fix PG search Immersion wihout geoparams
Browse files Browse the repository at this point in the history
  • Loading branch information
bbohec committed Jul 24, 2024
1 parent 84563a4 commit ffe3b57
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,40 @@ describe("PgEstablishmentAggregateRepository", () => {
);
});

it("no location but keys provided in params - case occured from usecase without location", async () => {
await pgEstablishmentAggregateRepository.insertEstablishmentAggregate(
establishmentWithOfferA1101_AtPosition,
);

expectToEqual(
await pgEstablishmentAggregateRepository.searchImmersionResults({
searchMade: {
lat: undefined,
lon: undefined,
distanceKm: undefined,
sortedBy: "date",
voluntaryToImmersion: true,
place: undefined,
appellationCodes: undefined,
romeCode: undefined,
establishmentSearchableBy: "jobSeekers",
acquisitionCampaign: undefined,
acquisitionKeyword: undefined,
},
}),
[
makeExpectedSearchResult({
establishment: establishmentWithOfferA1101_AtPosition,
withOffers: [offer_A1101_11987],
withLocationAndDistance: {
...locationOfSearchPosition,
distance: undefined,
},
}),
],
);
});

describe("with `maxResults` parameter", () => {
beforeEach(async () => {
await pgEstablishmentAggregateRepository.insertEstablishmentAggregate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const makeExpectedSearchResult = ({
}: {
establishment: EstablishmentAggregate;
withOffers: OfferEntity[];
withLocationAndDistance: Location & { distance: number };
withLocationAndDistance: Location & { distance?: number };
}): SearchResultDto => {
const firstOffer = withOffers.at(0);
if (!firstOffer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,9 @@ const searchImmersionResultsQuery = (
sortedBy: SearchSortedBy;
},
) => {
const geoParams = filters?.geoParams;
const searchableBy = filters?.searchableBy;

const query = transaction
.with("filtered_results", (qb) =>
pipeWithValue(
Expand All @@ -888,7 +891,6 @@ const searchImmersionResultsQuery = (
.select("siret")
.where("is_open", "=", true),
(qb) => {
const searchableBy = filters?.searchableBy;
if (searchableBy === "jobSeekers")
return qb.whereRef(
"searchable_by_job_seekers",
Expand All @@ -907,9 +909,8 @@ const searchImmersionResultsQuery = (
eb
.selectFrom("establishments_locations")
.select(["establishment_siret as siret", "id", "position"]),
(eb) => {
const geoParams = filters?.geoParams;
return geoParams
(eb) =>
geoParams && hasSearchGeoParams(geoParams)
? eb.where(({ fn }) =>
fn("ST_DWithin", [
"position",
Expand All @@ -919,8 +920,7 @@ const searchImmersionResultsQuery = (
sql`${(1000 * geoParams.distanceKm).toString()}`,
]),
)
: eb;
},
: eb,
).as("loc"),
(join) => join.onRef("loc.siret", "=", "e.siret"),
)
Expand Down Expand Up @@ -979,10 +979,8 @@ const searchImmersionResultsQuery = (
),
)
.innerJoin("public_romes_data as ro", "ro.code_rome", "r.code_rome")
.select(({ ref, fn }) => {
const geoParams = filters?.geoParams;

return jsonStripNulls(
.select(({ ref, fn }) =>
jsonStripNulls(
jsonBuildObject({
naf: ref("e.naf_code"),
siret: ref("e.siret"),
Expand All @@ -1009,7 +1007,7 @@ const searchImmersionResultsQuery = (
lat: ref("loc.lat"),
}),
locationId: ref("loc.id"),
...(geoParams
...(geoParams && hasSearchGeoParams(geoParams)
? {
distance_m: fn("ST_Distance", [
ref("loc.position"),
Expand All @@ -1022,8 +1020,8 @@ const searchImmersionResultsQuery = (
voluntaryToImmersion: sql`TRUE`,
appellations: ref("r.appelations"),
}),
).as("search_immersion_result");
})
).as("search_immersion_result"),
)
.orderBy("r.rank");

return query.execute();
Expand Down

0 comments on commit ffe3b57

Please sign in to comment.