Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: strapi 5 compatibility #16

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions server/src/controllers/index-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,35 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
const localeFilter = allLocales?.map(
(locale: any) => locale.code
);
const findManyBaseOptions = { populate };
const findManyBaseOptions = {
populate,
};
const findManyOptions = localeFilter
? {
...findManyBaseOptions,
locale: localeFilter,
}
: { ...findManyBaseOptions };

const articlesStrapi = await strapi
.documents(name as UID.ContentType)
.findMany(findManyOptions);
// Can't fetch draft & published articles in the same query (no status filter = draft only)
const publishedArticlesStrapi =
(await strapi
.documents(name as UID.ContentType)
.findMany({ ...findManyOptions, status: 'published' })) ?? [];
const draftArticlesStrapi =
(await strapi
.documents(name as UID.ContentType)
.findMany({ ...findManyOptions, status: 'draft' })) ?? [];
// Concatenate all published articles + any draft versions which aren't published
// Filtering out any draft articles which have a published version
const articlesStrapi = publishedArticlesStrapi.concat(
draftArticlesStrapi.filter(
(draft: any) =>
!publishedArticlesStrapi.some(
(published: any) => published.id === draft.id
)
)
);
const articles = (articlesStrapi ?? []).map((article: any) =>
utilsService.filterProperties(article, hideFields)
);
Expand Down
10 changes: 7 additions & 3 deletions server/src/services/strapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
}

const strapiObject = await strapi.documents(modelUid).findOne({
documentId: entryId,
documentId: event.result.documentId,
// the documentId can have a published & unpublished version associated
// without a status filter, the unpublished version could be returned even if a published on exists,
// which would incorrectly de-index.
status: 'published',
populate,
});

Expand Down Expand Up @@ -64,7 +68,7 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({

if (strapiObject.publishedAt === null) {
objectsIdsToDelete.push(entryId);
} else if (strapiObject.publishedAt !== null) {
} else {
objectsToSave.push({
objectID: entryId,
...strapiObject,
Expand Down Expand Up @@ -107,7 +111,7 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({

if (article.publishedAt === null) {
objectsIdsToDelete.push(entryIdWithPrefix);
} else if (article.publishedAt !== null) {
} else {
objectsToSave.push({
objectID: entryIdWithPrefix,
...article,
Expand Down
33 changes: 24 additions & 9 deletions tests/strapi-algolia.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ describe('strapi-algolia plugin', () => {
let strapi: any;
const fakeArticle = {
article: {
id: 'id',
id: 123,
documentId: 'id',
title: 'title',
content: 'content',
publishedAt: null,
Expand All @@ -238,7 +239,8 @@ describe('strapi-algolia plugin', () => {
} as any;
const fakeArticleWithoutHide = {
article: {
id: 'id',
id: 123,
documentId: 'id',
title: 'title',
content: 'content',
publishedAt: null,
Expand Down Expand Up @@ -290,7 +292,8 @@ describe('strapi-algolia plugin', () => {
strapiService({ strapi }).getStrapiObject(
{
result: {
id: 'id',
id: 123,
documentId: 'id',
},
model: {
uid: 'api::contentType.contentType',
Expand All @@ -300,7 +303,7 @@ describe('strapi-algolia plugin', () => {
[]
)
).rejects.toThrow(
'No entry found for api::contentType.contentType with ID id'
'No entry found for api::contentType.contentType with ID 123'
);
});

Expand All @@ -310,7 +313,8 @@ describe('strapi-algolia plugin', () => {
}).getStrapiObject(
{
result: {
id: 'id',
id: 123,
documentId: 'id',
},
model: {
uid: 'api::contentType.contentType',
Expand All @@ -327,6 +331,7 @@ describe('strapi-algolia plugin', () => {
expect(strapi.documents().findOne).toHaveBeenCalledWith({
documentId: 'id',
populate: '*',
status: 'published',
});
});

Expand All @@ -337,12 +342,15 @@ describe('strapi-algolia plugin', () => {
{
params: {
where: {
id: 'id',
id: 123,
},
},
model: {
uid: 'api::contentType.contentType',
},
result: {
documentId: 'id',
},
} as any,
'*',
[]
Expand All @@ -355,6 +363,7 @@ describe('strapi-algolia plugin', () => {
expect(strapi.documents().findOne).toHaveBeenCalledWith({
documentId: 'id',
populate: '*',
status: 'published',
});
});

Expand All @@ -365,12 +374,16 @@ describe('strapi-algolia plugin', () => {
{
params: {
where: {
id: 'id',
id: 123,
status: 'published',
},
},
model: {
uid: 'api::contentType.contentType',
},
result: {
documentId: 'id',
},
} as any,
'*',
['hide']
Expand All @@ -383,6 +396,7 @@ describe('strapi-algolia plugin', () => {
expect(strapi.documents().findOne).toHaveBeenCalledWith({
documentId: 'id',
populate: '*',
status: 'published',
});
});
});
Expand All @@ -396,10 +410,11 @@ describe('strapi-algolia plugin', () => {
expect(
utilsService({ strapi: {} as any }).getEntryId({
result: {
id: 'idresult',
documentId: 'idresult',
id: 123,
},
} as any)
).toEqual('idresult');
).toEqual(123);
expect(
utilsService({ strapi: {} as any }).getEntryId({
params: {
Expand Down