Skip to content

Commit

Permalink
fix: fix projects xls import
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusDec committed Mar 3, 2022
1 parent 387c612 commit c302526
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/utils/xls.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,14 @@ export const tableDataFromXlsx = (xlsx, model) => {

export const collapseTablesData = (tableData, model) => {
// Todo recursion
const removeModelKeyInModels = [
'projectLocations',
'coBenefits',
'relatedProjects',
'projectRatings',
'estimations',
];

const collapsed = { [model.name]: tableData[model.name] };

let associations = model.getAssociatedModels();
Expand All @@ -536,7 +544,7 @@ export const collapseTablesData = (tableData, model) => {
data[dataKey] = tableData[association.model.name]?.data?.find((row) => {
let found = false;

if (association.model.name === 'issuance') {
if (association.model.name === 'issuance' && !association.pluralize) {
if (row[model.name + 'Id'] === data[association.model.name + 'Id']) {
found = true;
delete row[model.name + 'Id'];
Expand Down Expand Up @@ -565,6 +573,23 @@ export const collapseTablesData = (tableData, model) => {
});
});

collapsed[model.name]?.data?.forEach((data) => {
associations.forEach((association) => {
if (!association.pluralize) return;

const key = formatModelAssociationName(association);
if (data[key] != null && !Array.isArray(data[key])) {
data[key] = [data[key]];
}

if (removeModelKeyInModels.includes(key)) {
data[key].forEach((data) => {
delete data[model.primaryKeyAttributes[0]];
});
}
});
});

collapsed[model.name]?.data?.forEach((data, index) => {
associations.forEach((association) => {
if (association.model.name !== 'label') {
Expand Down

0 comments on commit c302526

Please sign in to comment.