Skip to content

Commit

Permalink
[#175573274] Allow AI tracking on service's visibility (#87)
Browse files Browse the repository at this point in the history
* [#175573274] Allow AI track service's visibility

* [#175573274] Fix over review

* [#175573274] Refactor over review

* [#175573274] Remove useless trackEvent wrap

* [#175573274] Fix lint
  • Loading branch information
AleDore authored Nov 6, 2020
1 parent 2af6f68 commit 188bc02
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 49 deletions.
44 changes: 33 additions & 11 deletions CreateService/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ const mockUlidGenerator = jest.fn(() => aServiceId);
const productName = "IO_API_SERVICES" as NonEmptyString;
const sandboxFiscalCode = "BBBCCC00A11B123X" as NonEmptyString;

const mockAppinsights = {
trackEvent: jest.fn()
};

describe("CreateServiceHandler", () => {
it("should respond with a created service with subscriptionKeys by providing a servicePayload", async () => {
const apiClientMock = {
Expand All @@ -127,6 +131,7 @@ describe("CreateServiceHandler", () => {
};

const createServiceHandler = CreateServiceHandler(
mockAppinsights as any,
apiClientMock as any,
mockUlidGenerator as any,
productName,
Expand All @@ -143,6 +148,7 @@ describe("CreateServiceHandler", () => {
expect(apiClientMock.createSubscription).toHaveBeenCalledTimes(1);
expect(apiClientMock.createService).toHaveBeenCalledTimes(1);
expect(apiClientMock.getUser).toHaveBeenCalledTimes(1);
expect(mockAppinsights.trackEvent).toHaveBeenCalledTimes(1);
expect(result.kind).toBe("IResponseSuccessJson");
if (result.kind === "IResponseSuccessJson") {
expect(result.value).toEqual({
Expand All @@ -166,6 +172,7 @@ describe("CreateServiceHandler", () => {
};

const createServiceHandler = CreateServiceHandler(
mockAppinsights as any,
apiClientMock as any,
mockUlidGenerator as any,
productName,
Expand Down Expand Up @@ -206,6 +213,7 @@ describe("CreateServiceHandler", () => {
};

const createServiceHandler = CreateServiceHandler(
mockAppinsights as any,
apiClientMock as any,
mockUlidGenerator as any,
productName,
Expand All @@ -221,7 +229,7 @@ describe("CreateServiceHandler", () => {

expect(apiClientMock.createSubscription).toHaveBeenCalledTimes(1);
expect(apiClientMock.createService).not.toHaveBeenCalled();

expect(mockAppinsights.trackEvent).not.toHaveBeenCalled();
expect(result.kind).toBe("IResponseErrorInternal");
});

Expand All @@ -240,6 +248,7 @@ describe("CreateServiceHandler", () => {
.mockImplementation(() => ["ValidationErrors"]);

const createServiceHandler = CreateServiceHandler(
mockAppinsights as any,
apiClientMock as any,
mockUlidGenerator as any,
productName,
Expand All @@ -255,7 +264,7 @@ describe("CreateServiceHandler", () => {

expect(apiClientMock.createSubscription).toHaveBeenCalledTimes(1);
expect(apiClientMock.createService).not.toHaveBeenCalled();

expect(mockAppinsights.trackEvent).not.toHaveBeenCalled();
expect(result.kind).toBe("IResponseErrorInternal");
});

Expand All @@ -268,6 +277,7 @@ describe("CreateServiceHandler", () => {
};

const createServiceHandler = CreateServiceHandler(
mockAppinsights as any,
apiClientMock as any,
mockUlidGenerator as any,
productName,
Expand All @@ -283,6 +293,7 @@ describe("CreateServiceHandler", () => {

expect(apiClientMock.createSubscription).toHaveBeenCalledTimes(1);
expect(apiClientMock.createService).not.toHaveBeenCalled();
expect(mockAppinsights.trackEvent).not.toHaveBeenCalled();

expect(result.kind).toBe("IResponseErrorInternal");
});
Expand All @@ -296,6 +307,7 @@ describe("CreateServiceHandler", () => {
};

const createServiceHandler = CreateServiceHandler(
mockAppinsights as any,
apiClientMock as any,
mockUlidGenerator as any,
productName,
Expand All @@ -311,7 +323,7 @@ describe("CreateServiceHandler", () => {

expect(apiClientMock.createSubscription).toHaveBeenCalledTimes(1);
expect(apiClientMock.createService).not.toHaveBeenCalled();

expect(mockAppinsights.trackEvent).not.toHaveBeenCalled();
expect(result.kind).toBe("IResponseErrorForbiddenNotAuthorized");
});

Expand All @@ -324,6 +336,7 @@ describe("CreateServiceHandler", () => {
};

const createServiceHandler = CreateServiceHandler(
mockAppinsights as any,
apiClientMock as any,
mockUlidGenerator as any,
productName,
Expand All @@ -339,7 +352,7 @@ describe("CreateServiceHandler", () => {

expect(apiClientMock.createSubscription).toHaveBeenCalledTimes(1);
expect(apiClientMock.createService).not.toHaveBeenCalled();

expect(mockAppinsights.trackEvent).not.toHaveBeenCalled();
expect(result.kind).toBe("IResponseErrorNotFound");
if (result.kind === "IResponseErrorNotFound") {
expect(result.detail).toEqual("Not found: Resource not found");
Expand All @@ -358,6 +371,7 @@ describe("CreateServiceHandler", () => {
};

const createServiceHandler = CreateServiceHandler(
mockAppinsights as any,
apiClientMock as any,
mockUlidGenerator as any,
productName,
Expand All @@ -374,7 +388,7 @@ describe("CreateServiceHandler", () => {
expect(apiClientMock.createSubscription).toHaveBeenCalledTimes(1);
expect(apiClientMock.createService).not.toHaveBeenCalled();
expect(apiClientMock.getUser).toHaveBeenCalledTimes(1);

expect(mockAppinsights.trackEvent).not.toHaveBeenCalled();
expect(result.kind).toBe("IResponseErrorInternal");
});

Expand All @@ -393,6 +407,7 @@ describe("CreateServiceHandler", () => {
.spyOn(reporters, "errorsToReadableMessages")
.mockImplementation(() => ["ValidationErrors"]);
const createServiceHandler = CreateServiceHandler(
mockAppinsights as any,
apiClientMock as any,
mockUlidGenerator as any,
productName,
Expand All @@ -409,7 +424,7 @@ describe("CreateServiceHandler", () => {
expect(apiClientMock.createSubscription).toHaveBeenCalledTimes(1);
expect(apiClientMock.createService).not.toHaveBeenCalled();
expect(apiClientMock.getUser).toHaveBeenCalledTimes(1);

expect(mockAppinsights.trackEvent).not.toHaveBeenCalled();
expect(result.kind).toBe("IResponseErrorInternal");
});

Expand All @@ -425,6 +440,7 @@ describe("CreateServiceHandler", () => {
};

const createServiceHandler = CreateServiceHandler(
mockAppinsights as any,
apiClientMock as any,
mockUlidGenerator as any,
productName,
Expand All @@ -441,7 +457,7 @@ describe("CreateServiceHandler", () => {
expect(apiClientMock.createSubscription).toHaveBeenCalledTimes(1);
expect(apiClientMock.createService).not.toHaveBeenCalled();
expect(apiClientMock.getUser).toHaveBeenCalledTimes(1);

expect(mockAppinsights.trackEvent).not.toHaveBeenCalled();
expect(result.kind).toBe("IResponseErrorNotFound");
if (result.kind === "IResponseErrorNotFound") {
expect(result.detail).toEqual("Not found: Resource not found");
Expand All @@ -460,6 +476,7 @@ describe("CreateServiceHandler", () => {
};

const createServiceHandler = CreateServiceHandler(
mockAppinsights as any,
apiClientMock as any,
mockUlidGenerator as any,
productName,
Expand All @@ -476,7 +493,7 @@ describe("CreateServiceHandler", () => {
expect(apiClientMock.createSubscription).toHaveBeenCalledTimes(1);
expect(apiClientMock.createService).not.toHaveBeenCalled();
expect(apiClientMock.getUser).toHaveBeenCalledTimes(1);

expect(mockAppinsights.trackEvent).not.toHaveBeenCalled();
expect(result.kind).toBe("IResponseErrorInternal");
});

Expand All @@ -492,6 +509,7 @@ describe("CreateServiceHandler", () => {
};

const createServiceHandler = CreateServiceHandler(
mockAppinsights as any,
apiClientMock as any,
mockUlidGenerator as any,
productName,
Expand All @@ -508,7 +526,7 @@ describe("CreateServiceHandler", () => {
expect(apiClientMock.createSubscription).toHaveBeenCalledTimes(1);
expect(apiClientMock.createService).not.toHaveBeenCalled();
expect(apiClientMock.getUser).toHaveBeenCalledTimes(1);

expect(mockAppinsights.trackEvent).not.toHaveBeenCalled();
expect(result.kind).toBe("IResponseErrorForbiddenNotAuthorized");
});
it("should respond with an internal error if createService does not respond", async () => {
Expand All @@ -523,6 +541,7 @@ describe("CreateServiceHandler", () => {
};

const createServiceHandler = CreateServiceHandler(
mockAppinsights as any,
apiClientMock as any,
mockUlidGenerator as any,
productName,
Expand All @@ -539,7 +558,7 @@ describe("CreateServiceHandler", () => {
expect(apiClientMock.createSubscription).toHaveBeenCalledTimes(1);
expect(apiClientMock.createService).toHaveBeenCalledTimes(1);
expect(apiClientMock.getUser).toHaveBeenCalledTimes(1);

expect(mockAppinsights.trackEvent).not.toHaveBeenCalled();
expect(result.kind).toBe("IResponseErrorInternal");
});

Expand All @@ -561,6 +580,7 @@ describe("CreateServiceHandler", () => {
.mockImplementation(() => ["ValidationErrors"]);

const createServiceHandler = CreateServiceHandler(
mockAppinsights as any,
apiClientMock as any,
mockUlidGenerator as any,
productName,
Expand All @@ -577,6 +597,7 @@ describe("CreateServiceHandler", () => {
expect(apiClientMock.createSubscription).toHaveBeenCalledTimes(1);
expect(apiClientMock.createService).toHaveBeenCalledTimes(1);
expect(apiClientMock.getUser).toHaveBeenCalledTimes(1);
expect(mockAppinsights.trackEvent).not.toHaveBeenCalled();
expect(result.kind).toBe("IResponseErrorInternal");
});

Expand All @@ -592,6 +613,7 @@ describe("CreateServiceHandler", () => {
};

const createServiceHandler = CreateServiceHandler(
mockAppinsights as any,
apiClientMock as any,
mockUlidGenerator as any,
productName,
Expand All @@ -608,7 +630,7 @@ describe("CreateServiceHandler", () => {
expect(apiClientMock.createSubscription).toHaveBeenCalledTimes(1);
expect(apiClientMock.createService).toHaveBeenCalledTimes(1);
expect(apiClientMock.getUser).toHaveBeenCalledTimes(1);

expect(mockAppinsights.trackEvent).not.toHaveBeenCalled();
expect(result.kind).toBe("IResponseErrorUnauthorized");
if (result.kind === "IResponseErrorUnauthorized") {
expect(result.detail).toEqual("Unauthorized: Unauthorized");
Expand Down
20 changes: 16 additions & 4 deletions CreateService/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
ObjectIdGenerator,
ulidGenerator
} from "io-functions-commons/dist/src/utils/strings";
import { initAppInsights } from "italia-ts-commons/lib/appinsights";
import {
EmailString,
FiscalCode,
Expand Down Expand Up @@ -144,6 +145,7 @@ const createServiceTask = (
* Handles requests for create a service by a Service Payload.
*/
export function CreateServiceHandler(
telemetryClient: ReturnType<typeof initAppInsights>,
apiClient: APIClient,
generateObjectId: ObjectIdGenerator,
productName: NonEmptyString,
Expand Down Expand Up @@ -174,13 +176,21 @@ export function CreateServiceHandler(
subscriptionId,
(sandboxFiscalCode as unknown) as FiscalCode,
userInfo.token_name
).map(service =>
ResponseSuccessJson({
).map(service => {
telemetryClient.trackEvent({
name: "api.services.create",
properties: {
isVisible: String(service.is_visible),
requesterUserEmail: userAttributes.email,
subscriptionId
}
});
return ResponseSuccessJson({
...service,
primary_key: subscription.primary_key,
secondary_key: subscription.secondary_key
})
)
});
})
)
)
.fold<ResponseTypes>(identity, identity)
Expand All @@ -192,12 +202,14 @@ export function CreateServiceHandler(
* Wraps a CreateService handler inside an Express request handler.
*/
export function CreateService(
telemetryClient: ReturnType<typeof initAppInsights>,
serviceModel: ServiceModel,
client: APIClient,
productName: NonEmptyString,
sandboxFiscalCode: NonEmptyString
): express.RequestHandler {
const handler = CreateServiceHandler(
telemetryClient,
client,
ulidGenerator,
productName,
Expand Down
6 changes: 6 additions & 0 deletions CreateService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import createAzureFunctionHandler from "io-functions-express/dist/src/createAzur
import { apiClient } from "../clients/admin";
import { CreateService } from "./handler";

import { initTelemetryClient } from "../utils/appinsights";
import { getConfigOrThrow } from "../utils/config";

const config = getConfigOrThrow();
Expand All @@ -30,9 +31,14 @@ const serviceModel = new ServiceModel(
cosmosdbInstance.container(SERVICE_COLLECTION_NAME)
);

const telemetryClient = initTelemetryClient(
config.APPINSIGHTS_INSTRUMENTATIONKEY
);

app.post(
"/api/v1/services",
CreateService(
telemetryClient,
serviceModel,
apiClient,
config.DEFAULT_SUBSCRIPTION_PRODUCT_NAME,
Expand Down
Loading

0 comments on commit 188bc02

Please sign in to comment.