diff --git a/backend/src/event/EVENT_ADMIN_CONTRIBUTION_MESSAGE_CREATE.ts b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_MESSAGE_CREATE.ts new file mode 100644 index 000000000..f07d38e98 --- /dev/null +++ b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_MESSAGE_CREATE.ts @@ -0,0 +1,23 @@ +import { User as DbUser } from '@entity/User' +import { Contribution as DbContribution } from '@entity/Contribution' +import { ContributionMessage as DbContributionMessage } from '@entity/ContributionMessage' +import { Event as DbEvent } from '@entity/Event' +import { Event, EventType } from './Event' + +export const EVENT_ADMIN_CONTRIBUTION_MESSAGE_CREATE = async ( + user: DbUser, + moderator: DbUser, + contribution: DbContribution, + contributionMessage: DbContributionMessage, +): Promise => + Event( + EventType.ADMIN_CONTRIBUTION_MESSAGE_CREATE, + user, + moderator, + null, + null, + contribution, + contributionMessage, + null, + null, + ).save() diff --git a/backend/src/event/EVENT_CONTRIBUTION_MESSAGE_CREATE.ts b/backend/src/event/EVENT_CONTRIBUTION_MESSAGE_CREATE.ts new file mode 100644 index 000000000..b06685a6d --- /dev/null +++ b/backend/src/event/EVENT_CONTRIBUTION_MESSAGE_CREATE.ts @@ -0,0 +1,22 @@ +import { User as DbUser } from '@entity/User' +import { Contribution as DbContribution } from '@entity/Contribution' +import { ContributionMessage as DbContributionMessage } from '@entity/ContributionMessage' +import { Event as DbEvent } from '@entity/Event' +import { Event, EventType } from './Event' + +export const EVENT_CONTRIBUTION_MESSAGE_CREATE = async ( + user: DbUser, + contribution: DbContribution, + contributionMessage: DbContributionMessage, +): Promise => + Event( + EventType.CONTRIBUTION_MESSAGE_CREATE, + user, + user, + null, + null, + contribution, + contributionMessage, + null, + null, + ).save() diff --git a/backend/src/event/Event.ts b/backend/src/event/Event.ts index 2e7cca6af..f1c4269c9 100644 --- a/backend/src/event/Event.ts +++ b/backend/src/event/Event.ts @@ -45,10 +45,12 @@ export { EVENT_ADMIN_CONTRIBUTION_UPDATE } from './EVENT_ADMIN_CONTRIBUTION_UPDA export { EVENT_ADMIN_CONTRIBUTION_LINK_CREATE } from './EVENT_ADMIN_CONTRIBUTION_LINK_CREATE' export { EVENT_ADMIN_CONTRIBUTION_LINK_DELETE } from './EVENT_ADMIN_CONTRIBUTION_LINK_DELETE' export { EVENT_ADMIN_CONTRIBUTION_LINK_UPDATE } from './EVENT_ADMIN_CONTRIBUTION_LINK_UPDATE' +export { EVENT_ADMIN_CONTRIBUTION_MESSAGE_CREATE } from './EVENT_ADMIN_CONTRIBUTION_MESSAGE_CREATE' export { EVENT_ADMIN_SEND_CONFIRMATION_EMAIL } from './EVENT_ADMIN_SEND_CONFIRMATION_EMAIL' export { EVENT_CONTRIBUTION_CREATE } from './EVENT_CONTRIBUTION_CREATE' export { EVENT_CONTRIBUTION_DELETE } from './EVENT_CONTRIBUTION_DELETE' export { EVENT_CONTRIBUTION_UPDATE } from './EVENT_CONTRIBUTION_UPDATE' +export { EVENT_CONTRIBUTION_MESSAGE_CREATE } from './EVENT_CONTRIBUTION_MESSAGE_CREATE' export { EVENT_LOGIN } from './EVENT_LOGIN' export { EVENT_REGISTER } from './EVENT_REGISTER' export { EVENT_SEND_ACCOUNT_MULTIREGISTRATION_EMAIL } from './EVENT_SEND_ACCOUNT_MULTIREGISTRATION_EMAIL' diff --git a/backend/src/event/EventType.ts b/backend/src/event/EventType.ts index b219a49ba..dda571b5a 100644 --- a/backend/src/event/EventType.ts +++ b/backend/src/event/EventType.ts @@ -9,10 +9,12 @@ export enum EventType { ADMIN_CONTRIBUTION_LINK_CREATE = 'ADMIN_CONTRIBUTION_LINK_CREATE', ADMIN_CONTRIBUTION_LINK_DELETE = 'ADMIN_CONTRIBUTION_LINK_DELETE', ADMIN_CONTRIBUTION_LINK_UPDATE = 'ADMIN_CONTRIBUTION_LINK_UPDATE', + ADMIN_CONTRIBUTION_MESSAGE_CREATE = 'ADMIN_CONTRIBUTION_MESSAGE_CREATE', ADMIN_SEND_CONFIRMATION_EMAIL = 'ADMIN_SEND_CONFIRMATION_EMAIL', CONTRIBUTION_CREATE = 'CONTRIBUTION_CREATE', CONTRIBUTION_DELETE = 'CONTRIBUTION_DELETE', CONTRIBUTION_UPDATE = 'CONTRIBUTION_UPDATE', + CONTRIBUTION_MESSAGE_CREATE = 'CONTRIBUTION_MESSAGE_CREATE', LOGIN = 'LOGIN', REGISTER = 'REGISTER', REDEEM_REGISTER = 'REDEEM_REGISTER', diff --git a/backend/src/graphql/resolver/ContributionMessageResolver.ts b/backend/src/graphql/resolver/ContributionMessageResolver.ts index adfcdf160..d4cab2340 100644 --- a/backend/src/graphql/resolver/ContributionMessageResolver.ts +++ b/backend/src/graphql/resolver/ContributionMessageResolver.ts @@ -16,6 +16,7 @@ import { RIGHTS } from '@/auth/RIGHTS' import { Context, getUser } from '@/server/context' import { sendAddedContributionMessageEmail } from '@/emails/sendEmailVariants' import LogError from '@/server/LogError' +import { EVENT_CONTRIBUTION_MESSAGE_CREATE } from '@/event/Event' @Resolver() export class ContributionMessageResolver { @@ -56,6 +57,11 @@ export class ContributionMessageResolver { await queryRunner.manager.update(DbContribution, { id: contributionId }, contribution) } await queryRunner.commitTransaction() + await EVENT_CONTRIBUTION_MESSAGE_CREATE( + user, + { id: contributionMessage.contributionId } as DbContribution, + contributionMessage, + ) } catch (e) { await queryRunner.rollbackTransaction() throw new LogError(`ContributionMessage was not sent successfully: ${e}`, e) @@ -97,7 +103,7 @@ export class ContributionMessageResolver { @Args() { contributionId, message }: ContributionMessageArgs, @Ctx() context: Context, ): Promise { - const user = getUser(context) + const moderator = getUser(context) const queryRunner = getConnection().createQueryRunner() await queryRunner.connect() @@ -111,7 +117,7 @@ export class ContributionMessageResolver { if (!contribution) { throw new LogError('Contribution not found', contributionId) } - if (contribution.userId === user.id) { + if (contribution.userId === moderator.id) { throw new LogError('Admin can not answer on his own contribution', contributionId) } if (!contribution.user.emailContact) { @@ -122,7 +128,7 @@ export class ContributionMessageResolver { contributionMessage.contributionId = contributionId contributionMessage.createdAt = new Date() contributionMessage.message = message - contributionMessage.userId = user.id + contributionMessage.userId = moderator.id contributionMessage.type = ContributionMessageType.DIALOG contributionMessage.isModerator = true await queryRunner.manager.insert(DbContributionMessage, contributionMessage) @@ -141,11 +147,12 @@ export class ContributionMessageResolver { lastName: contribution.user.lastName, email: contribution.user.emailContact.email, language: contribution.user.language, - senderFirstName: user.firstName, - senderLastName: user.lastName, + senderFirstName: moderator.firstName, + senderLastName: moderator.lastName, contributionMemo: contribution.memo, }) await queryRunner.commitTransaction() + await EVENT_ADMIN_CONTRIBUTION_MESSAGE_CREATE({id: contribution.userId} as DbUser, moderator, contribution, contributionMessage) } catch (e) { await queryRunner.rollbackTransaction() throw new LogError(`ContributionMessage was not sent successfully: ${e}`, e)