Hi guys, im wondering if my code has to look something like this or is there a better coding guideline to have it more beautified and readable? as i am a none developer i dont have much relationship too developers and so im missing some deeper insights and knowledge about regularities. I dont want to overcomplicate stuff, if not needed so its just a small benchmark for me, to understand this.
Thanks:)
callbackRouter.get("/", async (req, res) => {
const apiClient = new pipedrive.ApiClient();
let oauth2 = apiClient.authentications.oauth2;
oauth2.clientId = process.env.CLIENT_ID; // OAuth 2 Client ID
oauth2.clientSecret = process.env.CLIENT_SECRET; // OAuth 2 Client Secret
oauth2.redirectUri = process.env.REDIRECT_URI; // OAuth 2 Redirection endpoint or Callback Uri
const authCode = req.query.code;
const promise = apiClient.authorize(authCode);
promise.then(async function () {
req.session.accessToken = apiClient.authentications.oauth2.accessToken;
if (
req.session.accessToken !== null &&
req.session.accessToken !== undefined
) {
apiClient.authentications.oauth2.accessToken = req.session.accessToken;
const usersAPI = new pipedrive.UsersApi(apiClient);
const userME = await usersAPI.getCurrentUser(); //get Pipedrive User that installs the App
const { name, company_id, company_domain } = userME.data; //deconstruct vars
req.session.company_id = company_id; //set company id
req.session.company_domain = company_domain;
const user = await getUserFromDB(name, company_id); //check if user is already created
if (user === null) {
//if user has no account
const newUser = await createUserInDB(userME.data); //create user in database with the use of all the data.
res.redirect("/dashboard");
}
}
(exception) => {
// error occurred, exception will be of type src/exceptions/OAuthProviderException
};
});
});
module.exports = callbackRouter;