diff --git a/apps/condo/domains/miniapp/schema/SendB2BAppPushMessageService.js b/apps/condo/domains/miniapp/schema/SendB2BAppPushMessageService.js index 434ee71b5a3..8cce59c4f0d 100644 --- a/apps/condo/domains/miniapp/schema/SendB2BAppPushMessageService.js +++ b/apps/condo/domains/miniapp/schema/SendB2BAppPushMessageService.js @@ -49,7 +49,7 @@ const ERRORS = { type: NOT_FOUND, message: 'No B2BAppAccessRight found for the provided user and B2BApp.', }, - NO_EMPLOYEE_FOR_USER: { + USER_IS_NOT_AN_EMPLOYEE: { mutation: 'sendB2BAppPushMessage', variable: ['data', 'user'], code: FORBIDDEN, @@ -128,6 +128,10 @@ const SendB2BAppPushMessageService = new GQLCustomSchema('SendB2BAppPushMessageS ...userFilter, deletedAt: null, }) + if (!user) { + throw new GQLError(ERRORS.USER_IS_NOT_AN_EMPLOYEE, context) + } + const messageLocale = get(user, 'locale', conf.DEFAULT_LOCALE) const [b2bAppContext] = await itemsQuery('B2BAppContext', { where: { @@ -167,7 +171,7 @@ const SendB2BAppPushMessageService = new GQLCustomSchema('SendB2BAppPushMessageS deletedAt: null, }) if (!employee) { - throw new GQLError(ERRORS.NO_EMPLOYEE_FOR_USER, context) + throw new GQLError(ERRORS.USER_IS_NOT_AN_EMPLOYEE, context) } const roleId = get(employee, 'role', null) @@ -184,7 +188,7 @@ const SendB2BAppPushMessageService = new GQLCustomSchema('SendB2BAppPushMessageS to: { user: userFilter }, organization: organizationFilter, type, - lang: get(user, 'locale', conf.DEFAULT_LOCALE), + lang: messageLocale, meta, dv, sender, diff --git a/apps/condo/domains/miniapp/schema/SendB2BAppPushMessageService.test.js b/apps/condo/domains/miniapp/schema/SendB2BAppPushMessageService.test.js index 51874c280b6..5a342e922d2 100644 --- a/apps/condo/domains/miniapp/schema/SendB2BAppPushMessageService.test.js +++ b/apps/condo/domains/miniapp/schema/SendB2BAppPushMessageService.test.js @@ -15,7 +15,7 @@ const { Message, syncRemoteClientWithPushTokenByTestClient } = require('@condo/d const { DEFAULT_ROLES } = require('@condo/domains/organization/constants/common') const { createTestOrganization, createTestOrganizationEmployeeRole, createTestOrganizationEmployee } = require('@condo/domains/organization/utils/testSchema') const { GQL_ERRORS } = require('@condo/domains/user/constants/errors') -const { User, makeClientWithServiceUser, makeClientWithSupportUser, makeClientWithNewRegisteredAndLoggedInUser } = require('@condo/domains/user/utils/testSchema') +const { User, updateTestUser, makeClientWithServiceUser, makeClientWithSupportUser, makeClientWithNewRegisteredAndLoggedInUser } = require('@condo/domains/user/utils/testSchema') const { ERRORS } = require('./SendB2BAppPushMessageService') @@ -29,10 +29,13 @@ describe('SendB2BAppPushMessageService', () => { organization, b2bApp - beforeEach(async () => { + beforeAll(async () => { admin = await makeLoggedInAdminClient() support = await makeClientWithSupportUser() anonymous = await makeClient() + }) + + beforeEach(async () => { staffClient = await makeClientWithNewRegisteredAndLoggedInUser({ locale: faker.helpers.arrayElement(Object.keys(LOCALES)), }) @@ -144,7 +147,7 @@ describe('SendB2BAppPushMessageService', () => { await expectToThrowGQLErrorToResult(async () => { await sendB2BAppPushMessageByTestClient(serviceUser, b2bApp, organization, user.user) - }, ERRORS.NO_EMPLOYEE_FOR_USER) + }, ERRORS.USER_IS_NOT_AN_EMPLOYEE) }) it('Throws an error if notifications are sent more often than specified in AppMessageSetting', async () => { @@ -208,5 +211,17 @@ describe('SendB2BAppPushMessageService', () => { }) }, ERRORS.NO_B2B_APP_ROLE_FOR_EMPLOYEE_ROLE_AND_B2B_APP) }) + + it('Throws an error if user is deleted', async () => { + await updateTestUser(admin, staffClient.user.id, { + deletedAt: new Date(), + }) + + await expectToThrowGQLErrorToResult(async () => { + await sendB2BAppPushMessageByTestClient(serviceUser, b2bApp, organization, staffClient.user, { + type: PASS_TICKET_CREATED_MESSAGE_TYPE, + }) + }, ERRORS.USER_IS_NOT_AN_EMPLOYEE) + }) }) }) \ No newline at end of file