feat:add declined, created event which is send document on webhook

This commit is contained in:
prafull-opensignlabs
2024-02-07 22:01:32 +05:30
parent 1a774bfcfa
commit eb01a0b17d
7 changed files with 211 additions and 48 deletions
@@ -0,0 +1,66 @@
import axios from 'axios';
export default async function callWebhook(request) {
const event = request.params.event;
const body = request.params.body;
const serverUrl = process.env.SERVER_URL;
const appId = process.env.APP_ID;
const userRes = await axios.get(serverUrl + '/users/me', {
headers: {
'X-Parse-Application-Id': appId,
'X-Parse-Session-Token': request.headers['sessiontoken'],
},
});
const userId = userRes.data && userRes.data.objectId;
if (userId) {
const extendcls = new Parse.Query('contracts_Users');
extendcls.equalTo('UserId', { __type: 'Pointer', className: '_User', objectId: userId });
const res = await extendcls.first({ useMasterKey: true });
if (res) {
const extUser = JSON.parse(JSON.stringify(res));
if (extUser?.Webhook) {
const params = {
event: event,
...body,
};
await axios
.post(extUser?.Webhook, params, {
headers: { 'Content-Type': 'application/json' },
})
.then(res => {
try {
// console.log('res ', res);
const webhook = new Parse.Object('contracts_Webhook');
webhook.set('Log', res?.status);
webhook.set('UserId', {
__type: 'Pointer',
className: '_User',
objectId: userId,
});
webhook.save(null, { useMasterKey: true });
} catch (err) {
console.log('err save in contracts_Webhook', err);
}
})
.catch(err => {
console.log('Err send data to webhook', err);
try {
const webhook = new Parse.Object('contracts_Webhook');
webhook.set('Log', err?.status);
webhook.set('UserId', {
__type: 'Pointer',
className: '_User',
objectId: userId,
});
webhook.save(null, { useMasterKey: true });
} catch (err) {
console.log('err save in contracts_Webhook', err);
}
});
}
return { message: 'webhook called!' };
}
} else {
return { message: 'User not found!' };
}
}