Browse Source

fix: only allow order by grouped column

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/6489/head
Pranav C 1 year ago
parent
commit
6d99f968c3
  1. 27
      packages/nocodb/src/db/BaseModelSqlv2.ts
  2. 6
      packages/nocodb/tests/unit/rest/tests/tableRow.test.ts
  3. 19
      tests/playwright/tests/db/general/groupCRUD.spec.ts

27
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -630,31 +630,28 @@ ${qb.toQuery()}
qb,
);
// group by using the column aliases
qb.groupBy(...groupBySelectors);
if (!sorts)
sorts = args.sortArr?.length
? args.sortArr
: await Sort.list({ viewId: this.viewId });
// if sort is provided filter out the group by columns
// and apply it directly using group by column alias
// since using the query directly in group by query is not supported in all databases
sorts = sorts.filter((sort) => {
// if sort is provided filter out the group by columns sort and apply
// since we are grouping by the column and applying sort on any other column is not required
for (const sort of sorts) {
if (!groupByColumns[sort.fk_column_id]) {
return true;
continue;
}
qb.orderBy(
groupByColumns[sort.fk_column_id].column_name ||
groupByColumns[sort.fk_column_id].title,
groupByColumns[sort.fk_column_id].title,
sort.direction,
sort.direction === 'desc' ? 'LAST' : 'FIRST',
);
});
}
// group by using the column aliases
qb.groupBy(...groupBySelectors);
if (sorts) await sortV2(this, sorts, qb);
applyPaginate(qb, rest);
return await qb;
}
@ -4632,14 +4629,16 @@ export function extractSortsObject(
let sorts = _sorts;
if (!Array.isArray(sorts)) sorts = sorts.split(',');
if (!Array.isArray(sorts)) sorts = sorts.split(/\s*,\s*/);
return sorts.map((s) => {
const sort: SortType = { direction: 'asc' };
if (s.startsWith('-')) {
sort.direction = 'desc';
sort.fk_column_id = aliasColObjMap[s.slice(1)]?.id;
} else sort.fk_column_id = aliasColObjMap[s]?.id;
}
// replace + at the beginning if present
else sort.fk_column_id = aliasColObjMap[s.replace(/^\+/, '')]?.id;
return new Sort(sort);
});

6
packages/nocodb/tests/unit/rest/tests/tableRow.test.ts

@ -1489,7 +1489,7 @@ function tableTest() {
});
const visibleColumns = [firstNameColumn];
const sortInfo = `-FirstName, +${rollupColumn.title}`;
const sortInfo = `-FirstName`;
const response = await request(context.app)
.get(
@ -1613,7 +1613,7 @@ function tableTest() {
});
const visibleColumns = [firstNameColumn];
const sortInfo = `-FirstName, +${rollupColumn.title}`;
const sortInfo = `-FirstName`;
const response = await request(context.app)
.get(
@ -1649,7 +1649,7 @@ function tableTest() {
});
const visibleColumns = [firstNameColumn];
const sortInfo = `-FirstName, +${rollupColumn.title}`;
const sortInfo = `-FirstName`;
const response = await request(context.app)
.get(

19
tests/playwright/tests/db/general/groupCRUD.spec.ts

@ -248,4 +248,23 @@ test.describe('GroupBy CRUD Operations', () => {
value: 'Zzzzzzzzzzzzzzzzzzz',
});
});
test.only('Single GroupBy CRUD Operations - Links', async ({ page }) => {
await dashboard.treeView.openTable({ title: 'Film' });
await toolbar.sort.add({ title: 'Actors', ascending: true, locallySaved: false });
await toolbar.clickGroupBy();
await toolbar.groupBy.add({ title: 'Actors', ascending: false, locallySaved: false });
await dashboard.grid.groupPage.openGroup({ indexMap: [0] });
await dashboard.grid.groupPage.addNewRow({
indexMap: [0],
index: 10,
columnHeader: 'Item',
value: 'Aaaaaaaaaaaaaaaaaaaa',
});
});
});

Loading…
Cancel
Save