mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
fix logger on contributionMessageResolver
This commit is contained in:
parent
ee8a338ec4
commit
33e87ddd92
@ -88,6 +88,7 @@ describe('ContributionMessageResolver', () => {
|
|||||||
|
|
||||||
describe('input not valid', () => {
|
describe('input not valid', () => {
|
||||||
it('throws error when contribution does not exist', async () => {
|
it('throws error when contribution does not exist', async () => {
|
||||||
|
jest.clearAllMocks()
|
||||||
await expect(
|
await expect(
|
||||||
mutate({
|
mutate({
|
||||||
mutation: adminCreateContributionMessage,
|
mutation: adminCreateContributionMessage,
|
||||||
@ -98,16 +99,20 @@ describe('ContributionMessageResolver', () => {
|
|||||||
}),
|
}),
|
||||||
).resolves.toEqual(
|
).resolves.toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
errors: [
|
errors: [new GraphQLError('ContributionMessage was not sent successfully')],
|
||||||
new GraphQLError(
|
|
||||||
'ContributionMessage was not successful: Error: Contribution not found',
|
|
||||||
),
|
|
||||||
],
|
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('logs the error thrown', () => {
|
||||||
|
expect(logger.error).toBeCalledWith(
|
||||||
|
'ContributionMessage was not sent successfully',
|
||||||
|
new Error('Contribution not found'),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
it('throws error when contribution.userId equals user.id', async () => {
|
it('throws error when contribution.userId equals user.id', async () => {
|
||||||
|
jest.clearAllMocks()
|
||||||
await mutate({
|
await mutate({
|
||||||
mutation: login,
|
mutation: login,
|
||||||
variables: { email: 'peter@lustig.de', password: 'Aa12345_' },
|
variables: { email: 'peter@lustig.de', password: 'Aa12345_' },
|
||||||
@ -130,14 +135,17 @@ describe('ContributionMessageResolver', () => {
|
|||||||
}),
|
}),
|
||||||
).resolves.toEqual(
|
).resolves.toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
errors: [
|
errors: [new GraphQLError('ContributionMessage was not sent successfully')],
|
||||||
new GraphQLError(
|
|
||||||
'ContributionMessage was not successful: Error: Admin can not answer on own contribution',
|
|
||||||
),
|
|
||||||
],
|
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('logs the error thrown', () => {
|
||||||
|
expect(logger.error).toBeCalledWith(
|
||||||
|
'ContributionMessage was not sent successfully',
|
||||||
|
new Error('Admin can not answer on his own contribution'),
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('valid input', () => {
|
describe('valid input', () => {
|
||||||
@ -210,6 +218,7 @@ describe('ContributionMessageResolver', () => {
|
|||||||
|
|
||||||
describe('input not valid', () => {
|
describe('input not valid', () => {
|
||||||
it('throws error when contribution does not exist', async () => {
|
it('throws error when contribution does not exist', async () => {
|
||||||
|
jest.clearAllMocks()
|
||||||
await expect(
|
await expect(
|
||||||
mutate({
|
mutate({
|
||||||
mutation: createContributionMessage,
|
mutation: createContributionMessage,
|
||||||
@ -220,16 +229,20 @@ describe('ContributionMessageResolver', () => {
|
|||||||
}),
|
}),
|
||||||
).resolves.toEqual(
|
).resolves.toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
errors: [
|
errors: [new GraphQLError('ContributionMessage was not sent successfully')],
|
||||||
new GraphQLError(
|
|
||||||
'ContributionMessage was not successful: Error: Contribution not found',
|
|
||||||
),
|
|
||||||
],
|
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('logs the error thrown', () => {
|
||||||
|
expect(logger.error).toBeCalledWith(
|
||||||
|
'ContributionMessage was not sent successfully',
|
||||||
|
new Error('Contribution not found'),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
it('throws error when other user tries to send createContributionMessage', async () => {
|
it('throws error when other user tries to send createContributionMessage', async () => {
|
||||||
|
jest.clearAllMocks()
|
||||||
await mutate({
|
await mutate({
|
||||||
mutation: login,
|
mutation: login,
|
||||||
variables: { email: 'peter@lustig.de', password: 'Aa12345_' },
|
variables: { email: 'peter@lustig.de', password: 'Aa12345_' },
|
||||||
@ -244,14 +257,17 @@ describe('ContributionMessageResolver', () => {
|
|||||||
}),
|
}),
|
||||||
).resolves.toEqual(
|
).resolves.toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
errors: [
|
errors: [new GraphQLError('ContributionMessage was not sent successfully')],
|
||||||
new GraphQLError(
|
|
||||||
'ContributionMessage was not successful: Error: Can not send message to contribution of another user',
|
|
||||||
),
|
|
||||||
],
|
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('logs the error thrown', () => {
|
||||||
|
expect(logger.error).toBeCalledWith(
|
||||||
|
'ContributionMessage was not sent successfully',
|
||||||
|
new Error('Can not send message to contribution of another user'),
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('valid input', () => {
|
describe('valid input', () => {
|
||||||
|
|||||||
@ -16,6 +16,7 @@ import { backendLogger as logger } from '@/server/logger'
|
|||||||
import { RIGHTS } from '@/auth/RIGHTS'
|
import { RIGHTS } from '@/auth/RIGHTS'
|
||||||
import { Context, getUser } from '@/server/context'
|
import { Context, getUser } from '@/server/context'
|
||||||
import { sendAddedContributionMessageEmail } from '@/emails/sendEmailVariants'
|
import { sendAddedContributionMessageEmail } from '@/emails/sendEmailVariants'
|
||||||
|
import LogError from '@/server/LogError'
|
||||||
|
|
||||||
@Resolver()
|
@Resolver()
|
||||||
export class ContributionMessageResolver {
|
export class ContributionMessageResolver {
|
||||||
@ -54,8 +55,7 @@ export class ContributionMessageResolver {
|
|||||||
await queryRunner.commitTransaction()
|
await queryRunner.commitTransaction()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
await queryRunner.rollbackTransaction()
|
await queryRunner.rollbackTransaction()
|
||||||
logger.error(`ContributionMessage was not successful: ${e}`)
|
throw new LogError('ContributionMessage was not sent successfully', e)
|
||||||
throw new Error(`ContributionMessage was not successful: ${e}`)
|
|
||||||
} finally {
|
} finally {
|
||||||
await queryRunner.release()
|
await queryRunner.release()
|
||||||
}
|
}
|
||||||
@ -95,9 +95,7 @@ export class ContributionMessageResolver {
|
|||||||
@Ctx() context: Context,
|
@Ctx() context: Context,
|
||||||
): Promise<ContributionMessage> {
|
): Promise<ContributionMessage> {
|
||||||
const user = getUser(context)
|
const user = getUser(context)
|
||||||
if (!user.emailContact) {
|
|
||||||
user.emailContact = await UserContact.findOneOrFail({ where: { id: user.emailId } })
|
|
||||||
}
|
|
||||||
const queryRunner = getConnection().createQueryRunner()
|
const queryRunner = getConnection().createQueryRunner()
|
||||||
await queryRunner.connect()
|
await queryRunner.connect()
|
||||||
await queryRunner.startTransaction('REPEATABLE READ')
|
await queryRunner.startTransaction('REPEATABLE READ')
|
||||||
@ -108,12 +106,10 @@ export class ContributionMessageResolver {
|
|||||||
relations: ['user'],
|
relations: ['user'],
|
||||||
})
|
})
|
||||||
if (!contribution) {
|
if (!contribution) {
|
||||||
logger.error('Contribution not found')
|
throw new LogError('Contribution not found', contributionId)
|
||||||
throw new Error('Contribution not found')
|
|
||||||
}
|
}
|
||||||
if (contribution.userId === user.id) {
|
if (contribution.userId === user.id) {
|
||||||
logger.error('Admin can not answer on own contribution')
|
throw new LogError('Admin can not answer on his own contribution', contributionId)
|
||||||
throw new Error('Admin can not answer on own contribution')
|
|
||||||
}
|
}
|
||||||
if (!contribution.user.emailContact) {
|
if (!contribution.user.emailContact) {
|
||||||
contribution.user.emailContact = await UserContact.findOneOrFail({
|
contribution.user.emailContact = await UserContact.findOneOrFail({
|
||||||
@ -149,8 +145,7 @@ export class ContributionMessageResolver {
|
|||||||
await queryRunner.commitTransaction()
|
await queryRunner.commitTransaction()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
await queryRunner.rollbackTransaction()
|
await queryRunner.rollbackTransaction()
|
||||||
logger.error(`ContributionMessage was not successful: ${e}`)
|
throw new LogError('ContributionMessage was not sent successfully', e)
|
||||||
throw new Error(`ContributionMessage was not successful: ${e}`)
|
|
||||||
} finally {
|
} finally {
|
||||||
await queryRunner.release()
|
await queryRunner.release()
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user