-
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 #364 from pagopa/IOPAE-1092-api-institutions
[IOPAE-1092] Add `institutions` endpoint
- Loading branch information
Showing
13 changed files
with
250 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,74 @@ | ||
import * as A from "fp-ts/lib/Array"; | ||
import { identity, pipe } from "fp-ts/lib/function"; | ||
import _ from "lodash"; | ||
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"; | ||
import { InstitutionWithScope, getInstitutions } from "../utils/institutions"; | ||
|
||
const filterByScope = ( | ||
institution: InstitutionWithScope, | ||
scope?: ServiceScopeEnum | ||
) => { | ||
if (!scope) { | ||
return true; | ||
} | ||
return institution.scope === scope; | ||
}; | ||
|
||
const filterBySearch = (institution: InstitutionWithScope, search?: string) => { | ||
if (!search) { | ||
return true; | ||
} | ||
return institution.name.toLowerCase().includes(search); | ||
}; | ||
|
||
export const getInstitutionsResponsePayload = ( | ||
limit: number = 20, | ||
offset: number = 0, | ||
scope?: ServiceScopeEnum, | ||
search?: string | ||
): InstitutionsResource => { | ||
const filteredInstitutions = pipe( | ||
ServicesDB.getAllServices(), | ||
getInstitutions, | ||
A.reduce([] as Institution[], (accumulator, institution) => { | ||
const isValidInstitution = pipe( | ||
[ | ||
(institution: InstitutionWithScope) => | ||
filterByScope(institution, scope), | ||
(institution: InstitutionWithScope) => | ||
filterBySearch(institution, search) | ||
], | ||
A.flap(institution), | ||
A.every(identity) | ||
); | ||
|
||
if (isValidInstitution) { | ||
return [ | ||
...accumulator, | ||
{ | ||
id: institution.id, | ||
name: institution.name, | ||
fiscal_code: institution.fiscal_code | ||
} | ||
]; | ||
} | ||
|
||
return accumulator; | ||
}) | ||
); | ||
|
||
const totalElements = filteredInstitutions.length; | ||
const startIndex = offset; | ||
const endIndex = offset + limit; | ||
const istitutionList = _.slice(filteredInstitutions, startIndex, endIndex); | ||
|
||
return { | ||
institutions: istitutionList, | ||
limit, | ||
offset, | ||
count: totalElements | ||
}; | ||
}; |
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,54 @@ | ||
import { sequenceT } from "fp-ts/lib/Apply"; | ||
import * as O from "fp-ts/lib/Option"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
import { ServiceScope } from "../../../../generated/definitions/backend/ServiceScope"; | ||
import { ioDevServerConfig } from "../../../config"; | ||
import { addHandler } from "../../../payloads/response"; | ||
import { getInstitutionsResponsePayload } from "../payloads/get-institutions"; | ||
import { addApiV2Prefix, serviceRouter } from "./router"; | ||
|
||
const serviceConfig = ioDevServerConfig.features.service; | ||
|
||
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 | ||
); | ||
|
||
// Find institutions | ||
addHandler(serviceRouter, "get", addApiV2Prefix("/institutions"), (req, res) => | ||
pipe( | ||
serviceConfig.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,7 @@ | ||
import { Router } from "express"; | ||
|
||
export const serviceRouter = Router(); | ||
|
||
export const SERVICES_PREFIX = "/api/v2"; | ||
|
||
export const addApiV2Prefix = (path: string) => `${SERVICES_PREFIX}${path}`; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as A from "fp-ts/lib/Array"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
import { ServicePublic } from "../../../../generated/definitions/backend/ServicePublic"; | ||
import { ServiceScopeEnum } from "../../../../generated/definitions/backend/ServiceScope"; | ||
import { Institution } from "../../../../generated/definitions/services/Institution"; | ||
|
||
export type InstitutionWithScope = Institution & { scope?: ServiceScopeEnum }; | ||
|
||
export const getInstitutions = ( | ||
services: ServicePublic[] | ||
): InstitutionWithScope[] => | ||
pipe( | ||
services, | ||
A.uniq({ | ||
equals: (x, y) => | ||
x.organization_fiscal_code === y.organization_fiscal_code | ||
}), | ||
A.map(service => ({ | ||
id: service.organization_fiscal_code, | ||
name: service.organization_name, | ||
fiscal_code: service.organization_fiscal_code, | ||
scope: service.service_metadata?.scope | ||
})) | ||
); |
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