Skip to content

Commit

Permalink
Fix Baboo7#91 Improve formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mitenka committed Mar 2, 2023
1 parent e1c7469 commit 3f37c4f
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions server/services/import/import-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ const importDataV2 = async (fileContent, { slug, user, idField }) => {
for (const slugFromFile of slugs) {
let slugFailures = [];
if (slugFromFile === CustomSlugToSlug[CustomSlugs.MEDIA]) {
slugFailures = await importMedia(Object.values(data[slugFromFile]), { user }).then((res) => res.slugFailures);
slugFailures = await importMedia(Object.values(data[slugFromFile]), {
user,
}).then((res) => res.slugFailures);
} else {
slugFailures = await importOtherSlug(Object.values(data[slugFromFile]), {
slug: slugFromFile,
Expand All @@ -54,7 +56,9 @@ const importDataV2 = async (fileContent, { slug, user, idField }) => {
for (const slugFromFile of slugs) {
let slugFailures = [];
if (slugFromFile === CustomSlugToSlug[CustomSlugs.MEDIA]) {
slugFailures = await importMedia(Object.values(data[slugFromFile]), { user }).then((res) => res.slugFailures);
slugFailures = await importMedia(Object.values(data[slugFromFile]), {
user,
}).then((res) => res.slugFailures);
} else {
slugFailures = await importOtherSlug(Object.values(data[slugFromFile]), {
slug: slugFromFile,
Expand All @@ -72,7 +76,9 @@ const importDataV2 = async (fileContent, { slug, user, idField }) => {
if (strapi.db.config.connection.client === 'postgres') {
for (const slugFromFile of slugs) {
const model = getModel(slugFromFile);
await strapi.db.connection.raw(`SELECT SETVAL((SELECT PG_GET_SERIAL_SEQUENCE('${model.collectionName}', 'id')), (SELECT MAX(id) FROM ${model.collectionName}) + 1, FALSE);`);
await strapi.db.connection.raw(
`SELECT SETVAL((SELECT PG_GET_SERIAL_SEQUENCE('${model.collectionName}', 'id')), (SELECT MAX(id) FROM ${model.collectionName}) + 1, FALSE);`,
);
}
}

Expand Down Expand Up @@ -157,12 +163,18 @@ const updateOrCreate = async (user, slug, datum, idField = 'id', { importStage }
datum = cloneDeep(datum);

if (importStage == 'simpleAttributes') {
const attributeNames = getModelAttributes(slug, { filterOutType: ['component', 'dynamiczone', 'media', 'relation'], addIdAttribute: true })
const attributeNames = getModelAttributes(slug, {
filterOutType: ['component', 'dynamiczone', 'media', 'relation'],
addIdAttribute: true,
})
.map(({ name }) => name)
.concat('localizations', 'locale');
datum = pick(datum, attributeNames);
} else if (importStage === 'relationAttributes') {
const attributeNames = getModelAttributes(slug, { filterType: ['component', 'dynamiczone', 'media', 'relation'], addIdAttribute: true })
const attributeNames = getModelAttributes(slug, {
filterType: ['component', 'dynamiczone', 'media', 'relation'],
addIdAttribute: true,
})
.map(({ name }) => name)
.concat('localizations', 'locale');
datum = pick(datum, attributeNames);
Expand All @@ -172,7 +184,10 @@ const updateOrCreate = async (user, slug, datum, idField = 'id', { importStage }
if (model.kind === 'singleType') {
await updateOrCreateSingleType(user, slug, datum, { importStage });
} else {
await updateOrCreateCollectionType(user, slug, datum, { idField, importStage });
await updateOrCreateCollectionType(user, slug, datum, {
idField,
importStage,
});
}
};

Expand Down Expand Up @@ -249,7 +264,9 @@ const updateOrCreateCollectionType = async (user, slug, datum, { idField, import
if (!entryDefaultLocale) {
await strapi.entityService.create(slug, { data: datum });
} else {
await strapi.entityService.update(slug, entryDefaultLocale.id, { data: omit({ ...datum }, ['id']) });
await strapi.entityService.update(slug, entryDefaultLocale.id, {
data: omit({ ...datum }, ['id']),
});
}
} else {
if (!entryDefaultLocale) {
Expand Down Expand Up @@ -291,14 +308,18 @@ const updateOrCreateSingleType = async (user, slug, datum, { importStage }) => {

let entryDefaultLocale = await strapi.db.query(slug).findOne({ where: { locale: defaultLocale } });
if (!entryDefaultLocale) {
entryDefaultLocale = await strapi.entityService.create(slug, { data: { ...datum, locale: defaultLocale } });
entryDefaultLocale = await strapi.entityService.create(slug, {
data: { ...datum, locale: defaultLocale },
});
}

if (isDatumInDefaultLocale) {
if (!entryDefaultLocale) {
await strapi.entityService.create(slug, { data: datum });
} else {
await strapi.entityService.update(slug, entryDefaultLocale.id, { data: datum });
await strapi.entityService.update(slug, entryDefaultLocale.id, {
data: datum,
});
}
} else {
const entryLocale = await strapi.db.query(slug).findOne({ where: { locale: datum.locale } });
Expand Down

0 comments on commit 3f37c4f

Please sign in to comment.