Skip to content

Commit 6b45329

Browse files
authored
fix/290: remove all comments after remove the source entry (#292)
1 parent 0d6a6c5 commit 6b45329

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

server/src/bootstrap.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { StrapiContext } from './@types';
22
import { setupGQL } from './graphql';
33
import permissions from './permissions';
4+
import { CONFIG_PARAMS } from './utils/constants';
5+
import { getPluginService } from './utils/getPluginService';
46

57
export default async ({ strapi }: StrapiContext) => {
68
if (strapi.plugin('graphql')) {
@@ -53,4 +55,20 @@ export default async ({ strapi }: StrapiContext) => {
5355
];
5456

5557
await strapi.admin.services.permission.actionProvider.registerMany(actions);
58+
59+
const commonService = getPluginService(strapi, 'common');
60+
const enabledCollections = await commonService.getConfig(CONFIG_PARAMS.ENABLED_COLLECTIONS, []);
61+
62+
if (enabledCollections.length) {
63+
strapi.db.lifecycles.subscribe({
64+
models: enabledCollections,
65+
afterDelete: async (event) => {
66+
const uid = event.model.uid;
67+
const { documentId, locale } = event.result;
68+
const relation = [uid, documentId].join(':');
69+
await commonService.perRemove(relation, locale);
70+
},
71+
});
72+
}
73+
5674
};

server/src/repositories/comment.repository.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export const getCommentRepositorySource = (strapi: CoreStrapi) => {
3434
return strapi.query(modelUid).delete(params)
3535
.then(result => (result ? shouldValidateObject(isValidationEnabled, commentResultValidator.findOne)(result) : null));
3636
},
37+
async deleteMany(params: Params) {
38+
return strapi.query(modelUid).deleteMany(params)
39+
},
3740
updateMany(params: Params) {
3841
return strapi.query(modelUid).updateMany(params);
3942
},

server/src/services/common.service.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ const commonService = ({ strapi }: StrapiContext) => ({
161161
isAdmin = false,
162162
omit = [],
163163
locale,
164-
limit
164+
limit,
165165
}: clientValidator.FindAllInHierarchyValidatorSchema,
166166
relatedEntity?: any,
167167
) {
@@ -342,6 +342,17 @@ const commonService = ({ strapi }: StrapiContext) => ({
342342
return content;
343343
},
344344

345+
async perRemove(related: string, locale?: string) {
346+
const defaultLocale = await strapi.plugin('i18n')?.service('locales').getDefaultLocale() || null;
347+
return getCommentRepository(strapi)
348+
.deleteMany({
349+
where: {
350+
related,
351+
$or: [{ locale }, defaultLocale === locale ? { locale: { $eq: null } } : null].filter(Boolean)
352+
},
353+
});
354+
},
355+
345356

346357
registerLifecycleHook(/*{ callback, contentTypeName, hookName }*/) {
347358
},

0 commit comments

Comments
 (0)