feat : add cloud function for public sign

This commit is contained in:
RaktimaNXG
2024-07-10 12:36:22 +05:30
parent 27a069e1a2
commit 1339259c3b
7 changed files with 321 additions and 18 deletions
@@ -21,7 +21,23 @@ async function AuthLoginAsMail(request) {
if (resOtp === otp) {
var result = await getToken(request);
return result;
if (result && !result?.emailVerified) {
const userQuery = new Parse.Query(Parse.User);
const user = await userQuery.get(result?.objectId, {
sessionToken: result.sessionToken,
});
// Update the emailVerified field to true
user.set('emailVerified', true);
// Save the user object
const res = await user.save(null, { useMasterKey: true });
if (res) {
return result;
} else {
reject('user not found!');
}
} else {
return result;
}
async function getToken(request) {
return new Promise(function (resolve, reject) {
@@ -56,6 +72,7 @@ async function AuthLoginAsMail(request) {
.catch(err => {
reject('user not found!');
});
// user couldn't find lets sign up!
})
.catch(() => {
@@ -4,8 +4,12 @@ async function GetPublicTemplate(request) {
if (username) {
const extUserQuery = new Parse.Query('contracts_Users');
extUserQuery.equalTo('UserName', username);
const res = await extUserQuery.first({ useMasterKey: true });
const userId = res.get('UserId').id;
const userRes = await extUserQuery.first({ useMasterKey: true });
const userId = userRes.get('UserId').id;
//get _user details
const userQuery = new Parse.Query(Parse.User);
const user = await userQuery.get(userId, { useMasterKey: true });
if (userId) {
const templatQuery = new Parse.Query('contracts_Template');
templatQuery.equalTo('CreatedBy', {
@@ -13,11 +17,20 @@ async function GetPublicTemplate(request) {
className: '_User',
objectId: userId,
});
templatQuery.descending('updatedAt');
templatQuery.equalTo('IsPublic', true);
const getTemplate = await templatQuery.find({ useMasterKey: true });
return getTemplate;
const extend_Res = await Parse.Cloud.run('getUserDetails', {
email: user.get('email'),
});
if (extend_Res) {
return { template: getTemplate, user: user, extend_User: extend_Res };
} else {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Template not found');
}
} else {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Template not found');
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'User does not exist');
}
} else {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Please provide required parameters!');
@@ -3,15 +3,19 @@ import axios from 'axios';
export default async function GetTemplate(request) {
const serverUrl = process.env.SERVER_URL;
const templateId = request.params.templateId;
const ispublic = request.params.ispublic;
try {
const userRes = await axios.get(serverUrl + '/users/me', {
headers: {
'X-Parse-Application-Id': process.env.APP_ID,
'X-Parse-Session-Token': request.headers['sessiontoken'],
},
});
const userId = userRes.data && userRes.data.objectId;
let userId, userRes;
if (!ispublic) {
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) {
@@ -60,6 +64,24 @@ export default async function GetTemplate(request) {
console.log('err', err);
return err;
}
} else if (templateId && ispublic) {
try {
const template = new Parse.Query('contracts_Template');
template.equalTo('objectId', templateId);
template.include('ExtUserPtr');
template.include('Signers');
template.include('CreatedBy');
const res = await template.first({ useMasterKey: true });
// console.log("res ", res)
if (res) {
return res;
} else {
return { error: "You don't have access of this document!" };
}
} catch (err) {
console.log('err', err);
return err;
}
} else {
return { error: 'Please pass required parameters!' };
}
@@ -0,0 +1,218 @@
// `saveRoleContact` is used to save user in contracts_Guest role and create contact
const saveRoleContact = async contact => {
try {
const Role = new Parse.Query(Parse.Role);
const guestRole = await Role.equalTo('name', 'contracts_Guest').first();
if (guestRole) {
// Check if the user is already in the role
const relation = guestRole.relation('users');
const usersInRoleQuery = relation.query();
usersInRoleQuery.equalTo('objectId', contact.UserId.objectId);
const usersInRole = await usersInRoleQuery.find();
if (usersInRole.length > 0) {
console.log('User already added to Guest role.');
} else {
relation.add({ __type: 'Pointer', className: '_User', id: contact.UserId.objectId });
await guestRole.save(null, { useMasterKey: true });
// console.log('User added to Guest role successfully.');
}
}
} catch (err) {
console.log('err in role save', err);
}
const contactQuery = new Parse.Object('contracts_Contactbook');
contactQuery.set('Name', contact.Name);
contactQuery.set('Email', contact.Email);
if (contact?.Phone) {
contactQuery.set('Phone', contact.Phone);
}
contactQuery.set('CreatedBy', contact.CreatedBy);
contactQuery.set('UserId', contact.UserId);
contactQuery.set('UserRole', 'contracts_Guest');
contactQuery.set('TenantId', contact.TenantId);
contactQuery.set('IsDeleted', false);
const acl = new Parse.ACL();
acl.setReadAccess(contact.CreatedBy.objectId, true);
acl.setWriteAccess(contact.CreatedBy.objectId, true);
acl.setReadAccess(contact.UserId.objectId, true);
acl.setWriteAccess(contact.UserId.objectId, true);
contactQuery.setACL(acl);
const contactRes = await contactQuery.save();
if (contactRes) {
return contactRes;
}
};
// `createDocumentFromTemplate` is used to create document from template
const createDocumentFromTemplate = async (template, existContact, index) => {
try {
if (template) {
const Doc = JSON.parse(JSON.stringify(template));
//update contact in placeholder, signers and update ACl in provide document
const object = new Parse.Object('contracts_Document');
object.set('Name', Doc?.Name);
object.set('Description', Doc?.Description);
object.set('Note', Doc?.Note);
object.set('TimeToCompleteDays', Doc.TimeToCompleteDays || 15);
object.set('SendinOrder', Doc?.SendinOrder);
object.set('AutomaticReminders', Doc.AutomaticReminders);
object.set('RemindOnceInEvery', Doc?.RemindOnceInEvery);
object.set('URL', Doc?.URL);
object.set('CreatedBy', Doc?.CreatedBy);
object.set('ExtUserPtr', Doc?.ExtUserPtr);
let signers = Doc?.Signers || [];
const signerobj = {
__type: 'Pointer',
className: 'contracts_Contactbook',
objectId: existContact.id,
};
signers = [...signers.slice(0, index), signerobj, ...signers.slice(index)];
object.set('Signers', signers);
object.set('SignedUrl', Doc.URL || Doc.SignedUrl);
const Placeholders = Doc?.Placeholders || [];
Placeholders[index] = {
...Placeholders[index],
signerObjId: existContact.id,
signerPtr: {
__type: 'Pointer',
className: 'contracts_Contactbook',
objectId: existContact.id,
},
};
object.set('Placeholders', Placeholders);
const resDoc = await object.save(null, { useMasterKey: true });
return resDoc;
}
} catch (err) {
console.log('err in create document from template', err);
}
};
// `PublicUserLinkContactToDoc` cloud function is used to create contact, add this contact in contracts_Guest role and
// 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 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'
const docQuery = new Parse.Query('contracts_Template');
docQuery.include('ExtUserPtr');
const tempRes = await docQuery.get(tempid, { 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.');
}
const _tempRes = JSON.parse(JSON.stringify(tempRes));
const Placeholders = _tempRes?.Placeholders || [];
let index;
if (role) {
index = Placeholders?.findIndex(x => x.Role && x.Role === role);
} else {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, '');
}
if (index !== -1) {
// Execute the query to check if a contact already exists in the 'contracts_Contactbook' class
const contactCls = new Parse.Query('contracts_Contactbook');
contactCls.equalTo('Email', email);
contactCls.equalTo('CreatedBy', _tempRes.CreatedBy);
contactCls.notEqualTo('IsDeleted', true);
const existContact = await contactCls.first({ useMasterKey: true });
if (existContact) {
//update contact in placeholder, signers and update ACl in provide document
const docRes = await createDocumentFromTemplate(tempRes, existContact, index);
if (docRes) {
return { contactId: existContact.id, docId: docRes.id };
}
} else {
// Execute the query to check if a user already exists in the 'contracts_Users' class
const extUserQuery = new Parse.Query('contracts_Users');
extUserQuery.equalTo('Email', email);
const extUser = await extUserQuery.first({ useMasterKey: true });
if (extUser) {
const _extUser = JSON.parse(JSON.stringify(extUser));
const contact = {
UserId: _extUser.UserId,
Name: _extUser.Name,
Email: email,
Phone: _extUser?.Phone ? _extUser.Phone : '',
CreatedBy: _tempRes.CreatedBy,
TenantId: _tempRes.ExtUserPtr.TenantId,
};
// if user present on platform create contact on the basis of extended user details
const contactRes = await saveRoleContact(contact);
const docRes = await createDocumentFromTemplate(tempRes, contactRes, index);
if (docRes) {
return { contactId: contactRes.id, docId: docRes.id };
}
} else if (name) {
try {
// Execute the query to check if a user already exists in the '_User' class
const userQuery = new Parse.Query(Parse.User);
userQuery.equalTo('email', email);
const userRes = await userQuery.first({ useMasterKey: true });
if (userRes) {
const contact = {
UserId: { __type: 'Pointer', className: '_User', objectId: userRes.id },
Name: name,
Email: email,
Phone: phone,
CreatedBy: _tempRes.CreatedBy,
TenantId: _tempRes.ExtUserPtr.TenantId,
};
// Create new contract on the basis provided contact details by user and userId from _User class
const contactRes = await saveRoleContact(contact);
//update contact in placeholder, signers and update ACl in provide document
const docRes = await createDocumentFromTemplate(tempRes, contactRes, index);
if (docRes) {
return { contactId: contactRes.id, docId: docRes.id };
}
} else {
// create new user in _User class on the basis of details provide by user
const _users = Parse.Object.extend('User');
const _user = new _users();
_user.set('name', name);
_user.set('username', email);
_user.set('email', email);
_user.set('password', email);
if (phone) {
_user.set('phone', phone);
}
const newUserRes = await _user.save();
const contact = {
UserId: { __type: 'Pointer', className: '_User', objectId: newUserRes.id },
Name: name,
Email: email,
Phone: phone,
CreatedBy: _tempRes.CreatedBy,
TenantId: _tempRes.ExtUserPtr.TenantId,
};
// Create new contract on the basis provided contact details by user and userId from _User class
const contactRes = await saveRoleContact(contact);
//update contact in placeholder, signers and update ACl in provide document
const docRes = await createDocumentFromTemplate(tempRes, contactRes, index);
if (docRes) {
return { contactId: contactRes.id, docId: docRes.id };
}
}
} catch (err) {
console.log('Err', err);
}
} else {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'User not found.');
}
}
} else {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Please provide required parameters!');
}
} else {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Template not found.');
}
} catch (err) {
console.log('err in publicuserlinkcontacttodoc', err);
throw err;
}
}