feat: add shareConnection mutation

This commit is contained in:
Ali BARIN
2023-11-13 10:24:24 +00:00
parent b6eea0b5fc
commit 3801f9cfa0
4 changed files with 64 additions and 1 deletions

View File

@@ -0,0 +1,55 @@
import Context from '../../types/express/context';
import Connection from '../../models/connection';
import SharedConnection from '../../models/shared-connection';
type Params = {
input: {
id: string;
roleIds: string[];
};
};
const shareConnection = async (
_parent: unknown,
params: Params,
context: Context
) => {
const conditions = context.currentUser.can('update', 'Connection');
if (conditions.isCreator) return;
const {
id,
roleIds,
} = params.input;
const connection = await Connection
.query()
.findById(id)
.throwIfNotFound();
try {
const updatedConnection = await Connection.transaction(async (trx) => {
await connection.$relatedQuery('sharedConnections', trx).delete();
if (roleIds?.length) {
const sharedConnections = roleIds.map((roleId) => ({
roleId,
connectionId: connection.id,
}));
await SharedConnection.query().insert(sharedConnections);
}
return await Connection
.query(trx)
.findById(id);
});
return updatedConnection;
} catch (err) {
throw new Error('The connection sharing preferences could not be updated!');
}
};
export default shareConnection;