refactor: Use generateAuthUrl method instead of createAuthData
This commit is contained in:
44
packages/backend/src/graphql/mutations/generate-auth-url.ts
Normal file
44
packages/backend/src/graphql/mutations/generate-auth-url.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import Context from '../../types/express/context';
|
||||
import axios from 'axios';
|
||||
import globalVariable from '../../helpers/global-variable';
|
||||
import App from '../../models/app';
|
||||
import GenerateAuthUrlError from '../../errors/generate-auth-url';
|
||||
|
||||
type Params = {
|
||||
input: {
|
||||
id: string;
|
||||
};
|
||||
};
|
||||
|
||||
const generateAuthUrl = async (
|
||||
_parent: unknown,
|
||||
params: Params,
|
||||
context: Context
|
||||
) => {
|
||||
const connection = await context.currentUser
|
||||
.$relatedQuery('connections')
|
||||
.findOne({
|
||||
id: params.input.id,
|
||||
})
|
||||
.throwIfNotFound();
|
||||
|
||||
if (!connection.formattedData) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const authInstance = (await import(`../../apps/${connection.key}/auth`))
|
||||
.default;
|
||||
const app = await App.findOneByKey(connection.key);
|
||||
|
||||
const $ = await globalVariable({ connection, app });
|
||||
try {
|
||||
await authInstance.generateAuthUrl($);
|
||||
await axios.get(connection.formattedData.url as string);
|
||||
} catch (error) {
|
||||
throw new GenerateAuthUrlError(error);
|
||||
}
|
||||
|
||||
return connection.formattedData;
|
||||
};
|
||||
|
||||
export default generateAuthUrl;
|
Reference in New Issue
Block a user