This commit is contained in:
prafull-opensignlabs
2026-01-07 09:40:36 +00:00
parent bce0e4971e
commit eb8e7b089b
73 changed files with 20018 additions and 2260 deletions
@@ -1,12 +1,16 @@
import { cloudServerUrl, serverAppId } from '../../Utils.js';
import reportJson from './reportsJson.js';
import reportJson, { applySearch } from './reportsJson.js';
import axios from 'axios';
// Escape regex special characters. Copied from filterDocs.js
function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
function buildClassesUrl({ serverUrl, clsName, paramsObj, keys, orderBy, skip, limit, include }) {
const url = new URL(`${serverUrl}/classes/${clsName}`);
url.searchParams.set('where', JSON.stringify(paramsObj));
url.searchParams.set('keys', keys.join(','));
url.searchParams.set('order', orderBy);
url.searchParams.set('skip', String(skip));
url.searchParams.set('limit', String(limit));
if (include) url.searchParams.set('include', include);
return url.toString();
}
export default async function getReport(request) {
const reportId = request.params.reportId;
const limit = request.params.limit;
@@ -25,78 +29,76 @@ export default async function getReport(request) {
},
});
const userId = userRes.data && userRes.data.objectId;
if (userId) {
const json = reportId && reportJson(reportId, userId);
const clsName = json?.reportClass ? json.reportClass : 'contracts_Document';
if (json) {
const { params, keys } = json;
const orderBy = '-updatedAt';
const strKeys = keys.join();
let paramsObj = { ...params };
if (reportId == '6TeaPr321t') {
const extUserQuery = new Parse.Query('contracts_Users');
extUserQuery.equalTo('Email', userRes.data.email);
extUserQuery.include('TeamIds');
const extUser = await extUserQuery.first({ useMasterKey: true });
if (extUser) {
const _extUser = JSON.parse(JSON.stringify(extUser));
if (_extUser?.TeamIds && _extUser.TeamIds?.length > 0) {
let teamArr = [];
_extUser?.TeamIds?.forEach(x => (teamArr = [...teamArr, ...x.Ancestors]));
paramsObj = {
...paramsObj,
$or: [
{ SharedWith: { $in: teamArr } },
{
ExtUserPtr: {
__type: 'Pointer',
className: 'contracts_Users',
objectId: extUser.id,
},
},
{
SharedWithUsers: {
__type: 'Pointer',
className: 'contracts_Users',
objectId: extUser.id,
},
},
],
};
} else {
paramsObj = {
...paramsObj,
CreatedBy: { __type: 'Pointer', className: '_User', objectId: userId },
};
}
}
if (!userId) {
return { error: 'Invalid session token' };
}
const json = reportId && reportJson(reportId, userId);
if (json) {
let paramsObj = { ...(json.params || {}) };
if (reportId == '6TeaPr321t') {
const extUserQuery = new Parse.Query('contracts_Users');
extUserQuery.equalTo('Email', userRes.data.email);
extUserQuery.include('TeamIds');
const extUser = await extUserQuery.first({ useMasterKey: true });
const userPtr = { __type: 'Pointer', className: '_User', objectId: userId };
if (!extUser) {
paramsObj = { ...paramsObj, CreatedBy: userPtr };
}
if (searchTerm) {
const escaped = escapeRegExp(searchTerm);
const _extUser = JSON.parse(JSON.stringify(extUser));
const extPtr = { __type: 'Pointer', className: 'contracts_Users', objectId: extUser.id };
if (_extUser?.TeamIds && _extUser.TeamIds?.length > 0) {
// Collect ancestors efficiently + de-dupe
const teamSet = new Set();
for (const team of _extUser?.TeamIds) {
const ancestors = team.Ancestors || [];
for (const a of ancestors) teamSet.add(a);
}
const teamArr = [...teamSet];
paramsObj = {
...paramsObj,
Name: { $regex: `.*${escaped}.*`, $options: 'i' },
$or: [
{ SharedWith: { $in: teamArr } },
{ ExtUserPtr: extPtr },
{ SharedWithUsers: extPtr },
],
};
}
const strParams = JSON.stringify(paramsObj);
const headers = {
'Content-Type': 'application/json',
'X-Parse-Application-Id': appId,
'X-Parse-Master-Key': masterKey,
};
const url = `${serverUrl}/classes/${clsName}?where=${strParams}&keys=${strKeys}&order=${orderBy}&skip=${skip}&limit=${limit}&include=AuditTrail.UserPtr,Placeholders.signerPtr,ExtUserPtr.TenantId`;
const res = await axios.get(url, { headers: headers });
if (res.data && res.data.results) {
return res.data.results;
} else {
return [];
paramsObj = { ...paramsObj, CreatedBy: userPtr };
}
} else {
return { error: 'Report is not available!' };
}
paramsObj = applySearch({ reportId, baseWhere: paramsObj, searchTerm });
const headers = {
'Content-Type': 'application/json',
'X-Parse-Application-Id': appId,
'X-Parse-Master-Key': masterKey,
};
const clsName = json?.reportClass ? json.reportClass : 'contracts_Document';
const orderBy = '-updatedAt';
const include = 'AuditTrail.UserPtr,Placeholders.signerPtr,ExtUserPtr.TenantId';
const url = buildClassesUrl({
serverUrl,
clsName,
paramsObj,
keys: json.keys || [],
orderBy,
skip,
limit,
include,
});
const res = await axios.get(url, { headers: headers });
if (res.data && res.data.results) {
return res.data.results;
} else {
return [];
}
} else {
return { error: 'Report is not available!' };
}
} catch (err) {
console.log('err', err.message);
const message = err?.response?.data?.error || err?.message || 'Something went wrong';
console.log('getreport error:', message);
if (err.code == 209) {
return { error: 'Invalid session token' };
} else {