diff --git a/src/controllers/notificationController.ts b/src/controllers/notificationController.ts index 9c3691ae6..676a015ff 100644 --- a/src/controllers/notificationController.ts +++ b/src/controllers/notificationController.ts @@ -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"; @@ -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 @@ -84,25 +82,28 @@ export default class NotificationController { public async createOrUpdateInstallation( req: express.Request - ): Promise> { - 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 + > { + 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" ); } } diff --git a/src/services/__tests__/notificationService.test.ts b/src/services/__tests__/notificationService.test.ts index adcb8389a..f83f891d0 100644 --- a/src/services/__tests__/notificationService.test.ts +++ b/src/services/__tests__/notificationService.test.ts @@ -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) ); diff --git a/src/services/notificationService.ts b/src/services/notificationService.ts index eca5f168c..0e6686c9a 100644 --- a/src/services/notificationService.ts +++ b/src/services/notificationService.ts @@ -75,9 +75,7 @@ export default class NotificationService { public readonly createOrUpdateInstallation = ( fiscalCode: FiscalCode, installation: Installation - ): Promise< - IResponseErrorInternal | IResponseSuccessJson - > => { + ): Promise> => { 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. @@ -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 = (