Skip to content

Commit 95bf5d6

Browse files
committed
Update BackofficeCourses on save method when the course already exists
1 parent bd15327 commit 95bf5d6

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

src/Contexts/Backoffice/Courses/infrastructure/persistence/ElasticBackofficeCourseRepository.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ export class ElasticBackofficeCourseRepository
1111
}
1212

1313
async save(course: BackofficeCourse): Promise<void> {
14-
return this.persist(course);
14+
const currentCourse = await this.findById(course.id.value, BackofficeCourse.fromPrimitives);
15+
if (currentCourse) {
16+
return this.update(course.id.value, course);
17+
}
18+
19+
return this.persist(course.id.value, course);
1520
}
1621

1722
async matching(criteria: Criteria): Promise<BackofficeCourse[]> {

src/Contexts/Shared/infrastructure/persistence/elasticsearch/ElasticRepository.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import bodybuilder, { Bodybuilder } from 'bodybuilder';
44
import httpStatus from 'http-status';
55
import { AggregateRoot } from '../../../domain/AggregateRoot';
66
import { Criteria } from '../../../domain/criteria/Criteria';
7+
import { Nullable } from '../../../domain/Nullable';
78
import ElasticConfig from './ElasticConfig';
89
import { ElasticCriteriaConverter, TypeQueryEnum } from './ElasticCriteriaConverter';
910

@@ -58,10 +59,30 @@ export abstract class ElasticRepository<T extends AggregateRoot> {
5859
return e instanceof ResponseError && (e as ResponseError).meta.statusCode === httpStatus.NOT_FOUND;
5960
}
6061

61-
protected async persist(aggregateRoot: T): Promise<void> {
62+
protected async persist(id: string, aggregateRoot: T): Promise<void> {
6263
const client = await this.client();
6364
const document = { ...aggregateRoot.toPrimitives() };
6465

65-
await client.index({ index: this.indexName(), body: document, refresh: 'wait_for' }); // wait_for wait for a refresh to make this operation visible to search
66+
await client.index({ index: this.indexName(), id, body: document, refresh: 'wait_for' }); // wait_for wait for a refresh to make this operation visible to search
67+
}
68+
69+
update(id: string, course: T): Promise<void> {
70+
return this.persist(id, course);
71+
}
72+
73+
protected async findById(id: string, unserializer: (data: any) => T): Promise<Nullable<T>> {
74+
const client = await this.client();
75+
try {
76+
const { body } = await client.get({
77+
index: this.indexName(),
78+
id
79+
});
80+
return unserializer(body._source);
81+
} catch (e) {
82+
if (this.isNotFoundError(e)) {
83+
return null;
84+
}
85+
throw e;
86+
}
6687
}
6788
}

tests/Contexts/Backoffice/Courses/infrastructure/BackofficeCourseRepository.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ afterEach(async () => {
1717
});
1818

1919
describe('BackofficeCourseRepository', () => {
20+
describe('#save', () => {
21+
it('should be able to persist the same course twice', async () => {
22+
const course = BackofficeCourseMother.random();
23+
24+
await repository.save(course);
25+
await repository.save(course);
26+
27+
const persistedCourses = await repository.searchAll();
28+
29+
expect(persistedCourses).toHaveLength(1);
30+
expect(persistedCourses).toEqual([course]);
31+
});
32+
});
33+
2034
describe('#searchAll', () => {
2135
it('should return the existing courses', async () => {
2236
const courses = [BackofficeCourseMother.random(), BackofficeCourseMother.random()];

0 commit comments

Comments
 (0)