Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working autoaffiliate POC for FC and existing users #22

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions api/src/controllers/citizen.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
CitizenCreate,
Funder,
CitizenFilter,
AffiliationCreate,
} from '../models';
import {InternalServerError} from '../validationError';
import {
Expand Down Expand Up @@ -295,6 +296,85 @@ export class CitizenController {
}
}

/**
* autoaffiliate connected citizen
* @param data has token needed to affiliate connected citizen
*/
@authenticate(AUTH_STRATEGY.KEYCLOAK, AUTH_STRATEGY.API_KEY)
@authorize({
allowedRoles: [Roles.SUPERVISORS, Roles.MANAGERS, Roles.API_KEY, Roles.CITIZENS],
})
@post('/v1/citizens/{citizenId}/autoaffiliate', {
'x-controller-name': 'Citizens',
summary: 'Affilie directement un citoyen à une entreprise',
security: SECURITY_SPEC_API_KEY_KC_PASSWORD,
responses: {
[StatusCode.NoContent]: {
description: "L'affiliation est validée",
},
...defaultSwaggerError,
},
})
async createAffiliation(
@param.path.string('citizenId', {
description: `L'identifiant du citoyen`,
})
citizenId: string,
@requestBody({
content: {
'application/json': {
schema: {
type: 'object',
properties: {
enterpriseId: {
type: 'string',
example: "Identifiant de l'entreprise professionnelle du citoyen",
},
},
},
},
},
})
data: {
enterpriseId: string;
},
): Promise<void> {
try {
//const user = this.currentUser;
// TODO: should probably check that current user is allowed (a.k.a is the citizen), here or in interceptor
let citizen: Citizen | null = await this.citizenService.getCitizenWithAffiliationById(citizenId);

Logger.debug(CitizenController.name, this.createAffiliation.name, 'citizen', citizen);

if (citizen.affiliation && citizen.affiliation.id) {
// Affiliation already exists, erase it with this one
// IDEA: maybe be smarter here ? This is pretty brutal
await this.affiliationRepository.updateById(citizen.affiliation.id, {
status: AFFILIATION_STATUS.AFFILIATED,
enterpriseId: data.enterpriseId,
enterpriseEmail: null
});
} else {

citizen.affiliation = {enterpriseId: data.enterpriseId} as Affiliation

// TODO: Not adding an entrepriseEmail doesn't seem to cause too many issues, but it might be smart to put one anyway
// ^ Especially if this results in a stuck user trying to update his profile (will entrepriseEmail be mandatory ?)
//citizen.affiliation.enterpriseEmail = citizen.personalInformation.email
const affiliation: Affiliation = await this.affiliationRepository.createAffiliation(
citizen,
false, // hasManualAffiliation
true // isAutoAffiliated
);

citizen.affiliation = affiliation
}
} catch (error) {
Logger.error(CitizenController.name, this.createAffiliation.name, 'Error', error);
throw error;
}
}

/**
* disaffiliate citizen by id
* @param citizenId id citizen
Expand Down
11 changes: 8 additions & 3 deletions api/src/repositories/affiliation.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ export class AffiliationRepository extends DefaultCrudRepository<
* Creates affiliation based on enterpriseEmail, enterpriseId and enterprise manual affiliation
* @param citizen Citizen
* @param hasManualAffiliation boolean
* @param isAutoAffiliated boolean
* @returns Promise<Affiliation>
*/
async createAffiliation(citizen: Citizen, hasManualAffiliation: boolean): Promise<Affiliation> {
// Create object : affiliation
async createAffiliation(citizen: Citizen, hasManualAffiliation: boolean, isAutoAffiliated?: boolean): Promise<Affiliation> {
// Create object : affiliation
const rawAffiliation: Affiliation = new Affiliation(citizen.affiliation);
// Set enterpriseEmail & enterpriseId to null if not present
// Set enterpriseEmail & enterpriseId to null if not present
rawAffiliation.enterpriseEmail = rawAffiliation?.enterpriseEmail || null;
rawAffiliation.enterpriseId = rawAffiliation?.enterpriseId || null;

Expand All @@ -45,6 +46,10 @@ export class AffiliationRepository extends DefaultCrudRepository<
rawAffiliation.status = AFFILIATION_STATUS.UNKNOWN;
}

if (isAutoAffiliated) {
rawAffiliation.status = AFFILIATION_STATUS.AFFILIATED;
}

// Assign citizenId
rawAffiliation.citizenId = citizen.id;

Expand Down
11 changes: 11 additions & 0 deletions website/src/apiMob/CitizenService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ export const requestCitizenAffiliation = async (
return data;
};

export const requestCitizenAutoAffiliation = async (
citizenId: string,
enterpriseId: string
): Promise<{}> => {
const { data } = await https.post(
`v1/citizens/${citizenId}/autoaffiliate`,
JSON.stringify({ enterpriseId })
);
return data;
};

export const requestCitizenDesaffiliation = async (
citizenId: string
): Promise<{}> => {
Expand Down
28 changes: 22 additions & 6 deletions website/src/pages/redirection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,33 @@ import { navigate } from 'gatsby';

import { useRoleAccepted } from '@utils/keycloakUtils';
import { browser } from '@utils/helpers';
import { useSession, useUser } from '../context';
import { requestCitizenAutoAffiliation } from '@api/CitizenService';
import { useQueryParam, StringParam } from 'use-query-params';

import { Roles } from '../constants';

const IndexPage = () => {
if (browser) {
const isSupervisor = useRoleAccepted(Roles.SUPERVISORS);
const isManager = useRoleAccepted(Roles.MANAGERS);
// TODO: adding useUser and useQueryParams might have an effect on performances
// This might be important for a page that is only supposed to redirect users
// Maybe there is a way to be smarter and only check userContext if autoaffiliate param is present ?
const { citizen, userFunder, refetchCitizen } = useUser();
const [entrepriseId] = useQueryParam('autoaffiliate', StringParam);

isSupervisor || isManager
? navigate('/mon-dashboard')
: navigate('/recherche');
if (browser) {
if (entrepriseId && citizen) {
requestCitizenAutoAffiliation(citizen.id, entrepriseId).then(() => {
refetchCitizen()
navigate('/recherche/?tab=AideEmployeur')
})
} else {
const isSupervisor = useRoleAccepted(Roles.SUPERVISORS);
const isManager = useRoleAccepted(Roles.MANAGERS);

isSupervisor || isManager
? navigate('/mon-dashboard')
: navigate('/recherche');
}
}
return null;
};
Expand Down