-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
452 additions
and
331 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Developer notes on persistence layer | ||
|
||
## Mapping aggregates to entities | ||
|
||
During development many aggregates were created and these aggregates have to be persisted as entities. | ||
Therefore, repositories were built which typically contain at least a _mapAggregateToData_ method and a _mapDataToAggregate_ method. | ||
|
||
### Troubleshooting | ||
|
||
If you encounter following error while writing such a method, 'Type Option<string> is not assignable to type string' e.g. on the id: | ||
|
||
```typescript | ||
function mapAggregateToData(aggregate: Aggregate<boolean>): RequiredEntityData<Aggregate> { | ||
return { | ||
// Don't assign createdAt and updatedAt, they are auto-generated! | ||
id: aggregate.id, //Here following error is shown: Type Option<string> is not assignable to type string | ||
personId: rel(PersonEntity, aggregate.personId), | ||
value: aggregate.value, | ||
}; | ||
} | ||
``` | ||
make sure that id is the primary key for the entity, additional declarations of primary key attributes may result in the error mentioned above, like e.g. in this entity: | ||
```typescript | ||
@Entity({ tableName: 'tableName' }) | ||
export class Entitiy extends TimestampedEntity { | ||
//... | ||
public [PrimaryKeyProp]?: ['anotherProp']; | ||
} | ||
``` | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ import { PersonRepository } from '../../person/persistence/person.repository.js' | |
import { EventModule, EventService } from '../../../core/eventbus/index.js'; | ||
import { EmailFactory } from './email.factory.js'; | ||
import { Email } from './email.js'; | ||
import { EmailID, PersonID, RolleID } from '../../../shared/types/index.js'; | ||
import { EmailAddressID, PersonID, RolleID } from '../../../shared/types/index.js'; | ||
import { EmailInvalidError } from '../error/email-invalid.error.js'; | ||
import { ClassLogger } from '../../../core/logging/class-logger.js'; | ||
import { EmailAddress } from './email-address.js'; | ||
|
@@ -39,16 +39,11 @@ import { PersonRenamedEvent } from '../../../shared/events/person-renamed.event. | |
import { Person } from '../../person/domain/person.js'; | ||
|
||
function getEmail(emaiGeneratorService: EmailGeneratorService, personRepository: PersonRepository): Email<true> { | ||
const fakeEmailId: EmailID = faker.string.uuid(); | ||
return Email.construct( | ||
fakeEmailId, | ||
faker.date.past(), | ||
faker.date.recent(), | ||
faker.string.uuid(), | ||
emaiGeneratorService, | ||
personRepository, | ||
[new EmailAddress<true>(fakeEmailId, faker.internet.email(), true)], | ||
); | ||
const fakePersonId: PersonID = faker.string.uuid(); | ||
const fakeEmailAddressId: string = faker.string.uuid(); | ||
return Email.construct(fakePersonId, emaiGeneratorService, personRepository, [ | ||
new EmailAddress<boolean>(fakeEmailAddressId, undefined, undefined, fakePersonId, faker.internet.email(), true), | ||
]); | ||
} | ||
|
||
function getPerson(): Person<true> { | ||
|
@@ -157,7 +152,7 @@ describe('Email Event Handler', () => { | |
describe('when existing email is found', () => { | ||
it('should enable existing email', async () => { | ||
const fakePersonId: PersonID = faker.string.uuid(); | ||
const emailId: EmailID = faker.string.uuid(); | ||
const emailAddressId: EmailAddressID = faker.string.uuid(); | ||
const event: PersonenkontextCreatedEvent = new PersonenkontextCreatedEvent( | ||
fakePersonId, | ||
faker.string.uuid(), | ||
|
@@ -168,7 +163,14 @@ describe('Email Event Handler', () => { | |
serviceProviderRepoMock.findByIds.mockResolvedValueOnce(spMap); | ||
|
||
const emailAddresses: EmailAddress<true>[] = [ | ||
new EmailAddress<true>(emailId, faker.internet.email(), true), | ||
new EmailAddress<true>( | ||
emailAddressId, | ||
faker.date.past(), | ||
faker.date.recent(), | ||
fakePersonId, | ||
faker.internet.email(), | ||
true, | ||
), | ||
]; | ||
// eslint-disable-next-line @typescript-eslint/require-await | ||
emailRepoMock.findByPerson.mockImplementationOnce(async (personId: PersonID) => { | ||
|
@@ -184,7 +186,7 @@ describe('Email Event Handler', () => { | |
get currentAddress(): Option<string> { | ||
return '[email protected]'; | ||
}, | ||
id: emailId, | ||
personId: fakePersonId, | ||
emailAddresses: emailAddresses, | ||
}), | ||
}; | ||
|
@@ -203,7 +205,7 @@ describe('Email Event Handler', () => { | |
describe('when existing email is found but enabling results in error', () => { | ||
it('should log error', async () => { | ||
const fakePersonId: PersonID = faker.string.uuid(); | ||
const emailId: EmailID = faker.string.uuid(); | ||
const emailAddressId: EmailAddressID = faker.string.uuid(); | ||
const event: PersonenkontextCreatedEvent = new PersonenkontextCreatedEvent( | ||
fakePersonId, | ||
faker.string.uuid(), | ||
|
@@ -214,7 +216,14 @@ describe('Email Event Handler', () => { | |
serviceProviderRepoMock.findByIds.mockResolvedValueOnce(spMap); | ||
|
||
const emailAddresses: EmailAddress<true>[] = [ | ||
new EmailAddress<true>(emailId, faker.internet.email(), true), | ||
new EmailAddress<true>( | ||
emailAddressId, | ||
faker.date.past(), | ||
faker.date.recent(), | ||
fakePersonId, | ||
faker.internet.email(), | ||
true, | ||
), | ||
]; | ||
// eslint-disable-next-line @typescript-eslint/require-await | ||
emailRepoMock.findByPerson.mockImplementationOnce(async (personId: PersonID) => { | ||
|
@@ -255,7 +264,16 @@ describe('Email Event Handler', () => { | |
|
||
emailFactoryMock.createNew.mockImplementationOnce((personId: PersonID) => { | ||
const emailMock: DeepMocked<Email<false>> = createMock<Email<false>>({ | ||
emailAddresses: [new EmailAddress<false>(undefined, faker.internet.email(), true)], | ||
emailAddresses: [ | ||
new EmailAddress<false>( | ||
undefined, | ||
undefined, | ||
undefined, | ||
personId, | ||
faker.internet.email(), | ||
true, | ||
), | ||
], | ||
personId: personId, | ||
}); | ||
const emailAddress: EmailAddress<false> = createMock<EmailAddress<false>>({ | ||
|
@@ -390,8 +408,6 @@ describe('Email Event Handler', () => { | |
|
||
describe('when deletion is successful', () => { | ||
it('should log info', async () => { | ||
emailRepoMock.deleteById.mockResolvedValueOnce(true); | ||
|
||
await emailEventHandler.asyncPersonDeletedEventHandler(event); | ||
|
||
expect(loggerMock.info).toHaveBeenCalledWith(`Successfully deactivated email-address:${emailAddress}`); | ||
|
@@ -503,7 +519,7 @@ describe('Email Event Handler', () => { | |
get currentAddress(): Option<string> { | ||
return '[email protected]'; | ||
}, | ||
id: faker.string.uuid(), | ||
personId: faker.string.uuid(), | ||
emailAddresses: [], | ||
}), | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.