Skip to content

Commit

Permalink
feat: do not show org selection if user has only one org
Browse files Browse the repository at this point in the history
  • Loading branch information
rdubigny committed Sep 25, 2023
1 parent c76d3b6 commit 73adff5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
6 changes: 0 additions & 6 deletions cypress/e2e/signin_from_agentconnect_client.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ describe('sign-in from agentconnect client', () => {
.contains('Se connecter')
.click();

cy.get('.fr-grid-row .fr-col-12:first-child .fr-tile__link').contains(
'Commune de lamalou-les-bains - Mairie'
);

cy.get('.fr-grid-row .fr-col-12:first-child .fr-tile__link').click();

cy.contains('[email protected]');
cy.contains('21340126800130');
});
Expand Down
10 changes: 2 additions & 8 deletions cypress/e2e/signin_from_standard_client.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const MONCOMPTEPRO_HOST =
Cypress.env('MONCOMPTEPRO_HOST') || 'http://localhost:3000';

describe('sign-in from standard client', () => {
it('should sign-in', function () {
it('should sign-in without org selection when having only one organization', function () {
cy.visit(`http://localhost:4000`);
cy.get('button.moncomptepro-button').click();

Expand All @@ -14,17 +14,11 @@ describe('sign-in from standard client', () => {
.contains('Se connecter')
.click();

cy.get('.fr-grid-row .fr-col-12:first-child .fr-tile__link').contains(
'Commune de lamalou-les-bains - Mairie'
);

cy.get('.fr-grid-row .fr-col-12:first-child .fr-tile__link').click();

cy.contains('[email protected]');
cy.contains('Commune de lamalou-les-bains - Mairie');
});

it('should sign-in with org selection', function () {
it('should sign-in with org selection when having two organization', function () {
cy.visit(`http://localhost:4000`);
cy.get('button.moncomptepro-button').click();

Expand Down
18 changes: 16 additions & 2 deletions src/middlewares/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { isEmpty } from 'lodash';
import { isUrlTrusted } from '../services/security';
import { updateEmailAddressVerificationStatus } from '../managers/user';
import { isEligibleToSponsorship } from '../services/organization';
import { getOrganizationsByUserId } from '../managers/organization/main';
import {
getOrganizationsByUserId,
selectOrganization,
} from '../managers/organization/main';
import {
greetForJoiningOrganization,
notifyAllMembers,
Expand Down Expand Up @@ -159,7 +162,18 @@ export const checkUserHasSelectedAnOrganizationMiddleware = (
req.session.mustReturnOneOrganizationInPayload &&
!selectedOrganizationId
) {
return res.redirect('/users/select-organization');
const userOrganisations = await getOrganizationsByUserId(
getUserFromLoggedInSession(req).id
);

if (userOrganisations.length === 1) {
await selectOrganization({
user_id: getUserFromLoggedInSession(req).id,
organization_id: userOrganisations[0].id,
});
} else {
return res.redirect('/users/select-organization');
}
}

return next();
Expand Down

0 comments on commit 73adff5

Please sign in to comment.