Skip to content

Commit 6678a58

Browse files
authored
Merge pull request #119 from Oak-Digital/fix/finds-duplicate-of-self
fix: duplicateCheck will use an ignore id
2 parents 9b0394d + 2eceacb commit 6678a58

File tree

8 files changed

+70
-22
lines changed

8 files changed

+70
-22
lines changed

.changeset/dry-jobs-joke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@pluginpal/webtools-core": patch
3+
---
4+
5+
Duplicate check will use an ignore id so that it does not update the url path because it is a duplicate of itself

packages/core/server/admin-api/__tests__/query-layer-decorator.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,26 @@ describe('Query layer decorator', () => {
155155
expect(updatedPage).toHaveProperty('url_alias.generated', false);
156156
});
157157

158+
it('Update - Should not duplicate check the same entry when updated', async () => {
159+
const page = await strapi.entityService.create("api::test.test", {
160+
data: {
161+
title: 'Unpublished page',
162+
},
163+
populate: ['url_alias']
164+
});
165+
const url = page.url_alias.url_path;
166+
167+
const updatedPage = await strapi.entityService.update("api::test.test", page.id, {
168+
data: {
169+
// @ts-ignore
170+
published_at: new Date(),
171+
},
172+
populate: ['url_alias']
173+
});
174+
175+
expect(updatedPage).toHaveProperty('url_alias.url_path', url);
176+
});
177+
158178
it('Delete - Should delete the corresponding URL alias as wel', async () => {
159179
const page = await strapi.entityService.create("api::test.test", {
160180
data: {

packages/core/server/admin-api/services/url-alias.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11

22

3-
import { EntityService } from '@strapi/types';
3+
import { EntityService, Entity } from '@strapi/types';
44
import { getPluginService } from '../../util/getPluginService';
55

66
/**
77
* Finds a path from the original path that is unique
88
*/
9-
const duplicateCheck = async (originalPath: string, ext = -1): Promise<string> => {
9+
const duplicateCheck = async (
10+
originalPath: string,
11+
ignoreId?: Entity.ID,
12+
ext: number = -1,
13+
): Promise<string> => {
1014
const extension = ext >= 0 ? `-${ext}` : '';
1115
const newPath = originalPath + extension;
12-
const pathAlreadyExists = await getPluginService('urlAliasService').findByPath(newPath);
16+
const pathAlreadyExists = await getPluginService('urlAliasService').findByPath(newPath, ignoreId);
1317

1418
if (pathAlreadyExists) {
15-
return duplicateCheck(originalPath, ext + 1);
19+
return duplicateCheck(originalPath, ignoreId, ext + 1);
1620
}
1721

1822
return newPath;
@@ -85,7 +89,7 @@ const findMany = async (showDrafts: boolean = false, query: EntityService.Params
8589
* @param {string} path the path.
8690
* @param {number} id the id to ignore.
8791
*/
88-
const findByPath = async (path: string, id: number | string = 0) => {
92+
const findByPath = async (path: string, id: Entity.ID = 0) => {
8993
const pathEntity = await strapi.entityService.findMany('plugin::webtools.url-alias', {
9094
filters: {
9195
url_path: path,
@@ -106,8 +110,8 @@ const findByPath = async (path: string, id: number | string = 0) => {
106110
* @param {object} data the data.
107111
* @returns {void}
108112
*/
109-
const update = async (id: number | string, data: EntityService.Params.Pick<'plugin::webtools.url-alias', 'data'>['data']) => {
110-
const urlPath = await duplicateCheck(data.url_path);
113+
const update = async (id: Entity.ID, data: EntityService.Params.Pick<'plugin::webtools.url-alias', 'data'>['data']) => {
114+
const urlPath = await duplicateCheck(data.url_path, id);
111115

112116
const pathEntity = await strapi.entityService.update('plugin::webtools.url-alias', id, {
113117
data: {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import path from 'path';
2+
3+
export default ({ env }) => ({
4+
connection: {
5+
client: "sqlite",
6+
connection: {
7+
filename: path.join(
8+
__dirname,
9+
'..',
10+
// We need to go back once more to get out of the dist folder
11+
'..',
12+
env("DATABASE_TEST_FILENAME", ".tmp/test.db")),
13+
},
14+
useNullAsDefault: true,
15+
debug: false,
16+
},
17+
});

playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"@strapi/plugin-i18n": "^4.19.0",
1414
"@strapi/plugin-users-permissions": "^4.19.0",
1515
"@strapi/strapi": "^4.19.0",
16-
"better-sqlite3": "8.6.0",
16+
"better-sqlite3": "^9.4.0",
1717
"pg": "^8.10.0",
1818
"react": "^18.0.0",
1919
"react-dom": "^18.0.0",

playground/src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
*
99
* This gives you an opportunity to extend code.
1010
*/
11-
register(/*{ strapi }*/) {},
11+
register(/*{ strapi }*/) { },
1212

1313
/**
1414
* An asynchronous bootstrap function that runs before
@@ -64,7 +64,7 @@ module.exports = {
6464
.updateRole(publicRole.id, publicRole);
6565
}
6666

67-
strapi.entityService.create('plugin::webtools.url-pattern', {
67+
await strapi.entityService.create('plugin::webtools.url-pattern', {
6868
data: {
6969
pattern: '/page/[title]',
7070
label: 'Test API pattern',
@@ -74,7 +74,7 @@ module.exports = {
7474
}
7575
});
7676

77-
strapi.entityService.create('plugin::webtools.url-pattern', {
77+
await strapi.entityService.create('plugin::webtools.url-pattern', {
7878
data: {
7979
pattern: '/category/[title]',
8080
label: 'Category API pattern',
@@ -84,7 +84,7 @@ module.exports = {
8484
}
8585
});
8686

87-
strapi.entityService.create('plugin::webtools.url-pattern', {
87+
await strapi.entityService.create('plugin::webtools.url-pattern', {
8888
data: {
8989
pattern: '/private-category/[title]',
9090
label: 'Private category API pattern',
@@ -114,7 +114,7 @@ module.exports = {
114114
}
115115
});
116116

117-
strapi.entityService.create('api::test.test', {
117+
await strapi.entityService.create('api::test.test', {
118118
data: {
119119
title: 'Published test page',
120120
publishedAt: new Date(),
@@ -123,7 +123,7 @@ module.exports = {
123123
}
124124
});
125125

126-
strapi.entityService.create('api::test.test', {
126+
await strapi.entityService.create('api::test.test', {
127127
data: {
128128
title: 'Unpublished test page',
129129
category: publishedCategory.id,

playground/tests/helpers.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ async function setupStrapi() {
4646
appDir: './playground',
4747
distDir: './playground/dist'
4848
}).load();
49-
await waitForServer();
5049

5150
instance = strapi; // strapi is global now
51+
52+
await instance.server.mount();
5253
}
5354
return instance;
5455
}
@@ -58,16 +59,17 @@ async function setupStrapi() {
5859
*/
5960
async function stopStrapi() {
6061
if (instance) {
61-
62+
await instance.server.httpServer.close();
63+
await instance.db.connection.destroy();
6264
instance.destroy();
63-
6465
const tmpDbFile = strapi.config.get(
6566
"database.connection.connection.filename"
6667
);
6768

6869
if (fs.existsSync(tmpDbFile)) {
6970
fs.unlinkSync(tmpDbFile);
7071
}
72+
7173
}
7274
return instance;
7375
}

playground/yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4079,14 +4079,14 @@ __metadata:
40794079
languageName: node
40804080
linkType: hard
40814081

4082-
"better-sqlite3@npm:8.6.0":
4083-
version: 8.6.0
4084-
resolution: "better-sqlite3@npm:8.6.0"
4082+
"better-sqlite3@npm:^9.4.0":
4083+
version: 9.4.0
4084+
resolution: "better-sqlite3@npm:9.4.0"
40854085
dependencies:
40864086
bindings: "npm:^1.5.0"
40874087
node-gyp: "npm:latest"
40884088
prebuild-install: "npm:^7.1.1"
4089-
checksum: 4d5481bd173331c163add61c2fd12577e235d62abb1f72500b3856fd2af70a448a5515a3f12dd48f34bb97cfdf13d200ca81cd2ae16fc55627b96c5b9bd47bb0
4089+
checksum: 42b2edfa46d62763514b87122245a3513a5ff20f05fef4fb49fec33f3de0a51a29025596178f57c634b8013f16bbdf8169a308fb3e3b8d126d715788d72d1e74
40904090
languageName: node
40914091
linkType: hard
40924092

@@ -10544,7 +10544,7 @@ __metadata:
1054410544
"@strapi/plugin-i18n": "npm:^4.19.0"
1054510545
"@strapi/plugin-users-permissions": "npm:^4.19.0"
1054610546
"@strapi/strapi": "npm:^4.19.0"
10547-
better-sqlite3: "npm:8.6.0"
10547+
better-sqlite3: "npm:^9.4.0"
1054810548
pg: "npm:^8.10.0"
1054910549
react: "npm:^18.0.0"
1055010550
react-dom: "npm:^18.0.0"

0 commit comments

Comments
 (0)