Skip to content

Commit

Permalink
fix(south): fix export/import of items with group forms and optional …
Browse files Browse the repository at this point in the history
…fields
  • Loading branch information
burgerni10 committed Apr 19, 2024
1 parent bad2d9a commit 0bd328b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 38 deletions.
31 changes: 17 additions & 14 deletions backend/src/web-server/controllers/history-query.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -934,20 +934,23 @@ describe('History query controller', () => {

expect(ctx.ok).toHaveBeenCalled();
expect(ctx.body).toEqual('csv content');
expect(csv.unparse).toHaveBeenCalledWith([
{
name: 'name',
enabled: true,
settings_field: 'value'
},
{
name: 'item2',
enabled: true,
settings_objectArray: '[]',
settings_objectSettings: '{}',
settings_objectValue: 1
}
]);
expect(csv.unparse).toHaveBeenCalledWith(
[
{
name: 'name',
enabled: true,
settings_field: 'value'
},
{
name: 'item2',
enabled: true,
settings_objectArray: '[]',
settings_objectSettings: '{}',
settings_objectValue: 1
}
],
{ columns: ['name', 'enabled', 'settings_field', 'settings_objectSettings', 'settings_objectArray', 'settings_objectValue'] }
);
});

it('checkImportSouthItems() should check import of items in a csv file with new history', async () => {
Expand Down
10 changes: 5 additions & 5 deletions backend/src/web-server/controllers/history-query.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,14 @@ export default class HistoryQueryController extends AbstractController {
}

async exportSouthItems(ctx: KoaContext<void, any>): Promise<void> {
const columns: Set<string> = new Set<string>(['name', 'enabled']);

const southItems = ctx.app.repositoryService.historyQueryItemRepository.getHistoryItems(ctx.params.historyQueryId).map(item => {
const flattenedItem: Record<string, any> = {
...item
};
for (const [itemSettingsKey, itemSettingsValue] of Object.entries(item.settings)) {
columns.add(`settings_${itemSettingsKey}`);
if (typeof itemSettingsValue === 'object') {
flattenedItem[`settings_${itemSettingsKey}`] = JSON.stringify(itemSettingsValue);
} else {
Expand All @@ -343,7 +346,7 @@ export default class HistoryQueryController extends AbstractController {
delete flattenedItem.connectorId;
return flattenedItem;
});
ctx.body = csv.unparse(southItems);
ctx.body = csv.unparse(southItems, { columns: Array.from(columns) });
ctx.set('Content-disposition', 'attachment; filename=items.csv');
ctx.set('Content-Type', 'application/force-download');
ctx.ok();
Expand All @@ -356,9 +359,6 @@ export default class HistoryQueryController extends AbstractController {
}

const file = ctx.request.file;
// if (file.mimetype !== 'text/csv') {
// return ctx.badRequest();
// }

const existingItems: Array<SouthConnectorItemDTO> =
ctx.params.historyQueryId === 'create'
Expand Down Expand Up @@ -388,7 +388,7 @@ export default class HistoryQueryController extends AbstractController {
if (!manifestSettings) {
throw new Error(`Settings "${settingsKey}" not accepted in manifest`);
}
if (manifestSettings.type === 'OibArray' || manifestSettings.type === 'OibFormGroup') {
if ((manifestSettings.type === 'OibArray' || manifestSettings.type === 'OibFormGroup') && value) {
item.settings[settingsKey] = JSON.parse(value as string);
} else {
item.settings[settingsKey] = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,22 +810,35 @@ describe('South connector controller', () => {

expect(ctx.ok).toHaveBeenCalled();
expect(ctx.body).toEqual('csv content');
expect(csv.unparse).toHaveBeenCalledWith([
{
name: 'name',
enabled: true,
scanMode: 'scanMode',
settings_regex: '.*'
},
expect(csv.unparse).toHaveBeenCalledWith(
[
{
name: 'name',
enabled: true,
scanMode: 'scanMode',
settings_regex: '.*'
},
{
name: 'item2',
enabled: true,
scanMode: '',
settings_objectArray: '[]',
settings_objectSettings: '{}',
settings_objectValue: 1
}
],
{
name: 'item2',
enabled: true,
scanMode: '',
settings_objectArray: '[]',
settings_objectSettings: '{}',
settings_objectValue: 1
columns: [
'name',
'enabled',
'scanMode',
'settings_regex',
'settings_objectSettings',
'settings_objectArray',
'settings_objectValue'
]
}
]);
);
});

it('checkImportSouthItems() should check import of items in a csv file with new south', async () => {
Expand Down
10 changes: 5 additions & 5 deletions backend/src/web-server/controllers/south-connector.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,14 @@ export default class SouthConnectorController {

async exportSouthItems(ctx: KoaContext<void, any>): Promise<void> {
const scanModes = ctx.app.repositoryService.scanModeRepository.getScanModes();
const columns: Set<string> = new Set<string>(['name', 'enabled', 'scanMode']);
const southItems = ctx.app.repositoryService.southItemRepository.getSouthItems(ctx.params.southId).map(item => {
const flattenedItem: Record<string, any> = {
...item
};
flattenedItem.scanMode = scanModes.find(scanMode => scanMode.id === flattenedItem.scanModeId)?.name ?? '';
for (const [itemSettingsKey, itemSettingsValue] of Object.entries(item.settings)) {
columns.add(`settings_${itemSettingsKey}`);
if (typeof itemSettingsValue === 'object') {
flattenedItem[`settings_${itemSettingsKey}`] = JSON.stringify(itemSettingsValue);
} else {
Expand All @@ -285,7 +287,7 @@ export default class SouthConnectorController {
delete flattenedItem.connectorId;
return flattenedItem;
});
ctx.body = csv.unparse(southItems);
ctx.body = csv.unparse(southItems, { columns: Array.from(columns) });
ctx.set('Content-disposition', 'attachment; filename=items.csv');
ctx.set('Content-Type', 'application/force-download');
ctx.ok();
Expand All @@ -305,9 +307,6 @@ export default class SouthConnectorController {
return ctx.throw(400, 'Could not parse item ids to delete array');
}

// if (file.mimetype !== 'text/csv') {
// return ctx.badRequest('Bad type of file');
// }
const scanModes = ctx.app.repositoryService.scanModeRepository.getScanModes();

const existingItems: Array<SouthConnectorItemDTO> =
Expand Down Expand Up @@ -340,7 +339,8 @@ export default class SouthConnectorController {
if (!manifestSettings) {
throw new Error(`Settings "${settingsKey}" not accepted in manifest`);
}
if (manifestSettings.type === 'OibArray' || manifestSettings.type === 'OibFormGroup') {

if ((manifestSettings.type === 'OibArray' || manifestSettings.type === 'OibFormGroup') && value) {
item.settings[settingsKey] = JSON.parse(value as string);
} else {
item.settings[settingsKey] = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export default class JoiValidator {
break;
}
});
schema = this.handleConditionalDisplay(formControl, schema) as Joi.ObjectSchema;
return {
[formControl.key]: schema
};
Expand Down

0 comments on commit 0bd328b

Please sign in to comment.