fix: handle getTemplate cloud function condition

This commit is contained in:
RaktimaNXG
2024-07-10 18:28:45 +05:30
11 changed files with 268 additions and 210 deletions
@@ -6,63 +6,60 @@ export default async function GetTemplate(request) {
const ispublic = request.params.ispublic;
try {
let userId, userRes;
if (!ispublic) {
userRes = await axios.get(serverUrl + '/users/me', {
const userRes = await axios.get(serverUrl + '/users/me', {
headers: {
'X-Parse-Application-Id': process.env.APP_ID,
'X-Parse-Session-Token': request.headers['sessiontoken'],
},
});
userId = userRes.data && userRes.data.objectId;
}
// console.log("templateId ", templateId)
// console.log("userId ",userId)
if (templateId && userId) {
try {
let template = new Parse.Query('contracts_Template');
template.equalTo('objectId', templateId);
template.include('ExtUserPtr');
template.include('Signers');
template.include('CreatedBy');
const userId = userRes.data && userRes.data.objectId;
if (templateId && userId) {
try {
let template = new Parse.Query('contracts_Template');
template.equalTo('objectId', templateId);
template.include('ExtUserPtr');
template.include('Signers');
template.include('CreatedBy');
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 teamsArr = [];
_extUser?.TeamIds?.forEach(x => (teamsArr = [...teamsArr, ...x.Ancestors]));
// Create the first query
const sharedWithQuery = new Parse.Query('contracts_Template');
sharedWithQuery.containedIn('SharedWith', teamsArr);
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 teamsArr = [];
_extUser?.TeamIds?.forEach(x => (teamsArr = [...teamsArr, ...x.Ancestors]));
// Create the first query
const sharedWithQuery = new Parse.Query('contracts_Template');
sharedWithQuery.containedIn('SharedWith', teamsArr);
// Create the second query
const createdByQuery = new Parse.Query('contracts_Template');
createdByQuery.equalTo('ExtUserPtr', {
__type: 'Pointer',
className: 'contracts_Users',
objectId: extUser.id,
});
template = Parse.Query.or(sharedWithQuery, createdByQuery);
template.equalTo('objectId', templateId);
template.include('ExtUserPtr');
template.include('Signers');
template.include('CreatedBy');
// Create the second query
const createdByQuery = new Parse.Query('contracts_Template');
createdByQuery.equalTo('ExtUserPtr', {
__type: 'Pointer',
className: 'contracts_Users',
objectId: extUser.id,
});
template = Parse.Query.or(sharedWithQuery, createdByQuery);
template.equalTo('objectId', templateId);
template.include('ExtUserPtr');
template.include('Signers');
template.include('CreatedBy');
}
}
const res = await template.first({ useMasterKey: true });
if (res) {
// console.log("res ",res)
return res;
} else {
return { error: "You don't have access of this document!" };
}
} catch (err) {
console.log('err', err);
return err;
}
const res = await template.first({ useMasterKey: true });
if (res) {
// console.log("res ",res)
return res;
} else {
return { error: "You don't have access of this document!" };
}
} catch (err) {
console.log('err', err);
return err;
}
} else if (templateId && ispublic) {
try {
@@ -92,16 +92,16 @@ const createDocumentFromTemplate = async (template, existContact, index) => {
// create new document from template and save contact pointer in placeholder, signers and ACL of Document
export default async function PublicUserLinkContactToDoc(req) {
const email = req.params.email;
const tempid = req.params.tempid;
const templateid = req.params.templateid;
const name = req.params.name;
const phone = req.params.phone;
const role = req.params.role;
try {
if (tempid) {
// Execute the query to get the template with the specified 'tempid'
if (templateid) {
// Execute the query to get the template with the specified 'templateid'
const docQuery = new Parse.Query('contracts_Template');
docQuery.include('ExtUserPtr');
const tempRes = await docQuery.get(tempid, { useMasterKey: true });
const tempRes = await docQuery.get(templateid, { useMasterKey: true });
// Check if the template was found; if not, throw an error indicating the template was not found
if (!tempRes) {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Template not found.');
@@ -65,7 +65,7 @@ export default async function SubscriptionAftersave(request) {
const extUserRes = await extUserQuery.first({ useMasterKey: true });
if (extUserRes) {
const extUser = JSON.parse(JSON.stringify(extUserRes));
if (extUser?.UserRole !== 'contracts_Admin') {
if (!extUser?.OrganizationId) {
await addTeamAndOrg(extUser);
}
}
@@ -86,7 +86,7 @@ export default async function SubscriptionAftersave(request) {
const extUserRes = await extUserQuery.first({ useMasterKey: true });
if (extUserRes) {
const extUser = JSON.parse(JSON.stringify(extUserRes));
if (extUser?.UserRole !== 'contracts_Admin') {
if (!extUser?.OrganizationId) {
await addTeamAndOrg(extUser);
}
}