Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IP-387] Upgrade fp-ts #165

Merged
merged 74 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from 68 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
1e775ae
update dependencies
balanza Sep 6, 2021
a8edf51
[#ip-387] update utils/orchestrator.tsgst
balanza Sep 6, 2021
9855f22
[#ip-387] update utils/random.tsgst
balanza Sep 6, 2021
3bd2341
[#ip-387] update utils/sessionApiClient.ts
balanza Sep 15, 2021
4c138cb
[#ip-387] update utils/subscription_feed.tsgst
balanza Sep 6, 2021
456f86c
[#ip-387] update utils/middlewaresgst
balanza Sep 6, 2021
22b0e74
[#ip-387] update utils/featureFlags.tsgst
balanza Sep 6, 2021
8a84112
[#ip-387] update utils/extensions/modelsgst
balanza Sep 6, 2021
72f9616
[#ip-387] update utils/conversions.tsgst
balanza Sep 6, 2021
ab1bea7
[#ip-387] [#ip-387] update utils/config.ts
balanza Sep 6, 2021
dc1ef08
[#ip-387] update utils/appinsights.ts
balanza Sep 6, 2021
c801e8f
[#ip-387] update utils/apim.ts
balanza Sep 7, 2021
9048311
[#ip-387] update UserDataProcessingTrigger
balanza Sep 7, 2021
6c77f34
[#ip-387] update UserDataProcessingRecoveryOrchestrator
balanza Sep 7, 2021
39f9233
[#ip-387] update UserDataProcessingProcessFailedRecords
balanza Sep 7, 2021
41b4f13
[#ip-387] update UserDataProcessingFindFailureReasonActivity
balanza Sep 7, 2021
1e31b83
[#ip-387] update __mocks__
balanza Sep 7, 2021
13aa5b7
[#ip-387] update UserDataProcessingCheckLastStatusActivity
balanza Sep 7, 2021
765e2d3
[#ip-387] update UserDataDownloadOrchestrator
balanza Sep 7, 2021
188e43a
[#ip-387] update UserDataDeleteOrchestratorV2
balanza Sep 7, 2021
ea02635
[#ip-387] update UserDataDeleteOrchestrator
balanza Sep 7, 2021
16f6a9c
[#ip-387] update UpsertServiceOrchestrator
balanza Sep 7, 2021
b2e4444
[#ip-387] update UploadServiceLogo
balanza Sep 9, 2021
a45bb84
[#ip-387] [#ip-387] update UploadOrganizationLogo
balanza Sep 9, 2021
214ca59
[#ip-387] update UpdateVisibleServicesCacheOrchestrator
balanza Sep 9, 2021
f36fd76
[#ip-387] update UpdateVisibleServicesCacheActivity
balanza Sep 9, 2021
4d7a482
[#ip-387] update UpdateVisibleServicesCache
balanza Sep 9, 2021
743baa3
[#ip-387] [#ip-387] update UpdateVisibleServicesActivity
balanza Sep 9, 2021
c33b8d0
[#ip-387] update UpdateUserGroups
balanza Sep 9, 2021
ad5ae4b
[#ip-387] update UpdateUser
balanza Sep 9, 2021
18b3fa1
[#ip-387] update UpdateSubscriptionsFeedActivity
balanza Sep 9, 2021
c7bbf1d
[#ip-387] update UpdateService
balanza Sep 9, 2021
c0fd570
[#ip-387] update SetUserSessionLockActivity
balanza Sep 9, 2021
bca6339
[#ip-387] update SetUserDataProcessingStatusActivity
balanza Sep 9, 2021
5bf8390
[#ip-387] update SetUserDataProcessingStatus
balanza Sep 9, 2021
263496f
[#ip-387] update SendUserDataDownloadMessageActivity
balanza Sep 9, 2021
317a0a1
[#ip-387] update SendUserDataDeleteEmailActivity
balanza Sep 10, 2021
22feac6
[#ip-387] update RegenerateSubscriptionKeys
balanza Sep 10, 2021
1fa1cac
[#ip-387] update IsFailedUserDataProcessingActivity
balanza Sep 10, 2021
db18172
[#ip-387] update Info
balanza Sep 10, 2021
23fce0c
[#ip-387] update GetUsers
balanza Sep 10, 2021
8191a8c
[#ip-387] update GetUserDataProcessingActivity
balanza Sep 10, 2021
99b3662
[#ip-387] update GetUser
balanza Sep 13, 2021
6dbeb14
[#ip-387] update GetSubscriptionKeys
balanza Sep 13, 2021
c126814
[#ip-387] update GetServicesPreferencesActivity
balanza Sep 13, 2021
d2b2dba
[#ip-387] update GetServices
balanza Sep 13, 2021
3de1e20
[#ip-387] update GetService
balanza Sep 13, 2021
9089b48
[#ip-387] update GetProfileActivity
balanza Sep 13, 2021
69ac9a0
[#ip-387] update GetFailedUserDataProcessingList
balanza Sep 13, 2021
730d10f
[#ip-387] update GetFailedUserDataProcessing
balanza Sep 13, 2021
b849aab
[#ip-387] update ExtractUserDataActivity
balanza Sep 13, 2021
4d8efe1
[#ip-387] [#ip-387] update DeleteUserDataActivity
balanza Sep 13, 2021
78a0951
[#ip-387] update CreateUser
balanza Sep 13, 2021
68ae861
[#ip-387] update CreateSubscription
balanza Sep 14, 2021
1b9c198
[#ip-387] update CreateService
balanza Sep 14, 2021
b76131d
[#ip-387] update CreateDevelopmentProfile
balanza Sep 14, 2021
f78dd8d
[#ip-387] fix after review
balanza Sep 15, 2021
1c8838d
fix after review
balanza Sep 15, 2021
80cd1c6
fix after review
balanza Sep 15, 2021
18d69f9
[#ip-387] fix after review
balanza Sep 15, 2021
7b2b7e0
[#ip-387] lint fix
balanza Sep 15, 2021
1381aae
[#ip-387] fix after review
balanza Sep 15, 2021
afa6858
[#ip-387] fix after review
balanza Sep 15, 2021
0eb3db8
[#ip-387] fix after review
balanza Sep 15, 2021
67fd68c
[#ip-387] fix after review
balanza Sep 15, 2021
179c051
[#ip-387] Revert "fix after review"
balanza Sep 16, 2021
409be2e
[#ip-387] [#ip-387] fix after review
balanza Sep 16, 2021
e44eb01
[#ip-387] fix after review
balanza Sep 16, 2021
928cf9c
[#ip-387] fix after review
balanza Sep 16, 2021
f2418a3
[#ip-387] fix after review
balanza Sep 17, 2021
1ecf36d
[#ip-387] fix after review
balanza Sep 17, 2021
4470544
[#ip-387] fix after review
balanza Sep 17, 2021
2c8dd17
[#ip-387] [#ip-387] fix after review
balanza Sep 20, 2021
b4441e9
Merge branch 'master' into ip-387--upgrade-fp-ts
Sep 21, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions CreateDevelopmentProfile/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as express from "express";

import { Context } from "@azure/functions";

import { isLeft } from "fp-ts/lib/Either";
import * as E from "fp-ts/lib/Either";

import { readableReport } from "@pagopa/ts-commons/lib/reporters";
import {
Expand Down Expand Up @@ -40,6 +40,7 @@ import {

import { ServicesPreferencesModeEnum } from "@pagopa/io-functions-commons/dist/generated/definitions/ServicesPreferencesMode";
import { NonNegativeInteger } from "@pagopa/ts-commons/lib/numbers";
import { pipe } from "fp-ts/lib/function";
import { DevelopmentProfile } from "../generated/definitions/DevelopmentProfile";

// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
Expand All @@ -66,9 +67,10 @@ export const DeveloperProfilePayloadMiddleware: IRequestMiddleware<
DevelopmentProfile
> = request =>
new Promise(resolve => {
const validation = DevelopmentProfile.decode(request.body);
const result = validation.mapLeft(
ResponseErrorFromValidationErrors(ExtendedProfile)
const result = pipe(
request.body,
DevelopmentProfile.decode,
E.mapLeft(ResponseErrorFromValidationErrors(ExtendedProfile))
);
resolve(result);
});
Expand Down Expand Up @@ -98,16 +100,16 @@ export function CreateDevelopmentProfileHandler(

const errorOrFiscalCode = FiscalCode.decode(sandboxFiscalCode);

if (isLeft(errorOrFiscalCode)) {
if (E.isLeft(errorOrFiscalCode)) {
context.log.error(
`${logPrefix}|ERROR=${readableReport(errorOrFiscalCode.value)}`
`${logPrefix}|ERROR=${readableReport(errorOrFiscalCode.left)}`
);
return ResponseErrorFromValidationErrors(FiscalCode)(
errorOrFiscalCode.value
errorOrFiscalCode.left
);
}

const fiscalCode = errorOrFiscalCode.value;
const fiscalCode = errorOrFiscalCode.right;

const newProfile: NewProfile = {
email: developmentProfilePayload.email,
Expand All @@ -121,10 +123,10 @@ export function CreateDevelopmentProfileHandler(
}
};

const errorOrCreatedProfile = await profileModel.create(newProfile).run();
const errorOrCreatedProfile = await profileModel.create(newProfile)();

if (isLeft(errorOrCreatedProfile)) {
const error = errorOrCreatedProfile.value;
if (E.isLeft(errorOrCreatedProfile)) {
const error = errorOrCreatedProfile.left;
context.log.error(`${logPrefix}|ERROR=${error}`);

if (error.kind === "COSMOS_ERROR_RESPONSE" && error.error.code === 409) {
Expand All @@ -139,7 +141,7 @@ export function CreateDevelopmentProfileHandler(
);
}

const createdProfile = errorOrCreatedProfile.value;
const createdProfile = errorOrCreatedProfile.right;

context.log.verbose(`${logPrefix}|SUCCESS`);

Expand Down
8 changes: 4 additions & 4 deletions CreateService/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as lolex from "lolex";

import { left, right } from "fp-ts/lib/Either";

import { fromEither, fromLeft } from "fp-ts/lib/TaskEither";
import * as TE from "fp-ts/lib/TaskEither";
import { toCosmosErrorResponse } from "@pagopa/io-functions-commons/dist/src/utils/cosmosdb_model";
import {
aNewService,
Expand All @@ -33,7 +33,7 @@ describe("CreateServiceHandler", () => {
it("should return a query error if the service fails to be created", async () => {
const mockServiceModel = {
create: jest.fn(_ => {
return fromLeft(
return TE.left(
toCosmosErrorResponse({ kind: "COSMOS_ERROR_RESPONSE" })
);
})
Expand All @@ -54,7 +54,7 @@ describe("CreateServiceHandler", () => {
it("should create a new service using the payload and return the created service", async () => {
const mockServiceModel = {
create: jest.fn(_ => {
return fromEither(right(aRetrievedService));
return TE.right(aRetrievedService);
})
};

Expand All @@ -76,7 +76,7 @@ describe("CreateServiceHandler", () => {
it("should start the orchestrator with an appropriate event after the service is created", async () => {
const mockServiceModel = {
create: jest.fn(_ => {
return fromEither(right(aRetrievedService));
return TE.right(aRetrievedService);
})
};

Expand Down
6 changes: 3 additions & 3 deletions CreateService/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ export function CreateServiceHandler(
...apiServiceToService(servicePayload),
kind: "INewService" as const
};
const errorOrCreatedService = await serviceModel.create(newService).run();
const errorOrCreatedService = await serviceModel.create(newService)();

if (isLeft(errorOrCreatedService)) {
return ResponseErrorQuery(
"CreateServiceHandler error",
errorOrCreatedService.value
errorOrCreatedService.left
);
}

const createdService = errorOrCreatedService.value;
const createdService = errorOrCreatedService.right;

const upsertServiceEvent = UpsertServiceEvent.encode({
newService: createdService,
Expand Down
16 changes: 8 additions & 8 deletions CreateSubscription/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
SubscriptionContract,
UserContract
} from "@azure/arm-apimanagement/esm/models";
import { isRight, left, right } from "fp-ts/lib/Either";
import { fromEither, fromLeft } from "fp-ts/lib/TaskEither";
import * as E from "fp-ts/lib/Either";
import * as TE from "fp-ts/lib/TaskEither";
import { ProductNamePayload } from "../../generated/definitions/ProductNamePayload";
import { UserInfo } from "../../generated/definitions/UserInfo";
import * as ApimUtils from "../../utils/apim";
Expand Down Expand Up @@ -85,7 +85,7 @@ mockApiManagementClient.mockImplementation(() => ({

const spyOnGetApiClient = jest.spyOn(ApimUtils, "getApiClient");
spyOnGetApiClient.mockImplementation(() =>
fromEither(right(new mockApiManagementClient()))
TE.of(new mockApiManagementClient())
);

const mockLog = jest.fn();
Expand All @@ -95,7 +95,7 @@ const mockedContext = { log: { error: mockLog } };
describe("CreateSubscription", () => {
it("should return an internal error response if the API management client can not be got", async () => {
spyOnGetApiClient.mockImplementationOnce(() =>
fromLeft(Error("Error from ApiManagementClient constructor"))
TE.left(Error("Error from ApiManagementClient constructor"))
);

const createSubscriptionHandler = CreateSubscriptionHandler(
Expand Down Expand Up @@ -309,10 +309,10 @@ describe("CreateSubscription", () => {
expect(response).toEqual({
apply: expect.any(Function),
kind: "IResponseSuccessJson",
value: subscriptionContractToApiSubscription(
aFakeApimSubscriptionContract
).value
value: E.toUnion(
subscriptionContractToApiSubscription(aFakeApimSubscriptionContract)
)
});
expect(isRight(UserInfo.decode((response as any).value))).toBeTruthy();
expect(E.isRight(UserInfo.decode((response as any).value))).toBeTruthy();
});
});
132 changes: 71 additions & 61 deletions CreateSubscription/handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context } from "@azure/functions";
import * as express from "express";
import { fromEither, fromPredicate, tryCatch } from "fp-ts/lib/TaskEither";
import * as TE from "fp-ts/lib/TaskEither";
import {
AzureApiAuthMiddleware,
IAzureApiAuthorization,
Expand All @@ -22,6 +22,7 @@ import {
ResponseSuccessJson
} from "@pagopa/ts-commons/lib/responses";
import { NonEmptyString } from "@pagopa/ts-commons/lib/strings";
import { flow, pipe } from "fp-ts/lib/function";
import { EmailAddress } from "../generated/definitions/EmailAddress";
import { ProductNamePayload } from "../generated/definitions/ProductNamePayload";
import { Subscription } from "../generated/definitions/Subscription";
Expand Down Expand Up @@ -75,15 +76,13 @@ export function CreateSubscriptionHandler(
errors,
errorMessage
);
const response = await getApiClient(
servicePrincipalCreds,
azureApimConfig.subscriptionId
)
.mapLeft<IResponseErrorInternal | IResponseErrorNotFound>(error =>
return pipe(
getApiClient(servicePrincipalCreds, azureApimConfig.subscriptionId),
TE.mapLeft(error =>
internalErrorHandler("Could not get the APIM client.", error)
)
.chain(apimClient =>
tryCatch(
),
TE.chainW(apimClient =>
TE.tryCatch(
() =>
apimClient.user
.listByService(
Expand All @@ -103,77 +102,88 @@ export function CreateSubscriptionHandler(
error as Error
)
)
)
.chain(
fromPredicate(
),
TE.chainW(
TE.fromPredicate(
taskResults => taskResults.userList.length !== 0,
() =>
ResponseErrorNotFound(
"Not found",
"The provided user does not exist"
)
)
)
.chain(taskResults =>
fromEither(NonEmptyString.decode(taskResults.userList[0].id))
.mapLeft(errors =>
),
TE.chainW(taskResults =>
pipe(
taskResults.userList[0].id,
NonEmptyString.decode,
TE.fromEither,
TE.mapLeft(errors =>
internalValidationErrorHandler(
"Could not get user id from user contract.",
errors
)
)
.map(userId => ({
),
TE.map(userId => ({
apimClient: taskResults.apimClient,
userId
}))
)
.chain(taskResults =>
tryCatch(
() =>
taskResults.apimClient.product.listByService(
azureApimConfig.apimResourceGroup,
azureApimConfig.apim,
{
filter: `name eq '${productNamePayload.product_name}'`
}
),
error =>
internalErrorHandler(
"Could not list the products by name.",
error as Error
)
).map(productList => ({
apimClient: taskResults.apimClient,
productList,
userId: taskResults.userId
}))
)
.chain(
fromPredicate(
)
),
TE.chainW(taskResults =>
pipe(
TE.tryCatch(
() =>
taskResults.apimClient.product.listByService(
azureApimConfig.apimResourceGroup,
azureApimConfig.apim,
{
filter: `name eq '${productNamePayload.product_name}'`
}
),
error =>
internalErrorHandler(
"Could not list the products by name.",
error as Error
)
),
TE.map(productList => ({
apimClient: taskResults.apimClient,
productList,
userId: taskResults.userId
}))
)
),
TE.chainW(
TE.fromPredicate(
taskResults => taskResults.productList.length !== 0,
() =>
ResponseErrorNotFound(
"Not found",
"The provided product does not exist"
)
)
)
.chain(taskResults =>
fromEither(NonEmptyString.decode(taskResults.productList[0].id))
.mapLeft(errors =>
),
TE.chainW(taskResults =>
pipe(
taskResults.productList[0].id,
NonEmptyString.decode,
TE.fromEither,
TE.mapLeft(errors =>
internalValidationErrorHandler(
"Could not get product id from product contract.",
errors
)
)
.map(productId => ({
),
TE.map(productId => ({
apimClient: taskResults.apimClient,
productId,
userId: taskResults.userId
}))
)
.chain(taskResults =>
tryCatch(
)
),
TE.chainW(taskResults =>
TE.tryCatch(
() =>
taskResults.apimClient.subscription.createOrUpdate(
azureApimConfig.apimResourceGroup,
Expand All @@ -192,22 +202,22 @@ export function CreateSubscriptionHandler(
error as Error
)
)
)
.chain(subscriptionResponse =>
fromEither(
subscriptionContractToApiSubscription(
subscriptionResponse
).mapLeft(error =>
),
TE.chainW(
flow(
subscriptionContractToApiSubscription,
TE.fromEither,
TE.mapLeft(error =>
internalErrorHandler(
"Invalid subscription contract from APIM.",
error
)
)
)
)
.map(ResponseSuccessJson)
.run();
return response.value;
),
TE.map(ResponseSuccessJson),
TE.toUnion
)();
};
}

Expand Down
Loading