Skip to content

Commit

Permalink
Fix Baboo7#91 Save repeatable components in content type's dynamic zone
Browse files Browse the repository at this point in the history
  • Loading branch information
mitenka committed Mar 2, 2023
1 parent 5dfb2ee commit a10ffda
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions server/services/import/import-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,83 @@ const importOtherSlug = async (data, { slug, user, idField, importStage }) => {
};
};

const getComponentData = (model, datum) => {
let componentData = {};

for (const [key, value] of Object.entries(datum)) {
if (!value) {
continue;
}
if (model.attributes[key]?.type === 'component') {
if (!model.attributes[key].repeatable) {
if (importDataV2.components[model.attributes[key].component].length === 0) {
continue;
}
const { id, ...payload } = importDataV2.components[model.attributes[key].component].filter((item) => {
return item.id === value;
})[0];

componentData[key] = {
__component: model.attributes[key].component,
...payload,
};

for (const [k, v] of Object.entries(payload)) {
if (!v) {
continue;
}
if (getModel(model.attributes[key].component).attributes[k].type === 'component') {
componentData[key] = getComponentData(getModel(model.attributes[key].component), {
...payload,
[k]: v,
});
}
}
}

if (model.attributes[key].repeatable) {
if (importDataV2.components[model.attributes[key].component].length === 0) {
continue;
}

const payload = [];

for (const componentId of value) {
let { id, ...itemPayload } = importDataV2.components[model.attributes[key].component].filter((item) => {
return item.id === componentId;
})[0];

for (const [k, v] of Object.entries(itemPayload)) {
if (!v) {
continue;
}
if (getModel(model.attributes[key].component).attributes[k].type === 'component') {
itemPayload = getComponentData(getModel(model.attributes[key].component), {
...itemPayload,
[k]: v,
});
}
}

payload.push({
__component: model.attributes[key].component,
...itemPayload,
});
}

componentData[key] = payload;
}
}
}

if (Object.keys(componentData).length) {
const { id, ...result } = { ...datum, ...componentData };
return result;
}

return null;
};

/**
* Update or create entries for a given model.
* @param {Object} user - User importing the data.
Expand Down Expand Up @@ -191,6 +268,19 @@ const updateOrCreate = async (user, slug, datum, idField = 'id', { importStage }
}

const model = getModel(slug);

const componentData = getComponentData(model, datum);

if (componentData) {
const entry = await strapi.entityService.findOne(model.uid, datum.id);
if (entry) {
await strapi.entityService.update(model.uid, datum.id, {
data: componentData,
});
return;
}
}

if (model.kind === 'singleType') {
await updateOrCreateSingleType(user, slug, datum, { importStage });
} else {
Expand Down

0 comments on commit a10ffda

Please sign in to comment.