Skip to content

Commit 1f29290

Browse files
Merge #426
426: Change getContentTypeEntries to getEntries and create getEntry methods name r=bidoubiwa a=bidoubiwa Since these method are located in the `content-type` service it is redundant to give context in the method's name. This PR updates the naming for ease of read Co-authored-by: Charlotte Vermandel <[email protected]>
2 parents 6107e03 + fbdc7b0 commit 1f29290

File tree

4 files changed

+152
-11
lines changed

4 files changed

+152
-11
lines changed

server/__tests__/content-types.test.js

Lines changed: 113 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,60 @@ describe('Tests content types', () => {
9898
expect(count).toEqual(2)
9999
})
100100

101-
test('Test fetching entries of content type', async () => {
101+
test('Test fetching entries of a content type with default parameters', async () => {
102102
const contentTypeServices = createContentTypeService({
103103
strapi: fakeStrapi,
104104
})
105105

106-
const count = await contentTypeServices.getContentTypeEntries({
106+
const count = await contentTypeServices.getEntries({
107107
contentType: 'api::restaurant.restaurant',
108108
})
109109

110+
expect(fakeStrapi.entityService.findMany).toHaveBeenCalledWith(
111+
'api::restaurant.restaurant',
112+
{
113+
fields: '*',
114+
start: 0,
115+
limit: 500,
116+
filters: {},
117+
sort: {},
118+
populate: '*',
119+
publicationState: 'live',
120+
}
121+
)
122+
expect(fakeStrapi.entityService.findMany).toHaveBeenCalledTimes(1)
123+
expect(count).toEqual([{ id: 1 }])
124+
})
125+
126+
test('Test fetching entries of a content type with custom parameters', async () => {
127+
const contentTypeServices = createContentTypeService({
128+
strapi: fakeStrapi,
129+
})
130+
131+
const count = await contentTypeServices.getEntries({
132+
contentType: 'api::restaurant.restaurant',
133+
fields: 'title',
134+
start: 1,
135+
limit: 2,
136+
filters: { where: { title: 'hello' } },
137+
sort: 'id',
138+
populate: {},
139+
publicationState: 'preview',
140+
})
141+
142+
expect(fakeStrapi.entityService.findMany).toHaveBeenCalledWith(
143+
'api::restaurant.restaurant',
144+
{
145+
fields: 'title',
146+
start: 1,
147+
limit: 2,
148+
filters: { where: { title: 'hello' } },
149+
sort: 'id',
150+
populate: {},
151+
publicationState: 'preview',
152+
}
153+
)
154+
expect(fakeStrapi.entityService.findMany).toHaveBeenCalledTimes(1)
110155
expect(count).toEqual([{ id: 1 }])
111156
})
112157

@@ -115,11 +160,75 @@ describe('Tests content types', () => {
115160
strapi: fakeStrapi,
116161
})
117162

118-
const count = await contentTypeServices.getContentTypeEntries({
163+
const entry = await contentTypeServices.getEntries({
164+
contentType: 'api::test.test',
165+
})
166+
167+
expect(fakeStrapi.entityService.findMany).toHaveBeenCalledTimes(0)
168+
expect(entry).toEqual([])
169+
})
170+
171+
test('Test fetching an entry of a content type with default parameters', async () => {
172+
const contentTypeServices = createContentTypeService({
173+
strapi: fakeStrapi,
174+
})
175+
176+
const entry = await contentTypeServices.getEntry({
177+
contentType: 'api::restaurant.restaurant',
178+
id: 200,
179+
})
180+
181+
expect(fakeStrapi.entityService.findOne).toHaveBeenCalledWith(
182+
'api::restaurant.restaurant',
183+
200,
184+
{
185+
fields: '*',
186+
populate: '*',
187+
}
188+
)
189+
expect(fakeStrapi.entityService.findOne).toHaveBeenCalledTimes(1)
190+
expect(entry).toEqual([{ id: 1 }])
191+
})
192+
193+
test('Test fetching an entry of a content type with custom parameters', async () => {
194+
const contentTypeServices = createContentTypeService({
195+
strapi: fakeStrapi,
196+
})
197+
198+
const entry = await contentTypeServices.getEntry({
199+
contentType: 'api::restaurant.restaurant',
200+
id: 200,
201+
fields: ['title'],
202+
populate: {
203+
subClass: true,
204+
},
205+
})
206+
207+
expect(fakeStrapi.entityService.findOne).toHaveBeenCalledWith(
208+
'api::restaurant.restaurant',
209+
200,
210+
{
211+
fields: ['title'],
212+
populate: {
213+
subClass: true,
214+
},
215+
}
216+
)
217+
expect(fakeStrapi.entityService.findOne).toHaveBeenCalledTimes(1)
218+
expect(entry).toEqual([{ id: 1 }])
219+
})
220+
221+
test('Test fetching an entry on a non existing content type', async () => {
222+
const contentTypeServices = createContentTypeService({
223+
strapi: fakeStrapi,
224+
})
225+
226+
const count = await contentTypeServices.getEntry({
119227
contentType: 'api::test.test',
120228
})
121229

122-
expect(count).toEqual([])
230+
expect(fakeStrapi.entityService.findOne).toHaveBeenCalledTimes(0)
231+
expect(count).toEqual({})
123232
})
124233

125234
test('Test operation in batches on entries', async () => {
@@ -137,8 +246,6 @@ describe('Tests content types', () => {
137246
})),
138247
})
139248

140-
console.log(entries)
141-
142249
expect(entries[0].id).toEqual(2)
143250
expect(entries[0].contentType).toEqual(contentType)
144251
})

server/__tests__/utils/fakes.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@ function createFakeStrapi({
5555
return [{ id: 1 }]
5656
})
5757

58+
const fakeFindOne = jest.fn(() => {
59+
return [{ id: 1 }]
60+
})
61+
5862
const fakeEntityService = {
5963
findMany: fakeFindMany,
64+
findOne: fakeFindOne,
6065
}
6166

6267
const fakeStrapi = {

server/services/content-types/content-types.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,38 @@ module.exports = ({ strapi }) => ({
120120
return entriesSum
121121
},
122122

123+
/**
124+
* Find an entry of a given content type.
125+
* More information: https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/entity-service/crud.html#findone
126+
*
127+
* @param {object} options
128+
* @param {string | number} [options.id] - Id of the entry.
129+
* @param {string | string[]} [options.fields] - Fields present in the returned entry.
130+
* @param {object} [options.populate] - Relations, components and dynamic zones to populate.
131+
* @param {string} [options.contentType] - Content type.
132+
*
133+
* @returns {Promise<object>} - Entries.
134+
*/
135+
async getEntry({ contentType, id, fields = '*', populate = '*' }) {
136+
const contentTypeUid = this.getContentTypeUid({ contentType })
137+
if (contentTypeUid === undefined) return {}
138+
139+
const entry = await strapi.entityService.findOne(contentTypeUid, id, {
140+
fields,
141+
populate,
142+
})
143+
144+
if (entry == null) {
145+
strapi.log.error(`Could not find entry with id ${id} in ${contentType}`)
146+
}
147+
148+
return entry || {}
149+
},
150+
123151
/**
124152
* Returns a batch of entries of a given content type.
125153
* More information: https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/entity-service/crud.html#findmany
154+
*
126155
* @param {object} options
127156
* @param {string | string[]} [options.fields] - Fields present in the returned entry.
128157
* @param {number} [options.start] - Pagination start.
@@ -135,15 +164,15 @@ module.exports = ({ strapi }) => ({
135164
*
136165
* @returns {Promise<object[]>} - Entries.
137166
*/
138-
async getContentTypeEntries({
167+
async getEntries({
139168
contentType,
140169
fields = '*',
141170
start = 0,
142171
limit = 500,
143172
filters = {},
144173
sort = {},
145174
populate = '*',
146-
publicationState,
175+
publicationState = 'live',
147176
}) {
148177
const contentTypeUid = this.getContentTypeUid({ contentType })
149178
if (contentTypeUid === undefined) return []
@@ -155,7 +184,7 @@ module.exports = ({ strapi }) => ({
155184
filters,
156185
sort,
157186
populate,
158-
publicationState: publicationState || 'live',
187+
publicationState: publicationState,
159188
})
160189
// Safe guard in case the content-type is a single type.
161190
// In which case it is wrapped in an array for consistency.
@@ -182,7 +211,7 @@ module.exports = ({ strapi }) => ({
182211
const cbResponse = []
183212
for (let index = 0; index <= entries_count; index += BATCH_SIZE) {
184213
const entries =
185-
(await this.getContentTypeEntries({
214+
(await this.getEntries({
186215
start: index,
187216
limit: BATCH_SIZE,
188217
contentType,

server/services/meilisearch/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module.exports = ({ strapi }) => {
4242
* @param {Array<Object>} options.entries - The data to convert. Conversion will use
4343
* the static method `toSearchIndex` defined in the model definition
4444
*
45-
* @return {Array<Object>} - Converted or mapped data
45+
* @return {Promise<Array<Object>>} - Converted or mapped data
4646
*/
4747
transformEntries: async function ({ contentType, entries = [] }) {
4848
const collection = contentTypeService.getCollectionName({ contentType })

0 commit comments

Comments
 (0)