Skip to content

Conversation

@uds5501
Copy link
Contributor

@uds5501 uds5501 commented Jun 8, 2019

Fixes #2771

Changes proposed in this pull request:

  • Fix transition syntax after the refactor

Checklist

  • I have read the Contribution & Best practices Guide.
  • My branch is up-to-date with the Upstream development branch.
  • The acceptance, integration, unit tests and linter pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

@auto-label auto-label bot added the fix label Jun 8, 2019
@uds5501
Copy link
Contributor Author

uds5501 commented Jun 8, 2019

Server PR : fossasia/open-event-server#6015

@uds5501
Copy link
Contributor Author

uds5501 commented Jun 8, 2019

@kushthedude @shreyanshdwivedi @mrsaicharan1 Please review!

kushthedude
kushthedude previously approved these changes Jun 8, 2019
prateekj117
prateekj117 previously approved these changes Jun 8, 2019
Copy link
Member

@niranjan94 niranjan94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When doing async operations inside beforeModel ensure that beforeModel returns a promise that resolves once all async operations are completed.

@uds5501
Copy link
Contributor Author

uds5501 commented Jun 10, 2019

@CosmicCoder96 @niranjan94 After this change, I think the element of network requests is resolved.
If I need to do something else too, please let me know.

@uds5501
Copy link
Contributor Author

uds5501 commented Jun 10, 2019

@mrsaicharan1 @shreyanshdwivedi @kushthedude Review please

kushthedude
kushthedude previously approved these changes Jun 10, 2019
Copy link
Member

@niranjan94 niranjan94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uds5501 just putting an async before the function name isn't gonna solve this. The promise returned by beforeModel will still resolve before any of the two loaders within it complete their async task. You must await for those two tasks to complete.

@niranjan94
Copy link
Member

@shreyanshdwivedi @kushthedude please review the code before pressing approve, other-wise no point in having peer-reviews. :)

@kushthedude
Copy link
Member

@uds5501 Can you make requested changes ASAP

@uds5501
Copy link
Contributor Author

uds5501 commented Jun 10, 2019

@niranjan94 @CosmicCoder96 please have a look now

if (this.get('authManager.currentUser.email') === user.email) {
if (this.session.isAuthenticated) {
if (this.authManager.currentUser.email === user.email) {
this.loader
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@niranjan94 adding an await here threw a build error so removed it, will it be any problem for now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uds5501 you cannot use await without using async perhaps that's why you had gotten the error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the implementation is still wrong. When using async-await, don't use then-catch.

Please read up more on async-await.

@shreyanshdwivedi
Copy link
Member

@niranjan94 actually the PR worked fine on my local. However didn't take a closer look on the code part. Will take care from next time

if (this.get('authManager.currentUser.email') === user.email) {
if (this.session.isAuthenticated) {
if (this.authManager.currentUser.email === user.email) {
this.loader
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uds5501 you cannot use await without using async perhaps that's why you had gotten the error.

if (this.get('authManager.currentUser.email') === user.email) {
if (this.session.isAuthenticated) {
if (this.authManager.currentUser.email === user.email) {
this.loader
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the implementation is still wrong. When using async-await, don't use then-catch.

Please read up more on async-await.

@niranjan94
Copy link
Member

actually the PR worked fine on my local. However didn't take a closer look on the code part. Will take care from next time

@shreyanshdwivedi Thanks ! Always look at the code itself before running it locally.

@uds5501
Copy link
Contributor Author

uds5501 commented Jun 10, 2019

@niranjan94 in this commit I have implemented the second loader within another asynchronous function and user await withing that instead.
As for using then/catch, if I understood what you wanted correctly, I have used try/catch blocks instead for exception handling.

if (this.authManager.currentUser.email === user.email) {
await this.loader
.post('/role_invites/accept-invite', payload)
.then(invite => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're using await, then no need of then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamareebjamal Okay, I see the error. Fixing it

@uds5501
Copy link
Contributor Author

uds5501 commented Jun 10, 2019

@iamareebjamal @CosmicCoder96 @niranjan94 Please review!!

Copy link
Member

@niranjan94 niranjan94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uds5501 there's a lot of code-duplication on this. I'm sure this can be much simpler and cleaner.

try {
let user = await this.loader.post('/role_invites/user', payload);
if (this.session.isAuthenticated) {
this.routingModel(payload, user);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If routingModel is asynchronous, shouldn't you be awaiting it too ?

@fossasia fossasia deleted a comment Jun 11, 2019
@iamareebjamal
Copy link
Member

Not related to this PR, but use const always unless you need mutable variables

@uds5501
Copy link
Contributor Author

uds5501 commented Jun 11, 2019

@niranjan94 I need a little help in reducing the repetition.

this.transitionTo('register', {
          queryParams: {
            event       : `${transition.resolvedModels.public.originalId}`,
            inviteToken : `${transition.to.queryParams.token}`,
            inviteEmail : `${user.email}`
          }
        });

is the repeating transition which can simply be performed in the beforeModel
IF this.transitionTo(getRoute, invite.event); doesn't happen. I tried remodelling the functions to

try {
      if (this.authManager.currentUser.email === user.email) {
        let invite = await this.loader.post('/role_invites/accept-invite', payload);
        let getRoute = (invite.role === 'organiser' || invite.role === 'coorganizer') ? 'events.view' : 'public';
        this.transitionTo(getRoute, invite.event);
        return true
      } else {
        this.set('session.skipRedirectOnInvalidation', true);
        this.session.invalidate();
        return false
      }
    }

and wanted to utilise this value to remove this redundant transition but I can't seem to be able to synchronise it.

@niranjan94
Copy link
Member

niranjan94 commented Jun 11, 2019

@uds5501 wouldn't this work ? This is an equivalent of your existing code but without code-repetition.

import Route from '@ember/routing/route';

export default Route.extend({
  async beforeModel(transition) {
    const { token } = transition.to.queryParams;
    const originalEventId = transition.resolvedModels.public.originalId;
    const payload = {
      data: { token }
    };

    const user = await this.loader.post('/role_invites/user', payload);

    if (this.session.isAuthenticated) {

      if (this.authManager.currentUser.email === user.email) {
        return this.transitionTo(
          ['organiser', 'coorganizer'].includes(invite.role) ? 'events.view' : 'public',
          await this.loader.post('/role_invites/accept-invite', payload)
        );
      }

      this.set('session.skipRedirectOnInvalidation', true);
      this.session.invalidate();
    }

    this.transitionTo('register', {
      queryParams: {
        event       : originalEventId,
        inviteToken : token,
        inviteEmail : user.email
      }
    });
  }
});

Also, lack of try catch is intentional here. We're letting ember handle the error and render the error page.

@uds5501
Copy link
Contributor Author

uds5501 commented Jun 11, 2019

@uds5501 would this work ? This is an equivalent of your existing code but without code-repetition.

import Route from '@ember/routing/route';

export default Route.extend({
  async beforeModel(transition) {
    const { token } = transition.to.queryParams;
    const originalEventId = transition.resolvedModels.public.originalId;
    const payload = {
      data: { token }
    };

    const user = await this.loader.post('/role_invites/user', payload);

    if (this.session.isAuthenticated) {

      if (this.authManager.currentUser.email === user.email) {
        return this.transitionTo(
          ['organiser', 'coorganizer'].includes(invite.role) ? 'events.view' : 'public',
          await this.loader.post('/role_invites/accept-invite', payload)
        );
      }

      this.set('session.skipRedirectOnInvalidation', true);
      this.session.invalidate();
    }

    this.transitionTo('register', {
      queryParams: {
        event       : originalEventId,
        inviteToken : token,
        inviteEmail : user.email
      }
    });
  }
});

Also, lack of try catch is intentional here. We're letting ember handle the error and render the error page.

This should work! I will test it in my local and push the changes.

@uds5501
Copy link
Contributor Author

uds5501 commented Jun 11, 2019

@niranjan94 it just took a minor change otherwise your code worked. Please review!

@fossasia fossasia deleted a comment Jun 11, 2019
@uds5501
Copy link
Contributor Author

uds5501 commented Jun 13, 2019

@niranjan94 @CosmicCoder96 please review

niranjan94
niranjan94 previously approved these changes Jun 13, 2019
Copy link
Member

@mariobehling mariobehling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moderators and all roles need to be able to see the dashboard, but depending on the role they don't have access to certain subpages. Please check how role models work on the UI of widely used systems like wordpress. We need to work on the details of roles, but this issue appears with all roles.

If I am an organizer I also receive an error page after an invite. Please take care of the organizer/co-organizer role first and ensure it is working.

The issue description of the linked issue simply says "does not work". This is not what we expect from an issue. Please write in future what behavior is expected - exactly -. So it can be solved accordingly.

No invited user should be directed to the public page. All invited event roles should be able to see the dashboard.

@uds5501 uds5501 changed the title fix: Redirect user to public page if role is other than co-organizer fix: Restructure and update role-invite flow Jun 14, 2019
@uds5501
Copy link
Contributor Author

uds5501 commented Jun 14, 2019

@mariobehling For now, I have made sure that organiser and co-organisers don't get a 400 error when accepting role invites. @niranjan94 @kushthedude @shreyanshdwivedi @mrsaicharan1 , Please review this

@kushthedude
Copy link
Member

@uds5501 Any GIF or Demo of the following would be awesome

@uds5501
Copy link
Contributor Author

uds5501 commented Jun 14, 2019

@kushthedude
ezgif com-video-to-gif (9)

@kushthedude
Copy link
Member

@uds5501 Looks great, Just one more doubt what if the invite is of other roles, Is the user gets redirected to the event dashboard?

@uds5501
Copy link
Contributor Author

uds5501 commented Jun 14, 2019

@uds5501 Looks great, Just one more doubt what if the invite is of other roles, Is the user gets redirected to the event dashboard?

@kushthedude
For now, let's focus on organisers and co-organisers. For the other roles, we will need to configure permissions individually in the server, I believe.

@kushthedude
Copy link
Member

@uds5501 Looks great, Just one more doubt what if the invite is of other roles, Is the user gets redirected to the event dashboard?

@kushthedude
For now, let's focus on organisers and co-organisers. For the other roles, we will need to configure permissions individually in the server, I believe.

If that's the case, I think the PR is good to go then!

@abhinavk96 abhinavk96 requested a review from niranjan94 June 14, 2019 16:38
@niranjan94 niranjan94 merged commit 4d05048 into fossasia:development Jun 15, 2019
@uds5501 uds5501 deleted the fix-2771 branch June 18, 2019 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

400 being encountered after accepting a role-invite

8 participants