It seems to work with the following code, but it might not be clean enough.
afterUpdate: async (result, params, data) => {
if (data.productTypes && result.parentCategory) {
let productCategory = await strapi.services['product-category'].findOne({ id: params.id })
let result = []
var parent = productCategory.parentCategory
do {
if (typeof parent.parentCategory === 'number') {
result.push(parent.id)
parent = await strapi.services['product-category'].findOne({ id: parent.parentCategory })
} else {
result.push(parent.id)
parent = parent.parentCategory
}
} while (parent)
result.map(async id => {
let parent = await strapi.services['product-category'].findOne({ id: id })
let updatedTypes = []
data.productTypes.map(type => {
if (!JSON.stringify(parent.productTypes).includes(type.name)) {
updatedTypes.push(type)
}
})
await strapi.services['product-category'].update(
{ id: id },
{
productTypes: updatedTypes,
},
)
})
}
},