Skip to content

Commit

Permalink
add featuredItemsSize config param
Browse files Browse the repository at this point in the history
  • Loading branch information
giuseppedipinto committed Apr 23, 2024
1 parent 5c73199 commit d9e0208
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ const defaultConfig: IoDevServerConfig = {
sessionTTLinMS: 60000
},
service: {
featuredItemsSize: 5,
response: {
featuredItemsResponseCode: 200,
institutionsResponseCode: 200
Expand Down
9 changes: 6 additions & 3 deletions src/features/services/payloads/get-featured-items.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { nonEmptyArray } from "fp-ts";
import * as A from "fp-ts/Array";
import * as A from "fp-ts/lib/Array";
import * as O from "fp-ts/lib/Option";
import { pipe } from "fp-ts/lib/function";
import { FeaturedItem } from "../../../../generated/definitions/services/FeaturedItem";
import { FeaturedItems } from "../../../../generated/definitions/services/FeaturedItems";
import { ioDevServerConfig } from "../../../config";
import ServicesDB from "../../../persistence/services";
import { getInstitutionsResponsePayload } from "./get-institutions";

const featuredItemsSize = ioDevServerConfig.features.service.featuredItemsSize;

/**
* Returns a random ordered array subset.
* @param array starting array of type T
Expand All @@ -16,7 +19,7 @@ import { getInstitutionsResponsePayload } from "./get-institutions";
const getRandomArraySubset = <T>(array: T[], size: number): T[] =>
pipe(
O.some(array),
O.fromPredicate(arr => O.isSome(arr) && size <= array.length),
O.fromPredicate(arr => O.isSome(arr) && size > 0 && size <= array.length),
O.fold(
() => [],
() => {
Expand Down Expand Up @@ -92,7 +95,7 @@ export const getFeaturedItemsResponsePayload = (): FeaturedItems => {
...featuredIntitutions,
...featuredNationalServices
],
arr => getRandomArraySubset(arr, arr.length)
arr => getRandomArraySubset(arr, featuredItemsSize)
);

return {
Expand Down
7 changes: 5 additions & 2 deletions src/features/services/types/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { WithinRangeNumber } from "@pagopa/ts-commons/lib/numbers";
import * as t from "io-ts";
import { HttpResponseCode } from "../../../types/httpResponseCode";

export const ServiceConfiguration = t.interface({
export const ServiceConfiguration = t.type({
// configure number of featured items
featuredItemsSize: WithinRangeNumber(0, 6),
// configure some API response error code
response: t.interface({
response: t.type({
// 200 success with payload
featuredItemsResponseCode: HttpResponseCode,
institutionsResponseCode: HttpResponseCode
Expand Down

0 comments on commit d9e0208

Please sign in to comment.