Skip to content

Commit

Permalink
Merge pull request #838 from navikt/feature/under18
Browse files Browse the repository at this point in the history
Legg til under 18 år i debug panelet, samt i spørringer uten at det brukes. Selve søkefelter ligger da klart og kan tas i bruk når vi har nok data.
  • Loading branch information
kenove authored Oct 23, 2024
2 parents 14ceab9 + a23f7f5 commit 6d3ee51
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/app/(sok)/_components/filters/Under18.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React, { ReactElement } from "react";
import { BodyShort, Checkbox, CheckboxGroup } from "@navikt/ds-react";
import mergeCount from "@/app/(sok)/_components/utils/mergeCount";
import { logFilterChanged } from "@/app/_common/monitoring/amplitude";
import { QueryNames } from "@/app/(sok)/_utils/QueryNames";
import useQuery from "@/app/(sok)/_components/QueryProvider";
import { FilterAggregation } from "@/app/(sok)/_types/FilterAggregations";

interface Under18Props {
initialValues: FilterAggregation[];
updatedValues: FilterAggregation[];
}

export default function Under18({ initialValues, updatedValues }: Under18Props): ReactElement {
const sortedValues = sortUnder18Values(initialValues);
const values = mergeCount(sortedValues, updatedValues);
const valuesWithOnlyUnder18 = values.filter((item) => item.key === "true");

const query = useQuery();

function handleChange(e: React.ChangeEvent<HTMLInputElement>): void {
const { value, checked } = e.target;
if (checked) {
query.append(QueryNames.UNDER18, value);
} else {
query.remove(QueryNames.UNDER18, value);
}
logFilterChanged({ name: "Under18", value, checked });
}

return (
<CheckboxGroup
value={query.getAll(QueryNames.UNDER18)}
className="mb-4"
legend={
<>
<BodyShort as="span" visuallyHidden>
Filtrer etter{" "}
</BodyShort>
<span>Under 18 år</span>
</>
}
>
{valuesWithOnlyUnder18.map((item) => (
<Checkbox name="under18[]" key={item.key} value={item.key} onChange={handleChange}>
{`${labelForUnder18(item.key)} (${item.count})`}
</Checkbox>
))}
</CheckboxGroup>
);
}

export const labelForUnder18 = (key: string): string => {
switch (key) {
case "true":
return "Passer for deg under 18 år";
case "false":
return "18 år eller over";
default:
return key;
}
};

function sortUnder18Values(facets: FilterAggregation[]): FilterAggregation[] {
if (!facets) {
return [];
}
const sortedPublishedValues = ["true", "false", "Ikke oppgitt"];
return facets.sort((a, b) => sortedPublishedValues.indexOf(a.key) - sortedPublishedValues.indexOf(b.key));
}
8 changes: 8 additions & 0 deletions src/app/(sok)/_components/searchBox/buildSelectedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ComboboxOption } from "@navikt/ds-react/cjs/form/combobox/types";
import { QueryNames } from "@/app/(sok)/_utils/QueryNames";
import { editedItemKey as editedOccupation } from "@/app/(sok)/_components/filters/Occupations";
import { PublishedLabels } from "@/app/(sok)/_utils/publishedLabels";
import { labelForUnder18 } from "@/app/(sok)/_components/filters/Under18";

function buildOption(key: string, value: string): ComboboxOption | undefined {
switch (key) {
Expand Down Expand Up @@ -91,6 +92,13 @@ function buildOption(key: string, value: string): ComboboxOption | undefined {
label: labelForNeedDriversLicense(value),
value: `${QueryNames.NEED_DRIVERS_LICENSE}-${value}`,
};
case QueryNames.UNDER18:
return value === "Ikke oppgitt"
? { label: "Under 18 ikke oppgitt", value: `${QueryNames.UNDER18}-${value}` }
: {
label: labelForUnder18(value),
value: `${QueryNames.UNDER18}-${value}`,
};
case QueryNames.EXPERIENCE:
return value === "Ikke oppgitt"
? { label: "Erfaring ikke oppgitt", value: `${QueryNames.EXPERIENCE}-${value}` }
Expand Down
1 change: 1 addition & 0 deletions src/app/(sok)/_types/FilterAggregations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default interface FilterAggregations {
remote: FilterAggregation[];
needDriversLicense: FilterAggregation[];
experience: FilterAggregation[];
under18: FilterAggregation[];
publishedTotalCount: number;
totalInternational: number;
}
1 change: 1 addition & 0 deletions src/app/(sok)/_utils/QueryNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const QueryNames = {
INTERNATIONAL: "international",
MUNICIPAL: "municipal",
NEED_DRIVERS_LICENSE: "needDriversLicense",
UNDER18: "under18",
OCCUPATION: "occupation",
OCCUPATION_FIRST_LEVEL: "occupationLevel1",
OCCUPATION_SECOND_LEVEL: "occupationLevel2",
Expand Down
77 changes: 77 additions & 0 deletions src/app/(sok)/_utils/elasticSearchRequestBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,41 @@ function filterNeedDriversLicense(needDriversLicense) {
return filters;
}

function filterUnder18(under18) {
const filters = [];
if (under18 && under18.length > 0) {
const filter = {
bool: {
should: [],
},
};
under18.forEach((item) => {
filter.bool.should.push({
term: {
under18_facet: item,
},
});
});

if (under18.includes("Ikke oppgitt")) {
filter.bool.should.push({
bool: {
must_not: [
{
exists: {
field: "under18_facet",
},
},
],
},
});
}

filters.push(filter);
}
return filters;
}

function filterExperience(experience) {
const filters = [];
if (experience && experience.length > 0) {
Expand Down Expand Up @@ -681,6 +716,7 @@ const elasticSearchRequestBody = (query) => {
education,
municipals,
needDriversLicense,
under18,
extent,
workLanguage,
remote,
Expand Down Expand Up @@ -713,6 +749,7 @@ const elasticSearchRequestBody = (query) => {
filter: [
...filterJanzzOccupation(occupations),
...filterNeedDriversLicense(needDriversLicense),
...filterUnder18(under18),
...filterExperience(experience),
...filterExtent(extent),
...filterEducation(education),
Expand All @@ -738,6 +775,7 @@ const elasticSearchRequestBody = (query) => {
"properties.applicationdue",
"properties.hasInterestform",
"properties.needDriversLicense",
"properties.under18",
"properties.experience",
"properties.education",
"properties.workLanguage",
Expand Down Expand Up @@ -774,6 +812,7 @@ const elasticSearchRequestBody = (query) => {
filter: [
...filterJanzzOccupation(occupations),
...filterNeedDriversLicense(needDriversLicense),
...filterUnder18(under18),
...filterExperience(experience),
...filterExtent(extent),
...filterEducation(education),
Expand Down Expand Up @@ -803,6 +842,7 @@ const elasticSearchRequestBody = (query) => {
filter: [
...filterJanzzOccupation(occupations),
...filterNeedDriversLicense(needDriversLicense),
...filterUnder18(under18),
...filterExperience(experience),
...filterExtent(extent),
...filterEducation(education),
Expand Down Expand Up @@ -845,6 +885,7 @@ const elasticSearchRequestBody = (query) => {
filter: [
...filterJanzzOccupation(occupations),
...filterNeedDriversLicense(needDriversLicense),
...filterUnder18(under18),
...filterExperience(experience),
...filterExtent(extent),
...filterEducation(education),
Expand All @@ -870,6 +911,7 @@ const elasticSearchRequestBody = (query) => {
filter: [
...filterJanzzOccupation(occupations),
...filterNeedDriversLicense(needDriversLicense),
...filterUnder18(under18),
...filterExperience(experience),
...filterRemote(remote),
...filterEducation(education),
Expand Down Expand Up @@ -917,6 +959,7 @@ const elasticSearchRequestBody = (query) => {
filter: [
...filterJanzzOccupation(occupations),
...filterNeedDriversLicense(needDriversLicense),
...filterUnder18(under18),
...filterExperience(experience),
...filterEducation(education),
...filterWorkLanguage(workLanguage),
Expand All @@ -941,6 +984,7 @@ const elasticSearchRequestBody = (query) => {
filter: [
...filterJanzzOccupation(occupations),
...filterNeedDriversLicense(needDriversLicense),
...filterUnder18(under18),
...filterExperience(experience),
...filterExtent(extent),
...filterRemote(remote),
Expand All @@ -966,6 +1010,7 @@ const elasticSearchRequestBody = (query) => {
filter: [
...filterJanzzOccupation(occupations),
...filterNeedDriversLicense(needDriversLicense),
...filterUnder18(under18),
...filterExperience(experience),
...filterExtent(extent),
...filterRemote(remote),
Expand Down Expand Up @@ -993,6 +1038,7 @@ const elasticSearchRequestBody = (query) => {
...filterExperience(experience),
...filterExtent(extent),
...filterEducation(education),
...filterUnder18(under18),
...filterRemote(remote),
...filterWorkLanguage(workLanguage),
filterLocation(counties, municipals, countries, international),
Expand All @@ -1010,12 +1056,39 @@ const elasticSearchRequestBody = (query) => {
},
},
},
under18: {
filter: {
bool: {
filter: [
...filterJanzzOccupation(occupations),
...filterExperience(experience),
...filterExtent(extent),
...filterEducation(education),
...filterNeedDriversLicense(needDriversLicense),
...filterRemote(remote),
...filterWorkLanguage(workLanguage),
filterLocation(counties, municipals, countries, international),
filterOccupation(occupationFirstLevels, occupationSecondLevels),
...filterEngagementType(engagementType),
...filterSector(sector),
...filterPublished(published),
filterWithinDrivingDistance(withinDrivingDistance),
],
},
},
aggs: {
values: {
terms: { field: "under18_facet", missing: NOT_DEFINED },
},
},
},
experience: {
filter: {
bool: {
filter: [
...filterJanzzOccupation(occupations),
...filterNeedDriversLicense(needDriversLicense),
...filterUnder18(under18),
...filterExtent(extent),
...filterEducation(education),
...filterRemote(remote),
Expand All @@ -1040,6 +1113,7 @@ const elasticSearchRequestBody = (query) => {
filter: [
...filterJanzzOccupation(occupations),
...filterNeedDriversLicense(needDriversLicense),
...filterUnder18(under18),
...filterExperience(experience),
...filterExtent(extent),
...filterEducation(education),
Expand All @@ -1065,6 +1139,7 @@ const elasticSearchRequestBody = (query) => {
filter: [
...filterJanzzOccupation(occupations),
...filterNeedDriversLicense(needDriversLicense),
...filterUnder18(under18),
...filterExperience(experience),
...filterExtent(extent),
...filterEducation(education),
Expand Down Expand Up @@ -1117,6 +1192,7 @@ const elasticSearchRequestBody = (query) => {
filter: [
...filterJanzzOccupation(occupations),
...filterNeedDriversLicense(needDriversLicense),
...filterUnder18(under18),
...filterExperience(experience),
...filterExtent(extent),
...filterEducation(education),
Expand Down Expand Up @@ -1168,6 +1244,7 @@ const elasticSearchRequestBody = (query) => {
filter: [
...filterJanzzOccupation(occupations),
...filterNeedDriversLicense(needDriversLicense),
...filterUnder18(under18),
...filterExperience(experience),
...filterExtent(extent),
...filterEducation(education),
Expand Down
2 changes: 2 additions & 0 deletions src/app/(sok)/_utils/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const defaultQuery = {
counties: [],
countries: [],
needDriversLicense: [],
under18: [],
education: [],
engagementType: [],
experience: [],
Expand Down Expand Up @@ -63,6 +64,7 @@ export function createQuery(searchParams) {
distance: searchParams.distance || defaultQuery.distance,
published: searchParams.published || defaultQuery.published,
needDriversLicense: asArray(searchParams.needDriversLicense) || defaultQuery.needDriversLicense,
under18: asArray(searchParams.under18) || defaultQuery.under18,
experience: asArray(searchParams.experience) || defaultQuery.experience,
extent: asArray(searchParams.extent) || defaultQuery.extent,
engagementType: asArray(searchParams.engagementType) || defaultQuery.engagementType,
Expand Down
4 changes: 4 additions & 0 deletions src/app/(sok)/_utils/simplifySearchResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export default function simplifySearchResponse(response) {
key: item.key,
count: item.doc_count,
})),
under18: response.aggregations.under18.values.buckets.map((item) => ({
key: item.key,
count: item.doc_count,
})),
experience: response.aggregations.experience.values.buckets.map((item) => ({
key: item.key,
count: item.doc_count,
Expand Down
2 changes: 2 additions & 0 deletions src/app/lib/stillingSoekSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const propertiesSchema = z.object({
remote: z.string().optional(),
adtext: z.string().optional(),
needDriversLicense: z.union([z.array(z.string()), z.undefined()]),
under18: z.union([z.array(z.string()), z.undefined()]),
hasInterestform: z.string().optional(),
workLanguage: z.union([z.array(z.string()), z.undefined()]),
applicationemail: z.string().optional(),
Expand Down Expand Up @@ -179,6 +180,7 @@ export const transformed = elasticSearchAdResultSchema.transform(({ _source, _id
education: properties?.education,
experience: properties?.experience,
needDriversLicense: properties?.needDriversLicense,
under18: properties?.under18,
};
});
export type AdDTORAWSchema = z.infer<typeof adDTORAWSchema>;
Expand Down
1 change: 1 addition & 0 deletions src/app/stilling/FetchAd.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const sourceIncludes = [
"properties.education",
"properties.employer",
"properties.needDriversLicense",
"properties.under18",
"properties.employerdescription",
"properties.employerhomepage",
"properties.engagementtype",
Expand Down
7 changes: 7 additions & 0 deletions src/app/stilling/[id]/_components/DebugAd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CheckmarkIcon, ExclamationmarkTriangleIcon, ThumbUpIcon, XMarkIcon } fr
import logAmplitudeEvent from "@/app/_common/monitoring/amplitude";
import { useRouter } from "next/navigation";
import { MappedAdDTO } from "@/app/lib/stillingSoekSchema";
import { labelForUnder18 } from "@/app/(sok)/_components/filters/Under18";

type DebugAdItemProps = {
value: Value;
Expand Down Expand Up @@ -190,6 +191,11 @@ export default function DebugAd({ adData }: PageProps): ReactNode {
isChecked: true,
}));

const under18Values = adData?.under18?.map((it) => ({
label: labelForUnder18(it),
isChecked: true,
}));

return (
<Box className="debugAd">
<Box paddingInline="4 4" paddingBlock="4 0">
Expand All @@ -209,6 +215,7 @@ export default function DebugAd({ adData }: PageProps): ReactNode {
<DebugAdGroup adUuid={adData.id} category="Erfaring" values={experienceValues} />
<DebugAdGroup adUuid={adData.id} category="Utdanning" values={educationValues} />
<DebugAdGroup adUuid={adData.id} category="Førerkort" values={driverLicenseValues} />
<DebugAdGroup adUuid={adData.id} category="Under18" values={under18Values} />

<Box paddingInline="4 4" paddingBlock="0 0">
<Button
Expand Down
1 change: 1 addition & 0 deletions src/app/stilling/_data/adDataActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const sourceIncludes = [
"categoryList", // For debugging
"properties.searchtags", // For debugging
"properties.needDriversLicense", // For debugging
"properties.under18", // For debugging
"properties.education", // For debugging
"properties.experience", // For debugging
"properties.experience", // For debugging
Expand Down

0 comments on commit 6d3ee51

Please sign in to comment.