Browse Source

feat: optimize user post processing

pull/7202/head
mertmit 12 months ago
parent
commit
311526c84b
  1. 61
      packages/nocodb/src/db/BaseModelSqlv2.ts
  2. 3
      packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts
  3. 3
      packages/nocodb/src/db/sortV2.ts

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

@ -45,6 +45,7 @@ import { NcError } from '~/helpers/catchError';
import getAst from '~/helpers/getAst'; import getAst from '~/helpers/getAst';
import { import {
Audit, Audit,
BaseUser,
Column, Column,
Filter, Filter,
Model, Model,
@ -4554,70 +4555,50 @@ class BaseModelSqlv2 {
} }
if (userColumns.length) { if (userColumns.length) {
const baseUsers = await BaseUser.getUsersList({
base_id: childTable ? childTable.base_id : this.model.base_id,
});
if (Array.isArray(data)) { if (Array.isArray(data)) {
data = await Promise.all( data = await Promise.all(
data.map((d) => this._convertUserFormat(userColumns, d)), data.map((d) => this._convertUserFormat(userColumns, baseUsers, d)),
); );
} else { } else {
data = await this._convertUserFormat(userColumns, data); data = await this._convertUserFormat(userColumns, baseUsers, data);
} }
} }
} }
return data; return data;
} }
protected async _convertUserFormat( protected _convertUserFormat(
userColumns: Record<string, any>[], userColumns: Record<string, any>[],
baseUsers: Partial<User>[],
d: Record<string, any>, d: Record<string, any>,
) { ) {
try { try {
if (d) { if (d) {
const promises = [];
for (const col of userColumns) { for (const col of userColumns) {
// we expect array of string of comma separated user ids in case of lookup
if (Array.isArray(d[col.id])) {
} else {
if (d[col.id] && d[col.id].length) { if (d[col.id] && d[col.id].length) {
d[col.id] = d[col.id].split(','); d[col.id] = d[col.id].split(',');
} else { } else {
d[col.id] = []; d[col.id] = null;
} }
if (d[col.id]?.length) { if (d[col.id]?.length) {
promises.push( d[col.id] = d[col.id].map((fid) => {
new Promise((resolve) => { const { id, email, display_name } = baseUsers.find(
const users = []; (u) => u.id === fid,
for (const userId of d[col.id]) { );
users.push(
User.get(userId)
.then((user) => {
const { id, email, display_name } = user;
return { return {
id, id,
email, email,
display_name: display_name?.length display_name: display_name?.length ? display_name : null,
? display_name
: null,
}; };
})
.catch((e) => {
console.log(e);
return null;
}),
);
}
Promise.all(users).then((users) => {
d[col.id] = users;
resolve(true);
}); });
}),
);
} }
} }
} }
await Promise.all(promises);
}
} catch {} } catch {}
return d; return d;
} }
@ -5492,6 +5473,10 @@ class BaseModelSqlv2 {
} catch (e) {} } catch (e) {}
} }
const baseUsers = await BaseUser.getUsersList({
base_id: this.model.base_id,
});
if (typeof data[column.column_name] === 'string') { if (typeof data[column.column_name] === 'string') {
const users = data[column.column_name] const users = data[column.column_name]
.split(',') .split(',')
@ -5499,13 +5484,13 @@ class BaseModelSqlv2 {
for (const user of users) { for (const user of users) {
try { try {
if (user.includes('@')) { if (user.includes('@')) {
const u = await User.getByEmail(user); const u = baseUsers.find((u) => u.email === user);
if (!u) { if (!u) {
throw new Error(`User with email '${user}' not found`); throw new Error(`User with email '${user}' not found`);
} }
userIds.push(u.id); userIds.push(u.id);
} else { } else {
const u = await User.get(user); const u = baseUsers.find((u) => u.id === user);
if (!u) { if (!u) {
throw new Error(`User with id '${user}' not found`); throw new Error(`User with id '${user}' not found`);
} }
@ -5525,13 +5510,13 @@ class BaseModelSqlv2 {
const user = extractProps(userObj, ['id', 'email']); const user = extractProps(userObj, ['id', 'email']);
try { try {
if (user.id) { if (user.id) {
const u = await User.get(user.id); const u = baseUsers.find((u) => u.id === user.id);
if (!u) { if (!u) {
throw new Error(`User with id '${user.id}' not found`); throw new Error(`User with id '${user.id}' not found`);
} }
userIds.push(u.id); userIds.push(u.id);
} else if (user.email) { } else if (user.email) {
const u = await User.getByEmail(user.email); const u = baseUsers.find((u) => u.email === user.email);
if (!u) { if (!u) {
throw new Error( throw new Error(
`User with email '${user.email}' not found`, `User with email '${user.email}' not found`,

3
packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts

@ -643,9 +643,6 @@ async function _formulaQueryBuilder(
const base = await Base.get(model.base_id); const base = await Base.get(model.base_id);
const baseUsers = await BaseUser.getUsersList({ const baseUsers = await BaseUser.getUsersList({
base_id: base.id, base_id: base.id,
...((base as any)?.fk_workspace_id
? { workspace_id: (base as any).fk_workspace_id }
: {}),
}); });
// create nested replace statement for each user // create nested replace statement for each user

3
packages/nocodb/src/db/sortV2.ts

@ -142,9 +142,6 @@ export default async function sortV2(
const base = await Base.get(model.base_id); const base = await Base.get(model.base_id);
const baseUsers = await BaseUser.getUsersList({ const baseUsers = await BaseUser.getUsersList({
base_id: base.id, base_id: base.id,
...((base as any)?.fk_workspace_id
? { workspace_id: (base as any).fk_workspace_id }
: {}),
}); });
// create nested replace statement for each user // create nested replace statement for each user

Loading…
Cancel
Save