mirror of
https://github.com/OpenSignLabs/OpenSign.git
synced 2026-08-20 06:35:54 +02:00
Merge pull request #964 from OpenSignLabs/multiuser_issue
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
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 });
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
export default async function addTeam(request) {
|
||||
const Name = request.params.Name;
|
||||
const ParentId = request.params.ParentId;
|
||||
const Ancestors = request.params.Ancestors;
|
||||
const ParentPtr = { __type: 'Pointer', className: 'contracts_Teams', objectId: ParentId };
|
||||
if (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 });
|
||||
const extUser = JSON.parse(JSON.stringify(resExt));
|
||||
if (!extUser) {
|
||||
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'User not found.');
|
||||
}
|
||||
const teamCls = new Parse.Query('contracts_Teams');
|
||||
teamCls.equalTo('Name', Name);
|
||||
teamCls.equalTo('OrganizationId', {
|
||||
__type: 'Pointer',
|
||||
className: 'contracts_Organizations',
|
||||
objectId: extUser.OrganizationId.objectId,
|
||||
});
|
||||
if (ParentId) {
|
||||
teamCls.equalTo('ParentId', ParentPtr);
|
||||
}
|
||||
const teamRes = await teamCls.first({ useMasterKey: true });
|
||||
if (teamRes) {
|
||||
throw new Parse.Error(Parse.Error.DUPLICATE_VALUE, 'Team already exists.');
|
||||
} else {
|
||||
const newteam = new Parse.Object('contracts_Teams');
|
||||
newteam.set('Name', Name);
|
||||
newteam.set('OrganizationId', {
|
||||
__type: 'Pointer',
|
||||
className: 'contracts_Organizations',
|
||||
objectId: extUser.OrganizationId.objectId,
|
||||
});
|
||||
if (ParentId) {
|
||||
newteam.set('ParentId', ParentPtr);
|
||||
}
|
||||
if (Ancestors && Ancestors.length > 0) {
|
||||
newteam.set('Ancestors', Ancestors);
|
||||
}
|
||||
newteam.set('IsActive', true);
|
||||
const newTeamRes = await newteam.save(null, { useMasterKey: true });
|
||||
return newTeamRes;
|
||||
}
|
||||
} catch (err) {
|
||||
console.log('err in getOrganizations', err);
|
||||
const code = err?.code || 400;
|
||||
const msg = err?.message || 'Something went wrong.';
|
||||
throw new Parse.Error(code, msg);
|
||||
}
|
||||
} else {
|
||||
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Please provide parameters');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
export default async function getOrganizations(request) {
|
||||
const limit = request.params.limit || 200;
|
||||
const skip = request.params.skip || 0;
|
||||
const extUserId = request.params.extUserId;
|
||||
if (!request?.user) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN, 'User is not authenticated.');
|
||||
}
|
||||
try {
|
||||
const orgQuery = new Parse.Query('contracts_Organizations');
|
||||
orgQuery.equalTo('CreatedBy', {
|
||||
__type: 'Pointer',
|
||||
className: '_User',
|
||||
objectId: request.user.id,
|
||||
});
|
||||
orgQuery.equalTo('ExtUserId', {
|
||||
__type: 'Pointer',
|
||||
className: 'contracts_Users',
|
||||
objectId: extUserId,
|
||||
});
|
||||
orgQuery.equalTo('IsActive', true);
|
||||
orgQuery.exclude('ExtUserId');
|
||||
orgQuery.limit(limit);
|
||||
orgQuery.skip(skip);
|
||||
const resOrg = await orgQuery.find({ useMasterKey: true });
|
||||
if (resOrg && resOrg.length > 0) {
|
||||
return resOrg;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
} catch (err) {
|
||||
console.log('err in getOrganizations', err);
|
||||
const code = err?.code || 400;
|
||||
const msg = err?.message || 'Something went wrong.';
|
||||
throw new Parse.Error(code, msg);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
export default async function getTeams(request) {
|
||||
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 });
|
||||
const extUser = JSON.parse(JSON.stringify(resExt));
|
||||
const teamCls = new Parse.Query('contracts_Teams');
|
||||
teamCls.equalTo('OrganizationId', {
|
||||
__type: 'Pointer',
|
||||
className: 'contracts_Organizations',
|
||||
objectId: extUser.OrganizationId.objectId,
|
||||
});
|
||||
teamCls.equalTo('IsActive', true);
|
||||
teamCls.descending('createdAt');
|
||||
const teamRes = await teamCls.find({ useMasterKey: true });
|
||||
if (teamRes && teamRes.length > 0) {
|
||||
return teamRes;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
} catch (err) {
|
||||
console.log('err in getOrganizations', err);
|
||||
const code = err?.code || 400;
|
||||
const msg = err?.message || 'Something went wrong.';
|
||||
throw new Parse.Error(code, msg);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
export default async function updateOrganization(request) {
|
||||
const orgId = request.params.orgId;
|
||||
const isactive = request.params.isactive;
|
||||
if (!request?.user) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN, 'User is not authenticated.');
|
||||
}
|
||||
try {
|
||||
const orgQuery = new Parse.Query('contracts_Organizations');
|
||||
orgQuery.equalTo('objectId', orgId);
|
||||
orgQuery.equalTo('CreatedBy', {
|
||||
__type: 'Pointer',
|
||||
className: '_User',
|
||||
objectId: request.user.id,
|
||||
});
|
||||
const resOrg = await orgQuery.first({ useMasterKey: true });
|
||||
if (resOrg) {
|
||||
const newOrg = new Parse.Object('contracts_Organizations');
|
||||
newOrg.id = orgId;
|
||||
newOrg.set('IsActive', isactive);
|
||||
const newResOrg = await newOrg.save(null, { useMasterKey: true });
|
||||
if (newResOrg) {
|
||||
return newResOrg;
|
||||
}
|
||||
} else {
|
||||
throw new Parse.Error(Parse.Error.OPERATION_FORBIDDEN, 'Premission denied.');
|
||||
}
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
export default async function updateTeam(request) {
|
||||
const TeamId = request.params.TeamId;
|
||||
const IsActive = request.params.IsActive;
|
||||
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 });
|
||||
const extUser = JSON.parse(JSON.stringify(resExt));
|
||||
if (!extUser) {
|
||||
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'User not found.');
|
||||
}
|
||||
const teamCls = new Parse.Query('contracts_Teams');
|
||||
teamCls.equalTo('objectId', TeamId);
|
||||
teamCls.equalTo('OrganizationId', {
|
||||
__type: 'Pointer',
|
||||
className: 'contracts_Organizations',
|
||||
objectId: extUser.OrganizationId.objectId,
|
||||
});
|
||||
const teamRes = await teamCls.first({ useMasterKey: true });
|
||||
if (teamRes) {
|
||||
const updateteam = new Parse.Object('contracts_Teams');
|
||||
updateteam.id = TeamId;
|
||||
if (Name) {
|
||||
updateteam.set('Name', Name);
|
||||
}
|
||||
if (IsActive) {
|
||||
updateteam.set('IsActive', IsActive);
|
||||
}
|
||||
const updateTeamRes = await updateteam.save(null, { useMasterKey: true });
|
||||
return updateTeamRes;
|
||||
} else {
|
||||
throw new Parse.Error(Parse.Error.OPERATION_FORBIDDEN, 'Premission denied.');
|
||||
}
|
||||
} catch (err) {
|
||||
console.log('err in getOrganizations', err);
|
||||
const code = err?.code || 400;
|
||||
const msg = err?.message || 'Something went wrong.';
|
||||
throw new Parse.Error(code, msg);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user