-
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.
- Loading branch information
Showing
12 changed files
with
219 additions
and
38 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
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 @@ | ||
export { serviceRouter } from "./routers"; |
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,73 @@ | ||
import * as A from "fp-ts/lib/Array"; | ||
import { identity, pipe } from "fp-ts/lib/function"; | ||
import _ from "lodash"; | ||
import { ServicePublic } from "../../../../generated/definitions/backend/ServicePublic"; | ||
import { ServiceScopeEnum } from "../../../../generated/definitions/backend/ServiceScope"; | ||
import { Institution } from "../../../../generated/definitions/services/Institution"; | ||
import { InstitutionsResource } from "../../../../generated/definitions/services/InstitutionsResource"; | ||
import ServicesDB from "../../../persistence/services"; | ||
|
||
const filterByScope = (service: ServicePublic, scope?: ServiceScopeEnum) => { | ||
if (!scope) { | ||
return true; | ||
} | ||
return service.service_metadata?.scope === scope; | ||
}; | ||
|
||
const filterBySearch = (service: ServicePublic, search?: string) => { | ||
if (!search) { | ||
return true; | ||
} | ||
return service.service_name.toLowerCase().includes(search); | ||
}; | ||
|
||
// services?offset=0&limit=20 | ||
// services?offset=20&limit=20 | ||
// services?offset=40&limit=20 | ||
export const getInstitutionsResponsePayload = ( | ||
limit: number = 20, | ||
offset: number = 0, | ||
scope?: ServiceScopeEnum, | ||
search?: string | ||
): InstitutionsResource => { | ||
const filteredInstitutions = pipe( | ||
ServicesDB.getAllServices(), | ||
A.reduce([] as Institution[], (accumulator, service) => { | ||
const isValidService = pipe( | ||
[ | ||
(service: ServicePublic) => filterByScope(service, scope), | ||
(service: ServicePublic) => filterBySearch(service, search) | ||
], | ||
A.flap(service), | ||
A.every(identity) | ||
); | ||
|
||
if (isValidService) { | ||
return [ | ||
...accumulator, | ||
{ | ||
id: service.service_id, | ||
name: service.service_name, | ||
fiscal_code: service.organization_fiscal_code | ||
} | ||
]; | ||
} | ||
|
||
return accumulator; | ||
}) | ||
); | ||
|
||
const totalElements = filteredInstitutions.length; | ||
const startIndex = offset; | ||
const endIndex = offset + limit; | ||
const istitutionList = _.slice(filteredInstitutions, startIndex, endIndex); | ||
|
||
const response: InstitutionsResource = { | ||
institutions: istitutionList, | ||
limit, | ||
offset, | ||
count: totalElements | ||
}; | ||
|
||
return response; | ||
}; |
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,3 @@ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { sequenceT } from "fp-ts/lib/Apply"; | ||
import * as O from "fp-ts/lib/Option"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
import { addHandler } from "../../../payloads/response"; | ||
import { addApiV1Prefix } from "../../../utils/strings"; | ||
import { ServiceScope } from "../../../../generated/definitions/backend/ServiceScope"; | ||
import { getInstitutionsResponsePayload } from "../payloads/get-institutions"; | ||
import { ioDevServerConfig } from "../../../config"; | ||
import { serviceRouter } from "./router"; | ||
|
||
type Query = string | qs.ParsedQs | string[] | qs.ParsedQs[] | undefined; | ||
|
||
const extractQuery = (query: Query) => | ||
pipe( | ||
query, | ||
O.fromNullable, | ||
O.map(s => parseInt(s as string, 10)), | ||
O.toUndefined | ||
); | ||
|
||
addHandler(serviceRouter, "get", addApiV1Prefix("/institutions"), (req, res) => | ||
pipe( | ||
ioDevServerConfig.features.service.response.institutionsResponseCode, | ||
O.fromPredicate(statusCode => statusCode !== 200), | ||
O.fold( | ||
() => | ||
pipe( | ||
sequenceT(O.Monad)( | ||
O.of(pipe(req.query.limit, extractQuery)), | ||
O.of(pipe(req.query.offset, extractQuery)), | ||
O.of( | ||
pipe( | ||
req.query.scope, | ||
ServiceScope.decode, | ||
O.fromEither, | ||
O.toUndefined | ||
) | ||
), | ||
O.of( | ||
pipe(req.query.search as string, O.fromNullable, O.toUndefined) | ||
) | ||
), | ||
O.map(args => getInstitutionsResponsePayload(...args)), | ||
O.fold( | ||
() => res.status(404), | ||
instituitions => res.status(200).json(instituitions) | ||
) | ||
), | ||
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Router } from "express"; | ||
|
||
export const serviceRouter = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as t from "io-ts"; | ||
import { HttpResponseCode } from "../../../types/httpResponseCode"; | ||
|
||
export const ServiceConfiguration = t.interface({ | ||
// configure some API response error code | ||
response: t.interface({ | ||
// 200 success with payload | ||
institutionsResponseCode: HttpResponseCode | ||
}) | ||
}); | ||
|
||
export type ServiceConfiguration = t.TypeOf<typeof ServiceConfiguration>; |
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
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