mirror of
https://github.com/OpenSignLabs/OpenSign.git
synced 2026-08-18 05:35:50 +02:00
71 lines
2.3 KiB
JavaScript
71 lines
2.3 KiB
JavaScript
export default async function addOrganization(request) {
|
|
const name = request.params.name;
|
|
|
|
if (!request?.user) {
|
|
throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN, 'User is not authenticated.');
|
|
}
|
|
try {
|
|
const extUserQuery = new Parse.Query('contracts_Users');
|
|
extUserQuery.equalTo('UserId', {
|
|
__type: 'Pointer',
|
|
className: '_User',
|
|
objectId: request.user.id,
|
|
});
|
|
extUserQuery.notEqualTo('IsDisabled', true);
|
|
const resExt = await extUserQuery.first({ useMasterKey: true });
|
|
if (!resExt) {
|
|
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'User not found.');
|
|
}
|
|
const _resExt = JSON.parse(JSON.stringify(resExt));
|
|
|
|
const orgQuery = new Parse.Query('contracts_Organizations');
|
|
orgQuery.equalTo('Name', name);
|
|
orgQuery.equalTo('CreatedBy', {
|
|
__type: 'Pointer',
|
|
className: '_User',
|
|
objectId: request.user.id,
|
|
});
|
|
const resOrg = await orgQuery.first({ useMasterKey: true });
|
|
if (resOrg) {
|
|
throw new Parse.Error(Parse.Error.DUPLICATE_VALUE, 'Organization already exists.');
|
|
} else {
|
|
const newOrg = new Parse.Object('contracts_Organizations');
|
|
newOrg.set('Name', name);
|
|
newOrg.set('IsActive', true);
|
|
newOrg.set('CreatedBy', {
|
|
__type: 'Pointer',
|
|
className: '_User',
|
|
objectId: request.user.id,
|
|
});
|
|
newOrg.set('TenantId', {
|
|
__type: 'Pointer',
|
|
className: 'partners_Tenant',
|
|
objectId: _resExt.TenantId.objectId,
|
|
});
|
|
newOrg.set('ExtUserId', {
|
|
__type: 'Pointer',
|
|
className: 'contracts_Users',
|
|
objectId: resExt.id,
|
|
});
|
|
const newResOrg = await newOrg.save(null, { useMasterKey: true });
|
|
const teamCls = new Parse.Object('contracts_Teams');
|
|
teamCls.set('Name', 'All Users');
|
|
teamCls.set('OrganizationId', {
|
|
__type: 'Pointer',
|
|
className: 'contracts_Organizations',
|
|
objectId: newResOrg.id,
|
|
});
|
|
teamCls.set('IsActive', true);
|
|
await teamCls.save(null, { useMasterKey: true });
|
|
if (newResOrg) {
|
|
return newResOrg;
|
|
}
|
|
}
|
|
} catch (err) {
|
|
console.log('err in addorganization', err);
|
|
const code = err?.code || 400;
|
|
const msg = err?.message || 'Something went wrong.';
|
|
throw new Parse.Error(code, msg);
|
|
}
|
|
}
|