Skip to content

Commit

Permalink
add personId and referrer to log-statements in OxEventHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
DPDS93CT committed Dec 16, 2024
1 parent b0d2683 commit 5e12068
Showing 2 changed files with 56 additions and 38 deletions.
46 changes: 28 additions & 18 deletions src/modules/ox/domain/ox-event-handler.spec.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import { DeepMocked, createMock } from '@golevelup/ts-jest';
import { Test, TestingModule } from '@nestjs/testing';
import { ConfigTestModule, LoggingTestModule } from '../../../../test/utils/index.js';
import { ClassLogger } from '../../../core/logging/class-logger.js';
import { PersonID } from '../../../shared/types/index.js';
import { PersonID, PersonReferrer } from '../../../shared/types/index.js';
import { OxEventHandler } from './ox-event-handler.js';
import { OxService } from './ox.service.js';
import { CreateUserAction } from '../actions/user/create-user.action.js';
@@ -476,21 +476,23 @@ describe('OxEventHandler', () => {

describe('handleEmailAddressGeneratedEvent', () => {
let personId: PersonID;
let referrer: PersonReferrer;
let event: EmailAddressGeneratedEvent;
let person: Person<true>;

beforeEach(() => {
jest.resetAllMocks();
personId = faker.string.uuid();
referrer = faker.internet.userName();
event = new EmailAddressGeneratedEvent(
personId,
faker.internet.userName(),
referrer,
faker.string.uuid(),
faker.internet.email(),
true,
faker.string.numeric(),
);
person = createMock<Person<true>>({ email: faker.internet.email(), referrer: faker.internet.userName() });
person = createMock<Person<true>>({ email: faker.internet.email(), referrer: referrer });
});

it('should skip event, if not enabled', async () => {
@@ -517,7 +519,7 @@ describe('OxEventHandler', () => {
expect(oxServiceMock.send).toHaveBeenLastCalledWith(expect.any(ExistsUserAction));
expect(oxServiceMock.send).toHaveBeenCalledTimes(1);
expect(loggerMock.error).toHaveBeenLastCalledWith(
`Cannot create user in OX, username:${person.referrer} already exists`,
`Cannot create user in OX, username:${person.referrer} already exists, personId:${personId}`,
);
});

@@ -527,7 +529,9 @@ describe('OxEventHandler', () => {
await sut.handleEmailAddressGeneratedEvent(event);

expect(oxServiceMock.send).toHaveBeenCalledTimes(0);
expect(loggerMock.error).toHaveBeenLastCalledWith(`Person not found for personId:${personId}`);
expect(loggerMock.error).toHaveBeenLastCalledWith(
`Person not found for personId:${personId}, referrer:${referrer}`,
);
});

it('should log error when person has no referrer set', async () => {
@@ -603,7 +607,7 @@ describe('OxEventHandler', () => {

expect(oxServiceMock.send).toHaveBeenCalledWith(expect.any(CreateUserAction));
expect(loggerMock.info).toHaveBeenCalledWith(
`User created in OX, oxUserId:${fakeOXUserId}, oxEmail:${event.address}, personId:${personId}`,
`User created in OX, oxUserId:${fakeOXUserId}, oxEmail:${event.address}, personId:${personId}, referrer:${referrer}`,
);
expect(loggerMock.info).toHaveBeenLastCalledWith(
`Successfully Added OxUser To OxGroup, oxUserId:${fakeOXUserId}, oxGroupId:${fakeOXGroupId}`,
@@ -660,7 +664,7 @@ describe('OxEventHandler', () => {

expect(oxServiceMock.send).toHaveBeenCalledWith(expect.any(CreateUserAction));
expect(loggerMock.error).toHaveBeenLastCalledWith(
`Could Not Adjust GlobalAddressBookDisabled For oxUserId:${fakeOXUserId}, error: Unknown OX-error`,
`Could Not Adjust GlobalAddressBookDisabled For oxUserId:${fakeOXUserId}, personId:${personId}, referrer:${referrer}, error: Unknown OX-error`,
);
expect(eventServiceMock.publish).toHaveBeenCalledTimes(1);
});
@@ -724,10 +728,10 @@ describe('OxEventHandler', () => {

expect(oxServiceMock.send).toHaveBeenCalledWith(expect.any(CreateUserAction));
expect(loggerMock.info).toHaveBeenLastCalledWith(
`User created in OX, oxUserId:${fakeOXUserId}, oxEmail:${event.address}, personId:${personId}`,
`User created in OX, oxUserId:${fakeOXUserId}, oxEmail:${event.address}, personId:${personId}, referrer:${referrer}`,
);
expect(loggerMock.error).toHaveBeenLastCalledWith(
`Persisting oxUserId on emailAddress for personId:${personId} failed`,
`Persisting oxUserId on emailAddress failed, personId:${personId}, referrer:${referrer}`,
);
expect(eventServiceMock.publish).toHaveBeenCalledTimes(0);
});
@@ -750,7 +754,9 @@ describe('OxEventHandler', () => {
await sut.handleEmailAddressGeneratedEvent(event);

expect(oxServiceMock.send).toHaveBeenLastCalledWith(expect.any(CreateUserAction));
expect(loggerMock.error).toHaveBeenLastCalledWith(`Could not create user in OX, error: Request failed`);
expect(loggerMock.error).toHaveBeenLastCalledWith(
`Could not create user in OX, personId:${personId}, referrer:${referrer}, error: Request failed`,
);
});
});

@@ -776,7 +782,7 @@ describe('OxEventHandler', () => {
contextName: faker.string.alpha();
event = new EmailAddressChangedEvent(
personId,
faker.internet.userName(),
referrer,
faker.string.uuid(),
faker.internet.email(),
faker.string.uuid(),
@@ -800,7 +806,9 @@ describe('OxEventHandler', () => {
await sut.handleEmailAddressChangedEvent(event);

expect(oxServiceMock.send).toHaveBeenCalledTimes(0);
expect(loggerMock.error).toHaveBeenLastCalledWith(`Person not found for personId:${personId}`);
expect(loggerMock.error).toHaveBeenLastCalledWith(
`Person not found for personId:${personId}, referrer:${referrer}`,
);
});

it('should log error when person has no referrer set', async () => {
@@ -822,7 +830,9 @@ describe('OxEventHandler', () => {
await sut.handleEmailAddressChangedEvent(event);

expect(oxServiceMock.send).toHaveBeenCalledTimes(0);
expect(loggerMock.error).toHaveBeenLastCalledWith(`Person with personId:${personId} has no OXUserId`);
expect(loggerMock.error).toHaveBeenLastCalledWith(
`Person has no OXUserId, personId:${personId}, referrer:${referrer}`,
);
});

it('should log error when no requestedEmailAddress is found for person', async () => {
@@ -850,7 +860,7 @@ describe('OxEventHandler', () => {

expect(oxServiceMock.send).toHaveBeenCalledTimes(1);
expect(loggerMock.error).toHaveBeenLastCalledWith(
`Cannot get data for user with username:${person.referrer} from OX, Aborting Email-Address Change`,
`Cannot get data for username:${person.referrer} from OX, Aborting Email-Address Change, personId:${personId}, referrer:${referrer}`,
);
});

@@ -876,7 +886,7 @@ describe('OxEventHandler', () => {

expect(oxServiceMock.send).toHaveBeenCalledTimes(2);
expect(loggerMock.error).toHaveBeenLastCalledWith(
`Could not change email-address for oxUserId:${person.oxUserId} in OX, error: Request failed`,
`Could not change email-address for oxUserId:${person.oxUserId}, personId:${personId}, referrer:${referrer}, error: Request failed`,
);
});

@@ -909,12 +919,12 @@ describe('OxEventHandler', () => {
`Found mostRecentRequested Email-Address:${JSON.stringify(email)} For personId:${personId}`,
);
//use regex, because strict comparison fails, local test-var currentAliases has changed by the implemented function when expect is checked here
expect(loggerMock.info).toHaveBeenCalledWith(expect.stringMatching(/Found Current aliases:.*/));
expect(loggerMock.info).toHaveBeenCalledWith(
expect.stringMatching(/Found Current aliases:.* For personId:/),
`Added New alias:${email}, personId:${personId}, referrer:${referrer}`,
);
expect(loggerMock.info).toHaveBeenCalledWith(`Added New alias:${email} For personId:${personId}`);
expect(loggerMock.info).toHaveBeenLastCalledWith(
`Changed primary email-address in OX for user, username:${person.referrer}, new email-address:${email}`,
`Changed primary email-address in OX for user, username:${person.referrer}, new email-address:${email}, personId:${personId}, referrer:${referrer}`,
);
expect(eventServiceMock.publish).toHaveBeenLastCalledWith(
expect.objectContaining({
48 changes: 28 additions & 20 deletions src/modules/ox/domain/ox-event-handler.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import { DomainError } from '../../../shared/error/index.js';
import { OxConfig } from '../../../shared/config/ox.config.js';
import { OxService } from './ox.service.js';
import { CreateUserAction, CreateUserParams, CreateUserResponse } from '../actions/user/create-user.action.js';
import { PersonID } from '../../../shared/types/index.js';
import { PersonID, PersonReferrer } from '../../../shared/types/index.js';
import { Person } from '../../person/domain/person.js';
import { PersonRepository } from '../../person/persistence/person.repository.js';
import { EmailAddressGeneratedEvent } from '../../../shared/events/email-address-generated.event.js';
@@ -84,27 +84,27 @@ export class OxEventHandler {
@EventHandler(EmailAddressChangedEvent)
public async handleEmailAddressChangedEvent(event: EmailAddressChangedEvent): Promise<void> {
this.logger.info(
`Received EmailAddressChangedEvent, personId:${event.personId}, oldEmailAddressId:${event.oldEmailAddressId}, oldAddress:${event.oldAddress}, newEmailAddressId:${event.newEmailAddressId}, newAddress:${event.newAddress}`,
`Received EmailAddressChangedEvent, personId:${event.personId}, referrer:${event.referrer}, oldEmailAddressId:${event.oldEmailAddressId}, oldAddress:${event.oldAddress}, newEmailAddressId:${event.newEmailAddressId}, newAddress:${event.newAddress}`,
);

if (!this.ENABLED) {
return this.logger.info('Not enabled, ignoring event');
}

await this.changeOxUser(event.personId);
await this.changeOxUser(event.personId, event.referrer);
}

@EventHandler(EmailAddressGeneratedEvent)
public async handleEmailAddressGeneratedEvent(event: EmailAddressGeneratedEvent): Promise<void> {
this.logger.info(
`Received EmailAddressGeneratedEvent, personId:${event.personId}, emailAddressId:${event.emailAddressId}, address:${event.address}`,
`Received EmailAddressGeneratedEvent, personId:${event.personId}, referrer:${event.referrer}, emailAddressId:${event.emailAddressId}, address:${event.address}`,
);

if (!this.ENABLED) {
return this.logger.info('Not enabled, ignoring event');
}

await this.createOxUser(event.personId, event.orgaKennung);
await this.createOxUser(event.personId, event.referrer, event.orgaKennung);
}

@EventHandler(EmailAddressAlreadyExistsEvent)
@@ -456,11 +456,11 @@ export class OxEventHandler {
return result;
}

private async createOxUser(personId: PersonID, orgaKennung: string): Promise<void> {
private async createOxUser(personId: PersonID, referrer: PersonReferrer, orgaKennung: string): Promise<void> {
const person: Option<Person<true>> = await this.personRepository.findById(personId);

if (!person) {
return this.logger.error(`Person not found for personId:${personId}`);
return this.logger.error(`Person not found for personId:${personId}, referrer:${referrer}`);
}
if (!person.referrer) {
return this.logger.error(`Person with personId:${personId} has no referrer: cannot create OXEmailAddress`);
@@ -483,7 +483,9 @@ export class OxEventHandler {
const existsResult: Result<ExistsUserResponse, DomainError> = await this.oxService.send(existsAction);

if (existsResult.ok && existsResult.value.exists) {
this.logger.error(`Cannot create user in OX, username:${person.referrer} already exists`);
this.logger.error(
`Cannot create user in OX, username:${person.referrer} already exists, personId:${personId}`,
);
return;
}

@@ -508,11 +510,13 @@ export class OxEventHandler {
mostRecentRequestedEmailAddress.failed();
await this.emailRepo.save(mostRecentRequestedEmailAddress);

return this.logger.error(`Could not create user in OX, error: ${createUserResult.error.message}`);
return this.logger.error(
`Could not create user in OX, personId:${personId}, referrer:${referrer}, error: ${createUserResult.error.message}`,
);
}

this.logger.info(
`User created in OX, oxUserId:${createUserResult.value.id}, oxEmail:${createUserResult.value.primaryEmail}, personId:${personId}`,
`User created in OX, oxUserId:${createUserResult.value.id}, oxEmail:${createUserResult.value.primaryEmail}, personId:${personId}, referrer:${referrer}`,
);

mostRecentRequestedEmailAddress.oxUserID = createUserResult.value.id;
@@ -522,7 +526,9 @@ export class OxEventHandler {
if (emailAddressUpdateResult instanceof DomainError) {
mostRecentRequestedEmailAddress.failed();
await this.emailRepo.save(mostRecentRequestedEmailAddress);
return this.logger.error(`Persisting oxUserId on emailAddress for personId:${personId} failed`);
return this.logger.error(
`Persisting oxUserId on emailAddress failed, personId:${personId}, referrer:${referrer}`,
);
}

const oxGroupId: Result<OXGroupID> = await this.getExistingOxGroupByNameOrCreateOxGroup(
@@ -563,7 +569,7 @@ export class OxEventHandler {
if (!changeByModuleAccessResult.ok) {
//only log error, do not set email-address status = FAILED, the ChangeByModuleAccessAction won't work against OX-DEV
this.logger.error(
`Could Not Adjust GlobalAddressBookDisabled For oxUserId:${createUserResult.value.id}, error: ${changeByModuleAccessResult.error.message}`,
`Could Not Adjust GlobalAddressBookDisabled For oxUserId:${createUserResult.value.id}, personId:${personId}, referrer:${referrer}, error: ${changeByModuleAccessResult.error.message}`,
);
}

@@ -580,19 +586,19 @@ export class OxEventHandler {
);
}

private async changeOxUser(personId: PersonID): Promise<void> {
private async changeOxUser(personId: PersonID, referrer: PersonReferrer): Promise<void> {
const person: Option<Person<true>> = await this.personRepository.findById(personId);

if (!person) {
return this.logger.error(`Person not found for personId:${personId}`);
return this.logger.error(`Person not found for personId:${personId}, referrer:${referrer}`);
}
if (!person.referrer) {
return this.logger.error(
`Person with personId:${personId} has no referrer: Cannot Change Email-Address In OX`,
);
}
if (!person.oxUserId) {
return this.logger.error(`Person with personId:${personId} has no OXUserId`);
return this.logger.error(`Person has no OXUserId, personId:${personId}, referrer:${referrer}`);
}

const mostRecentRequestedEmailAddress: Option<EmailAddress<true>> =
@@ -615,14 +621,16 @@ export class OxEventHandler {
mostRecentRequestedEmailAddress.failed();
await this.emailRepo.save(mostRecentRequestedEmailAddress);
return this.logger.error(
`Cannot get data for user with username:${person.referrer} from OX, Aborting Email-Address Change`,
`Cannot get data for username:${person.referrer} from OX, Aborting Email-Address Change, personId:${personId}, referrer:${referrer}`,
);
}
const newAliasesArray: string[] = getDataResult.value.aliases;
this.logger.info(`Found Current aliases:${JSON.stringify(newAliasesArray)} For personId:${personId}`);
this.logger.info(
`Found Current aliases:${JSON.stringify(newAliasesArray)}, personId:${personId}, referrer:${referrer}`,
);

newAliasesArray.push(requestedEmailAddressString);
this.logger.info(`Added New alias:${requestedEmailAddressString} For personId:${personId}`);
this.logger.info(`Added New alias:${requestedEmailAddressString}, personId:${personId}, referrer:${referrer}`);

const params: ChangeUserParams = {
contextId: this.contextID,
@@ -648,12 +656,12 @@ export class OxEventHandler {
await this.emailRepo.save(mostRecentRequestedEmailAddress);

return this.logger.error(
`Could not change email-address for oxUserId:${person.oxUserId} in OX, error: ${result.error.message}`,
`Could not change email-address for oxUserId:${person.oxUserId}, personId:${personId}, referrer:${referrer}, error: ${result.error.message}`,
);
}

this.logger.info(
`Changed primary email-address in OX for user, username:${person.referrer}, new email-address:${requestedEmailAddressString}`,
`Changed primary email-address in OX for user, username:${person.referrer}, new email-address:${requestedEmailAddressString}, personId:${personId}, referrer:${referrer}`,
);

this.eventService.publish(

0 comments on commit 5e12068

Please sign in to comment.