-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #370 from pagopa/IOPAE-1117-featured-items-api-mock
[IOPAE-1117] Add services `/featured` api mock
- Loading branch information
Showing
6 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as A from "fp-ts/lib/Array"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
import _ from "lodash"; | ||
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; | ||
|
||
export const getFeaturedItemsResponsePayload = (): FeaturedItems => { | ||
// take some casual national service | ||
const selectedNationalServices = _.sampleSize( | ||
ServicesDB.getNationalServices(), | ||
1 | ||
); | ||
// take some casual special service | ||
const selectedSpecialServices = _.sampleSize( | ||
ServicesDB.getSpecialServices(), | ||
3 | ||
); | ||
// take some casual institutions | ||
const featuredIntitutions = _.sampleSize( | ||
Array.from(getInstitutionsResponsePayload().institutions), | ||
1 | ||
); | ||
|
||
/** | ||
* Map national services to FeaturedService[] (add organization_name for layout testing purpose) | ||
*/ | ||
const featuredNationalServices: FeaturedItem[] = pipe( | ||
selectedNationalServices, | ||
A.map(service => ({ | ||
id: service.service_id, | ||
name: service.service_name, | ||
version: service.version, | ||
organization_name: service.organization_name | ||
})) | ||
); | ||
|
||
/** | ||
* Reduce special services to FeaturedService[] | ||
*/ | ||
const featuredSpecialServices: FeaturedItem[] = pipe( | ||
selectedSpecialServices, | ||
A.map(service => ({ | ||
id: service.service_id, | ||
name: service.service_name, | ||
version: service.version | ||
})) | ||
); | ||
|
||
// returns randomly ordered featured items | ||
const featuredItems = _.sampleSize( | ||
[ | ||
...featuredSpecialServices, | ||
...featuredIntitutions, | ||
...featuredNationalServices | ||
], | ||
featuredItemsSize | ||
); | ||
|
||
return { | ||
items: featuredItems | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as O from "fp-ts/lib/Option"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
import { ioDevServerConfig } from "../../../config"; | ||
import { addHandler } from "../../../payloads/response"; | ||
import { getFeaturedItemsResponsePayload } from "../payloads/get-featured-items"; | ||
import { addApiV2Prefix, serviceRouter } from "./router"; | ||
|
||
const serviceConfig = ioDevServerConfig.features.service; | ||
|
||
// Retrieve featured items | ||
addHandler(serviceRouter, "get", addApiV2Prefix("/featured"), (_, res) => | ||
pipe( | ||
serviceConfig.response.featuredItemsResponseCode, | ||
O.fromPredicate(statusCode => statusCode !== 200), | ||
O.fold( | ||
() => | ||
pipe( | ||
O.of(getFeaturedItemsResponsePayload()), | ||
O.fold( | ||
() => res.status(404), | ||
featuredItems => res.status(200).json(featuredItems) | ||
) | ||
), | ||
statusCode => res.sendStatus(statusCode) | ||
) | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import "./featured"; | ||
import "./institutions"; | ||
|
||
export { serviceRouter } from "./router"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters