Skip to content

Commit

Permalink
Migrated services to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Eric Garcia committed Jul 26, 2023
1 parent f17558a commit 13fb473
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 56 deletions.
8 changes: 4 additions & 4 deletions frontend/src/components/templates/Admin/UpdateTeamMember.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export const UpdateTeamMember = () => {
try {
setSuccess(false);
await updateTeamMember({
teamMemberId,
nom: nom.trim(),
prenom: prenom.trim(),
id: teamMemberId,
family_name: nom.trim(),
given_name: prenom.trim(),
email: email.trim(),
phoneNumber: phoneNumber.trim(),
phone_number: phoneNumber.trim(),
job: job.trim(),
});
setTeamMemberId('');
Expand Down
108 changes: 106 additions & 2 deletions frontend/src/components/templates/InstructorEnrollmentList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,77 @@ import { debounce } from 'lodash';
import useListItemNavigation from './hooks/use-list-item-navigation';
import InstructorEnrollmentListFilters from '../organisms/InstructorEnrollmentListFilters';
import StateBadge from '../molecules/StateBadge';
import { EnrollmentEvent } from '../../config/event-configuration';

const { REACT_APP_BACK_HOST: BACK_HOST } = process.env;

type Demandeur = {
export type Demandeur = {
created_at: Date;
email: string;
enrollment_id: number;
family_name: string;
given_name: string;
id: number;
type: string;
job: string;
phone_number: string;
updated_at: Date;
user_id: 227792459;
};

export type User = {
id: number;
family_name: string;
given_name: string;
email: string;
};

export type Event = {
comment: string;
created_at: Date;
diff?: any;
id: number;
name: EnrollmentEvent;
processed_at?: Date;
updated_at: Date;
user: User;
};

export enum TeamMemberType {
contact_metier = 'contact_metier',
delegue_protection_donnees = 'delegue_protection_donnees',
demandeur = 'demandeur',
responsable_metier = 'responsable_metier',
responsable_technique = 'responsable_technique',
responsable_traitement = 'responsable_traitement',
}

export type TeamMember = {
email: string;
family_name: string;
given_name: string;
id: number;
job: string;
phone_number: string;
type: TeamMemberType;
uid: string;
};

export type Contact = {
email?: string;
heading?: string;
id: string;
nom?: string;
phone_number?: string;
};

export type Document = {
id: number;
created_at: Date;
updated_at: Date;
attachment: Record<string, unknown>;
type: string;
};

export type Enrollment = {
updated_at: Date;
created_at: Date;
Expand All @@ -42,7 +104,49 @@ export type Enrollment = {
demandeurs: Demandeur[];
target_api: string;
status: EnrollmentStatus;
linked_franceconnect_enrollment_id?: number | null;
events: Array<any>;
acl: {
archive: boolean;
copy: boolean;
create: boolean;
destroy: boolean;
get_email_templates: boolean;
index: boolean;
mark_event_as_processed: boolean;
notify: boolean;
refuse: boolean;
request_changes: boolean;
revoke: boolean;
show: boolean;
submit: boolean;
update: boolean;
validate: boolean;
};
additional_content: Record<string, unknown>;
cgu_approved: boolean;
copied_from_enrollment_id?: number;
data_recipients: any;
data_retention_comment: any;
data_retention_period: any;
date_mise_en_production: any;
demarche: any;
description?: string;
documents?: Document[];
dpo_is_informed: boolean;
fondement_juridique_title: string;
fondement_juridique_url: string;
linked_token_manager_id?: number | null;
organization_id: number;
previous_enrollment_id: number;
scopes: Record<string, unknown>;
team_members: TeamMember[];
technical_team_type?: any;
technical_team_value?: any;
type_projet?: any;
volumetrie_approximative?: any;
zip_code: string;
contacts?: Contact[];
};

const columnHelper = createColumnHelper<Enrollment>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { memoize } from 'lodash';

const { REACT_APP_BACK_HOST: BACK_HOST } = process.env;

function getDataProviderConfiguration(targetApi) {
function getDataProviderConfiguration(targetApi: string) {
return httpClient
.get(`${BACK_HOST}/api/data_provider_configurations/${targetApi}`, {
headers: { 'Content-type': 'application/json' },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {
Enrollment,
TeamMember,
} from '../components/templates/InstructorEnrollmentList';
import { EnrollmentEvent } from '../config/event-configuration';
import { hashToQueryParams } from '../lib';
import httpClient from '../lib/http-client';
import jsonToFormData from '../lib/json-form-data';

const { REACT_APP_BACK_HOST: BACK_HOST } = process.env;

export function serializeEnrollment(enrollment) {
return jsonToFormData({ enrollment });
}

export function createOrUpdateEnrollment({
enrollment: {
export function serializeEnrollment(enrollment: Enrollment) {
const {
status,
updated_at,
created_at,
Expand All @@ -20,8 +21,15 @@ export function createOrUpdateEnrollment({
events,
team_members,
documents,
...enrollment
},
...enrollmentRest
} = enrollment;
return jsonToFormData({ enrollment: enrollmentRest });
}

export function createOrUpdateEnrollment({
enrollment,
}: {
enrollment: Enrollment;
}) {
const serializedEnrollment = serializeEnrollment(enrollment);
const config = {
Expand All @@ -30,9 +38,13 @@ export function createOrUpdateEnrollment({
},
};

if (id) {
if (enrollment.id) {
return httpClient
.patch(`${BACK_HOST}/api/enrollments/${id}`, serializedEnrollment, config)
.patch(
`${BACK_HOST}/api/enrollments/${enrollment.id}`,
serializedEnrollment,
config
)
.then(({ data: enrollment }) => enrollment);
}

Expand All @@ -45,7 +57,7 @@ export function createOrUpdateEnrollment({
);
}

export function getUserEnrollment(id) {
export function getUserEnrollment(id: number) {
return (
httpClient
.get(`${BACK_HOST}/api/enrollments/${id}`, {
Expand All @@ -59,13 +71,13 @@ export function getUserEnrollment(id) {
);
}

export function hasAccessToEnrollment(id) {
export function hasAccessToEnrollment(id: number) {
return getUserEnrollment(id)
.then(() => true)
.catch(() => false);
}

export function getEnrollmentCopies(id) {
export function getEnrollmentCopies(id: number) {
return httpClient
.get(`${BACK_HOST}/api/enrollments/${id}/copies`, {
headers: {
Expand All @@ -75,7 +87,7 @@ export function getEnrollmentCopies(id) {
.then(({ data: { enrollments: data } }) => data);
}

export function getNextEnrollments(id) {
export function getNextEnrollments(id: number) {
return httpClient
.get(`${BACK_HOST}/api/enrollments/${id}/next_enrollments`, {
headers: {
Expand Down Expand Up @@ -115,6 +127,12 @@ export function getEnrollments({
filter = [],
detailed = false,
max_per_page = null,
}: {
page?: number | null;
sortBy?: { id: string; desc: boolean }[];
filter?: { id: string; value: any }[];
detailed?: boolean;
max_per_page?: number | null;
}) {
const formatedSortBy = sortBy.map(({ id, desc }) => ({
[id]: desc ? 'desc' : 'asc',
Expand All @@ -140,7 +158,7 @@ export function getEnrollments({
.then(({ data }) => data);
}

export function getUserValidatedEnrollments(targetApi) {
export function getUserValidatedEnrollments(targetApi: string) {
// NB. if the user has more than 100 validated franceconnect enrollments, he won’t be able to choose amongst them all
// since we arbitrary limit the max size of the result to 100.
return getEnrollments({
Expand All @@ -163,15 +181,20 @@ export function getUserEnrollments() {
.then(({ data }) => data);
}

export function changeEnrollmentState({ event, id, comment }) {
const options = {
export function changeEnrollmentState({
event,
id,
comment,
}: {
event: EnrollmentEvent;
id: number;
comment: string | null | undefined;
}) {
let options = {
event,
...(comment ? { comment } : {}),
};

if (comment) {
options.comment = comment;
}

return httpClient.patch(
`${BACK_HOST}/api/enrollments/${id}/change_state`,
options,
Expand All @@ -183,24 +206,11 @@ export function changeEnrollmentState({ event, id, comment }) {
);
}

export function updateTeamMember({
teamMemberId,
nom,
prenom,
email,
phoneNumber,
job,
}) {
const team_member = {};
if (nom) team_member[`family_name`] = nom;
if (prenom) team_member[`given_name`] = prenom;
if (email) team_member[`email`] = email;
if (phoneNumber) team_member[`phone_number`] = phoneNumber;
if (job) team_member[`job`] = job;
const serializedTeamMember = jsonToFormData({ team_member });
export function updateTeamMember(teamMember: TeamMember) {
const serializedTeamMember = jsonToFormData({ team_member: teamMember });
return httpClient
.patch(
`${BACK_HOST}/api/team_members/${teamMemberId}`,
`${BACK_HOST}/api/team_members/${teamMember.id}`,
serializedTeamMember,
{
headers: {
Expand All @@ -211,15 +221,21 @@ export function updateTeamMember({
.then(({ data }) => data);
}

export function deleteEnrollment({ id }) {
export function deleteEnrollment({ id }: { id: number }) {
return httpClient.delete(`${BACK_HOST}/api/enrollments/${id}`, {
headers: {
'Content-Type': 'application/json',
},
});
}

export function getMostUsedComments({ event, targetApi } = {}) {
export function getMostUsedComments({
event,
targetApi,
}: {
event: string;
targetApi: string;
}) {
const queryParam = hashToQueryParams({
event,
target_api: targetApi,
Expand All @@ -229,18 +245,20 @@ export function getMostUsedComments({ event, targetApi } = {}) {
.get(`${BACK_HOST}/api/events/most-used-comments${queryParam}`, {
headers: { 'Content-type': 'application/json' },
})
.then(({ data }) => data.map(({ comment }) => comment));
.then(({ data }) =>
data.map(({ comment }: { comment: string }) => comment)
);
}

export function getEmailTemplates({ id }) {
export function getEmailTemplates({ id }: { id: number }) {
return httpClient
.get(`${BACK_HOST}/api/enrollments/${id}/email_templates`, {
headers: { 'Content-type': 'application/json' },
})
.then(({ data: { email_templates } }) => email_templates);
}

export function copyEnrollment({ id }) {
export function copyEnrollment({ id }: { id: number }) {
return httpClient
.post(`${BACK_HOST}/api/enrollments/${id}/copy`, {
headers: { 'Content-type': 'application/json' },
Expand All @@ -256,7 +274,13 @@ export function getHubeeValidatedEnrollments() {
.then(({ data: { enrollments } }) => enrollments);
}

export function markEventAsRead({ id, event_name }) {
export function markEventAsRead({
id,
event_name,
}: {
id: number;
event_name: EnrollmentEvent;
}) {
const queryParam = hashToQueryParams({
event_name,
});
Expand Down
Loading

0 comments on commit 13fb473

Please sign in to comment.