Skip to content

Commit 26163fe

Browse files
committed
fix: createGuestbook returns ID
1 parent 92f65b3 commit 26163fe

File tree

5 files changed

+36
-28
lines changed

5 files changed

+36
-28
lines changed

src/guestbooks/domain/repositories/IGuestbooksRepository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface IGuestbooksRepository {
55
createGuestbook(
66
collectionIdOrAlias: number | string,
77
guestbook: CreateGuestbookDTO
8-
): Promise<void>
8+
): Promise<number>
99
getGuestbook(guestbookId: number): Promise<Guestbook>
1010
getGuestbooksByCollectionId(collectionIdOrAlias: number | string): Promise<Guestbook[]>
1111
setGuestbookEnabled(
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
import { UseCase } from '../../../core/domain/useCases/UseCase'
21
import { CreateGuestbookDTO } from '../dtos/CreateGuestbookDTO'
32
import { IGuestbooksRepository } from '../repositories/IGuestbooksRepository'
43

5-
export class CreateGuestbook implements UseCase<void> {
4+
export class CreateGuestbook {
65
constructor(private readonly guestbooksRepository: IGuestbooksRepository) {}
76

87
/**
98
* Creates a guestbook for the given collection.
109
*
1110
* @param {CreateGuestbookDTO} guestbook - Guestbook creation payload.
1211
* @param {number | string} collectionIdOrAlias - Collection identifier (numeric id or alias).
13-
* @returns {Promise<void>}
12+
* @returns {Promise<number>} - The created guestbook identifier.
1413
*/
15-
async execute(guestbook: CreateGuestbookDTO, collectionIdOrAlias: number | string) {
14+
async execute(
15+
guestbook: CreateGuestbookDTO,
16+
collectionIdOrAlias: number | string
17+
): Promise<number> {
1618
return await this.guestbooksRepository.createGuestbook(collectionIdOrAlias, guestbook)
1719
}
1820
}

src/guestbooks/infra/repositories/GuestbooksRepository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ export class GuestbooksRepository extends ApiRepository implements IGuestbooksRe
1010
public async createGuestbook(
1111
collectionIdOrAlias: number | string,
1212
guestbook: CreateGuestbookDTO
13-
): Promise<void> {
13+
): Promise<number> {
1414
return this.doPost(
1515
this.buildApiEndpoint(this.guestbooksResourceName, `${collectionIdOrAlias}`),
1616
guestbook
1717
)
18-
.then(() => undefined)
18+
.then((response) => response.data.data.id)
1919
.catch((error) => {
2020
throw error
2121
})

test/integration/guestbooks/GuestbooksRepository.test.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,20 @@ describe('GuestbooksRepository', () => {
7979
describe('createGuestbook', () => {
8080
test('should create guestbook for collection', async () => {
8181
const actual = await sut.createGuestbook(testCollectionId, createGuestbookDTO)
82-
expect(actual).toBeUndefined()
82+
expect(actual).toEqual(expect.any(Number))
83+
expect(actual).toBeGreaterThan(0)
84+
85+
const getGuestbookResponse = await sut.getGuestbook(actual)
86+
expect(getGuestbookResponse.name).toBe(createGuestbookDTO.name)
8387
})
8488

8589
test('should create guestbook for collection by collection alias', async () => {
8690
const actual = await sut.createGuestbook(testCollectionAlias, createGuestbookDTO)
87-
expect(actual).toBeUndefined()
91+
expect(actual).toEqual(expect.any(Number))
92+
expect(actual).toBeGreaterThan(0)
93+
94+
const getGuestbookResponse = await sut.getGuestbook(actual)
95+
expect(getGuestbookResponse.name).toBe(createGuestbookDTO.name)
8896
})
8997

9098
test('should return error when collection does not exist', async () => {
@@ -94,16 +102,20 @@ describe('GuestbooksRepository', () => {
94102

95103
describe('getGuestbooksByCollectionId', () => {
96104
test('should list guestbooks for collection', async () => {
97-
await sut.createGuestbook(testCollectionId, createGuestbookDTO)
105+
createdGuestbookId = await sut.createGuestbook(testCollectionId, createGuestbookDTO)
98106
const actual = await sut.getGuestbooksByCollectionId(testCollectionId)
99107
expect(actual.length).toBeGreaterThan(0)
100-
createdGuestbookId = actual[0].id as number
108+
expect(actual.some((guestbook) => guestbook.id === createdGuestbookId)).toBe(true)
101109
})
102110

103111
test('should list guestbooks for collection by collection alias', async () => {
104-
await sut.createGuestbook(testCollectionAlias, createGuestbookDTO)
112+
const createdByAliasGuestbookId = await sut.createGuestbook(
113+
testCollectionAlias,
114+
createGuestbookDTO
115+
)
105116
const actual = await sut.getGuestbooksByCollectionId(testCollectionAlias)
106117
expect(actual.length).toBeGreaterThan(0)
118+
expect(actual.some((guestbook) => guestbook.id === createdByAliasGuestbookId)).toBe(true)
107119
})
108120

109121
test('should return error when collection does not exist', async () => {
@@ -113,8 +125,8 @@ describe('GuestbooksRepository', () => {
113125

114126
describe('getGuestbook', () => {
115127
test('should get guestbook by id', async () => {
116-
await sut.createGuestbook(testCollectionId, createGuestbookDTO)
117-
const actual = await sut.getGuestbook(createdGuestbookId as number)
128+
createdGuestbookId = await sut.createGuestbook(testCollectionId, createGuestbookDTO)
129+
const actual = await sut.getGuestbook(createdGuestbookId)
118130
expect(actual.id).toBe(createdGuestbookId)
119131
expect(actual.name).toBe(createGuestbookDTO.name)
120132
})
@@ -126,17 +138,17 @@ describe('GuestbooksRepository', () => {
126138

127139
describe('setGuestbookEnabled', () => {
128140
test('should disable guestbook', async () => {
129-
await sut.createGuestbook(testCollectionId, createGuestbookDTO)
141+
createdGuestbookId = await sut.createGuestbook(testCollectionId, createGuestbookDTO)
130142

131-
await sut.setGuestbookEnabled(testCollectionId, createdGuestbookId as number, false)
132-
const actual = await sut.getGuestbook(createdGuestbookId as number)
143+
await sut.setGuestbookEnabled(testCollectionId, createdGuestbookId, false)
144+
const actual = await sut.getGuestbook(createdGuestbookId)
133145

134146
expect(actual.enabled).toBe(false)
135147
})
136148

137149
test('should enable guestbook', async () => {
138-
await sut.setGuestbookEnabled(testCollectionId, createdGuestbookId as number, true)
139-
const actual = await sut.getGuestbook(createdGuestbookId as number)
150+
await sut.setGuestbookEnabled(testCollectionId, createdGuestbookId, true)
151+
const actual = await sut.getGuestbook(createdGuestbookId)
140152

141153
expect(actual.enabled).toBe(true)
142154
})
@@ -153,17 +165,11 @@ describe('GuestbooksRepository', () => {
153165
let assignableGuestbookId: number
154166

155167
beforeAll(async () => {
156-
await sut.createGuestbook(testCollectionId, {
168+
assignableGuestbookId = await sut.createGuestbook(testCollectionId, {
157169
...createGuestbookDTO,
158170
name: 'assign/remove guestbook test'
159171
})
160172

161-
const guestbooks = await sut.getGuestbooksByCollectionId(testCollectionId)
162-
const assignableGuestbook = guestbooks.find(
163-
(guestbook) => guestbook.name === 'assign/remove guestbook test'
164-
)
165-
assignableGuestbookId = assignableGuestbook?.id as number
166-
167173
testDatasetIds = await createDataset.execute(
168174
TestConstants.TEST_NEW_DATASET_DTO,
169175
testCollectionAlias

test/unit/guestbooks/CreateGuestbook.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ describe('CreateGuestbook', () => {
4545

4646
test('should create guestbook for collection', async () => {
4747
const repository: IGuestbooksRepository = {} as IGuestbooksRepository
48-
repository.createGuestbook = jest.fn().mockResolvedValue(undefined)
48+
repository.createGuestbook = jest.fn().mockResolvedValue(123)
4949

5050
const sut = new CreateGuestbook(repository)
5151
const actual = await sut.execute(createGuestbookDTO, collectionId)
5252

5353
expect(repository.createGuestbook).toHaveBeenCalledWith(collectionId, createGuestbookDTO)
54-
expect(actual).toBeUndefined()
54+
expect(actual).toBe(123)
5555
})
5656

5757
test('should throw WriteError when repository fails', async () => {

0 commit comments

Comments
 (0)