feat: Implement update credentials endpoint
This commit is contained in:
@@ -17,6 +17,16 @@ const createAuthLinkResolver = async (params: Params, req: RequestWithCurrentUse
|
||||
const appInstance = new appClass(credential.data)
|
||||
const authLink = await appInstance.createAuthLink();
|
||||
|
||||
await credential.$query().patch({
|
||||
data: {
|
||||
...credential.data,
|
||||
url: authLink.url,
|
||||
accessToken: authLink.oauth_token,
|
||||
accessSecret: authLink.oauth_token_secret,
|
||||
},
|
||||
verified: authLink.oauth_callback_confirmed === 'true' ? true : false
|
||||
})
|
||||
|
||||
return authLink;
|
||||
}
|
||||
|
||||
|
45
packages/backend/src/graphql/mutations/update-credential.ts
Normal file
45
packages/backend/src/graphql/mutations/update-credential.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { GraphQLString, GraphQLNonNull } from 'graphql';
|
||||
import Credential from '../../models/credential';
|
||||
import credentialType from '../types/credential';
|
||||
import twitterCredentialInputType from '../types/twitter-credential-input';
|
||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||
|
||||
type Params = {
|
||||
id: string,
|
||||
data: object
|
||||
}
|
||||
const updateCredentialResolver = async (params: Params, req: RequestWithCurrentUser) => {
|
||||
let credential = await Credential.query().findOne({
|
||||
user_id: req.currentUser.id,
|
||||
id: params.id
|
||||
})
|
||||
|
||||
credential = await credential.$query().patchAndFetch({
|
||||
data: {
|
||||
...credential.data,
|
||||
...params.data
|
||||
}
|
||||
})
|
||||
|
||||
const appClass = (await import(`../../apps/${credential.key}`)).default;
|
||||
|
||||
const appInstance = new appClass(credential.data)
|
||||
const verifiedCredentials = await appInstance.verifyCredentials();
|
||||
|
||||
credential = await credential.$query().patchAndFetch({
|
||||
data: verifiedCredentials
|
||||
})
|
||||
|
||||
return credential;
|
||||
}
|
||||
|
||||
const updateCredential = {
|
||||
type: credentialType,
|
||||
args: {
|
||||
id: { type: GraphQLNonNull(GraphQLString) },
|
||||
data: { type: GraphQLNonNull(twitterCredentialInputType) }
|
||||
},
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => updateCredentialResolver(params, req)
|
||||
};
|
||||
|
||||
export default updateCredential;
|
Reference in New Issue
Block a user