Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
45 changes: 23 additions & 22 deletions src/controllers/notificationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import * as express from "express";
import {
IResponseErrorInternal,
IResponseErrorValidation,
IResponseSuccessJson,
ResponseSuccessJson
IResponseSuccessJson
} from "italia-ts-commons/lib/responses";

import { Installation } from "../../generated/backend/Installation";
Expand All @@ -21,7 +20,6 @@ import { fromEither, tryCatch } from "fp-ts/lib/TaskEither";
import NotificationService from "../services/notificationService";
import RedisSessionStorage from "../services/redisSessionStorage";
import { withUserFromRequest } from "../types/user";
import { log } from "../utils/logger";
import {
withCatchAsInternalError,
withValidatedOrValidationError
Expand Down Expand Up @@ -84,25 +82,28 @@ export default class NotificationController {

public async createOrUpdateInstallation(
req: express.Request
): Promise<IResponseErrorValidation | IResponseSuccessJson<SuccessResponse>> {
return withUserFromRequest(req, async user =>
withValidatedOrValidationError(InstallationID.decode(req.params.id), _ =>
withValidatedOrValidationError(
Installation.decode(req.body),
installation => {
// async fire & forget
this.notificationService
.createOrUpdateInstallation(user.fiscal_code, installation)
.catch(err => {
log.error(
"Cannot create installation: %s",
JSON.stringify(err)
);
});
return ResponseSuccessJson({ message: "ok" });
}
)
)
): Promise<
| IResponseErrorValidation
| IResponseErrorInternal
| IResponseSuccessJson<SuccessResponse>
> {
return withCatchAsInternalError(
() =>
withUserFromRequest(req, async user =>
withValidatedOrValidationError(
InstallationID.decode(req.params.id),
_ =>
withValidatedOrValidationError(
Installation.decode(req.body),
installation =>
this.notificationService.createOrUpdateInstallation(
user.fiscal_code,
installation
)
)
)
),
"Error upserting installation"
);
}
}
11 changes: 2 additions & 9 deletions src/services/__tests__/notificationService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,9 @@ describe("NotificationService#createOrUpdateInstallation", () => {

const service = new NotificationService("", "");

const res = await service.createOrUpdateInstallation(
aFiscalCode,
anAppleDevice
);
const res = service.createOrUpdateInstallation(aFiscalCode, anAppleDevice);

expect(res).toEqual({
apply: expect.any(Function),
detail: expect.anything(),
kind: "IResponseErrorInternal"
});
await expect(res).rejects.toEqual(genericError);
expect(mockSendMessage).toBeCalledWith(
base64EncodeObject(anAppleInstallation)
);
Expand Down
11 changes: 2 additions & 9 deletions src/services/notificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ export default class NotificationService {
public readonly createOrUpdateInstallation = (
fiscalCode: FiscalCode,
installation: Installation
): Promise<
IResponseErrorInternal | IResponseSuccessJson<SuccessResponse>
> => {
): Promise<IResponseSuccessJson<SuccessResponse>> => {
const azureInstallation: CreateOrUpdateInstallationMessage = {
// When a single active session per user is allowed, the installation that must be created or updated
// will have an unique installationId referred to that user.
Expand All @@ -93,12 +91,7 @@ export default class NotificationService {
};
return this.notificationQueueClient
.sendMessage(base64EncodeObject(azureInstallation))
.then(() => ResponseSuccessJson({ message: "ok" }))
.catch(error =>
ResponseErrorInternal(
`Error while sending create or update installation message to the queue [${error.message}]`
)
);
.then(() => ResponseSuccessJson({ message: "ok" }));
};

public readonly deleteInstallation = (
Expand Down