Skip to content

Commit 5997bcb

Browse files
authored
Merge pull request #113 from pluginpal/feature/fix-105
fix: bug in query-layer-decorator
2 parents f95cd1e + e105eff commit 5997bcb

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

.changeset/old-worms-hear.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@pluginpal/webtools-core": patch
3+
---
4+
5+
Fix bug in the query-layer-decorator to allow updates to entities without an URL alias (#105)

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

+26-13
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,28 @@ const decorator = (service: IDecoratedService) => ({
6767
return service.update.call(this, uid, entityId, opts);
6868
}
6969

70+
// Manually fetch the entity that's being updated.
71+
// We do this becuase not all it's data is present in opts.data.
72+
const entity = await service.findOne.call(this, uid, entityId, {
73+
populate: {
74+
url_alias: {
75+
fields: ['id', 'generated'],
76+
},
77+
},
78+
});
79+
7080
// If a URL alias is allready present, fetch it.
7181
if (opts.data.url_alias) {
7282
urlAliasEntity = await getPluginService('urlAliasService').findOne(opts.data.url_alias);
83+
} else if (entity.url_alias) {
84+
urlAliasEntity = entity.url_alias;
7385
}
7486

7587
// If a URL alias is present and 'generated' is set to false, do nothing.
7688
if (urlAliasEntity?.generated === false) {
7789
return service.update.call(this, uid, entityId, opts);
7890
}
7991

80-
// Manually fetch the entity that's being updated.
81-
// We do this becuase not all it's data is present in opts.data.
82-
const entity = await service.findOne.call(this, uid, entityId);
83-
8492
// Generate the path.
8593
const generatedPath = await getPluginService('urlPatternService').resolvePattern(uid, { ...entity, ...opts.data });
8694

@@ -95,7 +103,13 @@ const decorator = (service: IDecoratedService) => ({
95103
}
96104

97105
// Eventually update the entity.
98-
return service.update.call(this, uid, entityId, opts);
106+
return service.update.call(this, uid, entityId, {
107+
...opts,
108+
data: {
109+
...opts.data,
110+
url_alias: urlAliasEntity.id,
111+
},
112+
});
99113
},
100114
async delete(uid: Common.UID.ContentType, entityId: number) {
101115
const hasWT = isContentTypeEnabled(uid);
@@ -106,18 +120,17 @@ const decorator = (service: IDecoratedService) => ({
106120
}
107121

108122
// Fetch the entity because we need the url_alias id.
109-
const entity = await strapi.entityService.findOne(uid, entityId, {
110-
// @ts-ignore
111-
populate: 'url_alias',
123+
const entity = await service.findOne.call(this, uid, entityId, {
124+
populate: {
125+
url_alias: {
126+
fields: ['id'],
127+
},
128+
},
112129
});
113130

114131
// If a URL alias is present, delete it.
115-
// @ts-ignore
116-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
117132
if (entity.url_alias?.id) {
118-
// @ts-ignore
119-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
120-
await getPluginService('urlAliasService').delete(Number(entity.url_alias.id));
133+
await getPluginService('urlAliasService').delete(entity.url_alias.id);
121134
}
122135

123136
// Eventually delete the entity.

packages/core/server/types/strapi.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Strapi } from '@strapi/strapi';
1+
import { Attribute, Strapi } from '@strapi/strapi';
22

33
/**
44
* An extension of the Strapi type to include unsupported types.
@@ -29,7 +29,9 @@ export interface IDecoratedService {
2929
call: (context: any, uid: any, options: any) => Promise<{ id: number }>
3030
}
3131
findOne: {
32-
call: (context: any, uid: any, options: any) => Promise<{ id: number }>
32+
call: (context: any, uid: any, id: any, options: any) => Promise<{
33+
id: number, url_alias?: Attribute.GetValues<'plugin::webtools.url-alias', Attribute.GetNonPopulatableKeys<'plugin::webtools.url-alias'>>
34+
}>
3335
}
3436
}
3537

0 commit comments

Comments
 (0)